codeparrot/xlcost-text-to-code huggingface dataset:
Sort elements by modulo with K | C ++ implementation of the approach ; Utility function to print the contents of an array ; Function to sort the array elements based on their modulo with K ; Create K empty vectors ; Update the vectors such that v [ i ] will contain all the elements that give remainder as i when divided by k ; Sorting all the vectors separately ; Replacing the elements in arr [ ] with the required modulo sorted elements ; Add all the elements of the current vector to the array ; Print the sorted array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void sortWithRemainder ( int arr [ ] , int n , int k ) { vector < int > v [ k ] ; for ( int i = 0 ; i < n ; i ++ ) { v [ arr [ i ] % k ] . push_back ( arr [ i ] ) ; } for ( int i = 0 ; i < k ; i ++ ) sort ( v [ i ] . begin ( ) , v [ i ] . end ( ) ) ; int j = 0 ; for ( int i = 0 ; i < k ; i ++ ) { for ( vector < int > :: iterator it = v [ i ] . begin ( ) ; it != v [ i ] . end ( ) ; it ++ ) { arr [ j ] = * it ; j ++ ; } } printArr ( arr , n ) ; } int main ( ) { int arr [ ] = { 10 , 7 , 2 , 6 , 12 , 3 , 33 , 46 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 4 ; sortWithRemainder ( arr , n , k ) ; return 0 ; }
Concatenate the strings in an order which maximises the occurrence of subsequence " ab " | C ++ implementation of the approach ; Custom sort function to sort the given string in the order which maximises the final score ; To store the count of occurrences of ' a ' and ' b ' in s1 ; Count the number of occurrences of ' a ' and ' b ' in s1 ; To store the count of occurrences of ' a ' and ' b ' in s2 ; Count the number of occurrences of ' a ' and ' b ' in s2 ; Since the number of subsequences ' ab ' is more when s1 is placed before s2 we return 1 so that s1 occurs before s2 in the combined string ; Function that return the concatenated string as S [ 0 ] + S [ 1 ] + ... + S [ N - 1 ] ; To store the concatenated string ; Concatenate every string in the order of appearance ; Return the concatenated string ; Function to return the maximum required score ; Sort the strings in the order which maximizes the score that we can get ; Get the concatenated string combined string ; Calculate the score of the combined string i . e . the count of occurrences of " ab " as subsequences ; Number of ' a ' has increased by one ; There are count_a number of ' a ' that can form subsequence ' ab ' with this ' b ' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool customSort ( string s1 , string s2 ) { int count_a1 = 0 , count_b1 = 0 ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( s1 [ i ] == ' a ' ) count_a1 ++ ; else count_b1 ++ ; } int count_a2 = 0 , count_b2 = 0 ; for ( int i = 0 ; i < s2 . size ( ) ; i ++ ) { if ( s2 [ i ] == ' a ' ) count_a2 ++ ; else count_b2 ++ ; } if ( count_a1 * count_b2 > count_b1 * count_a2 ) { return 1 ; } else { return 0 ; } } string concatenateStrings ( string S [ ] , int N ) { string str = " " ; for ( int i = 0 ; i < N ; i ++ ) str += S [ i ] ; return str ; } int getMaxScore ( string S [ ] , int N ) { sort ( S , S + N , customSort ) ; string combined_string = concatenateStrings ( S , N ) ; int final_score = 0 , count_a = 0 ; for ( int i = 0 ; i < combined_string . size ( ) ; i ++ ) { if ( combined_string [ i ] == ' a ' ) { count_a ++ ; } else { final_score += count_a ; } } return final_score ; } int main ( ) { string S [ ] = { " bab " , " aa " , " ba " , " b " } ; int N = sizeof ( S ) / sizeof ( string ) ; cout << getMaxScore ( S , N ) ; return 0 ; }
Minimum increment operations to make K elements equal | C ++ implementation of the approach ; Function to return the minimum number of increment operations required to make any k elements of the array equal ; Sort the array in increasing order ; Calculate the number of operations needed to make 1 st k elements equal to the kth element i . e . the 1 st window ; Answer will be the minimum of all possible k sized windows ; Find the operations needed to make k elements equal to ith element ; Slide the window to the right and subtract increments spent on leftmost element of the previous window ; Add increments needed to make the 1 st k - 1 elements of this window equal to the kth element of the current window ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( vector < int > ar , int k ) { sort ( ar . begin ( ) , ar . end ( ) ) ; int opsNeeded = 0 ; for ( int i = 0 ; i < k ; i ++ ) { opsNeeded += ar [ k - 1 ] - ar [ i ] ; } int ans = opsNeeded ; for ( int i = k ; i < ar . size ( ) ; i ++ ) { opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] ) ; opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] ) ; ans = min ( ans , opsNeeded ) ; } return ans ; } int main ( ) { vector < int > arr = { 3 , 1 , 9 , 100 } ; int n = arr . size ( ) ; int k = 3 ; cout << minOperations ( arr , k ) ; return 0 ; }
Lexicographical ordering using Heap Sort | C ++ implementation to print the string in Lexicographical order ; Used for index in heap ; Predefining the heap array ; Defining formation of the heap ; Iterative heapiFy ; Just swapping if the element is smaller than already stored element ; Swapping the current index with its child ; Moving upward in the heap ; Defining heap sort ; Taking output of the minimum element ; Set first element as a last one ; Decrement of the size of the string ; Initializing the left and right index ; Process of heap sort If root element is minimum than its both of the child then break ; Otherwise checking that the child which one is smaller , swap them with parent element ; Swapping ; Changing the left index and right index ; Utility function ; To heapiFy ; Calling heap sort function ; Driver Code
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int x = -1 ; string heap [ 1000 ] ; void heapForm ( string k ) { x ++ ; heap [ x ] = k ; int child = x ; string tmp ; int index = x / 2 ; while ( index >= 0 ) { if ( heap [ index ] > heap [ child ] ) { tmp = heap [ index ] ; heap [ index ] = heap [ child ] ; heap [ child ] = tmp ; child = index ; index = index / 2 ; } else { break ; } } } void heapSort ( ) { int left1 , right1 ; while ( x >= 0 ) { string k ; k = heap [ 0 ] ; cout << k << " ▁ " ; heap [ 0 ] = heap [ x ] ; x = x - 1 ; string tmp ; int index = 0 ; int length = x ; left1 = 1 ; right1 = left1 + 1 ; while ( left1 <= length ) { if ( heap [ index ] <= heap [ left1 ] && heap [ index ] <= heap [ right1 ] ) { break ; } else { if ( heap [ left1 ] < heap [ right1 ] ) { tmp = heap [ index ] ; heap [ index ] = heap [ left1 ] ; heap [ left1 ] = tmp ; index = left1 ; } else { tmp = heap [ index ] ; heap [ index ] = heap [ right1 ] ; heap [ right1 ] = tmp ; index = right1 ; } } left1 = 2 * left1 ; right1 = left1 + 1 ; } } } void sort ( string k [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { heapForm ( k [ i ] ) ; } heapSort ( ) ; } int main ( ) { string arr [ ] = { " banana " , " orange " , " apple " , " pineapple " , " berries " , " lichi " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sort ( arr , n ) ; }
Find Kth element in an array containing odd elements first and then even elements | C ++ implementation of the approach ; Function to return the kth element in the modified array ; First odd number ; Insert the odd number ; Next odd number ; First even number ; Insert the even number ; Next even number ; Return the kth element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getNumber ( int n , int k ) { int arr [ n ] ; int i = 0 ; int odd = 1 ; while ( odd <= n ) { arr [ i ++ ] = odd ; odd += 2 ; } int even = 2 ; while ( even <= n ) { arr [ i ++ ] = even ; even += 2 ; } return arr [ k - 1 ] ; } int main ( ) { int n = 8 , k = 5 ; cout << getNumber ( n , k ) ; return 0 ; }
Swap Alternate Boundary Pairs | C ++ implementation of the approach ; Utility function to print the contents of an array ; Function to update the array ; Initialize the pointers ; While there are elements to swap ; Update the pointers ; Print the updated array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void UpdateArr ( int arr [ ] , int n ) { int i = 0 , j = n - 1 ; while ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i += 2 ; j -= 2 ; } printArr ( arr , n ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; UpdateArr ( arr , n ) ; return 0 ; }
Count permutation such that sequence is non decreasing | C ++ implementation of the approach ; To store the factorials ; Function to update fact [ ] array such that fact [ i ] = i ! ; 0 ! = 1 ; i ! = i * ( i - 1 ) ! ; Function to return the count of possible permutations ; To store the result ; Sort the array ; Initial size of the block ; Increase the size of block ; Update the result for the previous block ; Reset the size to 1 ; Update the result for the last block ; Driver code ; Pre - calculating factorials
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  20 NEW_LINE int fact [ N ] ; void pre ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } int CountPermutation ( int a [ ] , int n ) { int ways = 1 ; sort ( a , a + n ) ; int size = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } int main ( ) { int a [ ] = { 1 , 2 , 4 , 4 , 2 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; pre ( ) ; cout << CountPermutation ( a , n ) ; return 0 ; }
Smallest element greater than X not present in the array | C ++ implementation of the approach ; Function to return the smallest element greater than x which is not present in a [ ] ; Sort the array ; Continue until low is less than or equals to high ; Find mid ; If element at mid is less than or equals to searching element ; If mid is equals to searching element ; Increment searching element ; Make high as N - 1 ; Make low as mid + 1 ; Make high as mid - 1 ; Return the next greater element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Next_greater ( int a [ ] , int n , int x ) { sort ( a , a + n ) ; int low = 0 , high = n - 1 , ans = x + 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( a [ mid ] <= ans ) { if ( a [ mid ] == ans ) { ans ++ ; high = n - 1 ; } low = mid + 1 ; } else high = mid - 1 ; } return ans ; } int main ( ) { int a [ ] = { 1 , 5 , 10 , 4 , 7 } , x = 4 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << Next_greater ( a , n , x ) ; return 0 ; }
Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | C ++ implementation of the approach ; Function to print the arrangement that satisifes the given condition ; Sort the array initially ; Array that stores the arrangement ; Once the array is sorted Re - fill the array again in the mentioned way in the approach ; Iterate in the array and check if the arrangement made satisfies the given condition or not ; For the first element the adjacents will be a [ 1 ] and a [ n - 1 ] ; For the last element the adjacents will be a [ 0 ] and a [ n - 2 ] ; If we reach this position then the arrangement is possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArrangement ( int a [ ] , int n ) { sort ( a , a + n ) ; int b [ n ] ; int low = 0 , high = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) b [ low ++ ] = a [ i ] ; else b [ high -- ] = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) { cout << -1 ; return ; } } else if ( i == ( n - 1 ) ) { if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) { cout << -1 ; return ; } } else { if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) { cout << -1 ; return ; } } } for ( int i = 0 ; i < n ; i ++ ) cout << b [ i ] << " ▁ " ; } int main ( ) { int a [ ] = { 1 , 4 , 4 , 3 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printArrangement ( a , n ) ; return 0 ; }
Greatest contiguous sub | C ++ implementation of the approach ; Function that returns the sub - array ; Data - structure to store all the sub - arrays of size K ; Iterate to find all the sub - arrays ; Store the sub - array elements in the array ; Push the vector in the container ; Sort the vector of elements ; The last sub - array in the sorted order will be the answer ; Driver code ; Get the sub - array
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > findSubarray ( int a [ ] , int k , int n ) { vector < vector < int > > vec ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { vector < int > temp ; for ( int j = i ; j < i + k ; j ++ ) { temp . push_back ( a [ j ] ) ; } vec . push_back ( temp ) ; } sort ( vec . begin ( ) , vec . end ( ) ) ; return vec [ vec . size ( ) - 1 ] ; } int main ( ) { int a [ ] = { 1 , 4 , 3 , 2 , 5 } ; int k = 4 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; vector < int > ans = findSubarray ( a , k , n ) ; for ( auto it : ans ) cout << it << " ▁ " ; }
Maximum Length Chain of Pairs | Set | C ++ implementation of the above approach ; Structure for storing pairs of first and second values . ; Comparator function which can compare the second element of structure used to sort pairs in increasing order of second value . ; Function for finding max length chain ; Initialize length l = 1 ; Sort all pair in increasing order according to second no of pair ; Pick up the first pair and assign the value of second element fo pair to a temporary variable s ; Iterate from second pair ( index of the second pair is 1 ) to the last pair ; If first of current pair is greater than previously selected pair then select current pair and update value of l and s ; Return maximum length ; Driver Code ; Declaration of array of structure ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct val { int first ; int second ; } ; bool comparator ( struct val p1 , struct val p2 ) { return ( p1 . second < p2 . second ) ; } int maxChainLen ( struct val p [ ] , int n ) { int l = 1 ; sort ( p , p + n , comparator ) ; int s = p [ 0 ] . second ; for ( int i = 1 ; i < n ; i ++ ) { if ( p [ i ] . first > s ) { l ++ ; s = p [ i ] . second ; } } return l ; } int main ( ) { val p [ ] = { { 5 , 24 } , { 39 , 60 } , { 15 , 28 } , { 27 , 40 } , { 50 , 90 } } ; int n = sizeof ( p ) / sizeof ( p [ 0 ] ) ; cout << maxChainLen ( p , n ) << endl ; return 0 ; }
Unbounded Fractional Knapsack | C ++ implementation of the approach ; Function to return the maximum required value ; maxratio will store the maximum value to weight ratio we can have for any item and maxindex will store the index of that element ; Find the maximum ratio ; The item with the maximum value to weight ratio will be put into the knapsack repeatedly until full ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float knapSack ( int W , float wt [ ] , float val [ ] , int n ) { float maxratio = INT_MIN ; int maxindex = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( val [ i ] / wt [ i ] ) > maxratio ) { maxratio = ( val [ i ] / wt [ i ] ) ; maxindex = i ; } } return ( W * maxratio ) ; } int main ( ) { float val [ ] = { 14 , 27 , 44 , 19 } ; float wt [ ] = { 6 , 7 , 9 , 8 } ; int n = sizeof ( val ) / sizeof ( val [ 0 ] ) ; int W = 50 ; cout << knapSack ( W , wt , val , n ) ; return 0 ; }
Sort an array without changing position of negative numbers | C ++ implementation of the approach ; Function to sort the array such that negative values do not get affected ; Store all non - negative values ; Sort non - negative values ; If current element is non - negative then update it such that all the non - negative values are sorted ; Print the sorted array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortArray ( int a [ ] , int n ) { vector < int > ans ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) ans . push_back ( a [ i ] ) ; } sort ( ans . begin ( ) , ans . end ( ) ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) { a [ i ] = ans [ j ] ; j ++ ; } } for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 2 , -6 , -3 , 8 , 4 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArray ( arr , n ) ; return 0 ; }
Find a triplet in an array whose sum is closest to a given number | C ++ implementation of the approach ; Function to return the sum of a triplet which is closest to x ; Sort the array ; To store the closest sum not using INT_MAX to avoid overflowing condition ; Fix the smallest number among the three integers ; Two pointers initially pointing at the last and the element next to the fixed element ; While there could be more pairs to check ; Calculate the sum of the current triplet ; If the sum is more closer than the current closest sum ; If sum is greater then x then decrement the second pointer to get a smaller sum ; Else increment the first pointer to get a larger sum ; Return the closest sum found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solution ( vector < int > & arr , int x ) { sort ( arr . begin ( ) , arr . end ( ) ) ; int closestSum = 1000000000 ; for ( int i = 0 ; i < arr . size ( ) - 2 ; i ++ ) { int ptr1 = i + 1 , ptr2 = arr . size ( ) - 1 ; while ( ptr1 < ptr2 ) { int sum = arr [ i ] + arr [ ptr1 ] + arr [ ptr2 ] ; if ( abs ( 1LL * x - sum ) < abs ( 1LL * x - closestSum ) ) { closestSum = sum ; } if ( sum > x ) { ptr2 -- ; } else { ptr1 ++ ; } } } return closestSum ; } int main ( ) { vector < int > arr = { -1 , 2 , 1 , -4 } ; int x = 1 ; cout << solution ( arr , x ) ; return 0 ; }
Print elements of an array according to the order defined by another array | set 2 | A C ++ program to print an array according to the order defined by another array ; Function to print an array according to the order defined by another array ; Declaring map and iterator ; Store the frequency of each number of a1 [ ] int the map ; Traverse through a2 [ ] ; Check whether number is present in map or not ; Print that number that many times of its frequency ; Print those numbers that are not present in a2 [ ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print_in_order ( int a1 [ ] , int a2 [ ] , int n , int m ) { map < int , int > mp ; map < int , int > :: iterator itr ; for ( int i = 0 ; i < n ; i ++ ) mp [ a1 [ i ] ] ++ ; for ( int i = 0 ; i < m ; i ++ ) { if ( mp . find ( a2 [ i ] ) != mp . end ( ) ) { itr = mp . find ( a2 [ i ] ) ; for ( int j = 0 ; j < itr -> second ; j ++ ) cout << itr -> first << " ▁ " ; mp . erase ( a2 [ i ] ) ; } } for ( itr = mp . begin ( ) ; itr != mp . end ( ) ; itr ++ ) { for ( int j = 0 ; j < itr -> second ; j ++ ) cout << itr -> first << " ▁ " ; } cout << endl ; } int main ( ) { int a1 [ ] = { 2 , 1 , 2 , 5 , 7 , 1 , 9 , 3 , 6 , 8 , 8 } ; int a2 [ ] = { 2 , 1 , 8 , 3 } ; int n = sizeof ( a1 ) / sizeof ( a1 [ 0 ] ) ; int m = sizeof ( a2 ) / sizeof ( a2 [ 0 ] ) ; print_in_order ( a1 , a2 , n , m ) ; return 0 ; }
Check whether we can sort two arrays by swapping A [ i ] and B [ i ] | C ++ implementation of above approach ; Function to check whether both the array can be sorted in ( strictly increasing ) ascending order ; Traverse through the array and find out the min and max variable at each position make one array of min variables and another of maximum variable ; Maximum and minimum variable ; Assign min value to B [ i ] and max value to A [ i ] ; Now check whether the array is sorted or not ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool IsSorted ( int A [ ] , int B [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = max ( A [ i ] , B [ i ] ) ; y = min ( A [ i ] , B [ i ] ) ; A [ i ] = x ; B [ i ] = y ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] <= A [ i - 1 ] B [ i ] <= B [ i - 1 ] ) return false ; } return true ; } int main ( ) { int A [ ] = { 1 , 4 , 3 , 5 , 7 } ; int B [ ] = { 2 , 2 , 5 , 8 , 9 } ; int n = sizeof ( A ) / sizeof ( int ) ; cout << ( IsSorted ( A , B , n ) ? " True " : " False " ) ; return 0 ; }
Find the largest possible k | C ++ program to find the largest possible k - multiple set ; Function to find the largest possible k - multiple set ; Sort the given array ; To store k - multiple set ; Traverse through the whole array ; Check if x / k is already present or not ; Print the k - multiple set ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void K_multiple ( int a [ ] , int n , int k ) { sort ( a , a + n ) ; set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] % k == 0 && s . find ( a [ i ] / k ) == s . end ( ) ) a [ i ] % k != 0 ) s . insert ( a [ i ] ) ; } for ( auto i = s . begin ( ) ; i != s . end ( ) ; i ++ ) { cout << * i << " ▁ " ; } } int main ( ) { int a [ ] = { 2 , 3 , 4 , 5 , 6 , 10 } ; int k = 2 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; K_multiple ( a , n , k ) ; return 0 ; }
Maximum water that can be stored between two buildings | C ++ implementation of the approach ; Return the maximum water that can be stored ; To store the maximum water so far ; Both the pointers are pointing at the first and the last buildings respectively ; While the water can be stored between the currently chosen buildings ; Update maximum water so far and increment i ; Update maximum water so far and decrement j ; Any of the pointers can be updated ( or both ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxWater ( int height [ ] , int n ) { int maximum = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( height [ i ] < height [ j ] ) { maximum = max ( maximum , ( j - i - 1 ) * height [ i ] ) ; i ++ ; } else if ( height [ j ] < height [ i ] ) { maximum = max ( maximum , ( j - i - 1 ) * height [ j ] ) ; j -- ; } else { maximum = max ( maximum , ( j - i - 1 ) * height [ i ] ) ; i ++ ; j -- ; } } return maximum ; } int main ( ) { int height [ ] = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = sizeof ( height ) / sizeof ( height [ 0 ] ) ; cout << ( maxWater ( height , n ) ) ; }
Count the maximum number of elements that can be selected from the array | C ++ implementation of the approach ; Function to return the maximum count of selection possible from the given array following the given process ; Initialize result ; Sorting the array ; Initialize the select variable ; Loop through array ; If selection is possible ; Increment result ; Increment selection variable ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSelectionCount ( int a [ ] , int n ) { int res = 0 ; sort ( a , a + n ) ; int select = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= select ) { res ++ ; select ++ ; } } return res ; } int main ( ) { int arr [ ] = { 4 , 2 , 1 , 3 , 5 , 1 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSelectionCount ( arr , N ) ; return 0 ; }