codeparrot/xlcost-text-to-code huggingface dataset:
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle | C ++ program for the above approach ; Function to find the maximum size of the subset of the given array such that a triangle can be formed from any three integers of the subset as sides ; Sort arr [ ] in increasing order ; Stores the maximum size of a valid subset of the given array ; Stores the starting index of the current window ; Stores the last index of the current window ; Iterate over the array arr [ ] ; Increment j till the value of arr [ i ] + arr [ i + 1 ] > arr [ j ] holds true ; Update the value of maxSize ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximizeSubset ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int maxSize = 0 ; int i = 0 ; int j = i + 2 ; while ( i < N - 2 ) { while ( arr [ i ] + arr [ i + 1 ] > arr [ j ] ) { j ++ ; } maxSize = max ( maxSize , j - i ) ; i ++ ; j = max ( j , i + 2 ) ; } return maxSize ; } int main ( ) { int arr [ ] = { 2 , 7 , 4 , 1 , 6 , 9 , 5 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximizeSubset ( arr , N ) << endl ; return 0 ; }
Maximum possible value of array elements that can be made based on given capacity conditions | C ++ program for the above approach ; Function to find the maximum element after shifting operations in arr [ ] ; Stores the sum of array element ; Stores the maximum element in cap [ ] ; Iterate to find maximum element ; Return the resultant maximum value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxShiftArrayValue ( int arr [ ] , int cap [ ] , int N ) { int sumVals = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sumVals += arr [ i ] ; } int maxCapacity = 0 ; for ( int i = 0 ; i < N ; i ++ ) { maxCapacity = max ( cap [ i ] , maxCapacity ) ; } return min ( maxCapacity , sumVals ) ; } int main ( ) { int arr [ ] = { 2 , 3 } ; int cap [ ] = { 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxShiftArrayValue ( arr , cap , N ) ; return 0 ; }
Minimum size of Array possible with given sum and product values | C ++ program for the above approach ; Function to find the minimum size of array with sum S and product P ; Base Case ; Iterate through all values of S and check the mentioned condition ; Otherwise , print " - 1" ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSizeArray ( int S , int P ) { if ( S == P ) { return 1 ; } for ( int i = 2 ; i <= S ; i ++ ) { double d = i ; if ( ( S / d ) >= pow ( P , 1.0 / d ) ) { return i ; } } return -1 ; } int main ( ) { int S = 5 , P = 6 ; cout << minimumSizeArray ( S , P ) ; return 0 ; }
Smallest possible integer to be added to N | C ++ program for the above approach ; Function to find the smallest positive integer X such that X is added to every element of A except one element to give array B ; Stores the unique elements of array A ; Insert A [ i ] into the set s ; Sort array A [ ] ; Sort array B [ ] ; Assume X value as B [ 0 ] - A [ 1 ] ; Check if X value assumed is negative or 0 ; Update the X value to B [ 0 ] - A [ 0 ] ; If assumed value is wrong ; Update X value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findVal ( int A [ ] , int B [ ] , int N ) { unordered_set < int > s ; for ( int i = 0 ; i < N ; i ++ ) { s . insert ( A [ i ] ) ; } sort ( A , A + N ) ; sort ( B , B + N - 1 ) ; int X = B [ 0 ] - A [ 1 ] ; if ( X <= 0 ) X = B [ 0 ] - A [ 0 ] ; else { for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( s . count ( B [ i ] - X ) == 0 ) { X = B [ 0 ] - A [ 0 ] ; break ; } } } cout << X << endl ; } int main ( ) { int A [ ] = { 1 , 4 , 3 , 8 } ; int B [ ] = { 15 , 8 , 11 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findVal ( A , B , N ) ; return 0 ; }
Count of integral points that lie at a distance D from origin | C ++ program for the above approach ; Function to find the total valid integer coordinates at a distance D from origin ; Stores the count of valid points ; Iterate over possibel x coordinates ; Find the respective y coordinate with the pythagoras theorem ; Adding 4 to compensate the coordinates present on x and y axes . ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPoints ( int D ) { int count = 0 ; for ( int x = 1 ; x * x < D * D ; x ++ ) { int y = ( int ) sqrt ( double ( D * D - x * x ) ) ; if ( x * x + y * y == D * D ) { count += 4 ; } } count += 4 ; return count ; } int main ( ) { int D = 5 ; cout << countPoints ( D ) ; return 0 ; }
Sum of the shortest distance between all 0 s to 1 in given binary string | C ++ program for the above approach ; Function to find the total sum of the shortest distance between every 0 to 1 in a given binary string ; Stores the prefix distance and suffix distance from 0 to 1 ; Stores the current distance from 1 to 0 ; Marks the 1 ; If current character is 1 ; Mark haveOne to true ; Assign the cnt to 0 ; Assign prefixDistance [ i ] as 0 ; If haveOne is true ; Update the cnt ; Update prefixDistance [ i ] ; Assign prefixDistance [ i ] as INT_MAX ; Assign haveOne as false ; If current character is 1 ; Mark haveOne to true ; Assign the cnt to 0 ; Assign the suffixDistance [ i ] as 0 ; If haveOne is true ; Update the cnt ; Update suffixDistance [ i ] as cnt ; Assign suffixDistance [ i ] as INT_MAX ; Stores the total sum of distances between 0 to nearest 1 ; If current character is 0 ; Update the value of sum ; Print the value of the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTotalDistance ( string S , int N ) { vector < int > prefixDistance ( N ) ; vector < int > suffixDistance ( N ) ; int cnt = 0 ; bool haveOne = false ; for ( int i = 0 ; i < N ; ++ i ) { if ( S [ i ] == '1' ) { haveOne = true ; cnt = 0 ; prefixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; prefixDistance [ i ] = cnt ; } else prefixDistance [ i ] = INT_MAX ; } haveOne = false ; for ( int i = N - 1 ; i >= 0 ; -- i ) { if ( S [ i ] == '1' ) { haveOne = true ; cnt = 0 ; suffixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; suffixDistance [ i ] = cnt ; } else suffixDistance [ i ] = INT_MAX ; } int sum = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( S [ i ] == '0' ) { sum += min ( prefixDistance [ i ] , suffixDistance [ i ] ) ; } } cout << sum << endl ; } int main ( ) { string S = "100100" ; int N = S . length ( ) ; findTotalDistance ( S , N ) ; return 0 ; }
Minimize value of | A | C ++ program for the above approach ; Function to find the minimum possible value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z for given A , B and C ; Stores the minimum value of | A - X | + | B - Y | + | C - Z | such that X * Y = Z ; Iterate over all values of i in the range [ 1 , 2 * C ] ; Iterate over all values of j such that i * j <= 2 * c ; Update the value of ans ; Return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimizeCost ( int A , int B , int C ) { int ans = A + B + C ; for ( int i = 1 ; i <= 2 * C ; i ++ ) { int j = 0 ; while ( i * j <= 2 * C ) { ans = min ( ans , abs ( A - i ) + abs ( B - j ) + abs ( i * j - C ) ) ; j ++ ; } } return ans ; } int main ( ) { int A = 19 , B = 28 , C = 522 ; cout << minimizeCost ( A , B , C ) ; return 0 ; }
Average value of set bit count in given Binary string after performing all possible choices of K operations | C ++ program for the above approach ; Function to calculate the average number of Set bits after after given operations ; Stores the average number of set bits after current operation ; Stores the average number of set bits after current operation ; Iterate through the array arr [ ] and update the values of p and q ; Store the value of p and q of the previous state before their updation ; Update average number of set bits after performing the ith operation ; Update average number of off bits after performing the ith operation ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double averageSetBits ( int N , int K , int arr [ ] ) { double p = N ; double q = 0 ; for ( int i = 0 ; i < K ; i ++ ) { double _p = p , _q = q ; p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N ; q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N ; } return p ; } int main ( ) { int N = 5 ; int arr [ ] = { 1 , 2 , 3 } ; int K = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << setprecision ( 10 ) << averageSetBits ( N , K , arr ) ; return 0 ; }
Find maximum sum of subsequence after flipping signs of at most K elements in given Array | C ++ implementation for the above approach ; Function to calculate the max sum of subsequence ; Variable to store the max sum ; Sort the array ; Iterate over the array ; Flip sign ; Decrement k ; Traverse over the array ; Add only positive elements ; Return the max sum ; Driver Code ; Given array ; Variable to store number of flips are allowed ; Function call to find the maximum sum of subsequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubseq ( int arr [ ] , int N , int K ) { int sum = 0 ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( K == 0 ) break ; if ( arr [ i ] < 0 ) { arr [ i ] = - arr [ i ] ; K -- ; } } for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] > 0 ) sum += arr [ i ] ; return sum ; } int main ( ) { int arr [ ] = { 6 , -10 , -1 , 0 , -4 , 2 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSubseq ( arr , N , K ) ; return 0 ; }
Minimum number of intervals to cover the target interval | C ++ program for the above approach ; Function to find the minimum number of intervals in the array A [ ] to cover the entire target interval ; Sort the array A [ ] in increasing order of starting point ; Insert a pair of INT_MAX to prevent going out of bounds ; Stores start of current interval ; Stores end of current interval ; Stores the count of intervals ; Iterate over all the intervals ; If starting point of current index <= start ; Update the value of start ; Increment the value of count ; If the target interval is already covered or it is not possible to move then break the loop ; If the entire target interval is not covered ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimizeSegment ( vector < pair < int , int > > A , pair < int , int > X ) { sort ( A . begin ( ) , A . end ( ) ) ; A . push_back ( { INT_MAX , INT_MAX } ) ; int start = X . first ; int end = X . first - 1 ; int cnt = 0 ; for ( int i = 0 ; i < A . size ( ) ; ) { if ( A [ i ] . first <= start ) { end = max ( A [ i ++ ] . second , end ) ; } else { start = end ; ++ cnt ; if ( A [ i ] . first > end end >= X . second ) { break ; } } } if ( end < X . second ) { return -1 ; } return cnt ; } int main ( ) { vector < pair < int , int > > A = { { 1 , 3 } , { 2 , 4 } , { 2 , 10 } , { 2 , 3 } , { 1 , 1 } } ; pair < int , int > X = { 1 , 10 } ; cout << minimizeSegment ( A , X ) ; return 0 ; }
K | C ++ implementation for the above approach ; Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long KthSolution ( long long X , long long K ) { long long ans = 0 ; for ( int i = 0 ; i < 64 ; i ++ ) { if ( ! ( X & ( 1LL << i ) ) ) { if ( K & 1 ) { ans |= ( 1LL << i ) ; } K >>= 1 ; if ( ! K ) { break ; } } } return ans ; } int main ( ) { long long X = 5 , K = 5 ; cout << KthSolution ( X , K ) ; return 0 ; }
Rearrange sorted array such that all odd indices elements comes before all even indices element | C ++ program for the above approach ; Function to print the array ; Function to rearrange the array such that odd indexed elements come before even indexed elements ; Reduces the size of array by one because last element does not need to be changed in case N = odd ; Initialize the variables ; Iterate over the range ; Add the modified element at the odd position ; Iterate over the range ; Add the modified element at the even position ; Iterate over the range ; Divide by thr maximum element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printTheArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) cout << arr [ i ] << " ▁ " ; } void rearrange ( int arr [ ] , int N ) { if ( N & 1 ) N -- ; int odd_idx = 1 , even_idx = 0 ; int i , max_elem = arr [ N - 1 ] + 1 ; for ( i = 0 ; i < N / 2 ; i ++ ) { arr [ i ] += ( arr [ odd_idx ] % max_elem ) * max_elem ; odd_idx += 2 ; } for ( ; i < N ; i ++ ) { arr [ i ] += ( arr [ even_idx ] % max_elem ) * max_elem ; even_idx += 2 ; } for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] / max_elem ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 16 , 18 , 19 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rearrange ( arr , N ) ; printTheArray ( arr , N ) ; return 0 ; }
Find range of values for S in given Array with values satisfying [ arr [ i ] = floor ( ( i * S ) / K ) ] | C ++ program for the above approach ; Function to find the range of values for S in a given array that satisfies the given condition ; Stores the left range value ; Stores the right range value ; Find the current left range value for S ; Find the current right range value for S ; Updating L value ; Updating R value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findRange ( int arr [ ] , int N , int K ) { int L = INT_MIN ; int R = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { int l = ( int ) ceil ( 1.0 * arr [ i ] * K / ( i + 1 ) ) ; int r = ( int ) ceil ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) ) - 1 ; L = max ( L , l ) ; R = min ( R , r ) ; } cout << L << " ▁ " << R ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 9 , 11 } ; int K = 10 ; int N = sizeof ( arr ) / sizeof ( int ) ; findRange ( arr , N , K ) ; return 0 ; }
Find an anagram of given String having different characters at corresponding indices | C ++ program for the above approach ; Function to find anagram of string such that characters at the same indices are different ; Copying our original string for comparison ; Declaring the two pointers ; Checking the given condition ; When string length is odd ; The mid element ; If the characters are the same , then perform the swap operation as illustrated ; Check if the corresponding indices has the same character or not ; If string follows required condition ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findAnagram ( string s ) { string check = s ; int i = 0 , j = s . length ( ) - 1 ; while ( i < s . length ( ) && j >= 0 ) { if ( s [ i ] != s [ j ] && check [ i ] != s [ j ] && check [ j ] != s [ i ] ) { swap ( s [ i ] , s [ j ] ) ; i ++ ; j = s . length ( ) - 1 ; } else { j -- ; } } if ( s . length ( ) % 2 != 0 ) { int mid = s . length ( ) / 2 ; if ( check [ mid ] == s [ mid ] ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( check [ i ] != s [ mid ] && s [ i ] != s [ mid ] ) { swap ( s [ i ] , s [ mid ] ) ; break ; } } } } bool ok = true ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( check [ i ] == s [ i ] ) { ok = false ; break ; } } if ( ok ) cout << s ; else cout << -1 ; } int main ( ) { string S = " geek " ; findAnagram ( S ) ; return 0 ; }
Lexicographically smallest permutation of [ 1 , N ] based on given Binary string | C ++ program for the above approach ; Function to generate the lexicographically smallest permutation according to the given criteria ; Stores the resultant permutation ; Initialize the first elements to 1 ; Traverse the given string S ; Number greater than last number ; Number equal to the last number ; Correct all numbers to the left of the current index ; Printing the permutation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructPermutation ( string S , int N ) { int ans [ N ] ; ans [ 0 ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) { if ( S [ i - 1 ] == '0' ) { ans [ i ] = i + 1 ; } else { ans [ i ] = ans [ i - 1 ] ; } for ( int j = 0 ; j < i ; ++ j ) { if ( ans [ j ] >= ans [ i ] ) { ans [ j ] ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) { cout << ans [ i ] ; if ( i != N - 1 ) { cout << " ▁ " ; } } } int main ( ) { string S = "100101" ; constructPermutation ( S , S . length ( ) + 1 ) ; return 0 ; }
Kth smallest positive integer Y such that its sum with X is same as its bitwise OR with X | C ++ implementation for the above approach ; Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long KthSolution ( long long X , long long K ) { long long ans = 0 ; for ( int i = 0 ; i < 64 ; i ++ ) { if ( ! ( X & ( 1LL << i ) ) ) { if ( K & 1 ) { ans |= ( 1LL << i ) ; } K >>= 1 ; if ( ! K ) { break ; } } } return ans ; } int main ( ) { long long X = 10 , K = 5 ; cout << KthSolution ( X , K ) ; return 0 ; }
Count of distinct N | C ++ program of the above approach ; Function to find the count of distinct arrays of size n having elements in range [ 1 , k ] and all adjacent elements ( P , Q ) follows ( P <= Q ) or ( P % Q > 0 ) ; Stores the divisors of all integers in the range [ 1 , k ] ; Calculate the divisors of all integers using the Sieve ; Stores the dp states such that dp [ i ] [ j ] with i elements having j as the last element of array ; Initialize the dp array ; Calculate the dp states using the derived relation ; Calculate the sum for len - 1 ; Subtract dp [ len - 1 ] [ j ] for each factor of j from [ 1 , K ] ; Calculate the final result ; Return the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countArrays ( int n , int k ) { vector < vector < int > > divisors ( k + 1 ) ; for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = 2 * i ; j <= k ; j += i ) { divisors [ j ] . push_back ( i ) ; } } vector < vector < int > > dp ( n + 1 , vector < int > ( k + 1 ) ) ; for ( int j = 1 ; j <= k ; j ++ ) { dp [ 1 ] [ j ] = 1 ; } for ( int x = 2 ; x <= n ; x ++ ) { int sum = 0 ; for ( int j = 1 ; j <= k ; j ++ ) { sum += dp [ x - 1 ] [ j ] ; } for ( int y = 1 ; y <= k ; y ++ ) { dp [ x ] [ y ] = sum ; for ( int d : divisors [ y ] ) { dp [ x ] [ y ] = ( dp [ x ] [ y ] - dp [ x - 1 ] [ d ] ) ; } } } int sum = 0 ; for ( int j = 1 ; j <= k ; j ++ ) { sum += dp [ n ] [ j ] ; } return sum ; } int main ( ) { int N = 2 , K = 3 ; cout << countArrays ( N , K ) ; return 0 ; }
Minimize increment | C ++ Program of the above approach ; Function to calculate the minimum number of operations to convert array A to array B by incrementing and decrementing adjacent elements ; Stores the final count ; Stores the sum of array A and B respectivelly ; Check of the sums are unequall ; Pointer to iterate through array ; Case 1 where A [ i ] > B [ i ] ; Stores the extra values for the current index ; Iterate the array from [ i - 1 , 0 ] ; Stores the count of values being transfered from A [ i ] to A [ j ] ; Add operation count ; Iterate the array in right direction id A [ i ] - B [ i ] > 0 ; Iterate the array from [ i + 1 , n - 1 ] ; Stores the count of values being transfered from A [ i ] to A [ j ] ; Add operation count ; Return Answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumMoves ( int A [ ] , int B [ ] , int N ) { int ans = 0 ; int sum_A = 0 , sum_B = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum_A += A [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { sum_B += B [ i ] ; } if ( sum_A != sum_B ) { return -1 ; } int i = 0 ; while ( i < N ) { if ( A [ i ] > B [ i ] ) { int temp = A [ i ] - B [ i ] ; int j = i - 1 ; while ( j >= 0 && temp > 0 ) { if ( B [ j ] > A [ j ] ) { int cnt = min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * abs ( j - i ) ) ; } j -- ; } if ( temp > 0 ) { int j = i + 1 ; while ( j < N && temp > 0 ) { if ( B [ j ] > A [ j ] ) { int cnt = min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * abs ( j - i ) ) ; } j ++ ; } } } i ++ ; } return ans ; } int main ( ) { int A [ ] = { 1 , 5 , 7 } ; int B [ ] = { 13 , 0 , 0 } ; int N = sizeof ( A ) / sizeof ( int ) ; cout << minimumMoves ( A , B , N ) ; return 0 ; }
Check if X can be reduced to 0 in exactly T moves by substracting D or 1 from it | C ++ Program to implement the above approach ; Function to check the above problem condition ; Check for base cases ; Check if D - 1 is a divisor of X - T ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int possibleReachingSequence ( int X , int D , int T ) { if ( X < T ) { cout << " NO " ; return 0 ; } if ( T * D < X ) { cout << " NO " ; return 0 ; } if ( ( X - T ) % ( D - 1 ) == 0 ) { cout << " YES " ; } else { cout << " NO " ; } return 0 ; } int main ( ) { int X = 10 , D = 3 , T = 6 ; possibleReachingSequence ( X , D , T ) ; }
Maximum number of times Array can be reduced in half when its all elements are even | C ++ code implementation for the above approach ; Function to return the number of operations possible ; counter to store the number of times the current element is divisible by 2 ; variable to store the final answer ; Initialize the counter to zero for each element ; update the counter till the number is divisible by 2 ; update the answer as the minimum of all the counts ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int arrayDivisionByTwo ( int arr [ ] , int n ) { int cnt = 0 ; int ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { cnt = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = arr [ i ] / 2 ; cnt ++ ; } ans = min ( ans , cnt ) ; } return ans ; } int main ( ) { int arr [ ] = { 8 , 12 , 40 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << arrayDivisionByTwo ( arr , n ) ; return 0 ; }
Find smallest number with given digits and sum of digits | C ++ Program of the above approach ; Function to print the minimum integer having only digits P and Q and the sum of digits as N ; If Q is greater that P then swap the values of P and Q ; If P and Q are both zero or if Q is zero and N is not divisible by P then there is no possible integer which satisfies the given conditions ; Loop to find the maximum value of count_P that also satisfy P * count_P + Q * count_Q = N ; If N is 0 , their is a valid integer possible that satisfies all the requires conditions ; Print Answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMinInteger ( int P , int Q , int N ) { if ( Q > P ) { swap ( P , Q ) ; } if ( Q == 0 && ( P == 0 N % P != 0 ) ) { cout << " Not ▁ Possible " ; return ; } int count_P = 0 , count_Q = 0 ; while ( N > 0 ) { if ( N % P == 0 ) { count_P += N / P ; N = 0 ; } else { N = N - Q ; count_Q ++ ; } } if ( N == 0 ) { for ( int i = 0 ; i < count_Q ; i ++ ) cout << Q ; for ( int i = 0 ; i < count_P ; i ++ ) cout << P ; } else { cout << " Not ▁ Possible " ; } } int main ( ) { int N = 32 ; int P = 7 ; int Q = 4 ; printMinInteger ( P , Q , N ) ; return 0 ; }
Minimum number of sum and modulo operations using given numbers to reach target | C ++ implementation of the above approach ; Function to find the minimum moves to reach K from N ; Initialization of dp vector ; dp [ i ] = minimum pushes required to reach i ; Traversing through the buttons ; Iterating through all the positions ; If not visited ; Next status of lock ; Advance to next state ; Return the final dp [ target ] ; Driver function ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minPushes ( int N , int K , vector < int > arr ) { vector < int > dp ( 100000 , -1 ) ; dp [ N ] = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { for ( int xx = 0 ; xx < 100000 ; xx ++ ) { int x = xx ; if ( dp [ x ] == -1 ) continue ; int next = ( x + arr [ i ] ) % 100000 ; while ( dp [ next ] == -1 dp [ next ] > dp [ x ] + 1 ) { dp [ next ] = dp [ x ] + 1 ; x = next ; next = ( next + arr [ i ] ) % 100000 ; } } } return dp [ K ] ; } int main ( ) { int N = 99880 , K = 89 ; vector < int > arr { 100 , 3 } ; cout << minPushes ( N , K , arr ) ; return 0 ; }