codeparrot/xlcost-text-to-code huggingface dataset:
Count numbers up to N having digit D in its octal representation | C ++ program to implement the above approach ; Function to count the numbers in given range whose octal representation contains atleast digit , d ; Store count of numbers up to n whose octal representation contains digit d ; Iterate over the range [ 1 , n ] ; Calculate digit of i in octal representation ; Check if octal representation of x contains digit d ; Update total ; Update x ; Print the answer ; Driver Code ; Given N and D ; Counts and prints numbers up to N having D as a digit in its octal representation
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countNumbers ( int n , int d ) { int total = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int x = i ; while ( x > 0 ) { if ( x % 8 == d ) { total ++ ; break ; } x = x / 8 ; } } cout << total ; } int main ( ) { int n = 20 , d = 7 ; countNumbers ( n , d ) ; return 0 ; }
Count number of triplets ( a , b , c ) from first N natural numbers such that a * b + c = N | C ++ program to implement the above approach ; Function to find the count of triplets ( a , b , c ) with a * b + c = N ; Stores count of triplets of 1 st N natural numbers which are of the form a * b + c = N ; Iterate over the range [ 1 , N ] ; If N is divisible by i ; Update cntTriplet ; Update cntTriplet ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCntTriplet ( int N ) { int cntTriplet = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( N % i != 0 ) { cntTriplet += N / i ; } else { cntTriplet += ( N / i ) - 1 ; } } return cntTriplet ; } int main ( ) { int N = 3 ; cout << findCntTriplet ( N ) ; return 0 ; }
Count divisors which generates same Quotient and Remainder | C ++ program of the above approach ; Function to calculate the count of numbers such that it gives same quotient and remainder ; Stores divisor of number N . ; Iterate through numbers from 2 to sqrt ( N ) and store divisors of N ; As N is also divisor of itself ; Iterate through divisors ; Checking whether x satisfies the required condition ; Print the count ; Driver Code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countDivisors ( long long int n ) { int count = 0 ; vector < long long int > divisor ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) divisor . push_back ( i ) ; else { divisor . push_back ( i ) ; divisor . push_back ( n / i ) ; } } } divisor . push_back ( n ) ; for ( auto x : divisor ) { x -= 1 ; if ( ( n / x ) == ( n % x ) ) count ++ ; } cout << count ; } int main ( ) { long long int N = 1000000000000 ; countDivisors ( N ) ; return 0 ; }
Smallest indexed array element required to be flipped to make sum of array equal to 0 | C ++ program to implement the above approach ; Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Traverse the given array ; Flip the sign of current element ; Stores the sum of array elements ; Find the sum of the array ; Update sum ; If sum is equal to 0 ; Update pos ; Reset the current element to its original value ; Reset the value of arr [ i ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestIndexArrayElementsFlip ( int arr [ ] , int N ) { int pos = -1 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] *= -1 ; int sum = 0 ; for ( int j = 0 ; j < N ; j ++ ) { sum += arr [ j ] ; } if ( sum == 0 ) { pos = i ; break ; } else { arr [ i ] *= -1 ; } } return pos ; } int main ( ) { int arr [ ] = { 1 , 3 , -5 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << smallestIndexArrayElementsFlip ( arr , N ) ; return 0 ; }
Smallest indexed array element required to be flipped to make sum of array equal to 0 | C ++ program to implement the above approach ; Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Stores the sum of the array ; Traverse the given array ; Update ArrSum ; Traverse the given array ; If sum of array elements double the value of the current element ; Update pos ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestIndexArrayElementsFlip ( int arr [ ] , int N ) { int pos = -1 ; int ArrSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ArrSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( 2 * arr [ i ] == ArrSum ) { pos = i ; break ; } } return pos ; } int main ( ) { int arr [ ] = { 1 , 3 , -5 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << smallestIndexArrayElementsFlip ( arr , N ) ; return 0 ; }
Check if a string can be converted to another given string by removal of a substring | C ++ program to implement the above approach ; Function to check if S can be converted to T by removing at most one substring from S ; Check if S can be converted to T by removing at most one substring from S ; Stores length of string T ; Stores length of string S ; Iterate over the range [ 0 , M - 1 ] ; Stores Length of the substring { S [ 0 ] , ... , S [ i ] } ; Stores Length of the substring { S [ 0 ] , ... , S [ i ] } ; Stores prefix substring ; Stores suffix substring ; Checking if prefix + suffix == T ; Driver Code ; Given String S and T ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string make_string_S_to_T ( string S , string T ) { bool possible = false ; int M = T . length ( ) ; int N = S . length ( ) ; for ( int i = 0 ; i <= M ; i ++ ) { int prefix_length = i ; int suffix_length = M - i ; string prefix = S . substr ( 0 , prefix_length ) ; string suffix = S . substr ( N - suffix_length , suffix_length ) ; if ( prefix + suffix == T ) { possible = true ; break ; } } if ( possible ) return " YES " ; else return " NO " ; } int main ( ) { string S = " ababcdcd " ; string T = " abcd " ; cout << make_string_S_to_T ( S , T ) ; return 0 ; }
Sum of maximum of all subarrays by adding even frequent maximum twice | C ++ program for the above approach ; Function to calculate sum of maximum of all subarrays ; Storeas the sum of maximums ; Traverse the array ; Store the frequency of the maximum element in subarray ; Finding maximum ; Increment frequency by 1 ; If new maximum is obtained ; If frequency of maximum is even , then add 2 * maxNumber . Otherwise , add maxNumber ; Print the sum obtained ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSum ( vector < int > a ) { int ans = 0 ; for ( int low = 0 ; low < a . size ( ) ; low ++ ) { for ( int high = low ; high < a . size ( ) ; high ++ ) { int count = 0 ; int maxNumber = 0 ; for ( int i = low ; i <= high ; i ++ ) { if ( a [ i ] == maxNumber ) count ++ ; else if ( a [ i ] > maxNumber ) { maxNumber = a [ i ] ; count = 1 ; } } ans += maxNumber * ( ( count % 2 == 0 ) ? 2 : 1 ) ; } } cout << ( ans ) ; } int main ( ) { vector < int > arr = { 2 , 1 , 4 , 4 , 2 } ; findSum ( arr ) ; }
Count pairs of indices having equal prefix and suffix sums | C ++ program for the above approach ; Function to find the count of index pairs having equal prefix and suffix sums ; Maps indices with prefix sums ; Traverse the array ; Update prefix sum ; Update frequency in Map ; Traverse the array in reverse ; Update suffix sum ; Check if any prefix sum of equal value exists or not ; Print the obtained count ; Driver code ; Given array ; Given size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int * arr , int n ) { unordered_map < int , int > mp1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; mp1 [ sum ] += 1 ; } sum = 0 ; int ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; if ( mp1 . find ( sum ) != mp1 . end ( ) ) { ans += mp1 [ sum ] ; } } cout << ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , n ) ; return 0 ; }
Queries to count Composite Magic Numbers from a given range [ L , R ] | C ++ program to implement the above approach ; Check if a number is magic number or not ; Check number is composite number or not ; Corner cases ; Check if the number is a multiple of 2 or 3 ; Check for multiples of remaining primes ; Function to find Composite Magic Numbers in given ranges ; dp [ i ] : Stores the count of composite Magic numbers up to i ; Traverse in the range [ 1 , 1e5 ) ; Check if number is Composite number as well as a Magic number or not ; Print results for each query ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool isMagic ( int num ) { return ( num % 9 == 1 ) ; } bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } void find ( int L [ ] , int R [ ] , int q ) { int dp [ 1000005 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; for ( int i = 1 ; i < 1000005 ; i ++ ) { if ( isComposite ( i ) && isMagic ( i ) ) { dp [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = dp [ i - 1 ] ; } for ( int i = 0 ; i < q ; i ++ ) cout << dp [ R [ i ] ] - dp [ L [ i ] - 1 ] << endl ; } int main ( ) { int L [ ] = { 10 , 3 } ; int R [ ] = { 100 , 2279 } ; int Q = 2 ; find ( L , R , Q ) ; return 0 ; }
Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K | C ++ program to implement the above approach ; Function to find the smallest positive integer that divides array elements to get the sum <= K ; Stores minimum possible of the smallest positive integer satisfying the condition ; Stores maximum possible of the smallest positive integer satisfying the condition ; Apply binary search over the range [ left , right ] ; Stores middle element of left and right ; Stores sum of array elements ; Traverse the array ; Update sum ; If sum is greater than K ; Update left ; Update right ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSmallestInteger ( int arr [ ] , int N , int K ) { int left = 1 ; int right = * max_element ( arr , arr + N ) ; while ( left < right ) { int mid = ( left + right ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( arr [ i ] + mid - 1 ) / mid ; } if ( sum > K ) { left = mid + 1 ; } else { right = mid ; } } return left ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; ; int K = 6 ; cout << findSmallestInteger ( arr , N , K ) ; }
Minimum Deci | C ++ Program to implement the above approach ; Function to find the count of minimum Deci - Binary numbers required to obtain S ; Stores the minimum count ; Iterate over the string s ; Convert the char to its equivalent integer ; If current character is the maximum so far ; Update the maximum digit ; Print the required result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimum_deci_binary_number ( string s ) { int m = INT_MIN ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { int temp = s [ i ] - '0' ; if ( temp > m ) { m = temp ; } } return m ; } int main ( ) { string S = "31" ; cout << minimum_deci_binary_number ( S ) ; return 0 ; }
Make all the elements of array odd by incrementing odd | C ++ program for the above approach ; Function to count minimum subarrays whose odd - indexed elements need to be incremented to make array odd ; Stores the minimum number of operations required ; Iterate over even - indices ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment number of operations ; Iterate over odd indexed positions of arr [ ] ; Check if the current element is odd ; If true , continue ; Otherwise , mark the starting of the subarray and iterate until i < n and arr [ i ] is even ; Increment the number of operations ; Print the number of operations ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperations ( int arr [ ] , int n ) { int flips = 0 ; for ( int i = 0 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } for ( int i = 1 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } cout << flips ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 3 , 5 , 3 , 2 } ; int N = sizeof ( arr ) / sizeof ( int ) ; minOperations ( arr , N ) ; return 0 ; }
Minimum substring reversals required to make given Binary String alternating | C ++ program for the above approach ; Function to count the minimum number of substrings required to be reversed to make the string S alternating ; Store count of consecutive pairs ; Stores the count of 1 s and 0 s ; Traverse through the string ; Increment 1 s count ; Increment 0 s count ; Increment K if consecutive same elements are found ; Increment 1 s count ; else Increment 0 s count ; Check if it is possible or not ; Otherwise , print the number of required operations ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumReverse ( string s , int n ) { int k = 0 , l = 0 ; int sum1 = 0 , sum0 = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) sum1 ++ ; else sum0 ++ ; if ( s [ i ] == s [ i - 1 ] && s [ i ] == '0' ) k ++ ; else if ( s [ i ] == s [ i - 1 ] && s [ i ] == '1' ) l ++ ; } if ( s [ 0 ] == '1' ) sum1 ++ ; sum0 ++ ; if ( abs ( sum1 - sum0 ) > 1 ) return -1 ; return max ( k , l ) ; } int main ( ) { string S = "10001" ; int N = S . size ( ) ; cout << minimumReverse ( S , N ) ; return 0 ; }
Count pairs with Even Product from two given arrays | C ++ program to implement the above approach ; Function to count pairs ( arr [ i ] , brr [ j ] ) whose product is an even number ; Stores count of odd numbers in arr [ ] ; Stores count of odd numbers in brr [ ] ; Traverse the array , arr [ ] ; If arr [ i ] is an odd number ; Update cntOddArr ; Traverse the array , brr [ ] ; If brr [ i ] is an odd number ; Update cntOddArr ; Return pairs whose product is an even number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cntPairsInTwoArray ( int arr [ ] , int brr [ ] , int N , int M ) { int cntOddArr = 0 ; int cntOddBrr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] & 1 ) { cntOddArr += 1 ; } } for ( int i = 0 ; i < M ; i ++ ) { if ( brr [ i ] & 1 ) { cntOddBrr += 1 ; } } return ( N * M ) - ( cntOddArr * cntOddBrr ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int brr [ ] = { 1 , 2 } ; int M = sizeof ( brr ) / sizeof ( brr [ 0 ] ) ; cout << cntPairsInTwoArray ( arr , brr , N , M ) ; return 0 ; }
Maximize count of persons receiving a chocolate | C ++ program for the above approach ; Function to count the maximum number of persons receiving a chocolate ; Initialize count as 0 ; Sort the given arrays ; Initialize a multiset ; Insert B [ ] array values into the multiset ; Traverse elements in array A [ ] ; Search for the lowest value in B [ ] ; If found , increment count , and delete from set ; Return the number of people ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMaxPersons ( int * A , int n , int * B , int m , int x ) { int count = 0 ; sort ( A , A + n ) ; sort ( B , B + m ) ; multiset < int > s ; for ( int i = 0 ; i < m ; i ++ ) s . insert ( B [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { int val = A [ i ] - x ; auto it = s . lower_bound ( val ) ; if ( it != s . end ( ) && * it <= A [ i ] + x ) { count ++ ; s . erase ( it ) ; } } return count ; } int main ( ) { int A [ ] = { 90 , 49 , 20 , 39 , 49 } ; int B [ ] = { 14 , 24 , 82 } ; int X = 15 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( B ) / sizeof ( B [ 0 ] ) ; cout << countMaxPersons ( A , N , B , M , X ) ; }
Maximize frequency of an element by at most one increment or decrement of all array elements | C ++ program to implement the above approach ; Function to maximize the frequency of an array element by incrementing or decrementing array elements at most once ; Stores the largest array element ; Stores the smallest array element ; freq [ i ] : Stores frequency of ( i + Min ) in the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores maximum frequency of an array element by incrementing or decrementing array elements ; Iterate all the numbers over the range [ Min , Max ] ; Stores sum of three consecutive numbers ; Update maxSum ; Print maxSum ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void max_freq ( int arr [ ] , int N ) { int Max = * max_element ( arr , arr + N ) ; int Min = * min_element ( arr , arr + N ) ; int freq [ Max - Min + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] - Min ] ++ ; } int maxSum = 0 ; for ( int i = 0 ; i < ( Max - Min - 1 ) ; i ++ ) { int val = freq [ i ] + freq [ i + 1 ] + freq [ i + 2 ] ; maxSum = max ( maxSum , val ) ; } cout << maxSum << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 3 , 1 , 4 , 1 , 5 , 9 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; max_freq ( arr , N ) ; return 0 ; }
Longest increasing subsequence consisting of elements from indices divisible by previously selected indices | C ++ program for the above approach ; Function to find length of longest subsequence generated that satisfies the specified conditions ; Stores the length of longest subsequences of all lengths ; Iterate through the given array ; Iterate through the multiples i ; Update dp [ j ] as maximum of dp [ j ] and dp [ i ] + 1 ; Return the maximum element in dp [ ] as the length of longest subsequence ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxLength ( int N , vector < int > arr ) { vector < int > dp ( N + 1 , 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 2 * i ; j <= N ; j += i ) { if ( arr [ i - 1 ] < arr [ j - 1 ] ) { dp [ j ] = max ( dp [ j ] , dp [ i ] + 1 ) ; } } } return * max_element ( dp . begin ( ) , dp . end ( ) ) ; } int main ( ) { vector < int > arr { 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int N = arr . size ( ) ; cout << findMaxLength ( N , arr ) ; return 0 ; }
Queries to check if any pair exists in an array having values at most equal to the given pair | C ++ program for the above approach ; Function that performs binary search to find value less than or equal to first value of the pair ; Perform binary search ; Find the mid ; Update the high ; Else update low ; Return the low index ; Function to modify the second value of each pair ; start from index 1 ; Function to evaluate each query ; Find value less than or equal to the first value of pair ; check if we got the required pair or not ; Function to find a pair whose values is less than the given pairs in query ; Find the size of the vector ; sort the vector based on the first value ; Function Call to modify the second value of each pair ; Traverse each queries ; Evaluate each query ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binary_search ( vector < pair < int , int > > vec , int n , int a ) { int low , high , mid ; low = 0 ; high = n - 1 ; while ( low < high ) { mid = low + ( high - low + 1 ) / 2 ; if ( vec [ mid ] . first > a ) { high = mid - 1 ; } else if ( vec [ mid ] . first <= a ) { low = mid ; } } return low ; } void modify_vec ( vector < pair < int , int > > & v , int n ) { for ( int i = 1 ; i < n ; i ++ ) { v [ i ] . second = min ( v [ i ] . second , v [ i - 1 ] . second ) ; } } int evaluate_query ( vector < pair < int , int > > v , int n , int m1 , int m2 ) { int temp = binary_search ( v , n , m1 ) ; if ( ( v [ temp ] . first <= m1 ) && ( v [ temp ] . second <= m2 ) ) { return 1 ; } return 0 ; } void checkPairs ( vector < pair < int , int > > & v , vector < pair < int , int > > & queries ) { int n = v . size ( ) ; sort ( v . begin ( ) , v . end ( ) ) ; modify_vec ( v , n ) ; int k = queries . size ( ) ; for ( int i = 0 ; i < k ; i ++ ) { int m1 = queries [ i ] . first ; int m2 = queries [ i ] . second ; int result = evaluate_query ( v , n , m1 , m2 ) ; if ( result > 0 ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; } } int main ( ) { vector < pair < int , int > > arr = { { 3 , 5 } , { 2 , 7 } , { 2 , 3 } , { 4 , 9 } } ; vector < pair < int , int > > queries = { { 3 , 4 } , { 3 , 2 } , { 4 , 1 } , { 3 , 7 } } ; checkPairs ( arr , queries ) ; return 0 ; }
Sort a stream of integers | C ++ program for the above approach ; Function to sort a stream of integers ; Stores the position of array elements ; Traverse through the array ; If any element is found to be greater than the current element ; If an element > num is not present ; Otherwise , place the number at its right position ; Function to print the sorted stream of integers ; Stores the sorted stream of integers ; Traverse the array ; Function Call ; Print the array after every insertion ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Sort ( vector < int > & ans , int num ) { int pos = -1 ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { if ( ans [ i ] >= num ) { pos = i ; break ; } } if ( pos == -1 ) ans . push_back ( num ) ; else ans . insert ( ans . begin ( ) + pos , num ) ; } void sortStream ( int arr [ ] , int N ) { vector < int > ans ; for ( int i = 0 ; i < N ; i ++ ) { Sort ( ans , arr [ i ] ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << ans [ i ] << " ▁ " ; } cout << endl ; } } int main ( ) { int arr [ ] = { 4 , 1 , 7 , 6 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortStream ( arr , N ) ; return 0 ; }
Count ways to split array into two equal sum subarrays by changing sign of any one array element | C ++ program for the above approach ; Function to count ways of splitting the array in two subarrays of equal sum by changing sign of any 1 element ; Stores the count of elements in prefix and suffix of array ; Stores the total sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto an index ; Stores sum of suffix from an index ; Stores the count of ways to split the array in 2 subarrays having equal sum ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix Map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix Map ; Store the difference between the subarrays ; Check if diff is even or not ; Count number of ways to split array at index i such that subarray sums are same ; Update the count ; Return the count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubArraySignChange ( int arr [ ] , int N ) { unordered_map < int , int > prefixCount ; unordered_map < int , int > suffixCount ; int total = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { total += arr [ i ] ; suffixCount [ arr [ i ] ] ++ ; } int prefixSum = 0 ; int suffixSum = 0 ; int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { prefixSum += arr [ i ] ; prefixCount [ arr [ i ] ] ++ ; suffixSum = total - prefixSum ; suffixCount [ arr [ i ] ] -- ; int diff = prefixSum - suffixSum ; if ( diff % 2 == 0 ) { int x = prefixCount + suffixCount [ - diff / 2 ] ; count = count + x ; } } return count ; } int main ( ) { int arr [ ] = { 2 , 2 , -3 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubArraySignChange ( arr , N ) ; return 0 ; }