Transforming an Array to an Odd Array with minimum Cost
Given an input array arr[], convert a given array to an ‘odd array’ consisting of only odd numbers. An even number can be made odd by swapping it with its adjacent element, with a cost equal to the total number of swaps made. If a number cannot be made odd, it can be deleted with a cost of 3, the task is to find the minimum cost to transform the input array to an odd array or determine if it is not possible.
Examples:
Input: arr[] = {126, 32, 16468}
Output: 6
Explanation: For making 126 an odd number we need to perform two swaps 1, 2 and 1, 6. So the cost will be 2. For making 32 an odd number we need to perform one swap 3, 2 only. So the cost will be 1. For making 16468 an odd number, the number of swaps will be 4 so instead of making it odd we can delete it with cost of 3. So total cost will be 2 + 1 + 3 = 6 .
Input: arr[] = {72, 46, 15, 120, 5680}
Output: 9
Explanation: For making 72 an odd number the cost will be 1. 46 cannot be converted into an odd number so we need to delete this and it will cost 3. 15 is already an odd number, so the cost here will be 0. For making 120 an odd number, the cost will be 2. For making 5680 an odd number, the cost will be 3. Total cost will be 1 + 3 + 0 + 2 + 3 = 9.
Approach: The idea is to follow a greedy approach.
Always choose between number of swaps and deletion, whichever costs minimum and add that minimum to the answer.
Below are the steps for the above approach:
- Create a function makeNumberOdd takes an integer as input and returns the cost of converting it into an odd number.
- Initialize a variable cost = 0.
- Run a loop till the number becomes odd or reaches 0.
- Divide the number by 10 and increment the cost by 1, to remove the last digit.
- Check if the number becomes 0 or odd after the loop, and return the cost, or else return -1.
- Initialize a variable minCost = 0.
- Iterate the elements in the vector and calls the makeNumberOdd function for each element.
- If the makeNumberOdd function returns a non-negative cost, add the minimum of 3 and the returned cost to the minimum cost. If it returns -1, add 3 to the minimum cost.
- Return the minimum cost.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int makeNumberOdd( int n)
{
int cost = 0;
while (n % 2 != 1 && n > 0) {
n /= 10;
cost++;
}
if (n == 0 || n % 2 == 0)
return -1;
return cost;
}
int makeArrayOdd(vector< int > arr)
{
int minCost = 0;
for ( int i = 0; i < arr.size(); i++) {
int cost = makeNumberOdd(arr[i]);
if (cost != -1)
minCost += min(3, cost);
else
minCost += 3;
}
return minCost;
}
int main()
{
vector< int > arr{ 72, 46, 15, 120, 5680 };
cout << makeArrayOdd(arr);
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static int makeNumberOdd( int n)
{
int cost = 0 ;
while (n % 2 != 1 && n > 0 ) {
n /= 10 ;
cost++;
}
if (n == 0 || n % 2 == 0 )
return - 1 ;
return cost;
}
public static int makeArrayOdd(List<Integer> arr)
{
int minCost = 0 ;
for ( int i = 0 ; i < arr.size(); i++) {
int cost = makeNumberOdd(arr.get(i));
if (cost != - 1 )
minCost += Math.min( 3 , cost);
else
minCost += 3 ;
}
return minCost;
}
public static void main(String[] args)
{
List<Integer> arr = new ArrayList<>();
arr.add( 72 );
arr.add( 46 );
arr.add( 15 );
arr.add( 120 );
arr.add( 5680 );
System.out.println(makeArrayOdd(arr));
}
}
|
Python3
def make_number_odd(n):
cost = 0
while n % 2 ! = 1 and n > 0 :
n / / = 10
cost + = 1
if n = = 0 or n % 2 = = 0 :
return - 1
return cost
def make_array_odd(arr):
min_cost = 0
for i in range ( len (arr)):
cost = make_number_odd(arr[i])
if cost ! = - 1 :
min_cost + = min ( 3 , cost)
else :
min_cost + = 3
return min_cost
arr = [ 72 , 46 , 15 , 120 , 5680 ]
print (make_array_odd(arr))
|
C#
using System;
using System.Collections.Generic;
class Program {
static int MakeNumberOdd( int n)
{
int cost = 0;
while (n % 2 != 1 && n > 0) {
n /= 10;
cost++;
}
if (n == 0 || n % 2 == 0) {
return -1;
}
return cost;
}
static int MakeArrayOdd(List< int > arr)
{
int minCost = 0;
for ( int i = 0; i < arr.Count; i++) {
int cost = MakeNumberOdd(arr[i]);
if (cost != -1) {
minCost += Math.Min(3, cost);
}
else {
minCost += 3;
}
}
return minCost;
}
static void Main( string [] args)
{
List< int > arr
= new List< int >{ 72, 46, 15, 120, 5680 };
Console.WriteLine(MakeArrayOdd(arr));
}
}
|
Javascript
const makeNumberOdd = (n) => {
let cost = 0;
while (n % 2 !== 1 && n > 0) {
n = Math.floor(n / 10);
cost++;
}
if (n === 0 || n % 2 === 0){
return -1;
}
return cost;
}
const makeArrayOdd = (arr) => {
let minCost = 0;
for (let i = 0; i < arr.length; i++) {
let cost = makeNumberOdd(arr[i]);
if (cost !== -1)
minCost += Math.min(3, cost);
else
minCost += 3;
}
return minCost;
}
const arr = [72, 46, 15, 120, 5680];
console.log(makeArrayOdd(arr));
|
Time Complexity: O(N), where N is the size of the input array
Auxiliary Space: O(1)
Last Updated :
28 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...