codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of Apples to be collected from trees to guarantee M red apples | C ++ program for the above approach ; Function to minimum no . of apples ; If we get all required apple from South ; If we required trees at East and West ; If we doesn 't have enough  red apples ; Driver Code ; No . of red apple for gift ; No . of red apple in each tree ; No . of tree in North ; No . of tree in South ; No . of tree in West ; No . of tree in East ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minApples ( int M , int K , int N , int S , int W , int E ) { if ( M <= S * K ) return M ; else if ( M <= S * K + E + W ) return S * K + ( M - S * K ) * K ; else return -1 ; } int main ( ) { int M = 10 ; int K = 15 ; int N = 0 ; int S = 1 ; int W = 0 ; int E = 0 ; int ans = minApples ( M , K , N , S , W , E ) ; cout << ans << endl ; }
Minimum increments or decrements required to signs of prefix sum array elements alternating | C ++ program for the above approach ; Function to find the minimum number of increments / decrements of array elements by 1 to make signs of prefix sum array elements alternating ; Case 1. neg - pos - neg ; Stores the current sign of the prefix sum of array ; Stores minimum number of operations for Case 1 ; Traverse the array ; Checking both conditions ; Update the current prefix1 to currentPrefixSum ; Case 2. pos - neg - pos ; Stores the prefix sum of array ; Stores minimum number of operations for Case 1 ; Checking both conditions ; Update the current prefix2 to currentPrefixSum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumOperations ( int A [ ] , int N ) { int cur_prefix_1 = 0 ; int parity = -1 ; int minOperationsCase1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_prefix_1 += A [ i ] ; if ( cur_prefix_1 == 0 parity * cur_prefix_1 < 0 ) { minOperationsCase1 += abs ( parity - cur_prefix_1 ) ; cur_prefix_1 = parity ; } parity *= -1 ; } int cur_prefix_2 = 0 ; parity = 1 ; int minOperationsCase2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_prefix_2 += A [ i ] ; if ( cur_prefix_2 == 0 parity * cur_prefix_2 < 0 ) { minOperationsCase2 += abs ( parity - cur_prefix_2 ) ; cur_prefix_2 = parity ; } parity *= -1 ; } return min ( minOperationsCase1 , minOperationsCase2 ) ; } int main ( ) { int A [ ] = { 1 , -3 , 1 , 0 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minimumOperations ( A , N ) ; return 0 ; }
Generate an array of maximum sum such that each element exceeds all elements present either on its left or right | C ++ code for the above approach ; Function to construct the array having maximum sum satisfying the given criteria ; Declaration of the array arrA [ ] and ans [ ] ; Stores the maximum sum of the resultant array ; Initialize the array arrA [ ] ; Traversing the array arrA [ ] ; Initialize the array arrB [ ] ; Assign the maximum element to the current element ; Form the first increasing till every index i ; Make the current element as the maximum element ; Forming decreasing from the index i + 1 to the index N ; Initialize the sum ; Find the total sum ; Check if the total sum is at least the sum found then make ans as ansB ; Print the final array formed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSumArray ( int arr [ ] , int N ) { vector < int > arrA ( N ) , ans ( N ) ; int maxSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) arrA [ i ] = arr [ i ] ; for ( int i = 0 ; i < N ; i ++ ) { vector < int > arrB ( N ) ; int maximum = arrA [ i ] ; arrB [ i ] = maximum ; for ( int j = i - 1 ; j >= 0 ; j -- ) { arrB [ j ] = min ( maximum , arrA [ j ] ) ; maximum = arrB [ j ] ; } maximum = arrA [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { arrB [ j ] = min ( maximum , arrA [ j ] ) ; maximum = arrB [ j ] ; } int sum = 0 ; for ( int j = 0 ; j < N ; j ++ ) sum += arrB [ j ] ; if ( sum > maxSum ) { maxSum = sum ; ans = arrB ; } } for ( int val : ans ) { cout << val << " ▁ " ; } } int main ( ) { int A [ ] = { 10 , 6 , 8 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; maximumSumArray ( A , N ) ; return 0 ; }
Minimize product of two scores possible by at most M reductions | C ++ program for the above approach ; Utility function to find the minimum product of R1 and R2 possible ; Reaching to its limit ; If M is remaining ; Function to find the minimum product of R1 and R2 ; Case 1 - R1 reduces first ; Case 2 - R2 reduces first ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minProductUtil ( int R1 , int B1 , int R2 , int B2 , int M ) { int x = min ( R1 - B1 , M ) ; M -= x ; R1 -= x ; if ( M > 0 ) { int y = min ( R2 - B2 , M ) ; M -= y ; R2 -= y ; } return R1 * R2 ; } int minProduct ( int R1 , int B1 , int R2 , int B2 , int M ) { int res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) ; int res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) ; return min ( res1 , res2 ) ; } int main ( ) { int R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ; cout << ( minProduct ( R1 , B1 , R2 , B2 , M ) ) ; return 0 ; }
Maximize the profit after selling the tickets | Set 2 ( For elements in range [ 1 , 10 ^ 6 ] ) | C ++ program for the above approach ; Function to find maximum profit after selling K tickets ; Frequency array to store freq of every element of the array ; Modify the arr [ ] so that the array is sorted in O ( N ) ; Variable to store answer ; Traverse the array while K > 0 and j >= 0 ; If arr [ i ] > arr [ j ] then ticket can be brought from counter [ j + 1 , N ] ; If arr [ j ] = = arr [ i ] decrement j until arr [ j ] != arr [ i ] ; Sell tickets from counter [ j + 1 , N ] ; All elements of array are equal Send tickets from each counter 1 time until K > 0. ; Converting answer from long long to int ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxAmount ( int n , int k , int arr [ ] ) { int A [ 1000001 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { A [ arr [ i ] ] ++ ; } int j = 0 ; for ( int i = 0 ; i < 1000001 ; i ++ ) { while ( A [ i ] != 0 ) { arr [ j ++ ] = i ; A [ i ] -- ; } } long long int ans = 0 ; int mod = 1e9 + 7 ; int i = n - 1 ; j = n - 2 ; while ( k > 0 && j >= 0 ) { if ( arr [ i ] > arr [ j ] ) { ans = ans + min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } else { while ( j >= 0 && arr [ j ] == arr [ i ] ) j -- ; if ( j < 0 ) break ; ans = ans + min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } } while ( k > 0 && arr [ i ] != 0 ) { ans = ans + min ( n , k ) * arr [ i ] ; k -= n ; arr [ i ] -- ; } ans = ans % mod ; int x = ans ; return x ; } int main ( ) { int n = 5 ; int k = 3 ; int arr [ n ] = { 4 , 3 , 6 , 2 , 4 } ; int ans = maxAmount ( n , k , arr ) ; cout << ans ; return 0 ; }
Maximize sum of averages of subsequences of lengths lying in a given range | C ++ program for the above approach ; Function to find the maximum sum of average of groups ; Sort the given array ; Stores the sum of averages ; Stores count of array element ; Add the current value to the variable sum ; Increment the count by 1 ; If the current size is X ; If the remaining elements can 't become a group ; Iterate until i is less than N ; Update the value of X ; Update the average ; Find the average ; Reset the sum and count ; Print maximum sum of averages ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxAverage ( int A [ ] , int N , int X , int Y ) { sort ( A , A + N ) ; int sum = 0 ; double res = 0 ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; count ++ ; if ( count == X ) { if ( N - i - 1 < X ) { i ++ ; int cnt = 0 ; while ( i < N ) { cnt ++ ; sum += A [ i ] ; i ++ ; } X = X + cnt ; res += ( double ) sum / double ( X ) ; break ; } res += ( double ) sum / double ( X ) ; sum = 0 ; count = 0 ; } } cout << fixed << setprecision ( 2 ) << res << " STRNEWLINE " ; } int main ( ) { int A [ ] = { 4 , 10 , 6 , 5 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int X = 2 , Y = 3 ; maxAverage ( A , N , X , Y ) ; return 0 ; }
Rearrange characters in a sorted string such that no pair of adjacent characters are the same | C ++ program for the above approach ; Function to check if a string S contains pair of adjacent characters that are equal or not ; Traverse the string S ; If S [ i ] and S [ i + 1 ] are equal ; Otherwise , return false ; Function to rearrange characters of a string such that no pair of adjacent characters are the same ; Initialize 3 variables ; Iterate until k < N ; If S [ i ] is not equal to S [ j ] ; Increment i and j by 1 ; If j equals k and increment the value of K by 1 ; Else ; If S [ j ] equals S [ k ] ; Increment k by 1 ; Else ; Swap ; Increment i and j by 1 ; If j equals k ; Increment k by 1 ; Function to rearrange characters in a string so that no two adjacent characters are same ; If string is already valid ; If size of the string is 2 ; Function Call ; Reversing the string ; Function Call ; If the string is valid ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isAdjChar ( string & s ) { for ( int i = 0 ; i < s . size ( ) - 1 ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) return true ; } return false ; } void rearrangeStringUtil ( string & S , int N ) { int i = 0 , j = 1 , k = 2 ; while ( k < N ) { if ( S [ i ] != S [ j ] ) { i ++ ; j ++ ; if ( j == k ) { k ++ ; } } else { if ( S [ j ] == S [ k ] ) { k ++ ; } else { swap ( S [ k ] , S [ j ] ) ; i ++ ; j ++ ; if ( j == k ) { k ++ ; } } } } } string rearrangeString ( string & S , int N ) { if ( isAdjChar ( S ) == false ) { return S ; } if ( S . size ( ) == 2 ) return " - 1" ; rearrangeStringUtil ( S , N ) ; reverse ( S . begin ( ) , S . end ( ) ) ; rearrangeStringUtil ( S , N ) ; if ( isAdjChar ( S ) == false ) { return S ; } return " - 1" ; } int main ( ) { string S = " aaabc " ; int N = S . length ( ) ; cout << rearrangeString ( S , N ) ; return 0 ; }
Lexicographically largest string possible by repeatedly appending first character of two given strings | C ++ program for the above approach ; Function to make the lexicographically largest string by merging two strings ; Stores the resultant string ; If the string word1 is lexographically greater than or equal to word2 ; Update the string merge ; Erase the first index of the string word1 ; Otherwise ; Update the string merge ; Erase the first index of the string word2 ; Return the final string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestMerge ( string word1 , string word2 ) { string merge = " " ; while ( word1 . size ( ) != 0 || word2 . size ( ) != 0 ) { if ( word1 >= word2 ) { merge = merge + word1 [ 0 ] ; word1 . erase ( word1 . begin ( ) + 0 ) ; } else { merge = merge + word2 [ 0 ] ; word2 . erase ( word2 . begin ( ) + 0 ) ; } } return merge ; } int main ( ) { string S1 = " xyzxyz " ; string S2 = " xywzxyx " ; cout << largestMerge ( S1 , S2 ) ; return 0 ; }
Maximum rods to put horizontally such that no two rods overlap on X coordinate | C ++ program for the above approach ; Function to find the maximum number of rods that can be put horizontally ; Stores the result ; Stores the last occupied point ; Traverse the array arr [ ] ; If the current point can be put on the left side ; Increment the ans by 1 ; Update prev ; Else if the given point can be put on the right side ; Increment the ans by 1 ; Update prev ; Otherwise , ; Update prev ; Return the ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaximumPoints ( int N , int X [ ] , int H [ ] ) { int ans = 0 ; int prev = INT_MIN ; for ( int i = 0 ; i < N ; ++ i ) { if ( prev < ( X [ i ] - H [ i ] ) ) { ++ ans ; prev = X [ i ] ; } else if ( i == N - 1 || ( X [ i ] + H [ i ] ) < X [ i + 1 ] ) { ++ ans ; prev = X [ i ] + H [ i ] ; } else { prev = X [ i ] ; } } return ans ; } int main ( ) { int X [ ] = { 1 , 2 , 3 } ; int H [ ] = { 2 , 5 , 5 } ; int N = sizeof ( X ) / sizeof ( X [ 0 ] ) ; cout << findMaximumPoints ( N , X , H ) ; }
Maximize the missing values in given time in HH : MM format | C ++ program for the above approach ; Function to find the maximum time by replacing ' ? ' by any digits ; If the 0 th index is ' ? ' ; If the 1 st index is ' ? ' ; If the 3 rd index is ' ? ' ; If the 4 th index is ' ? ' ; Return new string ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void maxTime ( string s ) { if ( s [ 0 ] == ' ? ' ) { if ( s [ 1 ] <= '3' s [ 1 ] == ' ? ' ) s [ 0 ] = '2' ; else s [ 0 ] = '1' ; } if ( s [ 1 ] == ' ? ' ) { if ( s [ 0 ] != '2' ) { s [ 1 ] = 9 ; } else s [ 1 ] = 3 ; } if ( s [ 3 ] == ' ? ' ) s [ 3 ] = '5' ; if ( s [ 4 ] == ' ? ' ) s [ 4 ] = '9' ; cout << s << endl ; } int main ( ) { string S = " ? 4:5 ? " ; maxTime ( S ) ; return 0 ; }
Maximum GCD of two numbers possible by adding same value to them | C ++ implementation of above approach . ; Function to calculate maximum gcd of two numbers possible by adding same value to both a and b ; Driver Code ; Given Input
#include <iostream> NEW_LINE using namespace std ; void maxGcd ( int a , int b ) { cout << abs ( a - b ) ; } int main ( ) { int a = 2231 ; int b = 343 ; maxGcd ( a , b ) ; return 0 ; }
Count the combination of 4 s and / or 5 s required to make each Array element 0 | C ++ program for the above approach ; Function to print the count of the combination of 4 or 5 required to make the arr [ i ] for each 0 < i < N ; Vector to store the answer ; Iterate in the range [ 0 , N - 1 ] ; Initialize sum to store the count of numbers and cnt for the current factor of 4 ; Iterate in the range [ 0 , arr [ i ] ] with increment of 4 ; Check if arr [ i ] - j ( the current factor of 4 ) is divisible by 5 or not ; If sum is not maximum then answer is found ; Finally , print the required answer ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOfCombinationOf4OR5 ( vector < int > arr , int N ) { vector < int > ans ( N , -1 ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 4 ) { continue ; } int sum = INT_MAX , cnt = 0 ; for ( int j = 0 ; j <= arr [ i ] ; j += 4 ) { if ( ( arr [ i ] - j ) % 5 == 0 ) { sum = min ( sum , cnt + ( arr [ i ] - j ) / 5 ) ; } cnt ++ ; } if ( sum != INT_MAX ) ans [ i ] = sum ; } for ( auto num : ans ) cout << num << " ▁ " ; } int main ( ) { vector < int > arr = { 7 , 15 , 17 , 22 } ; int N = arr . size ( ) ; sumOfCombinationOf4OR5 ( arr , N ) ; return 0 ; }
Find an N | C ++ program for the above approach ; Function to find an N - length binary string having maximum sum of elements from all given ranges ; Iterate over the range [ 1 , N ] ; If i is odd , then print 0 ; Otherwise , print 1 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printBinaryString ( int arr [ ] [ 3 ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 ) { cout << 0 ; } else { cout << 1 ; } } } int main ( ) { int N = 5 , M = 3 ; int arr [ ] [ 3 ] = { { 1 , 3 } , { 2 , 4 } , { 2 , 5 } } ; printBinaryString ( arr , N ) ; return 0 ; }
Maximize 0 s in given Array after replacing each element A [ i ] with ( A [ i ] * D + B [ i ] ) | C ++ program for the above approach ; Function to find the maximum number of 0 s in the array A [ ] after changing the array element to A [ i ] * D + B [ i ] ; Stores the frequency of fractions needed to make each element 0 ; Stores the maximum number of 0 ; Traverse the array ; Find the numerator and the denominator ; Check if den is not equal to 0 ; Divide num and den by their gcd ; Check if num is not greater than 0 ; Check if both num and den are equal to 0 ; Increment the value of { num , den } in the map ; Update the value of ans ; Print the value of ans + cnt ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxZeroes ( vector < int > & A , vector < int > & B ) { map < pair < int , int > , int > mp ; int N = A . size ( ) ; int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int num = - B [ i ] ; int den = A [ i ] ; int gc = __gcd ( num , den ) ; if ( den != 0 ) { num /= gc ; den /= gc ; } if ( num <= 0 ) { num *= -1 ; den *= -1 ; } if ( den == 0 and num == 0 ) cnt ++ ; if ( den != 0 ) { mp [ { num , den } ] ++ ; ans = max ( mp [ { num , den } ] , ans ) ; } } cout << ans + cnt << endl ; } int main ( ) { vector < int > A = { 1 , 2 , -1 } ; vector < int > B = { -6 , -12 , 6 } ; maxZeroes ( A , B ) ; return 0 ; }
Minimum cost to complete given tasks if cost of 1 , 7 and 30 days are given | C ++ program for the above approach ; Function to find the minimum cost to hire the workers for the given days in the array days [ ] ; Initialize the array dp ; Minimum Cost for Nth day ; Pointer of the array arr [ ] ; Traverse from right to left ; If worker is hired for 1 day ; If worker is hired for 7 days ; If worker is hired for 30 days ; Update the value of dp [ i ] as minimum of 3 options ; If the day is not at the array arr [ ] ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinCost ( int days [ ] , int cost [ ] , int N ) { int size = days [ N - 1 ] + 1 ; int dp [ size ] ; dp [ size - 1 ] = min ( cost [ 0 ] , min ( cost [ 1 ] , cost [ 2 ] ) ) ; int ptr = N - 2 ; for ( int i = size - 2 ; i > 0 ; i -- ) { if ( ptr >= 0 && days [ ptr ] == i ) { int val1 = dp [ i + 1 ] + cost [ 0 ] ; int val2 = cost [ 1 ] + ( ( i + 7 >= size ) ? 0 : dp [ i + 7 ] ) ; int val3 = cost [ 2 ] + ( ( i + 30 >= size ) ? 0 : dp [ i + 30 ] ) ; dp [ i ] = min ( val1 , min ( val2 , val3 ) ) ; ptr -- ; } else { dp [ i ] = dp [ i + 1 ] ; } } return dp [ 1 ] ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 7 , 8 , 10 , 17 } ; int cost [ ] = { 3 , 8 , 20 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MinCost ( arr , cost , N ) ; return 0 ; }
Minimum count of elements to be inserted in Array to form all values in [ 1 , K ] using subset sum | C ++ program for the above approach ; Function to find the count of minimum elements to be inserted to form every number in a range ; Stores the count of numbers needed ; Stores the numbers upto which every numbers can be formed ; Stores the index of the array arr [ ] ; Iterate until requiredSum is less than or equal to K ; If i is less than N and requiredSum is greater than or equal to arr [ i ] ; Increment requiredSum by arr [ i ] ; Increment i by 1 ; Otherwise ; Increment count by 1 ; Increment requiredSum by requiredSum ; Return result ; Driver Code ; Input ; Function Call
#include <iostream> NEW_LINE using namespace std ; int minElements ( int arr [ ] , int N , int K ) { int count = 0 ; long long requiredNum = 1 ; int i = 0 ; while ( requiredNum <= K ) { if ( i < N && requiredNum >= arr [ i ] ) { requiredNum += arr [ i ] ; i ++ ; } else { count ++ ; requiredNum += requiredNum ; } } return count ; } int main ( ) { int arr [ ] = { 1 , 3 } ; int K = 6 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minElements ( arr , N , K ) << endl ; return 0 ; }
Theft at World Bank | C ++ program for the above approach ; Custom comparator ; Function to find the maximum profit ; Stores the pairs of elements of B and A at the same index ; Iterate over the range [ 0 , N ] ; If current integer is perfect square ; Push the pair of B [ i ] and A [ i ] in vector V ; Sorts the vector using the custom comparator ; Stores the maximum profit ; Traverse the vector V ; If V [ i ] . second is less than W ; Increment profit by V [ i ] . first ; Decrement V [ i ] . second from W ; Otherwise ; Update profit ; Return the value of profit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool comp ( pair < long long , long long > p1 , pair < long long , long long > p2 ) { long double a = p1 . first , b = p1 . second ; long double c = p2 . first , d = p2 . second ; long double val1 = 0 , val2 = 0 ; val1 = a / b ; val2 = c / d ; return val1 > val2 ; } long double maximumProfit ( int A [ ] , int B [ ] , int N , long long W ) { vector < pair < long long , long long > > V ; for ( int i = 0 ; i < N ; i ++ ) { long long temp = sqrt ( A [ i ] ) ; if ( temp * temp == A [ i ] ) continue ; V . push_back ( { B [ i ] , A [ i ] } ) ; } sort ( V . begin ( ) , V . end ( ) , comp ) ; long double profit = 0.00 ; for ( int i = 0 ; i < V . size ( ) ; i ++ ) { if ( V [ i ] . second <= W ) { profit += V [ i ] . first ; W -= V [ i ] . second ; } else { profit += V [ i ] . first * ( ( long double ) W / V [ i ] . second ) ; break ; } } return profit ; } int main ( ) { int N = 3 ; long long W = 10 ; int A [ ] = { 4 , 5 , 7 } ; int B [ ] = { 8 , 5 , 4 } ; cout << maximumProfit ( A , B , N , W ) << endl ; return 0 ; }
Maximize the count of adjacent element pairs with even sum by rearranging the Array | C ++ program for the above approach ; Function to find maximum count pair of adjacent elements with even sum ; Stores count of odd numbers ; Stores count of even numbers ; Traverse the array arr [ ] ; If arr [ i ] % 2 is 1 ; Else ; If odd and even both are greater than 0 ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumCount ( int arr [ ] , int N ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 ) odd ++ ; else even ++ ; } if ( odd and even ) return N - 2 ; else return N - 1 ; } int main ( ) { int arr [ ] = { 9 , 13 , 15 , 3 , 16 , 9 , 13 , 18 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumCount ( arr , N ) ; return 0 ; }
Smallest number possible by repeatedly multiplying with K or 2 exactly N times starting from 1 | C ++ program for the above approach ; Function to find the minimum value of X after increment X by K or twice value of X in each of N operations ; Iterate over the range [ 1 , N ] ; If the value of X is less than equal to K ; Otherwise ; Return the minimum value of X ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minPossibleValue ( int N , int K , int X ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( X <= K ) { X = X * 2 ; } else { X = X + K ; } } return X ; } int main ( ) { int N = 7 , K = 4 , X = 1 ; cout << minPossibleValue ( N , K , X ) ; return 0 ; }
Smallest number that can replace all | C ++ program for the above approach ; Function to find the value of K to minimize the value of maximum absolute difference between adjacent elements ; Stores the maximum and minimum among array elements that are adjacent to " - 1" ; Traverse the given array arr [ ] ; If arr [ i ] is - 1 & arr [ i + 1 ] is not - 1 ; If arr [ i + 1 ] is - 1 & arr [ i ] is not - 1 ; If all array element is - 1 ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMissingValue ( int arr [ ] , int N ) { int minE = INT_MAX , maxE = INT_MIN ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == -1 && arr [ i + 1 ] != -1 ) { minE = min ( minE , arr [ i + 1 ] ) ; maxE = max ( maxE , arr [ i + 1 ] ) ; } if ( arr [ i ] != -1 && arr [ i + 1 ] == -1 ) { minE = min ( minE , arr [ i ] ) ; maxE = max ( maxE , arr [ i ] ) ; } } if ( minE == INT_MAX and maxE == INT_MIN ) { cout << "0" ; } else { cout << ( minE + maxE ) / 2 ; } } int main ( ) { int arr [ ] = { 1 , -1 , -1 , -1 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMissingValue ( arr , N ) ; return 0 ; }
Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times | C ++ program for the above approach ; Function to find the last element after performing given operations ; Length of the array ; Increment j until condition is satisfied ; In each pair every value is repeating r number of times ; Print the result according to the value of k ; Driver Code ; Given K ; Given arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findLastElement ( int N , vector < int > A ) { int l = A . size ( ) ; int j = 0 ; while ( N > l * ( pow ( 2 , j ) ) ) { N = N - l * pow ( 2 , j ) ; j += 1 ; } int k = 1 ; int r = pow ( 2 , j ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( N > r * i ) k += 1 ; } for ( int i = 0 ; i < l ; i ++ ) { if ( i + 1 == k ) { cout << ( A [ i ] ) ; return ; } } } int main ( ) { int K = 7 ; vector < int > A = { 1 , 2 , 3 } ; findLastElement ( K , A ) ; return 0 ; }