codeparrot/xlcost-text-to-code huggingface dataset:
Count number of elements between two given elements in array | Program to count number of elements between two given elements . ; Function to count number of elements occurs between the elements . ; Find num1 ; If num1 is not present or present at end ; Find num2 ; If num2 is not present ; return number of elements between the two elements . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int arr [ ] , int n , int num1 , int num2 ) { int i = 0 ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == num1 ) break ; if ( i >= n - 1 ) return 0 ; int j ; for ( j = n - 1 ; j >= i + 1 ; j -- ) if ( arr [ j ] == num2 ) break ; if ( j == i ) return 0 ; return ( j - i - 1 ) ; } int main ( ) { int arr [ ] = { 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int num1 = 5 , num2 = 4 ; cout << getCount ( arr , n , num1 , num2 ) ; return 0 ; }
Best meeting point in 2D binary array | C ++ program to find best meeting point in 2D array ; Find all members home 's position ; Sort positions so we can find most beneficial point ; middle position will always beneficial for all group members but it will be sorted which we have already done ; Now find total distance from best meeting point ( x , y ) using Manhattan Distance formula ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ROW  3 NEW_LINE #define COL  5 NEW_LINE int minTotalDistance ( int grid [ ] [ COL ] ) { if ( ROW == 0 COL == 0 ) return 0 ; vector < int > vertical ; vector < int > horizontal ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( grid [ i ] [ j ] == 1 ) { vertical . push_back ( i ) ; horizontal . push_back ( j ) ; } } } sort ( vertical . begin ( ) , vertical . end ( ) ) ; sort ( horizontal . begin ( ) , horizontal . end ( ) ) ; int size = vertical . size ( ) / 2 ; int x = vertical [ size ] ; int y = horizontal [ size ] ; int distance = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( grid [ i ] [ j ] == 1 ) distance += abs ( x - i ) + abs ( y - j ) ; return distance ; } int main ( ) { int grid [ ROW ] [ COL ] = { { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 } } ; cout << minTotalDistance ( grid ) ; return 0 ; }
Count numbers with difference between number and its digit sum greater than specific value | C ++ program to count total numbers which have difference with sum of digits greater than specific value ; Utility method to get sum of digits of K ; loop until K is not zero ; method returns count of numbers smaller than N , satisfying difference condition ; binary search while loop ; if difference between number and its sum of digit is smaller than given difference then smallest number will be on left side ; if difference between number and its sum of digit is greater than or equal to given difference then smallest number will be on right side ; return the difference between ' smallest ▁ number ▁ ▁ found ' and ' N ' as result ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfDigit ( int K ) { int sod = 0 ; while ( K ) { sod += K % 10 ; K /= 10 ; } return sod ; } int totalNumbersWithSpecificDifference ( int N , int diff ) { int low = 1 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( mid - sumOfDigit ( mid ) < diff ) low = mid + 1 ; else high = mid - 1 ; } return ( N - high ) ; } int main ( ) { int N = 13 ; int diff = 2 ; cout << totalNumbersWithSpecificDifference ( N , diff ) ; return 0 ; }
Randomized Binary Search Algorithm | C ++ program to implement iterative randomized algorithm . ; To generate random number between x and y ie . . [ x , y ] ; A iterative randomized binary search function . It returns location of x in given array arr [ l . . r ] if present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; if we reach here , then element was not present ; Driver code
#include <iostream> NEW_LINE #include <ctime> NEW_LINE using namespace std ; int getRandom ( int x , int y ) { srand ( time ( NULL ) ) ; return ( x + rand ( ) % ( y - x + 1 ) ) ; } int randomizedBinarySearch ( int arr [ ] , int l , int r , int x ) { while ( l <= r ) { int m = getRandom ( l , r ) ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return -1 ; } int main ( void ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 10 ; int result = randomizedBinarySearch ( arr , 0 , n - 1 , x ) ; ( result == -1 ) ? printf ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) : printf ( " Element ▁ is ▁ present ▁ at ▁ index ▁ % d " , result ) ; return 0 ; }
Number of buildings facing the sun | C ++ program to count buildings that can see sunlight . ; Returns count buildings that can see sunlight ; Initialuze result ( Note that first building always sees sunlight ) ; Start traversing element ; If curr_element is maximum or current element is equal , update maximum and increment count ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countBuildings ( int arr [ ] , int n ) { int count = 1 ; int curr_max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > curr_max arr [ i ] == curr_max ) { count ++ ; curr_max = arr [ i ] ; } } return count ; } int main ( ) { int arr [ ] = { 7 , 4 , 8 , 2 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countBuildings ( arr , n ) ; return 0 ; }
Find index of an extra element present in one sorted array | C ++ program to find an extra element present in arr1 [ ] ; Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Driver code ; Solve is passed both arrays
#include <iostream> NEW_LINE using namespace std ; int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ; return n ; } int main ( ) { int arr1 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arr2 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << findExtra ( arr1 , arr2 , n ) ; return 0 ; }
Find index of an extra element present in one sorted array | C ++ program to find an extra element present in arr1 [ ] ; Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Initialize result ; left and right are end points denoting the current range . ; If middle element is same of both arrays , it means that extra element is after mid so we update left to mid + 1 ; If middle element is different of the arrays , it means that the index we are searching for is either mid , or before mid . Hence we update right to mid - 1. ; when right is greater than left our search is complete . ; Driver code ; Solve is passed both arrays
#include <iostream> NEW_LINE using namespace std ; int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { int index = n ; int left = 0 , right = n - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr2 [ mid ] == arr1 [ mid ] ) left = mid + 1 ; else { index = mid ; right = mid - 1 ; } } return index ; } int main ( ) { int arr1 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arr2 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << findExtra ( arr1 , arr2 , n ) ; return 0 ; }
Make all array elements equal with minimum cost | C ++ program to find minimum cost to make all elements equal ; Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver code to test above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int computeCost ( int arr [ ] , int N , int X ) { int cost = 0 ; for ( int i = 0 ; i < N ; i ++ ) cost += abs ( arr [ i ] - X ) ; return cost ; } int minCostToMakeElementEqual ( int arr [ ] , int N ) { int low , high ; low = high = arr [ 0 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( low > arr [ i ] ) low = arr [ i ] ; if ( high < arr [ i ] ) high = arr [ i ] ; } while ( ( high - low ) > 2 ) { int mid1 = low + ( high - low ) / 3 ; int mid2 = high - ( high - low ) / 3 ; int cost1 = computeCost ( arr , N , mid1 ) ; int cost2 = computeCost ( arr , N , mid2 ) ; if ( cost1 < cost2 ) high = mid2 ; else low = mid1 ; } return computeCost ( arr , N , ( low + high ) / 2 ) ; } int main ( ) { int arr [ ] = { 1 , 100 , 101 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << minCostToMakeElementEqual ( arr , N ) ; return 0 ; }
Minimum number of jumps required to sort numbers placed on a number line | C ++ program for the above approach ; Function to find the minimum number of jumps required to sort the array ; Base Case ; Store the required result ; Stores the current position of elements and their respective maximum jump ; Used to check if a position is already taken by another element ; Stores the sorted array a [ ] ; Traverse the array w [ ] & update positions jumps array a [ ] ; Sort the array a [ ] ; Traverse the array a [ ] over the range [ 1 , N - 1 ] ; Store the index of current element and its just smaller element in array w [ ] ; Iterate until current element position is at most its just smaller element position ; Update the position of the current element ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minJumps ( int w [ ] , int l [ ] , int n ) { if ( n == 1 ) { cout << 0 ; return ; } int ans = 0 ; unordered_map < int , int > pos , jump ; unordered_map < int , bool > filled ; int a [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { pos [ w [ i ] ] = i ; filled [ i ] = true ; jump [ w [ i ] ] = l [ i ] ; a [ i ] = w [ i ] ; } sort ( a , a + n ) ; for ( int curr = 1 ; curr < n ; curr ++ ) { int currElementPos = pos [ a [ curr ] ] ; int prevElementPos = pos [ a [ curr - 1 ] ] ; if ( currElementPos > prevElementPos ) continue ; while ( currElementPos <= prevElementPos filled [ currElementPos ] ) { currElementPos += jump [ a [ curr ] ] ; ans ++ ; } pos [ a [ curr ] ] = currElementPos ; filled [ currElementPos ] = true ; } cout << ans ; } int main ( ) { int W [ ] = { 2 , 1 , 4 , 3 } ; int L [ ] = { 4 , 1 , 2 , 4 } ; int N = sizeof ( W ) / sizeof ( W [ 0 ] ) ; minJumps ( W , L , N ) ; return 0 ; }
Make an array strictly increasing by repeatedly subtracting and adding arr [ i | C ++ program for the above approach ; Function to check if an array can be made strictly increasing ; Traverse the given array arr [ ] ; Update the value of p , arr [ i ] , and arr [ i - 1 ] ; Traverse the given array ; Check if the array arr [ ] is strictly increasing or not ; Otherwise , array is increasing order , print Yes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= ( i - 1 ) ) { int p = arr [ i - 1 ] - ( i - 1 ) ; arr [ i ] += p ; arr [ i - 1 ] -= p ; } } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= arr [ i - 1 ] ) { cout << " No " ; return ; } } cout << " Yes " ; } int main ( ) { int arr [ ] = { 1 , 5 , 2 , 7 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; check ( arr , N ) ; return 0 ; }
Rearrange array to make decimal equivalents of reversed binary representations of array elements sorted | C ++ program for the above approach ; Function to reverse the bits of a number ; Stores the reversed number ; Divide rev by 2 ; If the value of N is odd ; Update the value of N ; Return the final value of rev ; Function for rearranging the array element according to the given rules ; Stores the new array elements ; Function for rearranging the array ; Stores the new array ; Function to sort the array by reversing binary representation ; Creating a new array ; Sort the array with the key ; Get arr from newArr ; Print the sorted array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int keyFunc ( int n ) { int rev = 0 ; while ( n > 0 ) { rev = rev << 1 ; if ( n & 1 == 1 ) rev = rev ^ 1 ; n = n >> 1 ; } return rev ; } vector < vector < int > > getNew ( vector < int > arr ) { vector < vector < int > > ans ; for ( int i : arr ) ans . push_back ( { keyFunc ( i ) , i } ) ; return ans ; } vector < int > getArr ( vector < vector < int > > arr ) { vector < int > ans ; for ( auto i : arr ) ans . push_back ( i [ 1 ] ) ; return ans ; } void sortArray ( vector < int > arr ) { vector < vector < int > > newArr = getNew ( arr ) ; sort ( newArr . begin ( ) , newArr . end ( ) ) ; arr = getArr ( newArr ) ; int n = arr . size ( ) ; cout << " [ " ; for ( int i = 0 ; i < n - 1 ; i ++ ) cout << arr [ i ] << " , ▁ " ; cout << arr [ n - 1 ] << " ] " ; } int main ( ) { vector < int > arr = { 43 , 52 , 61 , 41 } ; sortArray ( arr ) ; return 0 ; }
Minimize maximum array element by splitting array elements into powers of two at most K times | C ++ program for the above approach ; Function to find the minimum value of the maximum element of the array by splitting at most K array element into perfect powers of 2 ; Sort the array element in the ascending order ; Reverse the array ; If count of 0 is equal to N ; Otherwise , if K is greater than or equal to N ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumSize ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; reverse ( arr , arr + N ) ; if ( count ( arr , arr + N , 0 ) == N ) cout << 0 ; else if ( K >= N ) cout << 1 << endl ; else cout << arr [ K ] << endl ; } int main ( ) { int arr [ ] = { 2 , 4 , 8 , 2 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumSize ( arr , N , K ) ; return 0 ; }
Check if removal of a subsequence of non | C ++ program for the above approach ; Function to check if it is possible to sort the array or not ; Stores the index if there are two consecutive 1 's in the array ; Traverse the given array ; Check adjacent same elements having values 1 s ; If there are no two consecutive 1 s , then always remove all the 1 s from array & make it sorted ; If two consecutive 0 ' s ▁ are ▁ ▁ present ▁ after ▁ two ▁ consecutive ▁ ▁ 1' s then array can 't be sorted ; Otherwise , print Yes ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isPossibleToSort ( int arr [ ] , int N ) { int idx = -1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 1 && arr [ i - 1 ] == 1 ) { idx = i ; break ; } } if ( idx == -1 ) { cout << " YES " ; return ; } for ( int i = idx + 1 ; i < N ; i ++ ) { if ( arr [ i ] == 0 && arr [ i - 1 ] == 0 ) { cout << " NO " ; return ; } } cout << " YES " ; } int main ( ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; isPossibleToSort ( arr , N ) ; return 0 ; }
Maximize ropes of consecutive length possible by connecting given ropes | C ++ program for the above approach ; Function to find maximized count of ropes of consecutive length ; Stores the maximum count of ropes of consecutive length ; Sort the ropes by their length ; Traverse the array ; If size of the current rope is less than or equal to current maximum possible size + 1 , update the range to curSize + ropes [ i ] ; If a rope of size ( curSize + 1 ) cannot be obtained ; Driver Code ; Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxConsecutiveRopes ( int ropes [ ] , int N ) { int curSize = 0 ; sort ( ropes , ropes + N ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ropes [ i ] <= curSize + 1 ) { curSize = curSize + ropes [ i ] ; } else break ; } return curSize ; } int main ( ) { int N = 5 ; int ropes [ ] = { 1 , 2 , 7 , 1 , 1 } ; cout << maxConsecutiveRopes ( ropes , N ) ; return 0 ; }
Maximum ranges that can be uniquely represented by any integer from the range | C ++ program for the above approach ; Function to find the maximum number of ranges where each range can be uniquely represented by an integer ; Sort the ranges in ascending order ; Stores the count of ranges ; Stores previously assigned range ; Traverse the vector arr [ ] ; Skip the similar ranges of size 1 ; Find the range of integer available to be assigned ; Check if an integer is available to be assigned ; Update the previously assigned range ; Update the count of range ; Return the maximum count of ranges ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxRanges ( vector < vector < int > > arr , int N ) { sort ( arr . begin ( ) , arr . end ( ) ) ; int count = 1 ; vector < int > prev = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { vector < int > last = arr [ i - 1 ] ; vector < int > current = arr [ i ] ; if ( last [ 0 ] == current [ 0 ] && last [ 1 ] == current [ 1 ] && current [ 1 ] == current [ 0 ] ) continue ; int start = max ( prev [ 0 ] , current [ 0 ] - 1 ) ; int end = max ( prev [ 1 ] , current [ 1 ] ) ; if ( end - start > 0 ) { prev [ 0 ] = 1 + start ; prev [ 1 ] = end ; count ++ ; } } return count ; } int main ( ) { vector < vector < int > > range = { { 1 , 4 } , { 4 , 4 } , { 2 , 2 } , { 3 , 4 } , { 1 , 1 } } ; int N = range . size ( ) ; cout << maxRanges ( range , N ) ; return 0 ; }
Sort a string lexicographically by reversing a substring | C ++ program for the above approach ; Function to find the substring in S required to be reversed ; Stores the size of the string ; Stores the starting point of the substring ; Iterate over the string S while i < N ; Increment the value of i ; Stores the ending index of the substring ; If start <= 0 or i >= N ; If start >= 1 and i <= N ; Return the boolean value ; If start >= 1 ; Return the boolean value ; If i < N ; Return true if S [ start ] is less than or equal to S [ i ] ; Otherwise ; Function to check if it is possible to sort the string or not ; Stores the starting and the ending index of substring ; Stores whether it is possible to sort the substring ; Traverse the range [ 1 , N ] ; If S [ i ] is less than S [ i - 1 ] ; If flag stores true ; If adjust ( S , i , start , end ) return false ; Print - 1 ; Unset the flag ; Otherwise ; Print - 1 ; If start is equal to - 1 ; Update start and end ; Print the value of start and end ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool adjust ( string & S , int & i , int & start , int & end ) { int N = S . length ( ) ; start = i - 1 ; while ( i < N && S [ i ] < S [ i - 1 ] ) { i ++ ; } end = i - 1 ; if ( start <= 0 && i >= N ) return true ; if ( start >= 1 && i <= N ) { return ( S [ end ] >= S [ start - 1 ] && S [ start ] <= S [ i ] ) ; } if ( start >= 1 ) { return S [ end ] >= S [ start - 1 ] ; } if ( i < N ) { return S [ start ] <= S [ i ] ; } return false ; } void isPossible ( string & S , int N ) { int start = -1 , end = -1 ; bool flag = true ; for ( int i = 1 ; i < N ; i ++ ) { if ( S [ i ] < S [ i - 1 ] ) { if ( flag ) { if ( adjust ( S , i , start , end ) == false ) { cout << -1 << endl ; return ; } flag = false ; } else { cout << -1 << endl ; return ; } } } if ( start == -1 ) { start = end = 1 ; } cout << start << " ▁ " << end << " STRNEWLINE " ; } int main ( ) { string S = " abcyxuz " ; int N = S . length ( ) ; isPossible ( S , N ) ; return 0 ; }
Maximum amount of capital required for selecting at most K projects | C ++ program for the above approach ; Function to calculate maximum capital obtained after choosing at most K projects whose capital is less than the given cost of projects ; Stores all projects with capital at most W ; Stores the pair of { C [ i ] , i } ; Traverse the vector C [ ] ; Sort the vector v ; If capital is at most W ; Push the profit into priority queue ; Increment j by one ; If pq is not empty ; Update the capital W ; Delete the top of pq ; Decrement K by one ; Return the maximum capital ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximizedCapital ( int K , int W , vector < int > & profits , vector < int > & capital ) { priority_queue < int > pq ; vector < pair < int , int > > v ; for ( int i = 0 ; i < capital . size ( ) ; i ++ ) { v . push_back ( { capital [ i ] , i } ) ; } sort ( v . begin ( ) , v . end ( ) ) ; int j = 0 ; while ( K ) { while ( j < ( int ) capital . size ( ) && v [ j ] . first <= W ) { pq . push ( profits [ v [ j ] . second ] ) ; j ++ ; } if ( ! pq . empty ( ) ) { W = W + pq . top ( ) ; pq . pop ( ) ; } K -- ; } return W ; } int main ( ) { int K = 2 ; int W = 0 ; vector < int > P = { 1 , 2 , 3 } ; vector < int > C = { 0 , 1 , 1 } ; cout << maximizedCapital ( K , W , P , C ) ; return 0 ; }
Split array into K non | C ++ program for the above approach ; Function find maximum sum of minimum and maximum of K subsets ; Stores the result ; Sort the array arr [ ] in decreasing order ; Traverse the range [ 0 , K ] ; Sort the array S [ ] in ascending order ; Traverse the array S [ ] ; If S { i ] is 1 ; Stores the index of the minimum element of the i - th subset ; Traverse the array S [ ] ; Update the counter ; Return the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( int arr [ ] , int S [ ] , int N , int K ) { int ans = 0 ; sort ( arr , arr + N , greater < int > ( ) ) ; for ( int i = 0 ; i < K ; i ++ ) ans += arr [ i ] ; sort ( S , S + K ) ; for ( int i = 0 ; i < K ; i ++ ) { if ( S [ i ] == 1 ) ans += arr [ i ] ; S [ i ] -- ; } int counter = K - 1 ; for ( int i = 0 ; i < K ; i ++ ) { counter = counter + S [ i ] ; if ( S [ i ] != 0 ) ans += arr [ counter ] ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 13 , 7 , 17 } ; int S [ ] = { 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = sizeof ( S ) / sizeof ( S [ 0 ] ) ; cout << maximumSum ( arr , S , N , K ) ; return 0 ; }
Modify an array by sorting after reversal of bits of each array element | C ++ implementation of the above approach ; Function to convert binary number to equivalent decimal ; Set base value to 1 , i . e 2 ^ 0 ; Function to convert a decimal to equivalent binary representation ; Stores the binary representation ; Since ASCII value of '0' , '1' are 48 and 49 ; As the string is already reversed , no further reversal is required ; Function to convert the reversed binary representation to equivalent integer ; Stores reversed binary representation of given decimal ; Stores equivalent decimal value of the binary representation ; Return the resultant integer ; Utility function to print the sorted array ; Sort the array ; Traverse the array ; Print the array elements ; Utility function to reverse the binary representations of all array elements and sort the modified array ; Traverse the array ; Passing array elements to reversedBinaryDecimal function ; Pass the array to the sorted array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binaryToDecimal ( string n ) { string num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } string decimalToBinary ( int n ) { string binstr = " " ; while ( n > 0 ) { binstr += ( n % 2 + 48 ) ; n = n / 2 ; } return binstr ; } int reversedBinaryDecimal ( int N ) { string decimal_to_binar = decimalToBinary ( N ) ; int binary_to_decimal = binaryToDecimal ( decimal_to_binar ) ; return binary_to_decimal ; } void printSortedArray ( int arr [ ] , int size ) { sort ( arr , arr + size ) ; for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } void modifyArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { arr [ i ] = reversedBinaryDecimal ( arr [ i ] ) ; } printSortedArray ( arr , size ) ; } int main ( ) { int arr [ ] = { 98 , 43 , 66 , 83 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; modifyArray ( arr , n ) ; return 0 ; }