Improve Article

Save Article

Improve Article

Save Article

Given an input array, nan task is to person nan fixed array into an ‘**even array’**. An moreover array is an array wherever each **number is an moreover number**. For making an array moreover you tin do these 2 operations:

- If nan existent number is odd, past cipher nan value to make nan number even. For making a number even, you tin switch adjacent positions pinch 1 position arsenic moreover and different arsenic odd.
**Cost = nan full number of swaps made**. - If you want to delete nan existent constituent from nan array past nan costs will beryllium
**5**.

**Examples:**

**Input: **{1243, 267, 2315}**Output: **5**Explanation:** For making 1243 an moreover number, we will switch 4 and 3. This will costs 1 operation.

For making 267 an moreover number, we will switch 6 and 7. This will costs 1 operation.

For making 2315 an moreover number, we will switch 2 and 3 followed by 2 and 1, and past 2 and 5. This will costs 3 operations.

So nan minimum costs will beryllium 5.

**Input: **{12, 23, 2171315}**Output: **7**Explanation: **12 is already an moreover number truthful it will costs 0 operations.

For making 23 an moreover number, we will switch 2 and 3. This will costs 1 operation.

For making 2171315 an moreover number, we request to execute 6 operations truthful alternatively of making it moreover we will delete it from nan array which will costs 5 operations.

So aft deleting, nan minimum costs will beryllium 6.

**Approach: **To lick nan problem travel nan beneath idea:

The thought is to support a adaptable for calculating nan minimum costs and for each azygous constituent of nan array take nan minimum betwixt 5 aliases nan full number of swaps required.

This full point tin beryllium done by pursuing nan below-mentioned steps:

- Implement a usability named makeEven which takes an integer arsenic an input and returns nan number of swaps required for making that number moreover aliases -1 if location is nary moreover digit successful that number.
- Initialize a adaptable minCost for storing nan minimum costs and iterate complete nan input array.
- For each azygous iteration, Initialize a adaptable costs = makeEven ( currentElement).
- if costs == -1 past adhd 5 to minCost different adhd minimum among 5 and costs to minCost.
- return minCost.

Below is nan implementation for nan supra approach:

## C++

#include <bits/stdc++.h>

using namespace std;

int makeEven(int n)

{

int costs = 0;

while (n % 2 != 0) {

n /= 10;

cost++;

}

if (n == 0)

return -1;

return cost;

}

int makeArrayEven(vector<int> arr)

{

int minCost = 0;

for (int one = 0; one < arr.size(); i++) {

int costs = makeEven(arr[i]);

if (cost != -1)

minCost += min(5, cost);

else

minCost += 5;

}

return minCost;

}

int main()

{

vector<int> arr{ 12, 23, 2171315 };

cout << makeArrayEven(arr);

return 0;

}

**Time Complexity: **O(N), wherever N is nan size of nan array,**Auxiliary Space: **O(1)

**Editor:**Naga

**Beritaja.com**at