codeparrot/xlcost-text-to-code huggingface dataset:
Reverse tree path | CPP program to Reverse Tree path ; A Binary Tree Node ; ' data ' is input . We need to reverse path from root to data . ' level ' is current level . ' temp ' that stores path nodes . ' nextpos ' used to pick next item for reversing . ; return NULL if root NULL ; Final condition if the node is found then ; store the value in it 's level ; change the root value with the current next element of the map ; increment in k for the next element ; store the data in perticular level ; We go to right only when left does not contain given data . This way we make sure that correct path node is stored in temp [ ] ; If current node is part of the path , then do reversing . ; return NULL if not element found ; Reverse Tree path ; store per level data ; it is for replacing the data ; reverse tree path ; INORDER ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1 ; Reverse Tree Path ; Traverse inorder
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; Node * reverseTreePathUtil ( Node * root , int data , map < int , int > & temp , int level , int & nextpos ) { if ( root == NULL ) return NULL ; if ( data == root -> data ) { temp [ level ] = root -> data ; root -> data = temp [ nextpos ] ; nextpos ++ ; return root ; } temp [ level ] = root -> data ; Node * left , * right ; left = reverseTreePathUtil ( root -> left , data , temp , level + 1 , nextpos ) ; if ( left == NULL ) right = reverseTreePathUtil ( root -> right , data , temp , level + 1 , nextpos ) ; if ( left right ) { root -> data = temp [ nextpos ] ; nextpos ++ ; return ( left ? left : right ) ; } return NULL ; } void reverseTreePath ( Node * root , int data ) { map < int , int > temp ; int nextpos = 0 ; reverseTreePathUtil ( root , data , temp , 0 , nextpos ) ; } void inorder ( Node * root ) { if ( root != NULL ) { inorder ( root -> left ) ; cout << root -> data << " ▁ " ; inorder ( root -> right ) ; } } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 7 ) ; root -> left = newNode ( 6 ) ; root -> right = newNode ( 5 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 3 ) ; root -> right -> left = newNode ( 2 ) ; root -> right -> right = newNode ( 1 ) ; int data = 4 ; reverseTreePath ( root , data ) ; inorder ( root ) ; return 0 ; }
Longest Subarray with first element greater than or equal to Last element | CPP program to find length of the longest array with first element smaller than or equal to last element . ; Search function for searching the first element of the subarray which is greater or equal to the last element ( num ) ; Returns length of the longest array with first element smaller than the last element . ; Search space for the potential first elements . ; It will store the Indexes of the elements of search space in the original array . ; Initially the search space is empty . ; We will add an ith element in the search space if the search space is empty or if the ith element is greater than the last element of the search space . ; we will search for the index first element in the search space and we will use it find the index of it in the original array . ; Update the answer if the length of the subarray is greater than the previously calculated lengths . ; Driver Code
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int binarySearch ( int * searchSpace , int s , int e , int num ) { int ans ; while ( s <= e ) { int mid = ( s + e ) / 2 ; if ( searchSpace [ mid ] >= num ) { ans = mid ; e = mid - 1 ; } else s = mid + 1 ; } return ans ; } int longestSubArr ( int * arr , int n ) { int searchSpace [ n ] ; int index [ n ] ; int j = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( j == 0 or searchSpace [ j - 1 ] < arr [ i ] ) { searchSpace [ j ] = arr [ i ] ; index [ j ] = i ; j ++ ; } int idx = binarySearch ( searchSpace , 0 , j - 1 , arr [ i ] ) ; ans = max ( ans , i - index [ idx ] + 1 ) ; } return ans ; } int main ( int argc , char const * argv [ ] ) { int arr [ ] = { -5 , -1 , 7 , 5 , 1 , -2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestSubArr ( arr , n ) << endl ; return 0 ; }
Print all pairs with given sum | C ++ code to implement the above approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pairedElements ( int arr [ ] , int sum , int n ) { int low = 0 ; int high = n - 1 ; while ( low < high ) { if ( arr [ low ] + arr [ high ] == sum ) { cout << " The ▁ pair ▁ is ▁ : ▁ ( " << arr [ low ] << " , ▁ " << arr [ high ] << " ) " << endl ; } if ( arr [ low ] + arr [ high ] > sum ) { high -- ; } else { low ++ ; } } } int main ( ) { int arr [ ] = { 2 , 3 , 4 , -2 , 6 , 8 , 9 , 11 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sort ( arr , arr + n ) ; pairedElements ( arr , 6 , n ) ; }
Check if a string is suffix of another | CPP program to find if a string is suffix of another ; Driver code ; Test case - sensitive implementation of endsWith function
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool isSuffix ( string s1 , string s2 ) { int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) return false ; return true ; } int main ( ) { string s1 = " geeks " , s2 = " geeksforgeeks " ; bool result = isSuffix ( s1 , s2 ) ; if ( result ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Check if all occurrences of a character appear together | C ++ program to find if all occurrences of a character appear together in a string . ; To indicate if one or more occurrences of ' c ' are seen or not . ; Traverse given string ; If current character is same as c , we first check if c is already seen . ; If this is very first appearance of c , we traverse all consecutive occurrences . ; To indicate that character is seen once . ; Driver program
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool checkIfAllTogether ( string s , char c ) { bool oneSeen = false ; int i = 0 , n = s . length ( ) ; while ( i < n ) { if ( s [ i ] == c ) { if ( oneSeen == true ) return false ; while ( i < n && s [ i ] == c ) i ++ ; oneSeen = true ; } else i ++ ; } return true ; } int main ( ) { string s = "110029" ; if ( checkIfAllTogether ( s , '1' ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Front and Back Search in unsorted array | CPP program to implement front and back search ; Start searching from both ends ; Keep searching while two indexes do not cross . ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool search ( int arr [ ] , int n , int x ) { int front = 0 , back = n - 1 ; while ( front <= back ) { if ( arr [ front ] == x arr [ back ] == x ) return true ; front ++ ; back -- ; } return false ; } int main ( ) { int arr [ ] = { 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 } ; int x = 130 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( search ( arr , n , x ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Even | CPP program to find max ( X , Y ) / min ( X , Y ) after P turns ; Driver code ; 1 st test case ; 2 nd test case
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findValue ( int X , int Y , int P ) { if ( P % 2 == 0 ) return ( max ( X , Y ) / min ( X , Y ) ) ; else return ( max ( 2 * X , Y ) / min ( 2 * X , Y ) ) ; } int main ( ) { int X = 1 , Y = 2 , P = 1 ; cout << findValue ( X , Y , P ) << endl ; X = 3 , Y = 7 , P = 2 ; cout << findValue ( X , Y , P ) << endl ; }
The painter 's partition problem | A DP based CPP program for painter 's partition problem ; function to calculate sum between two indices in array ; bottom up tabular dp ; initialize table ; base cases k = 1 ; n = 1 ; 2 to k partitions for ( int i = 2 ; i <= k ; i ++ ) { 2 to n boards ; track minimum ; i - 1 th separator before position arr [ p = 1. . j ] ; required ; driver function ; Calculate size of array .
#include <climits> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int sum ( int arr [ ] , int from , int to ) { int total = 0 ; for ( int i = from ; i <= to ; i ++ ) total += arr [ i ] ; return total ; } int findMax ( int arr [ ] , int n , int k ) { int dp [ k + 1 ] [ n + 1 ] = { 0 } ; for ( int i = 1 ; i <= n ; i ++ ) dp [ 1 ] [ i ] = sum ( arr , 0 , i - 1 ) ; for ( int i = 1 ; i <= k ; i ++ ) dp [ i ] [ 1 ] = arr [ 0 ] ; for ( int j = 2 ; j <= n ; j ++ ) { int best = INT_MAX ; for ( int p = 1 ; p <= j ; p ++ ) best = min ( best , max ( dp [ i - 1 ] [ p ] , sum ( arr , p , j - 1 ) ) ) ; dp [ i ] [ j ] = best ; } } return dp [ k ] [ n ] ; } int main ( ) { int arr [ ] = { 10 , 20 , 60 , 50 , 30 , 40 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << findMax ( arr , n , k ) << endl ; return 0 ; }
Counting cross lines in an array | c ++ program to count cross line in array ; function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countCrossLine ( int arr [ ] , int n ) { int count_crossline = 0 ; int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; count_crossline ++ ; } arr [ j + 1 ] = key ; } return count_crossline ; } int main ( ) { int arr [ ] = { 4 , 3 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countCrossLine ( arr , n ) << endl ; return 0 ; }
Recursive Programs to find Minimum and Maximum elements of array | Recursive C ++ program to find maximum ; function to return maximum element using recursion ; if n = 0 means whole array has been traversed ; driver code to test above function ; Function calling
#include <iostream> NEW_LINE using namespace std ; int findMaxRec ( int A [ ] , int n ) { if ( n == 1 ) return A [ 0 ] ; return max ( A [ n - 1 ] , findMaxRec ( A , n - 1 ) ) ; } int main ( ) { int A [ ] = { 1 , 4 , 45 , 6 , -50 , 10 , 2 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << findMaxRec ( A , n ) ; return 0 ; }
Program to remove vowels from a String | C ++ program to remove vowels from a String ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string remVowel ( string str ) { regex r ( " [ aeiouAEIOU ] " ) ; return regex_replace ( str , r , " " ) ; } int main ( ) { string str = " GeeeksforGeeks ▁ - ▁ A ▁ Computer ▁ Science ▁ Portal ▁ for ▁ Geeks " ; cout << ( remVowel ( str ) ) ; return 0 ; }
MSD ( Most Significant Digit ) Radix Sort | C ++ implementation of MSD Radix Sort of MSD Radix Sort using counting sort ( ) ; A utility function to print an array ; A utility function to get the digit at index d in a integer ; The main function to sort array using MSD Radix Sort recursively ; recursion break condition ; temp is created to easily swap strings in arr [ ] ; Store occurrences of most significant character from each integer in count [ ] ; Change count [ ] so that count [ ] now contains actual position of this digits in temp [ ] ; Build the temp ; Copy all integers of temp to arr [ ] , so that arr [ ] now contains partially sorted integers ; Recursively MSD_sort ( ) on each partially sorted integers set to sort them by their next digit ; function find the largest integer ; Main function to call MSD_sort ; Find the maximum number to know number of digits ; get the length of the largest integer ; function call ; Driver Code ; Input array ; Size of the array ; Print the unsorted array ; Function Call ; Print the sorted array
#include <iostream> NEW_LINE #include <math.h> NEW_LINE #include <unordered_map> NEW_LINE using namespace std ; void print ( int * arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; } int digit_at ( int x , int d ) { return ( int ) ( x / pow ( 10 , d - 1 ) ) % 10 ; } void MSD_sort ( int * arr , int lo , int hi , int d ) { if ( hi <= lo ) { return ; } int count [ 10 + 2 ] = { 0 } ; unordered_map < int , int > temp ; for ( int i = lo ; i <= hi ; i ++ ) { int c = digit_at ( arr [ i ] , d ) ; count ++ ; } for ( int r = 0 ; r < 10 + 1 ; r ++ ) count [ r + 1 ] += count [ r ] ; for ( int i = lo ; i <= hi ; i ++ ) { int c = digit_at ( arr [ i ] , d ) ; temp [ count ++ ] = arr [ i ] ; } for ( int i = lo ; i <= hi ; i ++ ) arr [ i ] = temp [ i - lo ] ; for ( int r = 0 ; r < 10 ; r ++ ) MSD_sort ( arr , lo + count [ r ] , lo + count [ r + 1 ] - 1 , d - 1 ) ; } int getMax ( int arr [ ] , int n ) { int mx = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > mx ) mx = arr [ i ] ; return mx ; } void radixsort ( int * arr , int n ) { int m = getMax ( arr , n ) ; int d = floor ( log10 ( abs ( m ) ) ) + 1 ; MSD_sort ( arr , 0 , n - 1 , d ) ; } int main ( ) { int arr [ ] = { 9330 , 9950 , 718 , 8977 , 6790 , 95 , 9807 , 741 , 8586 , 5710 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( " Unsorted ▁ array ▁ : ▁ " ) ; print ( arr , n ) ; radixsort ( arr , n ) ; printf ( " Sorted ▁ array ▁ : ▁ " ) ; print ( arr , n ) ; return 0 ; }
Maximum money that can be collected by both the players in a game of removal of coins | C ++ program for the above approach ; Function to find the maximum score obtained by the players A and B ; Sort the array in descending order ; Stores the maximum amount of money obtained by A ; Stores the maximum amount of money obtained by B ; If the value of N is 1 ; Update the amountA ; Print the amount of money obtained by both players ; Update the amountA ; Update the amountB ; Traverse the array arr [ ] ; If i is an odd number ; Update the amountB ; Update the amountA ; Print the amount of money obtained by both the players ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findAmountPlayers ( int arr [ ] , int N ) { sort ( arr , arr + N , greater < int > ( ) ) ; int amountA = 0 ; int amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; cout << " ( A ▁ : ▁ " << amountA << " ) , ▁ " << " ( B ▁ : ▁ " << amountB << " ) " ; return ; } amountA = arr [ 0 ] ; amountB = arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { amountB += arr [ i ] ; } else { amountA += arr [ i ] ; } } cout << " ( A ▁ : ▁ " << amountA << " ) STRNEWLINE " << " ( B ▁ : ▁ " << amountB << " ) " ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findAmountPlayers ( arr , N ) ; return 0 ; }
Minimum number of swaps required to minimize sum of absolute differences between adjacent array elements | C ++ program for the above approach ; Comparator to sort in the descending order ; Function to find the minimum number of swaps required to sort the array in increasing order ; Stores the array elements with its index ; Sort the array in the increasing order ; Keeps the track of visited elements ; Stores the count of swaps required ; Traverse array elements ; If the element is already swapped or at correct position ; Find out the number of nodes in this cycle ; Update the value of j ; Move to the next element ; Increment cycle_size ; Update the ans by adding current cycle ; Function to find the minimum number of swaps required to sort the array in decreasing order ; Stores the array elements with its index ; Sort the array in the descending order ; Keeps track of visited elements ; Stores the count of resultant swap required ; Traverse array elements ; If the element is already swapped or at correct position ; Find out the number of node in this cycle ; Update the value of j ; Move to the next element ; Increment the cycle_size ; Update the ans by adding current cycle size ; Function to find minimum number of swaps required to minimize the sum of absolute difference of adjacent elements ; Sort in ascending order ; Sort in descending order ; Return the minimum value ; Drive Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool mycmp ( pair < int , int > a , pair < int , int > b ) { return a . first > b . first ; } int minSwapsAsc ( vector < int > arr , int n ) { pair < int , int > arrPos [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arrPos [ i ] . first = arr [ i ] ; arrPos [ i ] . second = i ; } sort ( arrPos , arrPos + n ) ; vector < bool > vis ( n , false ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] . second == i ) continue ; int cycle_size = 0 ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = 1 ; j = arrPos [ j ] . second ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } int minSwapsDes ( vector < int > arr , int n ) { pair < int , int > arrPos [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arrPos [ i ] . first = arr [ i ] ; arrPos [ i ] . second = i ; } sort ( arrPos , arrPos + n , mycmp ) ; vector < bool > vis ( n , false ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] . second == i ) continue ; int cycle_size = 0 ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = 1 ; j = arrPos [ j ] . second ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } int minimumSwaps ( vector < int > arr ) { int S1 = minSwapsAsc ( arr , arr . size ( ) ) ; int S2 = minSwapsDes ( arr , arr . size ( ) ) ; return min ( S1 , S2 ) ; } int main ( ) { vector < int > arr { 3 , 4 , 2 , 5 , 1 } ; cout << minimumSwaps ( arr ) ; return 0 ; }
Sort an array of 0 s , 1 s , 2 s and 3 s | C ++ program for the above approach ; Function to sort the array having array element only 0 , 1 , 2 , and 3 ; Iterate until mid <= j ; If arr [ mid ] is 0 ; Swap integers at indices i and mid ; Increment i ; Increment mid ; Otherwise if the value of arr [ mid ] is 3 ; Swap arr [ mid ] and arr [ j ] ; Decrement j ; Otherwise if the value of arr [ mid ] is either 1 or 2 ; Increment the value of mid ; Iterate until i <= j ; If arr [ i ] the value of is 2 ; Swap arr [ i ] and arr [ j ] ; Decrement j ; Otherwise , increment i ; Print the sorted array arr [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortArray ( int arr [ ] , int N ) { int i = 0 , j = N - 1 , mid = 0 ; while ( mid <= j ) { if ( arr [ mid ] == 0 ) { swap ( arr [ i ] , arr [ mid ] ) ; i ++ ; mid ++ ; } else if ( arr [ mid ] == 3 ) { swap ( arr [ mid ] , arr [ j ] ) ; j -- ; } else if ( arr [ mid ] == 1 arr [ mid ] == 2 ) { mid ++ ; } } while ( i <= j ) { if ( arr [ i ] == 2 ) { swap ( arr [ i ] , arr [ j ] ) ; j -- ; } else { i ++ ; } } for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArray ( arr , N ) ; return 0 ; }
Check whether an array can be made strictly increasing by removing at most one element | C ++ program for the above approach ; Function to find if is it possible to make the array strictly increasing by removing at most one element ; Stores the count of numbers that are needed to be removed ; Store the index of the element that needs to be removed ; Traverse the range [ 1 , N - 1 ] ; If arr [ i - 1 ] is greater than or equal to arr [ i ] ; Increment the count by 1 ; Update index ; If count is greater than one ; If no element is removed ; If only the last or the first element is removed ; If a [ index ] is removed ; If a [ index - 1 ] is removed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int arr [ ] , int n ) { int count = 0 ; int index = -1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] ) { count ++ ; index = i ; } } if ( count > 1 ) return false ; if ( count == 0 ) return true ; if ( index == n - 1 index == 1 ) return true ; if ( arr [ index - 1 ] < arr [ index + 1 ] ) return true ; if ( index - 2 >= 0 && arr [ index - 2 ] < arr [ index ] ) return true ; if ( index < 0 ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 3 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( check ( arr , N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Modify string by rearranging vowels in alphabetical order at their respective indices | C ++ program for the above approach ; Function to arrange the vowels in sorted order in the string at their respective places ; Store the size of the string ; Stores vowels of string S ; Traverse the string , S and push all the vowels to string vow ; If vow is empty , then print S and return ; Sort vow in alphabetical order ; Traverse the string , S ; Replace S [ i ] with vow [ j ] iif S [ i ] is a vowel , and increment j by 1 ; Print the string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortVowels ( string S ) { int n = S . size ( ) ; string vow = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) { vow += S [ i ] ; } } if ( vow . size ( ) == 0 ) { cout << S ; return ; } sort ( vow . begin ( ) , vow . end ( ) ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) { S [ i ] = vow [ j ++ ] ; } } cout << S ; } int main ( ) { string S = " geeksforgeeks " ; sortVowels ( S ) ; return 0 ; }
Maximum number of buckets that can be filled | C ++ program for the above approach ; Function to find the maximum number of buckets that can be filled with the amount of water available ; Find the total available water ; Sort the array in ascending order ; Check if bucket can be filled with available water ; Print count of buckets ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getBuckets ( int arr [ ] , int N ) { int availableWater = N * ( N - 1 ) / 2 ; sort ( arr , arr + N ) ; int i = 0 , sum = 0 ; while ( sum <= availableWater ) { sum += arr [ i ] ; i ++ ; } cout << i - 1 ; } int main ( ) { int arr [ ] = { 1 , 5 , 3 , 4 , 7 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; getBuckets ( arr , N ) ; return 0 ; }
Minimize the number of strictly increasing subsequences in an array | Set 2 | C ++ program for the above approach ; Function to find the number of strictly increasing subsequences in an array ; Sort the array ; Stores final count of subsequences ; Traverse the array ; Stores current element ; Stores frequency of the current element ; Count frequency of the current element ; If current element frequency is greater than count ; Print the final count ; Driver Code ; Given array ; Size of the array ; Function call to find the number of strictly increasing subsequences
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumIncreasingSubsequences ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int count = 0 ; int i = 0 ; while ( i < N ) { int x = arr [ i ] ; int freqX = 0 ; while ( i < N && arr [ i ] == x ) { freqX ++ ; i ++ ; } count = max ( count , freqX ) ; } cout << count ; } int main ( ) { int arr [ ] = { 2 , 1 , 2 , 1 , 4 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumIncreasingSubsequences ( arr , N ) ; }