codeparrot/xlcost-text-to-code huggingface dataset:
Sum of elements in an array whose difference with the mean of another array is less than k | C ++ implementation of the approach ; Function for finding sum of elements whose diff with mean is not more than k ; Find the mean of second array ; Find sum of elements from array1 whose difference with mean in not more than k ; Return result ; Driver code
#include <iostream> NEW_LINE using namespace std ; int findSumofEle ( int arr1 [ ] , int m , int arr2 [ ] , int n , int k ) { float arraySum = 0 ; for ( int i = 0 ; i < n ; i ++ ) arraySum += arr2 [ i ] ; float mean = arraySum / n ; int sumOfElements = 0 ; float difference ; for ( int i = 0 ; i < m ; i ++ ) { difference = arr1 [ i ] - mean ; if ( ( difference < 0 ) && ( k > ( -1 ) * difference ) ) { sumOfElements += arr1 [ i ] ; } if ( ( difference >= 0 ) && ( k > difference ) ) { sumOfElements += arr1 [ i ] ; } } return sumOfElements ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 3 , 4 , 7 , 9 } ; int arr2 [ ] = { 0 , 1 , 2 , 1 , 1 , 4 } ; int k = 2 ; int m , n ; m = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << findSumofEle ( arr1 , m , arr2 , n , k ) ; return 0 ; }
Find n positive integers that satisfy the given equations | C ++ implementation of the approach ; Function to find n positive integers that satisfy the given conditions ; To store n positive integers ; Place N - 1 one 's ; If can not place ( y - ( n - 1 ) ) as the Nth integer ; Place Nth integer ; To store the sum of squares of N integers ; If it is less than x ; Print the required integers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findIntegers ( int n , int x , int y ) { vector < int > ans ; for ( int i = 0 ; i < n - 1 ; i ++ ) ans . push_back ( 1 ) ; if ( y - ( n - 1 ) <= 0 ) { cout << " - 1" ; return ; } ans . push_back ( y - ( n - 1 ) ) ; int store = 0 ; for ( int i = 0 ; i < n ; i ++ ) store += ans [ i ] * ans [ i ] ; if ( store < x ) { cout << " - 1" ; return ; } for ( int i = 0 ; i < n ; i ++ ) cout << ans [ i ] << " ▁ " ; } int main ( ) { int n = 3 , x = 254 , y = 18 ; findIntegers ( n , x , y ) ; return 0 ; }
Find the minimum number of steps to reach M from N | CPP program to find minimum number of steps to reach M from N ; Function to find a minimum number of steps to reach M from N ; Continue till m is greater than n ; If m is odd ; add one ; divide m by 2 ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Minsteps ( int n , int m ) { int ans = 0 ; while ( m > n ) { if ( m & 1 ) { m ++ ; ans ++ ; } m /= 2 ; ans ++ ; } return ans + n - m ; } int main ( ) { int n = 4 , m = 6 ; cout << Minsteps ( n , m ) ; return 0 ; }
Find the number of jumps to reach X in the number line from zero | C ++ program to find the number of jumps to reach X in the number line from zero ; Utility function to calculate sum of numbers from 1 to x ; Function to find the number of jumps to reach X in the number line from zero ; First make number positive Answer will be same either it is Positive or negative ; To store required answer ; Continue till number is lesser or not in same parity ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getsum ( int x ) { return ( x * ( x + 1 ) ) / 2 ; } int countJumps ( int n ) { n = abs ( n ) ; int ans = 0 ; while ( getsum ( ans ) < n or ( getsum ( ans ) - n ) & 1 ) ans ++ ; return ans ; } int main ( ) { int n = 9 ; cout << countJumps ( n ) ; return 0 ; }
Maximum number of candies that can be bought | C ++ implementation of the approach ; Function to return the maximum candies that can be bought ; Buy all the candies of the last type ; Starting from second last ; Amount of candies of the current type that can be bought ; Add candies of current type that can be bought ; Update the previous bought amount ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxCandies ( int arr [ ] , int n ) { int prevBought = arr [ n - 1 ] ; int candies = prevBought ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int x = min ( prevBought - 1 , arr [ i ] ) ; if ( x >= 0 ) { candies += x ; prevBought = x ; } } return candies ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 3 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxCandies ( arr , n ) ; return 0 ; }
Replace all elements by difference of sums of positive and negative numbers after that element | C ++ program to implement above approach ; Function to print the array elements ; Function to replace all elements with absolute difference of absolute sums of positive and negative elements ; Calculate absolute sums of positive and negative elements in range i + 1 to N ; calculate difference of both sums ; replace i - th elements with absolute difference ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printArray ( int N , int arr [ ] ) { for ( int i = 0 ; i < N ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } void replacedArray ( int N , int arr [ ] ) { int pos_sum , neg_sum , i , j , diff ; for ( i = 0 ; i < N ; i ++ ) { pos_sum = 0 ; neg_sum = 0 ; for ( j = i + 1 ; j < N ; j ++ ) { if ( arr [ j ] > 0 ) pos_sum += arr [ j ] ; else neg_sum += arr [ j ] ; } diff = abs ( pos_sum ) - abs ( neg_sum ) ; arr [ i ] = abs ( diff ) ; } } int main ( ) { int N = 5 ; int arr [ ] = { 1 , -1 , 2 , 3 , -2 } ; replacedArray ( N , arr ) ; printArray ( N , arr ) ; N = 6 ; int arr1 [ ] = { -3 , -4 , -2 , 5 , 1 , -2 } ; replacedArray ( N , arr1 ) ; printArray ( N , arr1 ) ; return 0 ; }
Minimum cuts required to convert a palindromic string to a different palindromic string | CPP program to solve the above problem ; Function to check if string is palindrome or not ; Function to check if it is possible to get result by making just one cut ; Appending last element in front ; Removing last element ; Checking whether string s2 is palindrome and different from s . ; If length is <= 3 then it is impossible ; Array to store frequency of characters ; Store count of characters in a array ; Condition for edge cases ; Return 1 if it is possible to get palindromic string in just one cut . Else we can always reached in two cuttings . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPalindrome ( string s ) { for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s [ i ] != s [ s . length ( ) - i - 1 ] ) { return false ; } } return true ; } bool ans ( string s ) { string s2 = s ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { s2 = s2 . back ( ) + s2 ; s2 . pop_back ( ) ; if ( s != s2 && isPalindrome ( s2 ) ) { return true ; } } return false ; } int solve ( string s ) { if ( s . length ( ) <= 3 ) { return -1 ; } int cnt [ 25 ] = { } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; } if ( * max_element ( cnt , cnt + 25 ) >= ( s . length ( ) - 1 ) ) { return -1 ; } else { return ( ans ( s ) ? 1 : 2 ) ; } } int main ( ) { string s = " nolon " ; cout << solve ( s ) ; return 0 ; }
Minimum cuts required to convert a palindromic string to a different palindromic string | CPP program to solve the above problem ; Recursive function to find minimum number of cuts if length of string is even ; If length is odd then return 2 ; To check if half of palindromic string is itself a palindrome ; If not then return 1 ; Else call function with half palindromic string ; Function to find minimum number of cuts If length of string is odd ; If length is <= 3 then it is impossible ; Array to store frequency of characters ; Store count of characters in a array ; Condition for edge cases ; If length is even ; If length is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solveEven ( string s ) { if ( s . length ( ) % 2 == 1 ) return 2 ; string ls = s . substr ( 0 , s . length ( ) / 2 ) ; string rs = s . substr ( s . length ( ) / 2 , s . length ( ) ) ; if ( ls != rs ) return 1 ; return solveEven ( ls ) ; } int solveOdd ( string s ) { return 2 ; } int solve ( string s ) { if ( s . length ( ) <= 3 ) { return -1 ; } int cnt [ 25 ] = { } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { cnt [ s [ i ] - ' a ' ] ++ ; } if ( * max_element ( cnt , cnt + 25 ) >= s . length ( ) - 1 ) { return -1 ; } if ( s . length ( ) % 2 == 0 ) return solveEven ( s ) ; if ( s . length ( ) % 2 == 1 ) return solveOdd ( s ) ; } int main ( ) { string s = " nolon " ; cout << solve ( s ) ; return 0 ; }
Minimum changes required such that the string satisfies the given condition | C ++ implementation of the approach ; Function to return the minimum changes required ; To store the count of minimum changes , number of ones and the number of zeroes ; First character has to be '1' ; If condition fails changes need to be made ; Return the required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minChanges ( string str , int n ) { int count = 0 , zeros = 0 , ones = 0 ; if ( str [ 0 ] != '1' ) { count ++ ; ones ++ ; } for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) zeros ++ ; else ones ++ ; if ( zeros > ones ) { zeros -- ; ones ++ ; count ++ ; } } return count ; } int main ( ) { string str = "0000" ; int n = str . length ( ) ; cout << minChanges ( str , n ) ; return 0 ; }
Count possible moves in the given direction in a grid | C ++ implementation of the above approach ; Function to return the count of possible steps in a single direction ; It can cover infinite steps ; We are approaching towards X = N ; We are approaching towards X = 1 ; Function to return the count of steps ; Take the minimum of both moves independently ; Update count and current positions ; Driver code
#include <bits/stdc++.h> NEW_LINE #define int  long long NEW_LINE using namespace std ; int steps ( int cur , int x , int n ) { if ( x == 0 ) return INT_MAX ; if ( x > 0 ) return abs ( ( n - cur ) / x ) ; else return abs ( ( cur - 1 ) / x ) ; } int countSteps ( int curx , int cury , int n , int m , vector < pair < int , int > > moves ) { int count = 0 ; int k = moves . size ( ) ; for ( int i = 0 ; i < k ; i ++ ) { int x = moves [ i ] . first ; int y = moves [ i ] . second ; int stepct = min ( steps ( curx , x , n ) , steps ( cury , y , m ) ) ; count += stepct ; curx += stepct * x ; cury += stepct * y ; } return count ; } main ( ) { int n = 4 , m = 5 , x = 1 , y = 1 ; vector < pair < int , int > > moves = { { 1 , 1 } , { 1 , 1 } , { 0 , -2 } } ; int k = moves . size ( ) ; cout << countSteps ( x , y , n , m , moves ) ; return 0 ; }
Minimum number of elements that should be removed to make the array good | C ++ implementation of the approach ; Function to return the minimum number of elements that must be removed to make the given array good ; Count frequency of each element ; For each element check if there exists another element that makes a valid pair ; If does not exist then increment answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumRemoval ( int n , int a [ ] ) { map < int , int > c ; for ( int i = 0 ; i < n ; i ++ ) c [ a [ i ] ] ++ ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool ok = false ; for ( int j = 0 ; j < 31 ; j ++ ) { int x = ( 1 << j ) - a [ i ] ; if ( c . count ( x ) && ( c [ x ] > 1 || ( c [ x ] == 1 && x != a [ i ] ) ) ) { ok = true ; break ; } } if ( ! ok ) ans ++ ; } return ans ; } int main ( ) { int a [ ] = { 4 , 7 , 1 , 5 , 4 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << minimumRemoval ( n , a ) ; return 0 ; }
Minimum elements to be removed such that sum of adjacent elements is always odd | C ++ implementation of the above approach ; Returns the minimum number of eliminations ; Stores the previous element ; Stores the new value ; Check if the previous and current values are of same parity ; Previous value is now the current value ; Return the counter variable ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_elimination ( int n , int arr [ ] ) { int count = 0 ; int prev_val = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int curr_val = arr [ i ] ; if ( curr_val % 2 == prev_val % 2 ) count ++ ; prev_val = curr_val ; } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 7 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << min_elimination ( n , arr ) ; return 0 ; }
Count all possible N digit numbers that satisfy the given condition | C ++ implementation of above approach ; Function to return the count of required numbers ; If N is odd then return 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getCount ( int N ) { if ( N % 2 == 1 ) return 0 ; string result = "9" ; for ( int i = 1 ; i <= N / 2 - 1 ; i ++ ) result += "0" ; return result ; } int main ( ) { int N = 4 ; cout << getCount ( N ) ; return 0 ; }
Maximum number of teams that can be formed with given persons | C ++ implementation of the approach ; Function that returns true if it possible to form a team with the given n and m ; 1 person of Type1 and 2 persons of Type2 can be chosen ; 1 person of Type2 and 2 persons of Type1 can be chosen ; Cannot from a team ; Function to return the maximum number of teams that can be formed ; To store the required count of teams formed ; Choose 2 persons of Type1 ; And 1 person of Type2 ; Choose 2 persons of Type2 ; And 1 person of Type1 ; Another team has been formed ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canFormTeam ( int n , int m ) { if ( n >= 1 && m >= 2 ) return true ; if ( m >= 1 && n >= 2 ) return true ; return false ; } int maxTeams ( int n , int m ) { int count = 0 ; while ( canFormTeam ( n , m ) ) { if ( n > m ) { n -= 2 ; m -= 1 ; } else { m -= 2 ; n -= 1 ; } count ++ ; } return count ; } int main ( ) { int n = 4 , m = 5 ; cout << maxTeams ( n , m ) ; return 0 ; }
Find the Side of the smallest Square that can contain given 4 Big Squares | C ++ program to Find the Side of the smallest Square that can contain given 4 Big Squares ; Function to find the maximum of two values ; Function to find the smallest side of the suitable suitcase ; sort array to find the smallest and largest side of suitcases ; side of the suitcase will be smallest if they arranged in 2 x 2 way so find all possible sides of that arrangement ; since suitcase should be square so find maximum of all four side ; now find greatest side and that will be the smallest square ; return the result ; Driver program ; Get the side of the 4 small squares ; Find the smallest side ; Get the side of the 4 small squares ; Find the smallest side
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int max ( long long a , long long b ) { if ( a > b ) return a ; else return b ; } long long int smallestSide ( long long int a [ ] ) { sort ( a , a + 4 ) ; long long side1 , side2 , side3 , side4 , side11 , side12 , sideOfSquare ; side1 = a [ 0 ] + a [ 3 ] ; side2 = a [ 1 ] + a [ 2 ] ; side3 = a [ 0 ] + a [ 1 ] ; side4 = a [ 2 ] + a [ 3 ] ; side11 = max ( side1 , side2 ) ; side12 = max ( side3 , side4 ) ; sideOfSquare = max ( side11 , side12 ) ; return sideOfSquare ; } int main ( ) { long long int side [ 4 ] ; cout << " Test ▁ Case ▁ 1 STRNEWLINE " ; side [ 0 ] = 2 ; side [ 1 ] = 2 ; side [ 2 ] = 2 ; side [ 3 ] = 2 ; cout << smallestSide ( side ) << endl ; cout << " Test Case 2 " side [ 0 ] = 100000000000000 ; side [ 1 ] = 123450000000000 ; side [ 2 ] = 987650000000000 ; side [ 3 ] = 987654321000000 ; cout << smallestSide ( side ) << endl ; return 0 ; }
Rectangle with minimum possible difference between the length and the width | C ++ implementation of the approach ; Function to print the length ( l ) and breadth ( b ) of the rectangle having area = N and | l - b | as minimum as possible ; i is a factor ; l >= sqrt ( area ) >= i ; so here l is + ve always ; Here l and b are length and breadth of the rectangle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_rectangle ( int area ) { int l , b ; int M = sqrt ( area ) , ans ; for ( int i = M ; i >= 1 ; i -- ) { if ( area % i == 0 ) { l = ( area / i ) ; b = i ; break ; } } cout << " l ▁ = ▁ " << l << " , ▁ b ▁ = ▁ " << b << endl ; } int main ( ) { int area = 99 ; find_rectangle ( area ) ; return 0 ; }
Rectangle with minimum possible difference between the length and the width | C ++ implementation of the approach ; Function to print the length ( l ) and breadth ( b ) of the rectangle having area = N and | l - b | as minimum as possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find_rectangle ( int area ) { for ( int i = ceil ( sqrt ( area ) ) ; i <= area ; i ++ ) { if ( area / i * i == area ) { printf ( " % d ▁ % d " , i , area / i ) ; return ; } } } int main ( ) { int area = 99 ; find_rectangle ( area ) ; return 0 ; }
Largest sub | C ++ implementation of the approach ; Function to return the size of the required sub - set ; Sort the array ; Set to store the contents of the required sub - set ; Insert the elements satisfying the conditions ; Return the size of the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sizeSubSet ( int a [ ] , int k , int n ) { sort ( a , a + n ) ; unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % k != 0 || s . count ( a [ i ] / k ) == 0 ) s . insert ( a [ i ] ) ; } return s . size ( ) ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int k = 2 ; cout << sizeSubSet ( a , k , n ) ; return 0 ; }
Minimum number of sets with numbers less than Y | C ++ program to find the minimum number sets with consecutive numbers and less than Y ; Function to find the minimum number of shets ; Variable to count the number of sets ; Iterate in the string ; Add the number to string ; Mark that we got a number ; else Every time it exceeds ; Check if previous was anytime less than Y ; Current number ; Check for current number ; Check for last added number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSets ( string s , int y ) { int cnt = 0 ; int num = 0 ; int l = s . length ( ) ; int f = 0 ; for ( int i = 0 ; i < l ; i ++ ) { num = num * 10 + ( s [ i ] - '0' ) ; if ( num <= y ) f = 1 ; { if ( f ) cnt += 1 ; num = s [ i ] - '0' ; f = 0 ; if ( num <= y ) f = 1 ; else num = 0 ; } } if ( f ) cnt += 1 ; return cnt ; } int main ( ) { string s = "1234" ; int y = 30 ; cout << minimumSets ( s , y ) ; return 0 ; }
Find the non decreasing order array from given array | C ++ implementation of the approach ; Utility function to print the contents of the array ; Function to build array B [ ] ; Lower and upper limits ; To store the required array ; Apply greedy approach ; Print the built array b [ ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int b [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << b [ i ] << " ▁ " ; } void ModifiedArray ( int a [ ] , int n ) { int l = 0 , r = INT_MAX ; int b [ n ] = { 0 } ; for ( int i = 0 ; i < n / 2 ; i ++ ) { b [ i ] = max ( l , a [ i ] - r ) ; b [ n - i - 1 ] = a [ i ] - b [ i ] ; l = b [ i ] ; r = b [ n - i - 1 ] ; } printArr ( b , n ) ; } int main ( ) { int a [ ] = { 5 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; ModifiedArray ( a , 2 * n ) ; return 0 ; }
Count of lines required to write the given String | CPP implementation of the approach ; Function to return the number of lines required ; If string is empty ; Initialize lines and width ; Iterate through S ; Return lines and width used ; Driver Code ; Function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > numberOfLines ( string S , int * widths ) { if ( S . empty ( ) ) return { 0 , 0 } ; int lines = 1 , width = 0 ; for ( auto character : S ) { int w = widths [ character - ' a ' ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return { lines , width } ; } int main ( ) { string S = " bbbcccdddaa " ; int widths [ ] = { 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; pair < int , int > ans = numberOfLines ( S , widths ) ; cout << ans . first << " ▁ " << ans . second << endl ; return 0 ; }
Check if elements of an array can be arranged satisfying the given condition | C ++ implementation of the approach ; Function to return true if the elements can be arranged in the desired order ; If 2 * x is not found to pair ; Remove an occurrence of x and an occurrence of 2 * x ; Driver Code ; Function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; string canReorder ( int A [ ] , int n ) { map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ A [ i ] ] ++ ; sort ( A , A + n ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m [ A [ i ] ] == 0 ) continue ; if ( m [ 2 * A [ i ] ] ) { count += 2 ; m [ A [ i ] ] -= 1 ; m [ 2 * A [ i ] ] -= 1 ; } } if ( count == n ) return " true " ; else return " false " ; } int main ( ) { int A [ ] = { 4 , -2 , 2 , -4 } ; int n = sizeof ( A ) / sizeof ( int ) ; cout << ( canReorder ( A , n ) ) ; return 0 ; }
Maximum sum of all elements of array after performing given operations | CPP program to find the maximum sum after given operations ; Function to calculate Maximum Subarray Sum or Kadane 's Algorithm ; Function to find the maximum sum after given operations ; To store sum of all elements ; Maximum sum of a subarray ; Calculate the sum of all elements ; Driver Code ; size of an array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } int maxSum ( int a [ ] , int n ) { int S = 0 ; int S1 = maxSubArraySum ( a , n ) ; for ( int i = 0 ; i < n ; i ++ ) S += a [ i ] ; return ( 2 * S1 - S ) ; } int main ( ) { int a [ ] = { -35 , 32 , -24 , 0 , 27 , -10 , 0 , -19 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maxSum ( a , n ) ; return 0 ; }
Minimize the difference between minimum and maximum elements | C ++ program to minimize the difference between minimum and maximum elements ; Function to minimize the difference between minimum and maximum elements ; Find max and min elements of the array ; Check whether the difference between the max and min element is less than or equal to k or not ; Calculate average of max and min ; If the array element is greater than the average then decrease it by k ; If the array element is smaller than the average then increase it by k ; Find max and min of the modified array ; return the new difference ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimizeDiff ( int * arr , int n , int k ) { int max = * ( max_element ( arr , arr + n ) ) ; int min = * ( min_element ( arr , arr + n ) ) ; if ( ( max - min ) <= k ) { return ( max - min ) ; } int avg = ( max + min ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > avg ) arr [ i ] -= k ; else arr [ i ] += k ; } max = * ( max_element ( arr , arr + n ) ) ; min = * ( min_element ( arr , arr + n ) ) ; return ( max - min ) ; } int main ( ) { int arr [ ] = { 3 , 16 , 12 , 9 , 20 } ; int n = 5 ; int k = 3 ; cout << " Max ▁ height ▁ difference ▁ = ▁ " << minimizeDiff ( arr , n , k ) << endl ; return 0 ; }
Maximum litres of water that can be bought with N Rupees | CPP implementation of the above approach ; if buying glass bottles is profitable ; Glass bottles that can be bought ; Change budget according the bought bottles ; Plastic bottles that can be bought ; if only plastic bottles need to be bought ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxLitres ( int budget , int plastic , int glass , int refund ) { if ( glass - refund < plastic ) { int ans = max ( ( budget - refund ) / ( glass - refund ) , 0 ) ; budget -= ans * ( glass - refund ) ; ans += budget / plastic ; cout << ans << endl ; } else cout << ( budget / plastic ) << endl ; } int main ( ) { int budget = 10 , plastic = 11 , glass = 9 , refund = 8 ; maxLitres ( budget , plastic , glass , refund ) ; }