Count minimum steps to get the given desired array in C++

C++Server Side ProgrammingProgramming

We are given with an array target[] which has numbers in it. We must find the minimum steps in which array with all zeros [0,0,0,0…] can be converted to target using following two operations only −

  • Increment operation − all elements can be incremented by 1, each increment operation can be counted individually in steps. ( for n increments in n elements steps=n )

  • Doubling operation − the whole array is doubled. For all elements it is counted once. ( each doubling operation doubles all elements’ value, count it as 1 in steps

The goal is to find the minimum number of steps to reach the target. Eg [0,0,0] can become [1,1,1] in minimum 3 steps ( by increment operation on all elements ) and can become [2,2,2] by one more doubling operation, total 4 steps this time ( 3 increments, 1 doubling ).

Input

target[]= { 1,2,2,3 }

Output

Minimum steps to reach target from {0,0,0,0} : 6

Explanation

Initially we have { 0,0,0,0 }

3 increment operations { 0,1,1,1 } //increment occurs individually

1 doubling operation { 0,2,2,2 } // doubling occurs on all elements

2 increment operation { 1,2,2,3 }

Total steps= 3+1+2=6

Input

target[]= { 3,3,3 }

Output

Minimum steps to reach target from {0,0,0} : 7

Explanation

Initially we have { 0,0,0 }

3 increment operations { 1,1,1 } //increment occurs individually

1 doubling operation { 2,2,2 } // doubling occurs on all elements

3 increment operation { 3,3,3 }

Total steps= 3+1+3=7

Approach used in the below program is as follows

  • Integer array target[] stores the target elements to be reached.

  • Function minSteps(int target[],int n) takes the target array and its length ‘n’ as input and returns the count of minimum steps to reach to target from all zeroes.

  • Variable count is used to store step count, initially 0.

  • Variable max is used to store the highest element, initially target[0].

  • Variable pos is used to store the index of max found , initially 0.

  • If the target[] array has all zeros then return 0 as no steps required. ( for (i=0;i<n;i++) if (target[i]==0) count++; if(count==n)//all zeros)

  • Now in this approach we will reach from target[] to all zeroes.

  • Make all elements even by subtracting 1 from odd ones. Increment count for each subtraction ( same as increment operation )

  • Now we have all even numbers.

  • Also find the max value and its position in the same loop and initialize max and pos.

  • Now we start dividing the whole array by 2 until the max value does not become 1. If any number becomes odd, decrement 1 and increase count, for whole divide operation increment count once.

  • At the end all elements will be either 0 or 1, for all 1’s make them 0 and increment count again.

  • Return result as steps present in count.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int minSteps(int target[],int n){
   int i;
   int count=0;
   int max=target[0];
   int pos=0;
   for(i=0;i<n;i++)
      if(target[i]==0)
         count++;
      //if all are zeros, same as target
      if(count==n)
         return 0;
         count=0;
         //make all even by sbtracting 1
      for(i=0;i<n;i++){
         if(target[i]%2==1){
            target[i]=target[i]-1;
            count++;
      }
      //find max value and its position
      if(target[i]>=max){
         max=target[i];
         pos=i;
      }
   }
   //diving by 2 till all elements are 1 and increase count once
   while(target[pos]!=1){
      for(i=0;i<n;i++){
         if(target[i]%2==1){
             target[i]=target[i]-1;
            count++;
      }
      target[i]=target[i]/2;
   }
   count++;
}
//whole array is {1} make zeroes and increase count
while(target[pos]!=0){
   for(i=0;i<n;i++){
      if(target[i]!=0){
         target[i]=target[i]-1;
         count++;}
      }
   }
   return count;
}
int main(){
   int target[]={15,15,15};
   cout<<"\nMinimum steps to get the given desired array:"<<minSteps(target,3);
   return 0;
}

Output

Minimum steps to get the given desired array:15
raja
Published on 28-Jul-2020 14:51:47
Advertisements