Count minimum steps to get the given desired array
Last Updated :
20 Jul, 2023
Consider an array with n elements and value of all the elements is zero. We can perform following operations on the array.
- Incremental operations:Choose 1 element from the array and increment its value by 1.
- Doubling operation: Double the values of all the elements of array.
We are given desired array target[] containing n elements. Compute and return the smallest possible number of the operations needed to change the array from all zeros to desired array.
Examples:
Input: target[] = {2, 3}
Output: 4
To get the target array from {0, 0}, we
first increment both elements by 1 (2
operations), then double the array (1
operation). Finally increment second
element (1 more operation)
Input: target[] = {2, 1}
Output: 3
One of the optimal solution is to apply the
incremental operation 2 times to first and
once on second element.
Input: target[] = {16, 16, 16}
Output: 7
The output solution looks as follows. First
apply an incremental operation to each element.
Then apply the doubling operation four times.
Total number of operations is 3+4 = 7
One important thing to note is that the task is to count the number of steps to get the given target array (not to convert zero array to target array).
The idea is to follow reverse steps, i.e. to convert target to array of zeros. Below are steps.
Take the target array first.
Initialize result as 0.
If all are even, divide all elements by 2
and increment result by 1.
Find all odd elements, make them even by
reducing them by 1. and for every reduction,
increment result by 1.
Finally, we get all zeros in target array.
Below is the implementation of above algorithm.
C++
#include <bits/stdc++.h>
using namespace std;
int countMinOperations(unsigned int target[], int n)
{
int result = 0;
while (1)
{
int zero_count = 0;
int i;
for (i=0; i<n; i++)
{
if (target[i] & 1)
break ;
else if (target[i] == 0)
zero_count++;
}
if (zero_count == n)
return result;
if (i == n)
{
for ( int j=0; j<n; j++)
target[j] = target[j]/2;
result++;
}
for ( int j=i; j<n; j++)
{
if (target[j] & 1)
{
target[j]--;
result++;
}
}
}
}
int main()
{
unsigned int arr[] = {16, 16, 16};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "Minimum number of steps required to "
"get the given target array is "
<< countMinOperations(arr, n);
return 0;
}
|
Java
class Test
{
static int arr[] = new int []{ 16 , 16 , 16 } ;
static int countMinOperations( int n)
{
int result = 0 ;
while ( true )
{
int zero_count = 0 ;
int i;
for (i= 0 ; i<n; i++)
{
if (arr[i] % 2 == 1 )
break ;
else if (arr[i] == 0 )
zero_count++;
}
if (zero_count == n)
return result;
if (i == n)
{
for ( int j= 0 ; j<n; j++)
arr[j] = arr[j]/ 2 ;
result++;
}
for ( int j=i; j<n; j++)
{
if (arr[j] % 2 == 1 )
{
arr[j]--;
result++;
}
}
}
}
public static void main(String[] args) {
System.out.println( "Minimum number of steps required to \n" +
"get the given target array is " +
countMinOperations(arr.length));
}
}
|
Python3
def countMinOperations(target, n):
result = 0 ;
while ( True ):
zero_count = 0 ;
i = 0 ;
while (i < n):
if ((target[i] & 1 ) > 0 ):
break ;
elif (target[i] = = 0 ):
zero_count + = 1 ;
i + = 1 ;
if (zero_count = = n):
return result;
if (i = = n):
for j in range (n):
target[j] = target[j] / / 2 ;
result + = 1 ;
for j in range (i, n):
if (target[j] & 1 ):
target[j] - = 1 ;
result + = 1 ;
arr = [ 16 , 16 , 16 ];
n = len (arr);
print ( "Minimum number of steps required to" ,
"\nget the given target array is" ,
countMinOperations(arr, n));
|
C#
using System;
class GFG {
static int []arr = new int []{16, 16, 16} ;
static int countMinOperations( int n)
{
int result = 0;
while ( true )
{
int zero_count = 0;
int i;
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 1)
break ;
else if (arr[i] == 0)
zero_count++;
}
if (zero_count == n)
return result;
if (i == n)
{
for ( int j = 0; j < n; j++)
arr[j] = arr[j] / 2;
result++;
}
for ( int j = i; j < n; j++)
{
if (arr[j] %2 == 1)
{
arr[j]--;
result++;
}
}
}
}
public static void Main()
{
Console.Write( "Minimum number of steps required to \n" +
"get the given target array is " +
countMinOperations(arr.Length));
}
}
|
Javascript
<script>
let arr = [16, 16, 16] ;
function countMinOperations(n)
{
let result = 0;
while ( true )
{
let zero_count = 0;
let i;
for (i=0; i<n; i++)
{
if (arr[i] % 2 == 1)
break ;
else if (arr[i] == 0)
zero_count++;
}
if (zero_count == n)
return result;
if (i == n)
{
for (let j=0; j<n; j++)
arr[j] = arr[j]/2;
result++;
}
for (let j=i; j<n; j++)
{
if (arr[j] %2 == 1)
{
arr[j]--;
result++;
}
}
}
}
document.write( "Minimum number of steps required to " + "<br/>" +
"get the given target array is " +
countMinOperations(arr.length));
</script>
|
PHP
<?php
function countMinOperations( $target , $n )
{
$result = 0;
while (1)
{
$zero_count = 0;
$i = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $target [ $i ] & 1)
break ;
else if ( $target [ $i ] == 0)
$zero_count ++;
}
if ( $zero_count == $n )
return $result ;
if ( $i == $n )
{
for ( $j = 0; $j < $n ; $j ++)
$target [ $j ] = $target [ $j ] / 2;
$result ++;
}
for ( $j = $i ; $j < $n ; $j ++)
{
if ( $target [ $j ] & 1)
{
$target [ $j ]--;
$result ++;
}
}
}
}
$arr = array (16, 16, 16);
$n = sizeof( $arr );
echo "Minimum number of steps required to \n" .
"get the given target array is " .
countMinOperations( $arr , $n );
?>
|
Output
Minimum number of steps required to get the given target array is 7
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...