Given an array A containing N non-negative integers. Only following operation can be performed on the array:
A[i] = ( A[i] + 1 ) % 3
where A[i] is the element of array A at index i, and performing this operation once costs 1 unit. Find the minimum cost to make all elements equal.
Examples :
Input : 1 0 3 2
Output : 4 units
Explanation: First 3 is converted to 1(3 + 1 % 3 = 1).
Then, if we try to make all elements equal to 1, then
converting 0 to 1 will cost "1" and 2 to 1 will cost "2".
Therefore, in total cost=3 + 1(to convert 3 to 1) = 4,
which is minimum.
Input : 98 4 3 1
Output : 6 units
Explanation: 98, 4 and 3 are converted to 0, 2 and 1.
So, now array becomes {0, 2, 1, 1}. If we try to convert
every element in our new array equal to 1, then converting
0 to 1 will cost "1" and 2 to 1 will cost "2". So, total
cost= 3+ 3(conversion of 98, 4 and 3) = 6, which is the minimum cost.
Approach: At first, we will try to convert all the numbers greater than 2 to a number between 0 to 2.Since, numbers from 0 to 2 will not exceed 2 using the only allowed operation A(i)=(A(i)+1) % 3(No matter how many times we do this operation). Then we will fix one of the 3 possible values(0 to 2) and find the cost of making all elements equal to it.
Minimum cost out of the three + the extra cost to convert all numbers greater than 2 to a number between 0 to 2 will be our answer. One special case we need look here is, if all the elements are equal by default (like in array {7, 7, 7, 7}), then our answer will be zero.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int mincost( int a[], int n)
{
int c = 1;
for ( int i = 0; i < n; i++)
if (a[i] == a[i + 1])
c++;
if (c == n)
return 0;
int x = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] > 2) {
a[i] = (a[i] + 1) % 3;
x += 1;
}
}
int c0 = 0, c1 = 0, c2 = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] == 1) {
c0 += 2;
}
else if (a[i] == 2)
c0 += 1;
}
for ( int i = 0; i < n; i++)
{
if (a[i] == 2)
c1 += 2;
else if (a[i] == 0)
c1 += 1;
}
for ( int i = 0; i < n; i++) {
if (a[i] == 0)
c2 += 2;
else if (a[i] == 1)
c2 += 1;
}
return x + min({ c0, c1, c2 });
}
int main()
{
int a[] = { 98, 4, 3, 1 };
int n = sizeof (a)/ sizeof (a[0]);
cout << mincost(a, n)<< " units" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int mincost( int a[], int n)
{
int c = 1 ;
for ( int i = 0 ; i < n - 1 ; i++)
if (a[i] == a[i + 1 ])
c++;
if (c == n)
return 0 ;
int x = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (a[i] > 2 )
{
a[i] = (a[i] + 1 ) % 3 ;
x += 1 ;
}
}
int c0 = 0 , c1 = 0 , c2 = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (a[i] == 1 )
{
c0 += 2 ;
}
else if (a[i] == 2 )
c0 += 1 ;
}
for ( int i = 0 ; i < n - 1 ; i++)
{
if (a[i] == 2 )
c1 += 2 ;
else if (a[i] == 0 )
c1 += 1 ;
}
for ( int i = 0 ; i < n - 1 ; i++)
{
if (a[i] == 0 )
c2 += 2 ;
else if (a[i] == 1 )
c2 += 1 ;
}
return x + Math.min(c0,
Math.min(c1, c2));
}
public static void main (String[] args)
{
int a[] = new int []{ 98 , 4 , 3 , 1 };
int n = a.length;
System.out.println(mincost(a, n) +
" " + "units" );
}
}
|
Python3
def mincost(a, n):
c = 1
for i in range ( 0 , n - 1 ):
if (a[i] = = a[i + 1 ]):
c + = 1
if (c = = n):
return 0
x = 0
for i in range (n):
if a[i]> 2 :
a[i] = (a[i] + 1 ) % 3
x + = 1
c0 = c1 = c2 = 0
for i in a:
if (i = = 1 ):
c0 + = 2
elif (i = = 2 ):
c0 + = 1
for i in a:
if (i = = 0 ):
c1 + = 1
elif (i = = 2 ):
c1 + = 2
for i in a:
if (i = = 0 ):
c2 + = 2
elif (i = = 1 ):
c2 + = 1
return min (c1, c2, c0) + x
n = 4
a = [ 98 , 4 , 3 , 1 ]
c = 1
print (mincost(a, n), "units" )
|
C#
using System;
class GFG {
static int mincost( int []a, int n)
{
int c = 1;
for ( int i = 0; i < n - 1; i++)
if (a[i] == a[i + 1])
c++;
if (c == n)
return 0;
int x = 0;
for ( int i = 0; i < n - 1; i++)
{
if (a[i] > 2) {
a[i] = (a[i] + 1) % 3;
x += 1;
}
}
int c0 = 0, c1 = 0, c2 = 0;
for ( int i = 0; i < n - 1; i++)
{
if (a[i] == 1)
{
c0 += 2;
}
else if (a[i] == 2)
c0 += 1;
}
for ( int i = 0; i < n - 1; i++)
{
if (a[i] == 2)
c1 += 2;
else if (a[i] == 0)
c1 += 1;
}
for ( int i = 0; i < n - 1; i++)
{
if (a[i] == 0)
c2 += 2;
else if (a[i] == 1)
c2 += 1;
}
return x + Math.Min(c0,
Math.Min(c1, c2) );
}
public static void Main()
{
int []a = new int []{98, 4, 3, 1};
int n = a.Length;
Console.Write(mincost(a, n) +
" " + "units" );
}
}
|
PHP
<?php
function mincost( $a , $n )
{
$c = 1;
for ( $i = 0; $i < $n ; $i ++)
$c ++;
if ( $c == $n )
return 0;
$x = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] > 2)
{
$a [ $i ] = ( $a [ $i ] + 1) % 3;
$x += 1;
}
}
$c0 = 0; $c1 = 0; $c2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == 1)
{
$c0 += 2;
}
else if ( $a [ $i ] == 2)
$c0 += 1;
}
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == 2)
$c1 += 2;
else if ( $a [ $i ] == 0)
$c1 += 1;
}
for ( $i = 0; $i < $n ; $i ++)
{
if ( $a [ $i ] == 0)
$c2 += 2;
else if ( $a [ $i ] == 1)
$c2 += 1;
}
return $x + min( $c0 , $c1 , $c2 );
}
$a = array (98, 4, 3, 1);
$n = sizeof( $a );
echo mincost( $a , $n ), " units" ;
?>
|
Javascript
<script>
function mincost(a, n)
{
let c = 1;
for (let i = 0; i < n; i++)
if (a[i] == a[i + 1])
c++;
if (c == n)
return 0;
let x = 0;
for (let i = 0; i < n; i++)
{
if (a[i] > 2)
{
a[i] = (a[i] + 1) % 3;
x += 1;
}
}
let c0 = 0, c1 = 0, c2 = 0;
for (let i = 0; i < n; i++)
{
if (a[i] == 1)
{
c0 += 2;
}
else if (a[i] == 2)
c0 += 1;
}
for (let i = 0; i < n; i++)
{
if (a[i] == 2)
c1 += 2;
else if (a[i] == 0)
c1 += 1;
}
for (let i = 0; i < n; i++)
{
if (a[i] == 0)
c2 += 2;
else if (a[i] == 1)
c2 += 1;
}
let y = Math.min(c0, c1);
return x + Math.min(y, c2);
}
let a = [ 98, 4, 3, 1 ];
let n = a.length;
document.write(mincost(a, n) + " units" );
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Space Complexity: O(1) since constant variables are being used.