Suppose, we are given an array 'arr' of size n that contains positive integer numbers. We have to find the even numbers and decrease them by 1. We print out the array after this process.
So, if the input is like n = 7, arr = {10, 9, 7, 6, 4, 8, 3}, then the output will be 9 9 7 5 3 7 3.
Steps
To solve this, we will follow these steps −
for initialize i := 0, when i < n, update (increase i by 1), do: if arr[i] mod 2 is same as 0, then: (decrease arr[i] by 1) print(arr[i]) print a new line
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h>
using namespace std;
#define N 100
void solve(int n, int arr[]) {
for (int i = 0; i < n; i++){
if (arr[i] % 2 == 0)
arr[i]--;
cout<< arr[i] << " ";
}
cout<< endl;
}
int main() {
int n = 7, arr[] = {10, 9, 7, 6, 4, 8, 3};
solve(n, arr);
return 0;
}Input
7, {10, 9, 7, 6, 4, 8, 3}Output
9 9 7 5 3 7 3