codeparrot/xlcost-text-to-code huggingface dataset:
Count numbers whose maximum sum of distinct digit | C ++ implementation to find the Maximum count of numbers whose sum of distinct digit - sum less than or equal to the given number ; Function to find the digit - sum of a number ; Loop to iterate the number digit - wise to find digit - sum ; variable to store last digit ; Function to find the count of number ; Vector to store the Sum of Digits ; Sum of digits for each element in vector ; Sorting the digitSum vector ; Removing the duplicate elements ; Count variable to store the Count ; Finding the Count of Numbers ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SumofDigits ( int digit ) { int sum = 0 ; while ( digit != 0 ) { int rem = digit % 10 ; sum += rem ; digit /= 10 ; } return sum ; } int findCountofNumbers ( int arr [ ] , int n , int M ) { vector < int > SumDigits ; for ( int i = 0 ; i < n ; i ++ ) { int s = SumofDigits ( arr [ i ] ) ; SumDigits . push_back ( s ) ; } sort ( SumDigits . begin ( ) , SumDigits . end ( ) ) ; vector < int > :: iterator ip ; ip = unique ( SumDigits . begin ( ) , SumDigits . end ( ) ) ; SumDigits . resize ( distance ( SumDigits . begin ( ) , ip ) ) ; int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < SumDigits . size ( ) ; i ++ ) { if ( sum > M ) break ; sum += SumDigits [ i ] ; if ( sum <= M ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 1 , 45 , 16 , 17 , 219 , 32 , 22 } , M = 10 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findCountofNumbers ( arr , n , M ) ; return 0 ; }
Minimum sum of product of elements of pairs of the given array | C ++ program to find the minimum product of sum of pair of element in array arr [ ] ; Function to find the minimum product ; Sort the array using STL sort ( ) function ; Initialise product to 1 ; Find product of sum of all pairs ; Return the product ; Driver code ; Function call to find product
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int minimumProduct ( int * arr , int n ) { sort ( arr , arr + n ) ; int product = 1 ; for ( int i = 0 ; i < n ; i += 2 ) { product *= ( arr [ i ] + arr [ i + 1 ] ) ; } return product ; } int main ( ) { int arr [ ] = { 1 , 6 , 3 , 1 , 7 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumProduct ( arr , n ) << endl ; return 0 ; }
Minimum removals required to make ranges non |  ; Sort by minimum starting point ; If the current starting point is less than the previous interval 's ending point   (ie. there is an overlap) ; increase rem ; Remove the interval with the higher ending point ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minRemovals ( vector < vector < int > > & ranges ) { int size = ranges . size ( ) , rem = 0 ; if ( size <= 1 ) return 0 ; sort ( ranges . begin ( ) , ranges . end ( ) , [ ] ( const vector < int > & a , const vector < int > & b ) { return a [ 0 ] < b [ 0 ] ; } ) ; int end = ranges [ 0 ] [ 1 ] ; for ( int i = 1 ; i < ranges . size ( ) ; i ++ ) { if ( ranges [ i ] [ 0 ] < end ) { rem ++ ; end = min ( ranges [ i ] [ 1 ] , end ) ; } else end = ranges [ i ] [ 1 ] ; } return rem ; } int main ( ) { vector < vector < int > > input = { { 19 , 25 } , { 10 , 20 } , { 16 , 20 } } ; cout << minRemovals ( input ) << endl ; }
Check if elements of array can be arranged in AP , GP or HP | C ++ program to check if a given array form AP , GP or HP ; Returns true if arr [ 0. . n - 1 ] can form AP ; Base Case ; Sort array ; After sorting , difference between consecutive elements must be same . ; Traverse the given array and check if the difference between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form GP ; Base Case ; Sort array ; After sorting , common ratio between consecutive elements must be same . ; Traverse the given array and check if the common ratio between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form HP ; Base Case ; Find reciprocal of arr [ ] ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . ; Driver 's Code ; Function to check AP ; Function to check GP ; Function to check HP
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkIsAP ( double arr [ ] , int n ) { if ( n == 1 ) return true ; sort ( arr , arr + n ) ; double d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] != d ) { return false ; } } return true ; } bool checkIsGP ( double arr [ ] , int n ) { if ( n == 1 ) return true ; sort ( arr , arr + n ) ; double r = arr [ 1 ] / arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } bool checkIsHP ( double arr [ ] , int n ) { if ( n == 1 ) { return true ; } double rec [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } if ( checkIsAP ( rec , n ) ) return true ; else return false ; } int main ( ) { double arr [ ] = { 1.0 / 5.0 , 1.0 / 10.0 , 1.0 / 15.0 , 1.0 / 20.0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int flag = 0 ; if ( checkIsAP ( arr , n ) ) { cout << " Yes , ▁ An ▁ AP ▁ can ▁ be ▁ formed " << endl ; flag = 1 ; } if ( checkIsGP ( arr , n ) ) { cout << " Yes , ▁ A ▁ GP ▁ can ▁ be ▁ formed " << endl ; flag = 1 ; } if ( checkIsHP ( arr , n ) ) { cout << " Yes , ▁ A ▁ HP ▁ can ▁ be ▁ formed " << endl ; flag = 1 ; } else if ( flag == 0 ) { cout << " No " ; } return 0 ; }
Sort the Array by reversing the numbers in it | C ++ implementation of the approach ; Function to return the reverse of n ; Function to sort the array according to the reverse of elements ; Vector to store the reverse with respective elements ; Inserting reverse with elements in the vector pair ; Sort the vector , this will sort the pair according to the reverse of elements ; Print the sorted vector content ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } void sortArr ( int arr [ ] , int n ) { vector < pair < int , int > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( reversDigits ( arr [ i ] ) , arr [ i ] ) ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) cout << vp [ i ] . second << " ▁ " ; } int main ( ) { int arr [ ] = { 12 , 10 , 102 , 31 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n ) ; return 0 ; }
Count number of pairs with positive sum in an array | C ++ program to count the pairs with positive sum ; Returns number of pairs in arr [ 0. . n - 1 ] with positive sum ; Sort the array in increasing order ; Intialise result ; Intialise first and second pointer ; Till the pointers doesn 't converge  traverse array to  count the pairs ; If sum of arr [ i ] && arr [ j ] > 0 , then the count of pairs with positive sum is the difference between the two pointers ; Increase the count ; Driver 's Code ; Function call to count the pairs with positive sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountPairs ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int count = 0 ; int l = 0 , r = n - 1 ; while ( l < r ) { if ( arr [ l ] + arr [ r ] > 0 ) { count += ( r - l ) ; r -- ; } else { l ++ ; } } return count ; } int main ( ) { int arr [ ] = { -7 , -1 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CountPairs ( arr , n ) ; return 0 ; }
Sort the given Matrix | Memory Efficient Approach | C ++ implementation to sort the given matrix in strict order ; Function to sort the matrix ; Number of elements in matrix ; Loop to sort the matrix using Bubble Sort ; Condition to check if the Adjacent elements ; Swap if previous value is greater ; Loop to print the matrix ; Driver Code ; Function call to sort ; Function call to print matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE #define M  3 NEW_LINE void sortMat ( int data [ N ] [ M ] , int row , int col ) { int size = row * col ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size - 1 ; j ++ ) { if ( data [ j / col ] [ j % col ] > data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] ) { int temp = data [ j / col ] [ j % col ] ; data [ j / col ] [ j % col ] = data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] ; data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] = temp ; } } } } void printMat ( int mat [ N ] [ M ] , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { cout << mat [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { int mat [ N ] [ M ] = { { 5 , 4 , 7 } , { 1 , 3 , 8 } , { 2 , 9 , 6 } } ; int row = N ; int col = M ; sortMat ( mat , row , col ) ; printMat ( mat , row , col ) ; return 0 ; }
Partition the array into two odd length groups with minimized absolute difference between their median | C ++ program to minimise the median between partition array ; Function to find minimise the median between partition array ; Sort the given array arr [ ] ; Return the difference of two middle element of the arr [ ] ; Driver Code ; Size of arr [ ] ; Function that returns the minimum the absolute difference between median of partition array
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int minimiseMedian ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; return abs ( arr [ n / 2 ] - arr [ ( n / 2 ) - 1 ] ) ; } int main ( ) { int arr [ ] = { 15 , 25 , 35 , 50 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimiseMedian ( arr , n ) ; return 0 ; }
Maximum number of overlapping Intervals | C ++ program that print maximum number of overlap among given ranges ; Function that print maximum overlap among ranges ; variable to store the maximum count ; storing the x and y coordinates in data vector ; pushing the x coordinate ; pushing the y coordinate ; sorting of ranges ; Traverse the data vector to count number of overlaps ; if x occur it means a new range is added so we increase count ; if y occur it means a range is ended so we decrease count ; updating the value of ans after every traversal ; printing the maximum value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void overlap ( vector < pair < int , int > > v ) { int ans = 0 ; int count = 0 ; vector < pair < int , char > > data ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { data . push_back ( { v [ i ] . first , ' x ' } ) ; data . push_back ( { v [ i ] . second , ' y ' } ) ; } sort ( data . begin ( ) , data . end ( ) ) ; for ( int i = 0 ; i < data . size ( ) ; i ++ ) { if ( data [ i ] . second == ' x ' ) count ++ ; if ( data [ i ] . second == ' y ' ) count -- ; ans = max ( ans , count ) ; } cout << ans << endl ; } int main ( ) { vector < pair < int , int > > v = { { 1 , 2 } , { 2 , 4 } , { 3 , 6 } } ; overlap ( v ) ; return 0 ; }
Making three numbers equal with the given operations | C ++ implementation of the approach ; Function that returns true if a , b and c can be made equal with the given operations ; Sort the three numbers ; Find the sum of difference of the 3 rd and 2 nd element and the 3 rd and 1 st element ; Subtract the difference from k ; Check the required condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBeEqual ( int a , int b , int c , int k ) { int arr [ 3 ] ; arr [ 0 ] = a ; arr [ 1 ] = b ; arr [ 2 ] = c ; sort ( arr , arr + 3 ) ; int diff = 2 * arr [ 2 ] - arr [ 1 ] - arr [ 0 ] ; k = k - diff ; if ( k < 0 k % 3 != 0 ) return false ; return true ; } int main ( ) { int a1 = 6 , b1 = 3 , c1 = 2 , k1 = 7 ; if ( canBeEqual ( a1 , b1 , c1 , k1 ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Arrange numbers to form a valid sequence | C ++ implementation of the above approach ; Function to organize the given numbers to form a valid sequence . ; Sorting the array ; Two pointer technique to organize the numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > orgazineInOrder ( vector < int > vec , vector < int > op , int n ) { vector < int > result ( n ) ; sort ( vec . begin ( ) , vec . end ( ) ) ; int i = 0 , j = n - 1 , k = 0 ; while ( i <= j && k <= n - 2 ) { if ( op [ k ] == ' < ' ) { result [ k ] = vec [ i ++ ] ; } else { result [ k ] = vec [ j -- ] ; } k ++ ; } result [ n - 1 ] = vec [ i ] ; return result ; } int main ( ) { vector < int > vec ( { 8 , 2 , 7 , 1 , 5 , 9 } ) ; vector < int > op ( { ' > ' , ' > ' , ' < ' , ' > ' , ' < ' } ) ; vector < int > result = orgazineInOrder ( vec , op , vec . size ( ) ) ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { cout << result [ i ] << " ▁ " ; } return 0 ; }
Find the distance between two person after reconstruction of queue | C ++ implementation of the approach ; Function to find the correct order and then return the distance between the two persons ; Make pair of both height & infront and insert to vector ; Sort the vector in ascending order ; Find the correct place for every person ; Insert into position vector according to infront value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getDistance ( int arr [ ] [ 2 ] , int n , int a , int b ) { vector < pair < int , int > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( { arr [ i ] [ 0 ] , arr [ i ] [ 1 ] } ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; vector < int > pos ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) { int height = vp [ i ] . first ; int k = vp [ i ] . second ; pos . insert ( pos . begin ( ) + k , height ) ; } int first = -1 , second = -1 ; for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( pos [ i ] == a ) first = i ; if ( pos [ i ] == b ) second = i ; } return abs ( first - second ) ; } int main ( ) { int arr [ ] [ 2 ] = { { 5 , 0 } , { 3 , 0 } , { 2 , 0 } , { 6 , 4 } , { 1 , 0 } , { 4 , 3 } } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int a = 6 , b = 5 ; cout << getDistance ( arr , n , a , b ) ; return 0 ; }
Minimize the sum of differences of consecutive elements after removing exactly K elements | C ++ implementation of the above approach . ; function to find minimum sum ; variable to store final answer and initialising it with the values when 0 elements is removed from the left and K from the right . ; loop to simulate removal of elements ; removing i elements from the left and and K - i elements from the right and updating the answer correspondingly ; returning final answer ; driver function ; input values ; callin the required function ;
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int * arr , int n , int k ) { int ans = arr [ n - k - 1 ] - arr [ 0 ] ; for ( int i = 1 ; i <= k ; i ++ ) { ans = min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; } return ans ; } int32_t main ( ) { int arr [ ] = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findSum ( arr , n , k ) ; }
Minimum elements to be removed from the ends to make the array sorted | C ++ implementation of the approach ; Function to return the minimum number of elements to be removed from the ends of the array to make it sorted ; To store the final answer ; Two pointer loop ; While the array is increasing increment j ; Updating the ans ; Updating the left pointer ; Returning the final answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMin ( int * arr , int n ) { int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n and arr [ j ] > = arr [ j - 1 ] ) j ++ ; ans = max ( ans , j - i ) ; i = j - 1 ; } return n - ans ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findMin ( arr , n ) ; return 0 ; }
Number of subsequences of maximum length K containing no repeated elements | C ++ implementation of the approach ; Returns number of subsequences of maximum length k and contains no repeated element ; Sort the array a [ ] ; Store the frequencies of all the distinct element in the vector arr ; count is the the number of such subsequences ; Create a 2 - d array dp [ n + 1 ] [ m + 1 ] to store the intermediate result ; Initialize the first row to 1 ; Update the dp [ ] [ ] array based on the recurrence relation ; Return the number of subsequences ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubSeq ( int a [ ] , int n , int k ) { sort ( a , a + n ) ; vector < int > arr ; for ( int i = 0 ; i < n ; ) { int count = 1 , x = a [ i ] ; i ++ ; while ( i < n && a [ i ] == x ) { count ++ ; i ++ ; } arr . push_back ( count ) ; } int m = arr . size ( ) ; n = min ( m , k ) ; int count = 1 ; int dp [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m ; j >= 0 ; j -- ) { if ( j > m - i ) dp [ i ] [ j ] = 0 ; else { dp [ i ] [ j ] = dp [ i ] [ j + 1 ] + arr [ j ] * dp [ i - 1 ] [ j + 1 ] ; } } count = count + dp [ i ] [ 0 ] ; } return count ; } int main ( ) { int a [ ] = { 2 , 2 , 3 , 3 , 5 } ; int n = sizeof ( a ) / sizeof ( int ) ; int k = 3 ; cout << countSubSeq ( a , n , k ) ; return 0 ; }
Find the count of unvisited indices in an infinite array | C ++ implementation of the approach ; Function to return the count of unvisited indices starting from the index 0 ; Largest index that cannot be visited ; Push the index to the queue ; To store the required count ; Current index that cannot be visited ; Increment the count for the current index ; ( curr - m ) and ( curr - n ) are also unreachable if they are valid indices ; Return the required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countUnvisited ( int n , int m ) { int X = ( m * n ) - m - n ; queue < int > queue ; queue . push ( X ) ; int count = 0 ; while ( queue . size ( ) > 0 ) { int curr = queue . front ( ) ; queue . pop ( ) ; count ++ ; if ( curr - m > 0 ) queue . push ( curr - m ) ; if ( curr - n > 0 ) queue . push ( curr - n ) ; } return count ; } int main ( ) { int n = 2 , m = 5 ; cout << countUnvisited ( n , m ) ; return 0 ; }
Check whether the string S1 can be made equal to S2 with the given operation | CPP implementation of the approach ; Function to return the string formed by the odd indexed characters of s ; Function to return the string formed by the even indexed characters of s ; Function that returns true if s1 can be made equal to s2 with the given operation ; Get the string formed by the even indexed characters of s1 ; Get the string formed by the even indexed characters of s2 ; Get the string formed by the odd indexed characters of s1 ; Get the string formed by the odd indexed characters of s2 ; Sorting all the lists ; If the strings can be made equal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string partOdd ( string s ) { string st = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 != 0 ) st += s [ i ] ; } return st ; } string partEven ( string str ) { string s = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( i % 2 == 0 ) s += str [ i ] ; } return s ; } bool canBeMadeEqual ( string s1 , string s2 ) { string even_s1 = partEven ( s1 ) ; string even_s2 = partEven ( s2 ) ; string odd_s1 = partOdd ( s1 ) ; string odd_s2 = partOdd ( s2 ) ; sort ( even_s1 . begin ( ) , even_s1 . end ( ) ) ; sort ( even_s2 . begin ( ) , even_s2 . end ( ) ) ; sort ( odd_s1 . begin ( ) , odd_s1 . end ( ) ) ; sort ( odd_s2 . begin ( ) , odd_s2 . end ( ) ) ; if ( even_s1 == even_s2 and odd_s1 == odd_s2 ) return true ; return false ; } int main ( ) { string s1 = " cdab " ; string s2 = " abcd " ; if ( canBeMadeEqual ( s1 , s2 ) ) cout << " Yes " << endl ; else cout << " No " << endl ; }
Sort the given stack elements based on their modulo with K | C ++ implementation of the above approach ; Function to sort the stack using another stack based on the values of elements modulo k ; Pop out the first element ; While temporary stack is not empty ; The top of the stack modulo k is greater than ( temp & k ) or if they are equal then compare the values ; Pop from temporary stack and push it to the input stack ; Push temp in temporary of stack ; Push all the elements in the original stack to get the ascending order ; Print the sorted elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortStack ( stack < int > & input , int k ) { stack < int > tmpStack ; while ( ! input . empty ( ) ) { int tmp = input . top ( ) ; input . pop ( ) ; while ( ! tmpStack . empty ( ) ) { int tmpStackMod = tmpStack . top ( ) % k ; int tmpMod = tmp % k ; if ( ( tmpStackMod > tmpMod ) || ( tmpStackMod == tmpMod && tmpStack . top ( ) > tmp ) ) { input . push ( tmpStack . top ( ) ) ; tmpStack . pop ( ) ; } else break ; } tmpStack . push ( tmp ) ; } while ( ! tmpStack . empty ( ) ) { input . push ( tmpStack . top ( ) ) ; tmpStack . pop ( ) ; } while ( ! input . empty ( ) ) { cout << input . top ( ) << " ▁ " ; input . pop ( ) ; } } int main ( ) { stack < int > input ; input . push ( 10 ) ; input . push ( 3 ) ; input . push ( 2 ) ; input . push ( 6 ) ; input . push ( 12 ) ; int k = 4 ; sortStack ( input , k ) ; return 0 ; }
Longest sub | C ++ implementation of the approach ; Function to return the length of the largest subsequence with non - negative sum ; To store the current sum ; Sort the input array in non - increasing order ; Traverse through the array ; Add the current element to the sum ; Condition when c_sum falls below zero ; Complete array has a non - negative sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLen ( int * arr , int n ) { int c_sum = 0 ; sort ( arr , arr + n , greater < int > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { c_sum += arr [ i ] ; if ( c_sum < 0 ) return i ; } return n ; } int main ( ) { int arr [ ] = { 3 , 5 , -6 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << maxLen ( arr , n ) ; return 0 ; }
Find Partition Line such that sum of values on left and right is equal | C ++ implementation of the approach ; Function that returns true if the required line exists ; To handle negative values from x [ ] ; Update arr [ ] such that arr [ i ] contains the sum of all v [ j ] such that x [ j ] = i for all valid values of j ; Update arr [ i ] such that arr [ i ] contains the sum of the subarray arr [ 0. . . i ] from the original array ; If all the points add to 0 then the line can be drawn anywhere ; If the line is drawn touching the leftmost possible points ; If the line is drawn just before the current point ; If the line is drawn touching the current point ; If the line is drawn just after the current point ; If the line is drawn touching the rightmost possible points ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; bool lineExists ( int x [ ] , int y [ ] , int v [ ] , int n ) { int size = ( 2 * MAX ) + 1 ; long arr [ size ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { arr [ x [ i ] + MAX ] += v [ i ] ; } for ( int i = 1 ; i < size ; i ++ ) arr [ i ] += arr [ i - 1 ] ; if ( arr [ size - 1 ] == 0 ) return true ; if ( arr [ size - 1 ] - arr [ 0 ] == 0 ) return true ; for ( int i = 1 ; i < size - 1 ; i ++ ) { if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i - 1 ] ) return true ; if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i ] ) return true ; if ( arr [ i ] == arr [ size - 1 ] - arr [ i ] ) return true ; } if ( arr [ size - 2 ] == 0 ) return true ; return false ; } int main ( ) { int x [ ] = { -3 , 5 , 8 } ; int y [ ] = { 8 , 7 , 9 } ; int v [ ] = { 8 , 2 , 10 } ; int n = sizeof ( x ) / sizeof ( int ) ; if ( lineExists ( x , y , v , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximum sum of minimums of pairs in an array | C ++ implementation of the approach ; Function to return the maximum required sum of the pairs ; Sort the array ; To store the sum ; Start making pairs of every two consecutive elements as n is even ; Minimum element of the current pair ; Return the maximum possible sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int a [ ] , int n ) { sort ( a , a + n ) ; int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { sum += a [ i ] ; } return sum ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 1 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSum ( arr , n ) ; return 0 ; }