codeparrot/xlcost-text-to-code huggingface dataset:
N / 3 repeated number in an array with O ( 1 ) space | CPP program to find if any element appears more than n / 3. ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int appearsNBy3 ( int arr [ ] , int n ) { int count1 = 0 , count2 = 0 ; int first = INT_MAX , second = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( first == arr [ i ] ) count1 ++ ; else if ( second == arr [ i ] ) count2 ++ ; else if ( count1 == 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 == 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > n / 3 ) return first ; if ( count2 > n / 3 ) return second ; return -1 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << appearsNBy3 ( arr , n ) << endl ; return 0 ; }
Count pairs in array whose sum is divisible by 4 | C ++ Program to count pairs whose sum divisible by '4' ; Program to count pairs whose sum divisible by '4' ; Create a frequency array to count occurrences of all remainders when divided by 4 ; Count occurrences of all remainders ; If both pairs are divisible by '4' ; If both pairs are 2 modulo 4 ; If one of them is equal to 1 modulo 4 and the other is equal to 3 modulo 4 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count4Divisibiles ( int arr [ ] , int n ) { int freq [ 4 ] = { 0 , 0 , 0 , 0 } ; for ( int i = 0 ; i < n ; i ++ ) ++ freq [ arr [ i ] % 4 ] ; int ans = freq [ 0 ] * ( freq [ 0 ] - 1 ) / 2 ; ans += freq [ 2 ] * ( freq [ 2 ] - 1 ) / 2 ; ans += freq [ 1 ] * freq [ 3 ] ; return ans ; } int main ( ) { int arr [ ] = { 2 , 2 , 1 , 7 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << count4Divisibiles ( arr , n ) ; return 0 ; }
Find Sum of all unique sub | C ++ for finding sum of all unique subarray sum ; function for finding grandSum ; calculate cumulative sum of array cArray [ 0 ] will store sum of zero elements ; store all subarray sum in vector ; sort the vector ; mark all duplicate sub - array sum to zero ; calculate total sum ; return totalSum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int findSubarraySum ( int arr [ ] , int n ) { int i , j ; long long int cArray [ n + 1 ] = { 0 } ; for ( i = 0 ; i < n ; i ++ ) cArray [ i + 1 ] = cArray [ i ] + arr [ i ] ; vector < long long int > subArrSum ; for ( i = 1 ; i <= n ; i ++ ) for ( j = i ; j <= n ; j ++ ) subArrSum . push_back ( cArray [ j ] - cArray [ i - 1 ] ) ; sort ( subArrSum . begin ( ) , subArrSum . end ( ) ) ; long long totalSum = 0 ; for ( i = 0 ; i < subArrSum . size ( ) - 1 ; i ++ ) { if ( subArrSum [ i ] == subArrSum [ i + 1 ] ) { j = i + 1 ; while ( subArrSum [ j ] == subArrSum [ i ] && j < subArrSum . size ( ) ) { subArrSum [ j ] = 0 ; j ++ ; } subArrSum [ i ] = 0 ; } } for ( i = 0 ; i < subArrSum . size ( ) ; i ++ ) totalSum += subArrSum [ i ] ; return totalSum ; } int main ( ) { int arr [ ] = { 3 , 2 , 3 , 1 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findSubarraySum ( arr , n ) ; return 0 ; }
Smallest number whose set bits are maximum in a given range | C ++ program to find number whose set bits are maximum among ' l ' and ' r ' ; Returns smallest number whose set bits are maximum in given range . ; Initialize the maximum count and final answer as ' num ' ; Traverse for every bit of ' i ' number ; If count is greater than previous calculated max_count , update it ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMaxSetBits ( int left , int right ) { int max_count = -1 , num ; for ( int i = left ; i <= right ; ++ i ) { int temp = i , cnt = 0 ; while ( temp ) { if ( temp & 1 ) ++ cnt ; temp >>= 1 ; } if ( cnt > max_count ) { max_count = cnt ; num = i ; } } return num ; } int main ( ) { int l = 1 , r = 5 ; cout << countMaxSetBits ( l , r ) << " STRNEWLINE " ; l = 1 , r = 10 ; cout << countMaxSetBits ( l , r ) ; return 0 ; }
Find Sum of all unique sub | C ++ for finding sum of all unique subarray sum ; function for finding grandSum ; Go through all subarrays , compute sums and count occurrences of sums . ; Print all those sums that appear once . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int findSubarraySum ( int arr [ ] , int n ) { int res = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; m [ sum ] ++ ; } } for ( auto x : m ) if ( x . second == 1 ) res += x . first ; return res ; } int main ( ) { int arr [ ] = { 3 , 2 , 3 , 1 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findSubarraySum ( arr , n ) ; return 0 ; }
Quickselect Algorithm | CPP program for implementation of QuickSelect ; Standard partition process of QuickSort ( ) . It considers the last element as pivot and moves all smaller element to left of it and greater elements to right ; This function returns k 'th smallest element in arr[l..r] using QuickSort based method. ASSUMPTION: ALL ELEMENTS IN ARR[] ARE DISTINCT ; If k is smaller than number of elements in array ; Partition the array around last element and get position of pivot element in sorted array ; If position is same as k ; If position is more , recur for left subarray ; Else recur for right subarray ; If k is more than number of elements in array ; Driver program to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int partition ( int arr [ ] , int l , int r ) { int x = arr [ r ] , i = l ; for ( int j = l ; j <= r - 1 ; j ++ ) { if ( arr [ j ] <= x ) { swap ( arr [ i ] , arr [ j ] ) ; i ++ ; } } swap ( arr [ i ] , arr [ r ] ) ; return i ; } int kthSmallest ( int arr [ ] , int l , int r , int k ) { if ( k > 0 && k <= r - l + 1 ) { int index = partition ( arr , l , r ) ; if ( index - l == k - 1 ) return arr [ index ] ; if ( index - l > k - 1 ) return kthSmallest ( arr , l , index - 1 , k ) ; return kthSmallest ( arr , index + 1 , r , k - index + l - 1 ) ; } return INT_MAX ; } int main ( ) { int arr [ ] = { 10 , 4 , 5 , 8 , 6 , 11 , 26 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << " K - th ▁ smallest ▁ element ▁ is ▁ " << kthSmallest ( arr , 0 , n - 1 , k ) ; return 0 ; }
Recaman 's sequence | C ++ program to print n - th number in Recaman 's sequence ; Prints first n terms of Recaman sequence ; Create an array to store terms ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int recaman ( int n ) { int arr [ n ] ; arr [ 0 ] = 0 ; printf ( " % d , ▁ " , arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int curr = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == curr ) curr < 0 ) { curr = arr [ i - 1 ] + i ; break ; } } arr [ i ] = curr ; printf ( " % d , ▁ " , arr [ i ] ) ; } } int main ( ) { int n = 17 ; recaman ( n ) ; return 0 ; }
Recaman 's sequence | C ++ program to print n - th number in Recaman 's sequence ; Prints first n terms of Recaman sequence ; Print first term and store it in a hash ; Print remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void recaman ( int n ) { if ( n <= 0 ) return ; printf ( " % d , ▁ " , 0 ) ; unordered_set < int > s ; s . insert ( 0 ) ; int prev = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int curr = prev - i ; if ( curr < 0 || s . find ( curr ) != s . end ( ) ) curr = prev + i ; s . insert ( curr ) ; printf ( " % d , ▁ " , curr ) ; prev = curr ; } } int main ( ) { int n = 17 ; recaman ( n ) ; return 0 ; }
Find index of an extra element present in one sorted array | C ++ code for above approach ; function return sum of array elements ; function return index of given element ; Function to find Index ; Calculating extra element ; returns index of extra element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int arr [ ] , int n ) { int summ = 0 ; for ( int i = 0 ; i < n ; i ++ ) { summ += arr [ i ] ; } return summ ; } int indexOf ( int arr [ ] , int element , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == element ) { return i ; } } return -1 ; } int find_extra_element_index ( int arrA [ ] , int arrB [ ] , int n , int m ) { int extra_element = sum ( arrA , n ) - sum ( arrB , m ) ; return indexOf ( arrA , extra_element , n ) ; } int main ( ) { int arrA [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arrB [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = sizeof ( arrA ) / sizeof ( arrA [ 0 ] ) ; int m = sizeof ( arrB ) / sizeof ( arrB [ 0 ] ) ; cout << find_extra_element_index ( arrA , arrB , n , m ) ; }
Largest subset whose all elements are Fibonacci numbers | C ++ program to find largest Fibonacci subset ; Prints largest subset of an array whose all elements are fibonacci numbers ; Find maximum element in arr [ ] ; Generate all Fibonacci numbers till max and store them in hash . ; Npw iterate through all numbers and quickly check for Fibonacci using hash . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findFibSubset ( int arr [ ] , int n ) { int max = * std :: max_element ( arr , arr + n ) ; int a = 0 , b = 1 ; unordered_set < int > hash ; hash . insert ( a ) ; hash . insert ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; hash . insert ( b ) ; } for ( int i = 0 ; i < n ; i ++ ) if ( hash . find ( arr [ i ] ) != hash . end ( ) ) printf ( " % d ▁ " , arr [ i ] ) ; } int main ( ) { int arr [ ] = { 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findFibSubset ( arr , n ) ; return 0 ; }
Pairs of Amicable Numbers | Efficient C ++ program to count Amicable pairs in an array . ; Calculate the sum of proper divisors ; 1 is a proper divisor ; To handle perfect squares ; Check if pair is amicable ; This function prints count of amicable pairs present in the input array ; Map to store the numbers ; Iterate through each number , and find the sum of proper divisors and check if it 's  also present in the array ; It 's sum of proper divisors ; As the pairs are counted twice , thus divide by 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumOfDiv ( int x ) { int sum = 1 ; for ( int i = 2 ; i <= sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { sum += i ; if ( x / i != i ) sum += x / i ; } } return sum ; } bool isAmicable ( int a , int b ) { return ( sumOfDiv ( a ) == b && sumOfDiv ( b ) == a ) ; } int countPairs ( int arr [ ] , int n ) { unordered_set < int > s ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) s . insert ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( sumOfDiv ( arr [ i ] ) ) != s . end ( ) ) { int sum = sumOfDiv ( arr [ i ] ) ; if ( isAmicable ( arr [ i ] , sum ) ) count ++ ; } } return count / 2 ; } int main ( ) { int arr1 [ ] = { 220 , 284 , 1184 , 1210 , 2 , 5 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << countPairs ( arr1 , n1 ) << endl ; int arr2 [ ] = { 2620 , 2924 , 5020 , 5564 , 6232 , 6368 } ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << countPairs ( arr2 , n2 ) << endl ; return 0 ; }
Minimum number of characters required to be removed such that every character occurs same number of times | C ++ program for the above approach ; Function to find minimum number of character removals required to make frequency of all distinct characters the same ; Stores the frequency of each character ; Traverse the string ; Stores the frequency of each charachter in sorted order ; Traverse the Map ; Insert the frequency in multiset ; Stores the count of elements required to be removed ; Stores the size of multiset ; Traverse the multiset ; Update the ans ; Increment i by 1 ; Return ; Driver Code ; Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumDeletion ( string s , int n ) { map < char , int > countMap ; for ( int i = 0 ; i < n ; i ++ ) { countMap [ s [ i ] ] ++ ; } multiset < int > countMultiset ; for ( auto it : countMap ) { countMultiset . insert ( it . second ) ; } int ans = INT_MAX ; int i = 0 ; int m = countMultiset . size ( ) ; for ( auto j : countMultiset ) { ans = min ( ans , n - ( m - i ) * j ) ; i ++ ; } return ans ; } int main ( ) { string S = " geeksforgeeks " ; int N = S . length ( ) ; cout << minimumDeletion ( S , N ) ; return 0 ; }
Minimum number of distinct elements present in a K | C ++ program for the above approach ; Function to count the minimum number of distinct elements present in any subsequence of length K of the given array ; Stores the frequency of each array element ; Traverse the array ; Update frequency of array elements ; Store the required result ; Store the length of the required subsequence ; Store the frequencies in decreasing order ; Traverse the map ; Push the frequencies into the HashMap ; Sort the array in decreasing order ; Add the elements into the subsequence starting from one with highest frequency ; If length of subsequence is >= k ; Print the result ; Driver Code ; Store the size of the array ; Function Call to count minimum number of distinct elmeents present in a K - length subsequence
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinimumDistinct ( int A [ ] , int N , int K ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) mp [ A [ i ] ] ++ ; int count = 0 ; int len = 0 ; vector < int > counts ; for ( auto i : mp ) counts . push_back ( i . second ) ; sort ( counts . begin ( ) , counts . end ( ) , greater < int > ( ) ) ; for ( int i = 0 ; i < counts . size ( ) ; i ++ ) { if ( len >= K ) break ; len += counts [ i ] ; count ++ ; } cout << count ; } int main ( ) { int A [ ] = { 3 , 1 , 3 , 2 , 3 , 4 , 5 , 4 } ; int K = 4 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findMinimumDistinct ( A , N , K ) ; return 0 ; }
Calculate cost of visiting all array elements in increasing order | C ++ implementation of the above approach ; Function to calculate total cost of visiting array elements in increasing order ; Stores the pair of element and their positions ; Traverse the array arr [ ] ; Push the pair { arr [ i ] , i } in v ; Sort the vector in ascending order . ; Stores the total cost ; Stores the index of last element visited ; Traverse the vector v ; Increment ans ; Assign ; Return ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateDistance ( int arr [ ] , int N ) { vector < pair < int , int > > v ; for ( int i = 0 ; i < N ; i ++ ) v . push_back ( { arr [ i ] , i } ) ; sort ( v . begin ( ) , v . end ( ) ) ; int ans = 0 ; int last = 0 ; for ( auto j : v ) { ans += abs ( j . second - last ) ; last = j . second ; } return ans ; } int main ( ) { int arr [ ] = { 4 , 3 , 2 , 5 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << calculateDistance ( arr , N ) ; }
Modify array by sorting nearest perfect squares of array elements having their digits sorted in decreasing order | C ++ program of the above approach ; Function to sort array in ascending order after replacing array elements by nearest perfect square of decreasing order of digits ; Traverse the array of strings ; Convert the current array element to a string ; Sort each string in descending order ; Convert the string to equivalent integer ; Calculate square root of current array element ; Calculate perfect square ; Find the nearest perfect square ; Sort the array in ascending order ; Print the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortArr ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { string s = to_string ( arr [ i ] ) ; sort ( s . begin ( ) , s . end ( ) , greater < char > ( ) ) ; arr [ i ] = stoi ( s ) ; int sr = sqrt ( arr [ i ] ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) arr [ i ] = a ; else arr [ i ] = b ; } sort ( arr , arr + N ) ; for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 29 , 43 , 28 , 12 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , N ) ; return 0 ; }
Minimize cost required to make all array elements greater than or equal to zero | C ++ program for the above approach ; Function to find the minimum cost to make all array elements greater than or equal to 0 ; Sort the array in ascending order ; Stores the cost to make current array element >= 0 ; Stores the cost to make all array elements >= 0 ; Traverse the array and insert all the elements which are < 0 ; If current array element is negative ; Cost to make all array elements >= 0 ; Update curr if ans is minimum ; Print the minimum cost ; Driver Code ; Given array ; Size of the array ; Given value of X ; Function call to find minimum cost to make all array elements >= 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minCost ( int arr [ ] , int N , int X ) { sort ( arr , arr + N ) ; int sum = 0 ; int cost = 0 ; int min_cost = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cost = abs ( arr [ i ] ) * X + ( sum - abs ( arr [ i ] ) * i ) ; sum += abs ( arr [ i ] ) ; min_cost = min ( min_cost , cost ) ; } } cout << min_cost ; } int main ( ) { int arr [ ] = { -1 , -3 , -2 , 4 , -1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int X = 2 ; minCost ( arr , N , X ) ; return 0 ; }
Count arrays having at least K elements exceeding XOR of all given array elements by X given operations | C ++ program for the above approach ; Stores the final answer ; Utility function to count arrays having at least K elements exceeding XOR of all given array elements ; If no operations are left ; Stores the count of possible arrays ; Count array elements are greater than XOR ; Stores first element ; Delete first element ; Recursive call ; Insert first element into vector ; Stores the last element ; Remove last element from vector ; Recursive call ; Push last element into vector ; Increment first element ; Recursive call ; Decrement first element ; Increment last element ; Recursive call ; Decrement last element ; Function to find the count of arrays having atleast K elements greater than XOR of array ; Stores the XOR value of original array ; Traverse the vector ; Print the answer ; Driver Code ; Given vector ; Given value of X & K
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = 0 ; void countArraysUtil ( vector < int > & arr , int X , int K , int xorVal ) { if ( X == 0 ) { int cnt = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr [ i ] > xorVal ) cnt ++ ; } if ( cnt >= K ) ans ++ ; return ; } int temp = arr [ 0 ] ; arr . erase ( arr . begin ( ) ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . insert ( arr . begin ( ) , temp ) ; temp = arr . back ( ) ; arr . pop_back ( ) ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr . push_back ( temp ) ; arr [ 0 ] ++ ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr [ 0 ] -- ; arr [ arr . size ( ) - 1 ] ++ ; countArraysUtil ( arr , X - 1 , K , xorVal ) ; arr [ arr . size ( ) - 1 ] -- ; } void countArrays ( vector < int > & arr , int X , int K ) { int xorVal = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) xorVal = xorVal ^ arr [ i ] ; countArraysUtil ( arr , X , K , xorVal ) ; cout << ans ; } int main ( ) { vector < int > arr = { 10 , 2 , 10 , 5 } ; int X = 3 , K = 3 ; countArrays ( arr , X , K ) ; return 0 ; }
Difference between maximum and minimum of a set of anagrams from an array | C ++ program for the above approach ; Utility function to find the hash value for each element of the given array ; Initialize an array with first 10 prime numbers ; Iterate over digits of N ; Update Hash Value ; Update N ; Function to find the set of anagrams in the array and print the difference between the maximum and minimum of these numbers ; Map to store the hash value and the array elements having that hash value ; Find the hash value for each arr [ i ] by calling hash function ; Iterate over the map ; If size of vector at m [ i ] greater than 1 then it must contain the anagrams ; Find the minimum and maximum element of this anagrams vector ; Display the difference ; If the end of Map is reached , then no anagrams are present ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int hashFunction ( int N ) { int prime [ 10 ] = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 } ; int value = 1 , r ; while ( N != 0 ) { r = N % 10 ; value = value * prime [ r ] ; N = N / 10 ; } return value ; } void findDiff ( int arr [ ] , int n ) { map < int , vector < int > > m ; int h , min , max ; for ( int i = 0 ; i < n ; i ++ ) { h = hashFunction ( arr [ i ] ) ; m [ h ] . push_back ( arr [ i ] ) ; } for ( auto i = 0 ; i != m . size ( ) ; i ++ ) { if ( m [ i ] . size ( ) > 1 ) { min = * min_element ( m [ i ] . begin ( ) , m [ i ] . end ( ) ) ; max = * max_element ( m [ i ] . begin ( ) , m [ i ] . end ( ) ) ; cout << max - min ; break ; } else if ( i == m . size ( ) - 1 ) cout << -1 ; } } int main ( ) { int arr [ ] = { 121 , 312 , 234 , 211 , 112 , 102 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findDiff ( arr , N ) ; return 0 ; }
Maximum number of apples that can be eaten by a person | C ++ program of the above approach ; Function to find the maximum number of apples a person can eat such that the person eat at most one apple in a day . ; Stores count of apples and number of days those apples are edible ; Store count of apples and number of days those apples are edible ; Stores indices of the array ; Stores count of days ; Stores maximum count of edible apples ; Traverse both the arrays ; If top element of the apple is not already expired ; Insert count of apples and their expiration date ; Remove outdated apples ; Insert all the apples produces by tree on current day ; Stores top element of pq ; Remove top element of pq ; If count of apples in curr is greater than 0 ; Insert count of apples and their expiration date ; Update total_apples ; Update index ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntMaxApples ( vector < int > apples , vector < int > days ) { typedef pair < int , int > P ; priority_queue < P , vector < p > , greater < p > > pq ; int i = 0 ; int n = apples . size ( ) ; int total_apples = 0 ; while ( i < n || ! pq . empty ( ) ) { if ( i < n && apples [ i ] != 0 ) { pq . push ( { i + days [ i ] - 1 , apples [ i ] } ) ; } while ( ! pq . empty ( ) && pq . top ( ) . first < i ) { pq . pop ( ) ; } if ( ! pq . empty ( ) ) { auto curr = pq . top ( ) ; pq . pop ( ) ; if ( curr . second > 1 ) { pq . push ( { curr . first , curr . second - 1 } ) ; } ++ total_apples ; } ++ i ; } return total_apples ; } int main ( ) { vector < int > apples = { 1 , 2 , 3 , 5 , 2 } ; vector < int > days = { 3 , 2 , 1 , 4 , 2 } ; cout << cntMaxApples ( apples , days ) ; return 0 ; }
Maximum area rectangle by picking four sides from array | CPP program for finding maximum area possible of a rectangle ; function for finding max area ; sort array in non - increasing order ; Initialize two sides of rectangle ; traverse through array ; if any element occurs twice store that as dimension ; return the product of dimensions ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findArea ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; int dimension [ 2 ] = { 0 , 0 } ; for ( int i = 0 , j = 0 ; i < n - 1 && j < 2 ; i ++ ) if ( arr [ i ] == arr [ i + 1 ] ) dimension [ j ++ ] = arr [ i ++ ] ; return ( dimension [ 0 ] * dimension [ 1 ] ) ; } int main ( ) { int arr [ ] = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findArea ( arr , n ) ; return 0 ; }