
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count minimum steps to get the given desired array in C++
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
#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
- Related Articles
- Finding minimum steps to make array elements equal in JavaScript
- Minimum number of page turns to get to a desired page using C++.
- Minimum steps to make all the elements of the array divisible by 4 in C++
- Count minimum right flips to set all values in an array in C++
- C++ program to count number of minimum coins needed to get sum k
- Count minimum number of “move-to-front” moves to sort an array in C++
- Minimum Insertion Steps to Make a String Palindrome in C++
- Count valid pairs in the array satisfying given conditions in C++
- C++ code to count local extrema of given array
- Return a new array of given shape filled with zeros and also set the desired output in Numpy
- Return a new array of given shape filled with zeros and also set the desired datatype in Numpy
- Minimum sum submatrix in a given 2D array in C++
- Native Querying MongoDB inside array and get the count
- Find minimum steps required to reach the end of a matrix in C++
- Find the minimum number of steps to reach M from N in C++
