codeparrot/xlcost-text-to-code huggingface dataset:
Minimum possible value of D which when added to or subtracted from K repeatedly obtains every array element | C ++ program for the above approach ; Recursive function tox previous gcd of a and b ; Function to find the maximum value of D such that every element in the array can be obtained by performing K + D or K - D ; Traverse the array arr [ ] ; Update arr [ i ] ; Stores GCD of the array ; Iterate over the range [ 1 , N ] ; Update the value of D ; Print the value of D ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int findMaxD ( int arr [ ] , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = abs ( arr [ i ] - K ) ; } int D = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { D = gcd ( D , arr [ i ] ) ; } return D ; } int main ( ) { int arr [ ] = { 1 , 7 , 11 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; cout << findMaxD ( arr , N , K ) ; return 0 ; }
Maximize the number of times a character can be removed from substring 01 from given Binary String | C ++ program for the above approach ; Function to find the maximum moves that can be performed on a string ; Stores 0 s in suffix ; Stores 1 s in prefix ; Iterate over the characters of the string ; Iterate until i is greater than or equal to 0 ; If N is equal to x + y ; Return answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOperations ( string S , int N ) { int X = 0 ; int Y = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == '0' ) break ; Y ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S [ i ] == '1' ) break ; X ++ ; } if ( N == X + Y ) return 0 ; return N - ( X + Y ) - 1 ; } int main ( ) { string S = "001111" ; int N = S . length ( ) ; cout << maxOperations ( S , N ) << endl ; return 0 ; }
Maximize frequency sum of K chosen characters from given string | C ++ program for the above approach ; Function to find the maximum sum of frequencies of the exactly K chosen characters from the string S ; Stores the resultant maximum sum ; Stores the frequency of array elements ; Find the frequency of character ; Sort the frequency array in the descending order ; Iterate to choose K elements greedily ; If the freq [ i ] cards are chosen ; K cards have been picked ; Return the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( string S , int N , int K ) { int sum = 0 ; int freq [ 256 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { freq [ int ( S [ i ] ) ] ++ ; } sort ( freq , freq + 256 , greater < int > ( ) ) ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( K > freq [ i ] ) { sum += freq [ i ] * freq [ i ] ; K -= freq [ i ] ; } else { sum += freq [ i ] * K ; break ; } } return sum ; } int main ( ) { string S = " GEEKSFORGEEKS " ; int K = 10 ; int N = S . length ( ) ; cout << maximumSum ( S , N , K ) ; return 0 ; }
Count of N | C ++ program for the above approach ; Function to count N - digit numbers having absolute difference between adjacent digits in non - increasing order ; If digit = n + 1 , a valid n - digit number has been formed ; If the state has already been computed ; If the current digit is 1 , then any digit from [ 1 - 9 ] can be placed ; If the current digit is 2 , any digit from [ 0 - 9 ] can be placed ; For other digits , any digit i can be placed which satisfies abs ( prev1 - i ) <= abs ( prev1 - prev2 ) ; If absolute difference is less than or equal to diff ; Function to count N - digit numbers with absolute difference between adjacent digits in non increasing order ; Initialize dp table with - 1 ; Function Call ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 10 ] [ 10 ] ; int countOfNumbers ( int digit , int prev1 , int prev2 , int n ) { if ( digit == n + 1 ) { return 1 ; } int & val = dp [ digit ] [ prev1 ] [ prev2 ] ; if ( val != -1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else if ( digit == 2 ) { for ( int i = 0 ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else { int diff = abs ( prev2 - prev1 ) ; for ( int i = 0 ; i <= 9 ; ++ i ) { if ( abs ( prev1 - i ) <= diff ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } } return val ; } int countNumbersUtil ( int N ) { memset ( dp , -1 , sizeof dp ) ; cout << countOfNumbers ( 1 , 0 , 0 , N ) ; } int main ( ) { int N = 3 ; countNumbersUtil ( N ) ; return 0 ; }
Print all numbers that can be obtained by adding A or B to N exactly M times | C ++ program for the above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly N times ; If number of steps is 0 and only possible number is N ; Add A to N and make a recursive call for M - 1 steps ; Add B to N and make a recursive call for M - 1 steps . ; Driver Code ; Given Inputs ; Stores all possible numbers ; Function call ; Print all possible numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; void possibleNumbers ( set < int > & numbers , int N , int M , int A , int B ) { if ( M == 0 ) { numbers . insert ( N ) ; return ; } possibleNumbers ( numbers , N + A , M - 1 , A , B ) ; possibleNumbers ( numbers , N + B , M - 1 , A , B ) ; } int main ( ) { int N = 5 , M = 3 , A = 4 , B = 6 ; set < int > numbers ; possibleNumbers ( numbers , N , M , A , B ) ; for ( int x : numbers ) { cout << x << " ▁ " ; } return 0 ; }
Maximum sum of array after removing a positive or negative subarray | C ++ program for the above approach ; Function to find the maximum sum of array after removing either the contiguous positive or negative elements ; Store the total sum of array ; Store the maximum contiguous negative sum ; Store the sum of current contiguous negative elements ; Store the minimum element of array ; Traverse the array , arr [ ] ; Update the overall sum ; Store minimum element of array ; If arr [ i ] is positive ; Update temp_sum to 0 ; Add arr [ i ] to temp_sum ; Update max_neg ; If no negative element in array then remove smallest positive element ; Print the required sum ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSum ( int arr [ ] , int n ) { int sum = 0 ; int max_neg = INT_MAX ; int tempsum = 0 ; int small = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; small = min ( small , arr [ i ] ) ; if ( arr [ i ] > 0 ) { tempsum = 0 ; } else { tempsum += arr [ i ] ; } max_neg = min ( max_neg , tempsum ) ; } if ( max_neg == 0 ) { max_neg = small ; } cout << sum - max_neg ; } int main ( ) { int arr [ ] = { -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxSum ( arr , n ) ; return 0 ; }
Lexicographically largest string possible by at most K replacements | C ++ implementation of the above approach ; Traverse each element of the string ; If the current character can be replaced with ' z ' ; Return the modified string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestString ( string s , int k ) { for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] != ' z ' && k > 0 ) { s [ i ] = ' z ' ; k -- ; } } return s ; } int main ( ) { string s = " dbza " ; int k = 1 ; cout << largestString ( s , k ) << endl ; return 0 ; }
Count characters of a string which when removed individually makes the string equal to another string | C ++ program for the above approach ; Function to count characters from string A whose removal makes the strings A and B equal ; Stores the index of the longest prefix ; Stores the index of the longest suffix ; Traverse the string B ; Traverse the string B ; If N - M is equal to 1 and Y is less than or equal to X ; Print the count of characters ; Print the positions of the characters ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void RemoveOneChar ( string A , string B , int N , int M ) { int X = 0 ; int Y = N - 1 ; for ( int i = 0 ; i < M ; i ++ ) { if ( A [ X ] != B [ i ] ) break ; X ++ ; } for ( int i = M - 1 ; i >= 0 ; i -- ) { if ( A [ Y ] != B [ i ] ) break ; Y -- ; } if ( N - M == 1 && Y < X ) { cout << X - Y + 1 << endl ; for ( int i = Y ; i <= X ; i ++ ) cout << i + 1 << " ▁ " ; cout << endl ; } else cout << -1 << endl ; } int main ( ) { string A = " abaac " ; string B = " abac " ; int N = A . length ( ) ; int M = B . length ( ) ; RemoveOneChar ( A , B , N , M ) ; }
Minimum flips or swapping of adjacent characters required to make a string equal to another | C ++ program for the above approach ; Function to find minimum operations required to convert string A to B ; Store the size of the string ; Store the required result ; Traverse the string , a ; If a [ i ] is equal to b [ i ] ; Check if swapping adjacent characters make the same - indexed characters equal or not ; Otherwise , flip the current bit ; Print the minimum number of operations ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumOperation ( string a , string b ) { int n = a . length ( ) ; int i = 0 ; int minoperation = 0 ; while ( i < n ) { if ( a [ i ] == b [ i ] ) { i = i + 1 ; continue ; } else if ( a [ i ] == b [ i + 1 ] && a [ i + 1 ] == b [ i ] && i < n - 1 ) { minoperation ++ ; i = i + 2 ; } else if ( a [ i ] != b [ i ] ) { minoperation ++ ; i = i + 1 ; } else { ++ i ; } } cout << minoperation ; } int main ( ) { string a = "10010010" , b = "00001000" ; minimumOperation ( a , b ) ; return 0 ; }
Minimum replacements required to make sum of all K | C ++ program for the above approach ; Function to find minimum number of operations required to make sum of all subarrays of size K equal ; Stores number of operations ; Iterate in the range [ 0 , K - 1 ] ; Stores frequency of elements separated by distance K ; Stores maximum frequency and corresponding element ; Find max frequency element and its frequency ; Update the number of operations ; Print the result ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinOperations ( int arr [ ] , int N , int K ) { int operations = 0 ; for ( int i = 0 ; i < K ; i ++ ) { unordered_map < int , int > freq ; for ( int j = i ; j < N ; j += K ) freq [ arr [ j ] ] ++ ; int max1 = 0 , num ; for ( auto x : freq ) { if ( x . second > max1 ) { max1 = x . second ; num = x . first ; } } for ( auto x : freq ) { if ( x . first != num ) operations += x . second ; } } cout << operations ; } int main ( ) { int arr [ ] = { 3 , 4 , 3 , 5 , 6 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMinOperations ( arr , N , K ) ; return 0 ; }
Count of pairs of integers up to X and Y that generates equal Quotient and Remainder | C ++ Program for the above approach ; Function to calculate the number of pairs satisfying ( m / n = m % n ) ; Iterate from 1 to sqrt ( x ) ; Combining the conditions - 1 ) n > k 2 ) n <= y 3 ) n <= ( x / k - 1 ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countOfPairs ( int x , int y ) { int count = 0 ; for ( int k = 1 ; k * k <= x ; ++ k ) { count += max ( 0 , min ( y , x / k - 1 ) - k ) ; } cout << count << " STRNEWLINE " ; } int main ( ) { int x = 4 ; int y = 5 ; countOfPairs ( x , y ) ; return 0 ; }
Count permutations of first N natural numbers having sum of adjacent elements equal to a perfect square | C ++ program for the above approach ; Function to count total number of permutation of the first N natural number having the sum of adjacent elements as perfect square ; Create an adjacency matrix ; Count elements whose indegree is 1 ; Generate adjacency matrix ; Find the sum of i and j ; If sum is perfect square . then move from i to j ; Add it in adjacency list of i ; If any list is of size 1 , then the indegree is 1 ; If there is no element whose indegree is 1 , then N such permutations are possible ; If there is 1 or 2 elements whose indegree is 1 , then 2 permutations are possible ; If there are more than 2 elements whose indegree is 1 , then return 0 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPermutations ( int N ) { vector < vector < int > > adj ( 105 ) ; int indeg = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( i == j ) continue ; int sum = i + j ; if ( ceil ( sqrt ( sum ) ) == floor ( sqrt ( sum ) ) ) { adj [ i ] . push_back ( j ) ; } } if ( adj [ i ] . size ( ) == 1 ) indeg ++ ; } if ( indeg == 0 ) return N ; else if ( indeg <= 2 ) return 2 ; else return 0 ; } int main ( ) { int N = 17 ; cout << countPermutations ( N ) ; return 0 ; }
Generate a permutation of first N natural numbers having count of unique adjacent differences equal to K | C ++ program for the above approach ; Function to construct the list with exactly K unique adjacent element differences ; Stores the resultant array ; Stores the left and the right most element of the range ; Traverse the array ; If k is even , the add left to array and increment the left ; If k is odd , the add right to array and decrement the right ; Repeat the steps for k - 1 times ; Print the resultant list ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void makeList ( int N , int K ) { int list [ N ] ; int left = 1 ; int right = N ; for ( int i = 0 ; i < N ; i ++ ) { if ( K % 2 == 0 ) { list [ i ] = left ; left = left + 1 ; } else { list [ i ] = right ; right = right - 1 ; } if ( K > 1 ) K -- ; } for ( int i = 0 ; i < N ; i ++ ) { cout << list [ i ] << " ▁ " ; } } int main ( ) { int N = 6 ; int K = 3 ; makeList ( N , K ) ; }
Find the date after next half year from a given date | C ++ program for the above approach ; Function to find the date after the next half - year ; Stores the number of days in the months of a leap year ; List of months ; Days in half of a year ; Index of current month ; Starting day ; Decrement the value of cnt by 1 ; Increment cur_date ; If cnt is equal to 0 , then break out of the loop ; Update cur_month ; Update cur_date ; Print the resultant date ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; void getDate ( int d , string m ) { int days [ ] = { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; string month [ ] = { " January " , " February " , " March " , " April " , " May " , " June " , " July " , " August " , " September " , " October " , " November " , " December " } ; int cnt = 183 ; int cur_month ; for ( int i = 0 ; i < 12 ; i ++ ) if ( m == month [ i ] ) cur_month = i ; int cur_date = d ; while ( 1 ) { while ( cnt > 0 && cur_date <= days [ cur_month ] ) { cnt -= 1 ; cur_date += 1 ; } if ( cnt == 0 ) break ; cur_month = ( cur_month + 1 ) % 12 ; cur_date = 1 ; } cout << cur_date << " ▁ " << month [ cur_month ] << endl ; } int main ( ) { int D = 15 ; string M = " January " ; getDate ( D , M ) ; return 0 ; }
Maximum number made up of distinct digits whose sum is equal to N | C ++ program for the above approach ; Function to find the largest positive number made up of distinct digits having the sum of its digits as N ; If given number is greater than 45 , print - 1 ; Store the required number and the digit to be considered ; Loop until N > 0 and digit > 0 ; If the current digit is at most N then , add it to number num ; Update the value of num ; Decrement N by digit ; Consider the next lower digit ; Add 0 at the end and return the number num ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long largestNumber ( int N ) { if ( N > 45 ) return -1 ; int num = 0 , digit = 9 ; while ( N > 0 && digit > 0 ) { if ( digit <= N ) { num *= 10 ; num += digit ; N -= digit ; } digit -= 1 ; } return num * 10 ; } int main ( ) { int N = 25 ; cout << largestNumber ( N ) ; return 0 ; }
Minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | C ++ program for the above approach ; Function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the string S ; Stores the count of multiplication operator in the string ; Store the required result ; Iterate in the range to create the mask ; Checking the number of bits that are set in the mask ; Check if the number of bits that are set in the mask is multiplication operation ; Storing the elements that is to be added ; Apply the multiplications operation first ; If sign is ' * ' , then multiply last element of deque with arr [ i ] ; Push last multiplied element in the deque ; If the element is to be added , then add it to the deque ; Add all the element of the deque ; Minimize the answer with the given sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSum ( int A [ ] , int N , string S ) { int mul = 0 ; for ( int i = 0 ; i < ( int ) S . size ( ) ; i ++ ) { if ( S [ i ] == ' * ' ) mul += 1 ; } int ans = 1000000 ; for ( int i = 0 ; i < ( 1 << ( N - 1 ) ) ; i ++ ) { int cnt = 0 ; vector < char > v ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( ( 1 << j ) & ( i ) ) { cnt += 1 ; v . push_back ( ' * ' ) ; } else { v . push_back ( ' + ' ) ; } } if ( cnt == mul ) { deque < int > d ; d . push_back ( A [ 0 ] ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( v [ j ] == ' * ' ) { int x = d . back ( ) ; d . pop_back ( ) ; x = x * A [ j + 1 ] ; d . push_back ( x ) ; } else { d . push_back ( A [ j + 1 ] ) ; } } int sum = 0 ; while ( d . size ( ) > 0 ) { int x = d . front ( ) ; sum += x ; d . pop_front ( ) ; } ans = min ( ans , sum ) ; } } return ans ; } int main ( ) { int A [ ] = { 2 , 2 , 2 , 2 } ; string S = " * * + " ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minimumSum ( A , N , S ) ; return 0 ; }
Minimum sum of medians of all possible K length subsequences of a sorted array | C ++ program for the above approach ; Function to find the minimum sum of all the medians of the K sized sorted arrays formed from the given array ; Stores the distance between the medians ; Stores the number of subsequences required ; Stores the resultant sum ; Iterate from start and add all the medians ; Add the value of arr [ i ] to the variable minsum ; Increment i by select the median to get the next median index ; Decrement the value of totalArrays by 1 ; Print the resultant minimum sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOfMedians ( int arr [ ] , int N , int K ) { int selectMedian = ( K + 1 ) / 2 ; int totalArrays = N / K ; int minSum = 0 ; int i = selectMedian - 1 ; while ( i < N and totalArrays != 0 ) { minSum = minSum + arr [ i ] ; i = i + selectMedian ; totalArrays -- ; } cout << minSum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = sizeof ( arr ) / sizeof ( int ) ; int K = 2 ; sumOfMedians ( arr , N , K ) ; return 0 ; }
Find K positive integers not exceeding N and having sum S | C ++ implementation for the above approach ; Function to represent S as the sum of K positive integers less than or equal to N ; If S can cannot be represented as sum of K integers ; If sum of first i natural numbers exceeds S ; Insert i into nums [ ] ; Insert first K - 1 positive numbers into answer [ ] ; Insert the K - th number ; Traverse the array answer [ ] ; If current element exceeds N ; Add the extra value to the previous element ; Reduce current element to N ; Printing the K numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void solve ( int S , int K , int N ) { if ( K > N ) { cout << " - 1" << endl ; return ; } int max_sum = 0 , min_sum = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { min_sum += i ; max_sum += N - i + 1 ; } if ( S < min_sum S > max_sum ) { cout << " - 1" << endl ; return ; } int s1 = 0 ; vector < int > nums ; for ( int i = 1 ; i <= N ; i ++ ) { if ( s1 > S ) break ; s1 += i ; nums . push_back ( i ) ; } vector < int > answer ; int s2 = 0 ; for ( int i = 0 ; i < K - 1 ; i ++ ) { answer . push_back ( nums [ i ] ) ; s2 += nums [ i ] ; } answer . push_back ( S - s2 ) ; int Max = N ; for ( int i = answer . size ( ) - 1 ; i >= 0 ; i -- ) { if ( answer [ i ] > Max ) { int extra = answer [ i ] - Max ; if ( i - 1 >= 0 ) answer [ i - 1 ] += extra ; answer [ i ] = Max ; Max -- ; } else break ; } for ( auto x : answer ) cout << x << " ▁ " ; cout << endl ; } int main ( ) { int S = 15 , K = 4 , N = 8 ; solve ( S , K , N ) ; return 0 ; }
Generate an N | C ++ program for the above approach ; Function to minimize the maximum element present in an N - length array having sum of elements divisible by K ; Return the ceil value of ( K / N ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumValue ( int N , int K ) { return ceil ( ( double ) K / ( double ) N ) ; } int main ( ) { int N = 4 , K = 50 ; cout << minimumValue ( N , K ) ; return 0 ; }
Minimum removal of elements from end of an array required to obtain sum K | C ++ program for the above approach ; Function to find the minimum number of elements required to be removed from the ends of an array to obtain a sum K ; Number of elements removed from the left and right ends of the array ; Sum of left and right subarrays ; No element is taken from left initially ; Start taking elements from right side ; ( left + 1 ) : Count of elements removed from the left ( N - right ) : Count of elements removed from the right ; If sum is greater than K ; If it is not possible to obtain sum K ; Driver Code ; Given Array ; Given target sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSizeArr ( int A [ ] , int N , int K ) { int leftTaken = N , rightTaken = N ; int leftSum = 0 , rightSum = 0 ; for ( int left = -1 ; left < N ; left ++ ) { if ( left != -1 ) leftSum += A [ left ] ; rightSum = 0 ; for ( int right = N - 1 ; right > left ; right -- ) { rightSum += A [ right ] ; if ( leftSum + rightSum == K ) { if ( leftTaken + rightTaken > ( left + 1 ) + ( N - right ) ) { leftTaken = left + 1 ; rightTaken = N - right ; } break ; } if ( leftSum + rightSum > K ) break ; } } if ( leftTaken + rightTaken <= N ) { for ( int i = 0 ; i < leftTaken ; i ++ ) cout << A [ i ] << " ▁ " ; for ( int i = 0 ; i < rightTaken ; i ++ ) cout << A [ N - i - 1 ] << " ▁ " ; } else cout << -1 ; } int main ( ) { int N = 7 ; int A [ ] = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; return 0 ; }
Minimum removal of elements from end of an array required to obtain sum K | C ++ program for the above approach ; Function to find the smallest array that can be removed from the ends of an array to obtain sum K ; Sum of complete array ; If given number is greater than sum of the array ; If number is equal to the sum of array ; tar is sum of middle subarray ; Find the longest subarray with sum equal to tar ; If there is no subarray with sum equal to tar ; Driver Code ; Given Array ; Given target sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minSizeArr ( int A [ ] , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += A [ i ] ; if ( K > sum ) { cout << -1 ; return ; } if ( K == sum ) { for ( int i = 0 ; i < N ; i ++ ) { cout << A [ i ] << " ▁ " ; } return ; } int tar = sum - K ; unordered_map < int , int > um ; um [ 0 ] = -1 ; int left , right ; int cur = 0 , maxi = -1 ; for ( int i = 0 ; i < N ; i ++ ) { cur += A [ i ] ; if ( um . find ( cur - tar ) != um . end ( ) && i - um [ cur - tar ] > maxi ) { maxi = i - um [ cur - tar ] ; right = i ; left = um [ cur - tar ] ; } if ( um . find ( cur ) == um . end ( ) ) um [ cur ] = i ; } if ( maxi == -1 ) cout << -1 ; else { for ( int i = 0 ; i <= left ; i ++ ) cout << A [ i ] << " ▁ " ; for ( int i = 0 ; i < right ; i ++ ) cout << A [ N - i - 1 ] << " ▁ " ; } } int main ( ) { int N = 7 ; int A [ ] = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; return 0 ; }