codeparrot/xlcost-text-to-code huggingface dataset:
Minimize count of swaps of adjacent elements required to make an array increasing | C ++ program for the above approach ; Function to count minimum number of operations required to obtain an increasing array from given array A [ ] ; Store the required result ; Traverse the array A [ ] ; If the current element is not in its correct position ; Check if it is present at index i - 1 ; Check if it is present at index i - 2 ; Otherwise , print - 1 ( Since A [ i ] can not be swapped more than twice ) ; Print the result ; Driver Code ; Given array ; Store the size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumOperations ( int A [ ] , int n ) { int cnt = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != ( i + 1 ) ) { if ( ( ( i - 1 ) >= 0 ) && A [ i - 1 ] == ( i + 1 ) ) { cnt ++ ; swap ( A [ i ] , A [ i - 1 ] ) ; } else if ( ( ( i - 2 ) >= 0 ) && A [ i - 2 ] == ( i + 1 ) ) { cnt += 2 ; A [ i - 2 ] = A [ i - 1 ] ; A [ i - 1 ] = A [ i ] ; A [ i ] = i + 1 ; } else { cout << -1 ; return ; } } } cout << cnt ; } int main ( ) { int A [ ] = { 7 , 3 , 2 , 1 , 4 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; minimumOperations ( A , n ) ; return 0 ; }
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Set | C ++ program for the above approach ; Function to count the occurrences of X in the generated square matrix ; Store the required result ; Iterate over the range [ 1 , N ] ; Check if x is a multiple of i or not ; Check if the other multiple exists in the range or not ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOccurrences ( int n , int x ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( x % i == 0 ) { if ( x / i <= n ) count ++ ; } } cout << count ; } int main ( ) { int N = 7 , X = 12 ; countOccurrences ( N , X ) ; return 0 ; }
Reduce all array elements to zero by performing given operations thrice | C ++ program of the above approach ; Function to reduce all array elements to zero ; If size of array is 1 ; First operation ; 2 nd Operation ; 3 rd Operation ; Otherwise ; 1 st Operation ; 2 nd Operation ; 3 rd Operation ; Driver Code ; Input ; Function call to make all array elements equal to 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ConvertArray ( int arr [ ] , int N ) { if ( N == 1 ) { cout << " Operation ▁ 1 ▁ : ▁ " << 1 << " ▁ " << 1 << endl ; cout << " Added ▁ elements : ▁ " << -1 * arr [ 0 ] << endl ; cout << endl ; cout << " Operation ▁ 2 ▁ : ▁ " << 1 << " ▁ " << 1 << endl ; cout << " Added ▁ elements : ▁ " << 1 * arr [ 0 ] << endl ; cout << endl ; cout << " Operation ▁ 3 ▁ : ▁ " << 1 << " ▁ " << 1 << endl ; cout << " Added ▁ elements : ▁ " << -1 * arr [ 0 ] << endl ; } else { cout << " Operation ▁ 1 ▁ : ▁ " << 1 << " ▁ " << N << endl ; cout << " Added ▁ elements : ▁ " ; for ( int i = 0 ; i < N ; i ++ ) { cout << -1 * arr [ i ] * N << " ▁ " ; } cout << endl ; cout << endl ; cout << " Operation ▁ 2 ▁ : ▁ " << 1 << " ▁ " << N - 1 << endl ; cout << " Added ▁ elements : ▁ " ; for ( int i = 0 ; i < N - 1 ; i ++ ) { cout << arr [ i ] * ( N - 1 ) << " ▁ " ; } cout << endl ; cout << endl ; cout << " Operation ▁ 3 ▁ : ▁ " << N << " ▁ " << N << endl ; cout << " Added ▁ elements : ▁ " ; cout << arr [ N - 1 ] * ( N - 1 ) << endl ; } } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; ConvertArray ( arr , N ) ; return 0 ; }
Count pairs from an array with even product of count of distinct prime factors | C ++ implementation of the above approach ; Function to calculate count of distinct prime factors of a number ; Sieve of Eratosthenes ; Function to count pairs with even product of distinct prime factors ; Stores count of distinct prime factors ; Stores the count of numbers with even prime factors in B [ ] ; Stores the count of numbers with odd prime factors in B [ ] ; Even Product Pairs ; Traverse the array B [ ] ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; Increment oddCount by 1 ; Increment evenCount by 1 ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; odd * even = even ; If count of prime factors is even ; even * odd = even even * even = even ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000000 NEW_LINE void countOfPrimefactors ( vector < int > & CountDistinct ) { bool prime [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( long long int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( long long int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } } int CountEvenPair ( int A [ ] , int B [ ] , int N , int M ) { vector < int > countDistinct ( MAX + 1 ) ; countOfPrimefactors ( countDistinct ) ; int evenCount = 0 ; int oddCount = 0 ; int evenPairs = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( countDistinct [ B [ i ] ] == 0 ) continue ; if ( countDistinct [ B [ i ] ] & 1 ) { oddCount ++ ; } else { evenCount ++ ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( countDistinct [ A [ i ] ] == 0 ) continue ; if ( countDistinct [ A [ i ] ] & 1 ) { evenPairs += ( evenCount ) ; } else { evenPairs += evenCount + oddCount ; } } return evenPairs ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 4 , 5 , 6 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( B ) / sizeof ( B [ 0 ] ) ; cout << CountEvenPair ( A , B , N , M ) ; return 0 ; }
Minimum array elements required to be subtracted from either end to reduce K to 0 | C ++ program for the above approach ; Function to find the length of longest subarray having sum K ; Stores the index of the prefix sum ; Traverse the given array ; Update sum ; If the subarray starts from index 0 ; Add the current prefix sum with index if it is not present in the map um ; Check if sum - K is present in Map um or not ; Update the maxLength ; Return the required maximum length ; Function to find the minimum removal of array elements required to reduce K to 0 ; Stores the sum of the array ; Traverse the array arr [ ] ; Update sum of the array ; Find maxLen ; If the subarray with sum doesn 't exist ; Otherwise , print the required maximum length ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubarray ( int arr [ ] , int N , int K ) { unordered_map < int , int > um ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; if ( sum == K ) maxLen = i + 1 ; if ( um . find ( sum ) == um . end ( ) ) um [ sum ] = i ; if ( um . find ( sum - K ) != um . end ( ) ) { if ( maxLen < ( i - um [ sum - K ] ) ) maxLen = i - um [ sum - K ] ; } } return maxLen ; } void minRequiredOperation ( int arr [ ] , int N , int K ) { int TotalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) TotalSum += arr [ i ] ; int maxLen = longestSubarray ( arr , N , TotalSum - K ) ; if ( maxLen == -1 ) { cout << -1 ; } else cout << N - maxLen ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 1 , 2 } ; int K = 4 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minRequiredOperation ( arr , N , K ) ; return 0 ; }
Minimum number of digits required to be removed to make a number divisible by 4 | C ++ program for the above approach ; Function to count the minimum number of digits required to be removed to make a given number divisible by 4 ; Store the size of the string ; Stores the required result ; Check for every pair of digits if the number formed by them is divisible by 4 or not ; Store s [ i ] in a variable ; If it is divisible by 2 ; Store the number formed by s [ j ] and s [ i ] ; Check if it is divisible by 4 ; Store the number of digits required to be deleted ; Update ans ; If value of ans is unchanged , then check if any s [ i ] is divisible by 4 ; If true , update ans to n - 1 ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumDeletions ( string s ) { int n = s . length ( ) ; int ans = n ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int t = s [ i ] - '0' ; if ( t % 2 == 0 ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { int num = ( s [ j ] - '0' ) * 10 + t ; if ( num % 4 == 0 ) { int k1 = i - j - 1 ; int k2 = n - i - 1 ; ans = min ( ans , k1 + k2 ) ; } } } } if ( ans == n ) { for ( int i = 0 ; i < n ; i ++ ) { int num = s [ i ] - '0' ; if ( num % 4 == 0 ) { ans = n - 1 ; } } } cout << ans ; } int main ( ) { string str = "12367" ; minimumDeletions ( str ) ; return 0 ; }
Generate a sequence such that float division of array elements is maximized | C ++ program for the above approach ; Function to place the parenthesis such that the result is maximized ; Store the required string ; Add the first integer to string ; If the size of array is 1 ; If the size of array is 2 , print the 1 st integer followed by / operator followed by the next integer ; If size of array is exceeds two , print 1 st integer concatenated with operators ' / ' , ' ( ' and next integers with the operator ' / ' ; Add parenthesis at the end ; Print the final expression ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateSequence ( int arr [ ] , int n ) { string ans ; ans = to_string ( arr [ 0 ] ) ; if ( n == 1 ) cout << ans ; else if ( n == 2 ) { cout << ans + " / " << to_string ( arr [ 1 ] ) ; } else { ans += " / ( " + to_string ( arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { ans += " / " + to_string ( arr [ i ] ) ; } ans += " ) " ; cout << ans ; } } int main ( ) { int arr [ ] = { 1000 , 100 , 10 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; generateSequence ( arr , N ) ; return 0 ; }
Count of pairs having even and odd LCM from an array | C ++ program for the above approach ; Function to find count of distinct pairs having even LCM and odd LCM ; Store the total number of pairs ; Stores the count of odd numbers in the array ; Traverse the array arr [ ] ; Update the count of pairs with odd LCM ; Print the count of required pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LCMPairs ( int arr [ ] , int N ) { int total_pairs = ( N * ( N - 1 ) ) / 2 ; int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] & 1 ) odd ++ ; } odd = ( odd * ( odd - 1 ) ) / 2 ; cout << " Even ▁ = ▁ " << total_pairs - odd << " , ▁ Odd ▁ = ▁ " << odd ; } int main ( ) { int arr [ ] = { 3 , 6 , 5 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; LCMPairs ( arr , N ) ; return 0 ; }
Value required to be added to N to obtain the sum of first M multiples of K | C ++ program for the above approach ; Function to print the value to be added to N to obtain sum of first M multiples of K ; Store the sum of the first M multiples of K ; Store the value to be added to obtain N ; Driver Code ; Input
#include <iostream> NEW_LINE using namespace std ; static int printNumber ( int N , int K , int M ) { int sum = K * ( M * ( M + 1 ) / 2 ) ; return sum - N ; } int main ( ) { int N = 17 ; int K = 3 ; int M = 4 ; cout << printNumber ( N , K , M ) ; return 0 ; }
Count even and odd Bitwise XORs of consecutive numbers in a range [ L , R ] starting from L | C ++ program for the above approach ; Print count of even and odd numbers of XOR value from L to R ; Store the number of elements between L and R ; Count of even XOR values ; If L is odd and range % 4 = 3 ; Increment even by 1 ; If L is even and range % 4 != 0 ; Increment even by 1 ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countEvenOdd ( int L , int R ) { int range = R - L + 1 ; int even = ( range / 4 ) * 2 ; if ( ( L & 1 ) && ( range % 4 == 3 ) ) { even ++ ; } else if ( ! ( L & 1 ) && ( range % 4 ) ) { even ++ ; } cout << " Even ▁ = ▁ " << even << " , ▁ Odd ▁ = ▁ " << range - even ; } int main ( ) { int L = 2 , R = 7 ; countEvenOdd ( L , R ) ; return 0 ; }
Minimize swaps of same | C ++ program for the above approach ; Function to count the number of swaps required to make the sum of ASCII values of the characters of both strings odd ; Initialize alphabets with value ; Initialize values for each alphabet ; Size of the string ; Sum of string S ; Sum of string T ; Stores whether there is any index i such that S [ i ] and T [ i ] have different parities ; Traverse the strings ; Update sum1 and sum2 ; If S [ i ] and T [ i ] have different parities ; If sum1 and sum2 are both odd ; If sum1 and sum2 are both even ; If exists print 1 ; Otherwise ; If sum1 and sum2 are of different parities ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSwaps ( string S , string T ) { int value [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) value [ i ] = i + 1 ; int N = S . size ( ) ; int sum1 = 0 ; int sum2 = 0 ; bool flag = false ; for ( int i = 0 ; i < N ; i ++ ) { sum1 += value [ S [ i ] - ' a ' ] ; sum2 += value [ T [ i ] - ' a ' ] ; if ( ( value [ S [ i ] - ' a ' ] % 2 == 0 && value [ T [ i ] - ' a ' ] % 2 == 1 ) || ( value [ S [ i ] - ' a ' ] % 2 == 1 && value [ T [ i ] - ' a ' ] % 2 == 0 ) ) flag = false ; } if ( sum1 % 2 == 1 && sum2 % 2 == 1 ) cout << "0 STRNEWLINE " ; else if ( sum1 % 2 == 0 && sum2 % 2 == 0 ) { if ( flag ) cout << "1" ; else cout << " - 1" ; } else { cout << " - 1" ; } } int main ( ) { string S = " acd " ; string T = " dbf " ; countSwaps ( S , T ) ; return 0 ; }
Count distinct prime triplets up to N such that sum of two primes is equal to the third prime | C ++ program for the above approach ; Function to check if a number is a prime or not ; Function to count the number of valid prime triplets ; Stores the count of prime triplets ; Iterate from 2 to N and check for each p , whether p & ( p - 2 ) are prime or not ; Print the count obtained ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int N ) { if ( N <= 1 ) return false ; for ( int i = 2 ; i <= sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } void countPrimeTuples ( int N ) { int count = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime ( i ) && isPrime ( i - 2 ) ) count ++ ; } cout << count ; } int main ( ) { int N = 6 ; countPrimeTuples ( N ) ; return 0 ; }
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | C ++ program for the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Store the required answer ; Check for all possible pairs ; If the condition satisfy then increment count by 1 ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int A [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( A [ i ] A [ j ] ) > ( A [ i ] & A [ j ] ) ) { count ++ ; } } cout << count ; } int main ( ) { int A [ ] = { 1 , 4 , 7 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; countPairs ( A , N ) ; return 0 ; }
Count maximum number of consumable candies | C ++ implememtation of the above approach ; Function to find the count of maximum consumable candies ; Store the count of total candies ; Stores the count of maximum consumable candies ; Checks if it is safe to counsume all candies ; Traverse the array arr [ ] ; If A [ i ] + M is greater than B [ i ] ; Mark all_safe as false ; Update ans ; Update ans ; Increment total by A [ i ] ; If all_safe is true ; Otherwise , ; Driver Code ; Function call to find maximum consumable candies
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumCandy ( int candies [ ] , int safety [ ] , int N , int M ) { int total = 0 ; int ans = INT_MAX ; bool all_safe = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( candies [ i ] + M > safety [ i ] ) { all_safe = false ; ans = min ( ans , safety [ i ] ) ; } else { ans = min ( ans , candies [ i ] + M ) ; } total += candies [ i ] ; } if ( all_safe ) return total ; else return ans ; } int main ( ) { int A [ ] = { 2 , 4 , 1 , 9 , 6 } ; int B [ ] = { 8 , 7 , 3 , 12 , 7 } ; int M = 0 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << maximumCandy ( A , B , N , M ) ; return 0 ; }
Check if two arrays can be made equal by swapping pairs of one of the arrays | C ++ program for above approach ; Function to check if two arrays can be made equal or not by swapping pairs of only one of the arrays ; Stores elements required to be replaced ; To check if the arrays can be made equal or not ; Traverse the array ; If array elements are not equal ; Increment count by 1 ; Decrement count by 1 ; If flag is true and count is 0 , print " Yes " . Otherwise " No " ; Driver Code ; Given arrays ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkArrays ( int arr1 [ ] , int arr2 [ ] , int N ) { int count = 0 ; bool flag = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { if ( arr1 [ i ] == 0 ) count ++ ; else { count -- ; if ( count < 0 ) { flag = 0 ; break ; } } } } if ( flag && count == 0 ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { int arr1 [ ] = { 0 , 0 , 1 , 1 } ; int arr2 [ ] = { 1 , 1 , 0 , 0 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; checkArrays ( arr1 , arr2 , N ) ; return 0 ; }
Generate an N | C ++ program for the above approach ; Function to construct an array with sum of each subarray divisible by K ; Traverse a loop from 1 to N ; Print i - th multiple of K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void construct_Array ( int N , int K ) { for ( int i = 1 ; i <= N ; i ++ ) { cout << K * i << " ▁ " ; } } int main ( ) { int N = 3 , K = 3 ; construct_Array ( N , K ) ; return 0 ; }
Generate an N | C ++ Program to implement the above approach ; Fun dtion to print the required sequence ; Stores count of even and odd elements ; Stores sum of even and odd elements ; Print N / 2 even elements ; Print N / 2 - 1 odd elements ; Print final odd element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Print ( int N ) { if ( ( N / 2 ) % 2 == 1 || ( N % 2 == 1 ) ) { cout << -1 << endl ; return ; } int CurEven = 2 , CurOdd = 1 ; int SumOdd = 0 , SumEven = 0 ; for ( int i = 0 ; i < ( N / 2 ) ; i ++ ) { cout << CurEven << " ▁ " ; SumEven += CurEven ; CurEven += 2 ; } for ( int i = 0 ; i < N / 2 - 1 ; i ++ ) { cout << CurOdd << " ▁ " ; SumOdd += CurOdd ; CurOdd += 2 ; } CurOdd = SumEven - SumOdd ; cout << CurOdd << ' ' ; } int main ( ) { int N = 12 ; Print ( N ) ; return 0 ; }
Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements | C ++ program for the above approach ; Function to print the last remaining array element after after performing given operations ; If size of the array is 1 ; Check for the condition ; If condition is not satisfied ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canArrayBeReduced ( int arr [ ] , int N ) { if ( N == 1 ) { cout << arr [ 0 ] ; return ; } if ( arr [ 0 ] < arr [ N - 1 ] ) { cout << arr [ N - 1 ] ; } else cout << " Not ▁ Possible " ; } int main ( ) { int arr [ ] = { 6 , 5 , 2 , 4 , 1 , 3 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; canArrayBeReduced ( arr , N ) ; return 0 ; }
Minimum prefix increments required to make all elements of an array multiples of another array | C ++ program for the above approach ; Function to find minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores the carry ; Stores minimum difference of correspoinding element in prefix subarray ; Traverse the array ; Stores the closest greater or equal number to A [ i ] which is a multiple of B [ i ] ; Stores minimum difference ; Update totalOperations ; Update carry ; Driver Code ; Input arrays A [ ] and B [ ] ; Length of arrays
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumMoves ( int A [ ] , int B [ ] , int N ) { int totalOperations = 0 ; int carry = 0 ; int K = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { int nearestMultiple = ceil ( ( double ) ( A [ i ] + carry ) / ( double ) ( B [ i ] ) ) * B [ i ] ; K = nearestMultiple - ( A [ i ] + carry ) ; totalOperations += K ; carry += K ; } return totalOperations ; } int main ( ) { int A [ ] = { 3 , 4 , 5 , 2 , 5 , 5 , 9 } ; int B [ ] = { 1 , 1 , 9 , 6 , 3 , 8 , 7 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << MinimumMoves ( A , B , N ) << endl ; return 0 ; }
Minimize deviation of an array by given operations | C ++ implementation of the above approach ; Function to find the minimum deviation of the array A [ ] ; Store all array elements in sorted order ; Odd number are transformed using 2 nd operation ; ( Maximum - Minimum ) ; Check if the size of set is > 0 and the maximum element is divisible by 2 ; Maximum element of the set ; Erase the maximum element ; Using operation 1 ; ( Maximum - Minimum ) ; Print the Minimum Deviation Obtained ; Driver Code ; Function Call to find Minimum Deviation of A [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumDeviation ( int A [ ] , int N ) { set < int > s ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) s . insert ( A [ i ] ) ; else s . insert ( 2 * A [ i ] ) ; } int diff = * s . rbegin ( ) - * s . begin ( ) ; while ( ( int ) s . size ( ) && * s . rbegin ( ) % 2 == 0 ) { int maxEl = * s . rbegin ( ) ; s . erase ( maxEl ) ; s . insert ( maxEl / 2 ) ; diff = min ( diff , * s . rbegin ( ) - * s . begin ( ) ) ; } cout << diff ; } int main ( ) { int A [ ] = { 4 , 1 , 5 , 20 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; minimumDeviation ( A , N ) ; return 0 ; }
Maximize given integer by swapping pairs of unequal bits | C ++ implementation of the above approach ; Function to return the maximum possible value that can be obtained from the given integer by performing given operations ; Convert to equivalent binary representation ; Stores binary representation of the maximized value ; Store the count of 0 ' s ▁ and ▁ 1' s ; Stores the total Number of Bits ; If current bit is set ; Increment Count1 ; Increment Count0 ; Shift all set bits to left to maximize the value ; Shift all unset bits to right to maximize the value ; Return the maximized value ; Driver code ; Given "Input ; Function call to find the Maximum Possible Number
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxNum ( int num ) { bitset < 4 > b ( num ) ; string binaryNumber = b . to_string ( ) ; string maxBinaryNumber = " " ; int count0 = 0 , count1 = 0 ; int N = 4 ; for ( int i = 0 ; i < N ; i ++ ) { if ( binaryNumber [ i ] == '1' ) { count1 ++ ; } else { count0 ++ ; } } for ( int i = 0 ; i < count1 ; i ++ ) { maxBinaryNumber += '1' ; } for ( int i = 0 ; i < count0 ; i ++ ) { maxBinaryNumber += '0' ; } return stoi ( maxBinaryNumber , 0 , 2 ) ; } int main ( ) { int N = 11 ; cout << findMaxNum ( N ) ; return 0 ; }
Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2 | C ++ implementation of the above approach ; Function to find minimum count of digits required to be remove to make N odd and the sum of digits of N even ; Stores count of even digits ; Stores count of odd digits ; Iterate over the digits of N ; If current digit is even ; Update even ; Otherwise ; Update odd ; Base conditions ; Stores count of digits required to be removed to make N odd and the sum of digits of N even ; Iterate over the digits of N ; If current digit is even ; Update ans ; Otherwise , ; Update ans ; If count of odd digits is odd ; Update ans ; Finally print the ans ; Driver code ; Input string ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperations ( string & N ) { int even = 0 ; int odd = 0 ; for ( auto it : N ) { if ( ( it - '0' ) % 2 == 0 ) { even ++ ; } else { odd ++ ; } } if ( odd == 0 odd == 1 ) { cout << " Not ▁ Possible " << " STRNEWLINE " ; } else { int ans = 0 ; for ( auto it : N ) { if ( ( it - '0' ) % 2 == 0 ) { ans ++ ; } else { ans = 0 ; } } if ( odd % 2 ) { ans ++ ; } cout << ans << endl ; } } int main ( ) { string N = "12345" ; minOperations ( N ) ; }