codeparrot/xlcost-text-to-code huggingface dataset:
Count of primes in a given range that can be expressed as sum of perfect squares | C ++ Program to implement the above approach ; Function to check if a prime number satisfies the condition to be expressed as sum of two perfect squares ; Function to check if a number is prime or not ; Corner cases ; Function to return the count of primes in the range which can be expressed as the sum of two squares ; If i is a prime ; If i can be expressed as the sum of two squares ; Return the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sumSquare ( int p ) { return ( p - 1 ) % 4 == 0 ; } bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } int countOfPrimes ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime ( i ) ) { if ( sumSquare ( i ) ) count ++ ; } } return count ; } int main ( ) { int L = 5 , R = 41 ; cout << countOfPrimes ( L , R ) ; }
Count of array elements which are greater than all elements on its left | C ++ program to implement the above approach ; Function to return the count of array elements with all elements to its left smaller than it ; Stores the count ; Stores the maximum ; Iterate over the array ; If an element greater than maximum is obtained ; Increase count ; Update maximum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_elements ( int arr [ ] , int n ) { int count = 1 ; int max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { count += 1 ; max = arr [ i ] ; } } return count ; } int main ( ) { int arr [ ] = { 2 , 1 , 4 , 6 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( count_elements ( arr , n ) ) ; }
Count of bitonic substrings from the given string | C ++ + program for the above approach ; Function to find all the bitonic sub strings ; Pick starting point ; Iterate till length of the string ; Pick ending point for string ; Substring from i to j is obtained ; Substrings of length 1 ; Increase count ; For increasing sequence ; Check for strictly increasing ; Increase count ; Check for decreasing sequence ; Increase count ; Print the result ; Driver Code ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void subString ( char str [ ] , int n ) { int c = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; char temp = str [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp < str [ k ] && k <= j ) { temp = str [ k ] ; k ++ ; f = 2 ; } if ( k > j ) { c ++ ; f = 2 ; } while ( temp > str [ k ] && k <= j && f != 2 ) { k ++ ; f = 0 ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } cout << c << endl ; } int main ( ) { char str [ ] = " bade " ; subString ( str , strlen ( str ) ) ; return 0 ; }
Check if ceil of number divided by power of two exist in sorted array | C ++ 14 implementation to check if a number divided by power of two exist in the sorted array ; Function to find there exist a number or not in the array ; Loop to check if there exist a number by divided by power of 2 ; Binary Search ; Condition to check the number is found in the array or not ; Otherwise divide the number by increasing the one more power of 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumberDivByPowerofTwo ( int ar [ ] , int k , int n ) { int found = -1 , m = k ; while ( m > 0 ) { int l = 0 ; int r = n - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( ar [ mid ] == m ) { found = m ; break ; } else if ( ar [ mid ] > m ) { r = mid - 1 ; } else if ( ar [ mid ] < m ) { l = mid + 1 ; } } if ( found != -1 ) { break ; } m = m / 2 ; } return found ; } int main ( ) { int arr [ ] = { 3 , 5 , 7 , 8 , 10 } ; int k = 4 , n = 5 ; cout << findNumberDivByPowerofTwo ( arr , k , n ) ; }
Largest subset having with sum less than equal to sum of respective indices | C ++ Program to implement the above approach ; Function to find the length of the longest subset ; Stores the sum of differences between elements and their respective index ; Stores the size of the subset ; Iterate over the array ; If an element which is smaller than or equal to its index is encountered ; Increase count and sum ; Store the difference with index of the remaining elements ; Sort the differences in increasing order ; Include the differences while sum remains positive or ; Return the size ; Driver Code ; Function Calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSubset ( int * a , int n ) { int sum = 0 ; int cnt = 0 ; vector < int > v ; for ( int i = 1 ; i <= n ; i ++ ) { if ( a [ i - 1 ] - i <= 0 ) { sum += a [ i - 1 ] - i ; cnt += 1 ; } else { v . push_back ( a [ i - 1 ] - i ) ; } } sort ( v . begin ( ) , v . end ( ) ) ; int ptr = 0 ; while ( ptr < v . size ( ) && sum + v [ ptr ] <= 0 ) { cnt += 1 ; ptr += 1 ; sum += v [ ptr ] ; } return cnt ; } int main ( ) { int arr [ ] = { 4 , 1 , 6 , 7 , 8 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findSubset ( arr , n ) << endl ; }
Queries for count of array elements with values in given range with updates | C ++ code for queries for number of elements that lie in range [ l , r ] ( with updates ) ; Function to set arr [ index ] = x ; Function to get count of elements that lie in range [ l , r ] ; Traverse array ; If element lies in the range [ L , R ] ; Increase count ; Function to solve each query ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void setElement ( int * arr , int n , int index , int x ) { arr [ index ] = x ; } int getCount ( int * arr , int n , int l , int r ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l && arr [ i ] <= r ) { count ++ ; } } return count ; } void SolveQuery ( int arr [ ] , int n , vector < pair < int , pair < int , int > > > Q ) { int x ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { if ( Q [ i ] . first == 1 ) { x = getCount ( arr , n , Q [ i ] . second . first , Q [ i ] . second . second ) ; cout << x << " ▁ " ; } else { setElement ( arr , n , Q [ i ] . second . first , Q [ i ] . second . second ) ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < pair < int , pair < int , int > > > Q = { { 1 , { 3 , 5 } } , { 1 , { 2 , 4 } } , { 1 , { 1 , 2 } } , { 2 , { 1 , 7 } } , { 1 , { 1 , 2 } } } ; SolveQuery ( arr , n , Q ) ; return 0 ; }
Construct a sequence from given frequencies of N consecutive integers with unit adjacent difference | C ++ program for the above approach ; Function generates the sequence ; Map to store the frequency of numbers ; Sum of all frequencies ; Try all possibilities for the starting element ; If the frequency of current element is non - zero ; vector to store the answer ; Copy of the map for every possible starting element ; Decrement the frequency ; Push the starting element to the vector ; The last element inserted is i ; Try to fill the rest of the positions if possible ; If the frequency of last - 1 is non - zero ; Decrement the frequency of last - 1 ; Insert it into the sequence ; Update last number added to sequence ; Break from the inner loop ; If the size of the sequence vector is equal to sum of total frequqncies ; Return sequence ; If no such sequence if found return empty sequence ; Function Call to print the sequence ; The required sequence ; If the size of sequence if zero it means no such sequence was found ; Otherwise print the sequence ; Driver Code ; Frequency of all elements from 0 to n - 1 ; Number of elements whose frequencies are given ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > generateSequence ( int * freq , int n ) { map < int , int > m ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { m [ i ] = freq [ i ] ; total += freq [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( m [ i ] ) { vector < int > sequence ; auto mcopy = m ; mcopy [ i ] -- ; sequence . push_back ( i ) ; int last = i ; for ( int i = 0 ; i < total - 1 ; i ++ ) { if ( mcopy [ last - 1 ] ) { mcopy [ last - 1 ] -- ; sequence . push_back ( last - 1 ) ; last -- ; } else if ( mcopy [ last + 1 ] ) { mcopy [ last + 1 ] -- ; sequence . push_back ( last + 1 ) ; last ++ ; } else break ; } if ( sequence . size ( ) == total ) { return sequence ; } } } vector < int > empty ; return empty ; } void PrintSequence ( int freq [ ] , int n ) { vector < int > sequence = generateSequence ( freq , n ) ; if ( sequence . size ( ) == 0 ) { cout << " - 1" ; } else { for ( int i = 0 ; i < sequence . size ( ) ; i ++ ) { cout << sequence [ i ] << " ▁ " ; } } } int main ( ) { int freq [ ] = { 2 , 2 , 2 , 3 , 1 } ; int N = 5 ; PrintSequence ( freq , N ) ; return 0 ; }
Minimum distance between any most frequent and least frequent element of an array | C ++ implementation of the approach ; Function to find the minimum distance between any two most and least frequent element ; Initialize sets to store the least and the most frequent elements ; Initialize variables to store max and min frequency ; Initialize HashMap to store frequency of each element ; Loop through the array ; Store the count of each element ; Store the least and most frequent elements in the respective sets ; Store count of current element ; If count is equal to max count ; Store in max set ; If count is greater then max count ; Empty max set ; Update max count ; Store in max set ; If count is equal to min count ; Store in min set ; If count is less then max count ; Empty min set ; Update min count ; Store in min set ; Initialize a variable to store the minimum distance ; Initialize a variable to store the last index of least frequent element ; Traverse array ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Traverse array from the end ; If least frequent element ; Update last index of least frequent element ; If most frequent element ; Update minimum distance ; Print the minimum distance ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getMinimumDistance ( int a [ ] , int n ) { set < int > min_set ; set < int > max_set ; int max = 0 , min = INT_MAX ; map < int , int > frequency ; for ( int i = 0 ; i < n ; i ++ ) { frequency [ a [ i ] ] += 1 ; } for ( int i = 0 ; i < n ; i ++ ) { int count = frequency [ a [ i ] ] ; if ( count == max ) { max_set . insert ( a [ i ] ) ; } else if ( count > max ) { max_set . clear ( ) ; max = count ; max_set . insert ( a [ i ] ) ; } if ( count == min ) { min_set . insert ( a [ i ] ) ; } else if ( count < min ) { min_set . clear ( ) ; min = count ; min_set . insert ( a [ i ] ) ; } } int min_dist = INT_MAX ; int last_min_found = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min_set . find ( a [ i ] ) != min_set . end ( ) ) last_min_found = i ; if ( max_set . find ( a [ i ] ) != max_set . end ( ) && last_min_found != -1 ) { if ( ( i - last_min_found ) < min_dist ) min_dist = i - last_min_found ; } } last_min_found = -1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( min_set . find ( a [ i ] ) != min_set . end ( ) ) last_min_found = i ; if ( max_set . find ( a [ i ] ) != max_set . end ( ) && last_min_found != -1 ) { if ( ( last_min_found - i ) > min_dist ) min_dist = last_min_found - i ; } } cout << ( min_dist ) ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 3 , 2 , 3 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; getMinimumDistance ( arr , N ) ; }
Construct a string that has exactly K subsequences from given string | C ++ program for the above approach ; Function that computes the string s ; Length of the given string str ; List that stores all the prime factors of given k ; Find the prime factors ; Initialize the count of each character position as 1 ; Loop until the list becomes empty ; Increase the character count by multiplying it with the prime factor ; If we reach end then again start from beginning ; Store the output ; Print the string ; Driver code ; Given String ; Function Call
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void printSubsequenceString ( string str , long long k ) { int n = str . size ( ) ; int i ; vector < long long > factors ; for ( long long i = 2 ; i <= sqrt ( k ) ; i ++ ) { while ( k % i == 0 ) { factors . push_back ( i ) ; k /= i ; } } if ( k > 1 ) factors . push_back ( k ) ; vector < long long > count ( n , 1 ) ; int index = 0 ; while ( factors . size ( ) > 0 ) { count [ index ++ ] *= factors . back ( ) ; factors . pop_back ( ) ; if ( index == n ) index = 0 ; } string s ; for ( i = 0 ; i < n ; i ++ ) { while ( count [ i ] -- > 0 ) { s += str [ i ] ; } } cout << s ; } int main ( ) { string str = " code " ; long long k = 20 ; printSubsequenceString ( str , k ) ; return 0 ; }
Replace each element of Array with it 's corresponding rank | C ++ program for the above approach ; Function to assign rank to array elements ; Copy input array into newArray ; Sort newArray [ ] in ascending order ; Map to store the rank of the array element ; Update rank of element ; Assign ranks to elements ; Driver code ; Given array arr [ ] ; Function call ; Print the array elements
#include <bits/stdc++.h> NEW_LINE using namespace std ; void changeArr ( int input [ ] , int N ) { int newArray [ N ] ; copy ( input , input + N , newArray ) ; sort ( newArray , newArray + N ) ; int i ; map < int , int > ranks ; int rank = 1 ; for ( int index = 0 ; index < N ; index ++ ) { int element = newArray [ index ] ; if ( ranks [ element ] == 0 ) { ranks [ element ] = rank ; rank ++ ; } } for ( int index = 0 ; index < N ; index ++ ) { int element = input [ index ] ; input [ index ] = ranks [ input [ index ] ] ; } } int main ( ) { int arr [ ] = { 100 , 2 , 70 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; changeArr ( arr , N ) ; cout << " [ " ; for ( int i = 0 ; i < N - 1 ; i ++ ) { cout << arr [ i ] << " , ▁ " ; } cout << arr [ N - 1 ] << " ] " ; return 0 ; }
Find pairs in array whose sum does not exist in Array | C ++ program to implement the above approach ; Function to print all pairs with sum not present in the array ; Corner Case ; Stores the distinct array elements ; Generate all possible pairs ; Calculate sum of current pair ; Check if the sum exists in the HashSet or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPair ( int arr [ ] , int n ) { int i , j ; if ( n < 2 ) { cout << " - 1" << endl ; } set < int > hashMap ; for ( int k = 0 ; k < n ; k ++ ) { hashMap . insert ( arr [ k ] ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( hashMap . find ( sum ) == hashMap . end ( ) ) { cout << " ( " << arr [ i ] << " , ▁ " << arr [ j ] << " ) " << endl ; } } } } int main ( ) { int arr [ ] = { 2 , 4 , 2 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findPair ( arr , n ) ; return 0 ; }
Largest subset with M as smallest missing number | C ++ Program to implement the above approach ; Function to find and return the length of the longest subset whose smallest missing value is M ; Initialize a set ; If array element is not equal to M ; Insert into set ; Increment frequency ; Stores minimum missing number ; Iterate to find the minimum missing integer ; If minimum obtained is less than M ; Update answer ; Return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll findLengthOfMaxSubset ( int arr [ ] , int n , int m ) { set < int > s ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int tmp = arr [ i ] ; if ( tmp != m ) { s . insert ( tmp ) ; answer ++ ; } } int min = 1 ; while ( s . count ( min ) ) { min ++ ; } if ( min != m ) { answer = -1 ; } return answer ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = 3 ; cout << findLengthOfMaxSubset ( arr , N , M ) ; return 0 ; }
Count of submatrix with sum X in a given Matrix | C ++ program for the above approach ; Size of a column ; Function to find the count of submatrix whose sum is X ; Copying arr to dp and making it indexed 1 ; Precalculate and store the sum of all rectangles with upper left corner at ( 0 , 0 ) ; ; Calculating sum in a 2d grid ; Stores the answer ; Minimum length of square ; Maximum length of square ; Flag to set if sub - square with sum X is found ; Calculate lower right index if upper right corner is at { i , j } ; Calculate the sum of elements in the submatrix with upper left column { i , j } and lower right column at { ni , nj } ; ; If sum X is found ; If sum > X , then size of the square with sum X must be less than mid ; If sum < X , then size of the square with sum X must be greater than mid ; If found , increment count by 1 ; ; Driver Code ; Given Matrix arr [ ] [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define m  5 NEW_LINE int countSubsquare ( int arr [ ] [ m ] , int n , int X ) { int dp [ n + 1 ] [ m + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i + 1 ] [ j + 1 ] = arr [ i ] [ j ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { int lo = 1 ; int hi = min ( n - i , m - j ) + 1 ; bool found = false ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int ni = i + mid - 1 ; int nj = j + mid - 1 ; int sum = dp [ ni ] [ nj ] - dp [ ni ] [ j - 1 ] - dp [ i - 1 ] [ nj ] + dp [ i - 1 ] [ j - 1 ] ; if ( sum >= X ) { if ( sum == X ) { found = true ; } hi = mid - 1 ; } else { lo = mid + 1 ; } } if ( found == true ) { cnt ++ ; } } } return cnt ; } int main ( ) { int N = 4 , X = 10 ; int arr [ N ] [ m ] = { { 2 , 4 , 3 , 2 , 10 } , { 3 , 1 , 1 , 1 , 5 } , { 1 , 1 , 2 , 1 , 4 } , { 2 , 1 , 1 , 1 , 3 } } ; cout << countSubsquare ( arr , N , X ) << endl ; return 0 ; }
Check if two arrays can be made equal by reversing any subarray once | C ++ implementation to check whether two arrays can be made equal by reversing a sub - array only once ; Function to check whether two arrays can be made equal by reversing a sub - array only once ; Integer variable for storing the required starting and ending indices in the array ; Finding the smallest index for which A [ i ] != B [ i ] i . e the starting index of the unequal sub - array ; Finding the largest index for which A [ i ] != B [ i ] i . e the ending index of the unequal sub - array ; Reversing the sub - array A [ start ] , A [ start + 1 ] . . A [ end ] ; Checking whether on reversing the sub - array A [ start ] ... A [ end ] makes the arrays equal ; If any element of the two arrays is unequal print No and return ; Print Yes if arrays are equal after reversing the sub - array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkArray ( int A [ ] , int B [ ] , int N ) { int start = 0 ; int end = N - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { start = i ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { end = i ; break ; } } reverse ( A + start , A + end + 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { cout << " No " << endl ; return ; } } cout << " Yes " << endl ; } int main ( ) { int A [ ] = { 1 , 3 , 2 , 4 } ; int B [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; checkArray ( A , B , N ) ; return 0 ; }
Count of substrings having all distinct characters | C ++ Program to implement the above appraoach ; Function to count total number of valid substrings ; Stores the count of substrings ; Stores the frequency of characters ; Initialised both pointers to beginning of the string ; If all characters in substring from index i to j are distinct ; Increment count of j - th character ; Add all substring ending at j and starting at any index between i and j to the answer ; Increment 2 nd pointer ; If some characters are repeated or j pointer has reached to end ; Decrement count of j - th character ; Increment first pointer ; Return the final count of substrings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int countSub ( string str ) { int n = ( int ) str . size ( ) ; long long int ans = 0 ; int cnt [ 26 ] ; memset ( cnt , 0 , sizeof ( cnt ) ) ; int i = 0 , j = 0 ; while ( i < n ) { if ( j < n && ( cnt [ str [ j ] - ' a ' ] == 0 ) ) { cnt [ str [ j ] - ' a ' ] ++ ; ans += ( j - i + 1 ) ; j ++ ; } else { cnt [ str [ i ] - ' a ' ] -- ; i ++ ; } } return ans ; } int main ( ) { string str = " gffg " ; cout << countSub ( str ) ; return 0 ; }
Find largest factor of N such that N / F is less than K | C ++ program for the above approach ; Function to find the largest factor of N which is less than or equal to K ; Initialise the variable to store the largest factor of N <= K ; Loop to find all factors of N ; Check if j is a factor of N or not ; Check if j <= K If yes , then store the larger value between ans and j in ans ; Check if N / j <= K If yes , then store the larger value between ans and j in ans ; Since max value is always stored in ans , the maximum value divisible by N less than or equal to K will be returned . ; Driver Code ; Given N and K ; Function Call
#include <iostream> NEW_LINE using namespace std ; int solve ( int n , int k ) { int ans = 0 ; for ( int j = 1 ; j * j <= n ; j ++ ) { if ( n % j == 0 ) { if ( j <= k ) { ans = max ( ans , j ) ; } if ( n / j <= k ) { ans = max ( ans , n / j ) ; } } } return ans ; } int main ( ) { int N = 8 , K = 7 ; cout << ( N / solve ( N , K ) ) ; return 0 ; }
Maximum sum subarray of size range [ L , R ] | C ++ program to find Maximum sum subarray of size between L and R . ; function to find Maximum sum subarray of size between L and R ; calculating prefix sum ; maintain 0 for initial values of i upto R Once i = R , then we need to erase that 0 from our multiset as our first index of subarray cannot be 0 anymore . ; we maintain flag to counter if that initial 0 was erased from set or not . ; erase 0 from multiset once i = b ; insert pre [ i - L ] ; find minimum value in multiset . ; erase pre [ i - R ] ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void max_sum_subarray ( vector < int > arr , int L , int R ) { int n = arr . size ( ) ; int pre [ n ] = { 0 } ; pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + arr [ i ] ; } multiset < int > s1 ; s1 . insert ( 0 ) ; int ans = INT_MIN ; ans = max ( ans , pre [ L - 1 ] ) ; int flag = 0 ; for ( int i = L ; i < n ; i ++ ) { if ( i - R >= 0 ) { if ( flag == 0 ) { auto it = s1 . find ( 0 ) ; s1 . erase ( it ) ; flag = 1 ; } } if ( i - L >= 0 ) s1 . insert ( pre [ i - L ] ) ; ans = max ( ans , pre [ i ] - * s1 . begin ( ) ) ; if ( i - R >= 0 ) { auto it = s1 . find ( pre [ i - R ] ) ; s1 . erase ( it ) ; } } cout << ans << endl ; } int main ( ) { int L , R ; L = 1 ; R = 3 ; vector < int > arr = { 1 , 2 , 2 , 1 } ; max_sum_subarray ( arr , L , R ) ; return 0 ; }