codeparrot/xlcost-text-to-code huggingface dataset:
Minimum division by 10 and multiplication by 2 required to reduce given number to 1 | C # program for the above approach ; Function to find the minimum number operations required to reduce N to 1 ; Stores count of powers of 2 and 5 ; Calculating the primefactors 2 ; Calculating the primefactors 5 ; If n is 1 and cnt2 <= cnt5 ; Return the minimum operations ; Otherwise , n can 't be reduced ; Driver Code ; Given Number N ; Function Call
using System ; class GFG { static int minimumMoves ( int n ) { int cnt2 = 0 , cnt5 = 0 ; while ( n % 2 == 0 ) { n /= 2 ; cnt2 ++ ; } while ( n % 5 == 0 ) { n /= 5 ; cnt5 ++ ; } if ( n == 1 && cnt2 <= cnt5 ) { return 2 * cnt5 - cnt2 ; } else return - 1 ; } public static void Main ( ) { int N = 25 ; Console . WriteLine ( minimumMoves ( N ) ) ; } }
Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non | C # Program to implement the above approach ; Function to check if a permutation of first N natural numbers exist with Bitwise AND of adjacent elements not equal to 0 ; If n is a power of 2 ; Driver Code
using System ; class solution { static void check ( int n ) { if ( ( n & n - 1 ) != 0 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int n = 5 ; check ( n ) ; } }
Minimize adding odd and subtracting even numbers to make all array elements equal to K | C # program to implement the above approach ; Function to find the minimum operations required to make array elements equal to K ; Stores minimum count of operations ; Traverse the given array ; If K is greater than arr [ i ] ; If ( K - arr [ i ] ) is even ; Update cntOpe ; Update cntOpe ; If K is less than arr [ i ] ; If ( arr [ i ] - K ) is even ; Update cntOpe ; Update cntOpe ; Driver code
using System ; class GFG { public static int MinOperation ( int [ ] arr , int N , int K ) { int cntOpe = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( K > arr [ i ] ) { if ( ( K - arr [ i ] ) % 2 == 0 ) { cntOpe += 2 ; } else { cntOpe += 1 ; } } else if ( K < arr [ i ] ) { if ( ( K - arr [ i ] ) % 2 == 0 ) { cntOpe += 1 ; } else { cntOpe += 2 ; } } } return cntOpe ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 7 , 2 , 1 , 3 } ; int K = 5 ; int N = arr . Length ; Console . WriteLine ( MinOperation ( arr , N , K ) ) ; } }
Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same | C # program for the above approach ; Function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store frequency of elements ; Stores xor value ; Taking xor of all the values of both arrays ; Store frequency of B [ ] ; Find the array B [ ] after rearrangement ; If the updated value is present then decrement its frequency ; Otherwise return empty vector ; Utility function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store rearranged array B ; If rearrangement possible ; Otherwise return - 1 ; Driver Code ; Given vector A ; Given vector B ; Size of the vector ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static ArrayList rearrangeArray ( ArrayList A , ArrayList B , int N ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int xor_value = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xor_value ^= ( int ) A [ i ] ; xor_value ^= ( int ) B [ i ] ; if ( ! m . ContainsKey ( ( int ) B [ i ] ) ) m . Add ( ( int ) B [ i ] , 1 ) ; else m [ ( int ) B [ i ] ] = m [ ( int ) B [ i ] ] + 1 ; } for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = ( int ) A [ i ] ^ xor_value ; if ( m . ContainsKey ( ( int ) B [ i ] ) ) { m [ ( int ) B [ i ] ] -- ; } else return ( new ArrayList ( ) ) ; } return B ; } static void rearrangeArrayUtil ( ArrayList A , ArrayList B , int N ) { ArrayList ans = rearrangeArray ( A , B , N ) ; if ( ans . Count != 0 ) { for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } else { Console . WriteLine ( " - 1" ) ; } } public static void Main ( ) { ArrayList A = new ArrayList { 13 , 21 , 33 , 49 , 53 } ; ArrayList B = new ArrayList { 54 , 50 , 34 , 22 , 14 } ; int N = A . Count ; rearrangeArrayUtil ( A , B , N ) ; } }
Count sequences of given length having non | C # program for the above approach ; Function to find the Binomial Coefficient C ( n , r ) ; Stores the value C ( n , r ) ; Update C ( n , r ) = C ( n , n - r ) ; Find C ( n , r ) iteratively ; Return the final value ; Function to find number of sequence whose prefix sum at each index is always non - negative ; Find n ; Value of C ( 2 n , n ) ; Catalan number ; Print the answer ; Driver Code ; Given M and X ; Function Call
using System ; class GFG { static long binCoff ( long n , long r ) { long val = 1 ; int i ; if ( r > ( n - r ) ) r = ( n - r ) ; for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } static void findWays ( int M , int X ) { int n = M / 2 ; long a , b ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; Console . WriteLine ( b ) ; } public static void Main ( ) { int M = 4 ; int X = 5 ; findWays ( M , X ) ; } }
XOR of array elements whose modular inverse with a given number exists | C # program for the above approach ; Function to return the gcd of a & b ; Base Case ; Recursively calculate GCD ; Function to print the Bitwise XOR of elements of arr [ ] if gcd ( arr [ i ] , M ) is 1 ; Initialize xor ; Traversing the array ; GCD of M and arr [ i ] ; If GCD is 1 , update xor ; Print xor ; Drive Code ; Given array arr [ ] ; Given number M ; Size of the array ; Function Call
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void countInverse ( int [ ] arr , int N , int M ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int gcdOfMandelement = gcd ( M , arr [ i ] ) ; if ( gcdOfMandelement == 1 ) { XOR ^= arr [ i ] ; } } Console . WriteLine ( XOR ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int M = 4 ; int N = arr . Length ; countInverse ( arr , N , M ) ; } }
Count positions in a chessboard that can be visited by the Queen which are not visited by the King | C # program for the above approach ; Function to print the number of cells only visited by the queen ; Find all the moves ; Find all moves for x + 1 , y + 1 ; Find all moves for x - 1 , y - 1 ; Find all moves for x - 1 , y + 1 ; Find all moves for x + 1 , y - 1 ; Find all squares visited by King x + 1 , in same row ; x - 1 , in same row ; y + 1 , in same column ; y - 1 , in same column ; Return answer ; Driver Code ; Dimension of Board ; Position of Cell ; Function Call
using System ; class GFG { static int Moves_Calculator ( int x , int y , int row , int col ) { int total_moves = 0 ; if ( ( row - x ) > 0 && ( col - y ) > 0 ) total_moves += Math . Min ( ( row - x ) , ( col - y ) ) ; if ( ( y - 1 ) > 0 && ( x - 1 ) > 0 ) total_moves += Math . Min ( ( y - 1 ) , ( x - 1 ) ) ; if ( ( x - 1 ) > 0 && ( col - y ) > 0 ) total_moves += Math . Min ( ( x - 1 ) , ( col - y ) ) ; if ( ( row - x ) > 0 && ( y - 1 ) > 0 ) total_moves += Math . Min ( ( row - x ) , ( y - 1 ) ) ; total_moves += ( row - 1 ) + ( col - 1 ) ; int king_moves = 0 ; if ( x + 1 <= row ) king_moves += 1 ; if ( x - 1 > 0 ) king_moves += 1 ; if ( y + 1 <= col ) king_moves += 1 ; if ( y - 1 > 0 ) king_moves += 1 ; if ( x + 1 <= row && y + 1 <= col ) king_moves += 1 ; if ( x + 1 <= row && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y + 1 <= col ) king_moves += 1 ; return total_moves - king_moves ; } public static void Main ( ) { int n = 8 , m = 8 ; int x = 1 , y = 1 ; Console . WriteLine ( Moves_Calculator ( x , y , m , n ) ) ; } }
Nearest smaller number to N having multiplicative inverse under modulo N equal to that number | C # program to implement the above approach ; Function to find the nearest smaller number satisfying the condition ; Driver Code
using System ; class GFG { static int clstNum ( int N ) { return ( N - 1 ) ; } public static void Main ( ) { int N = 11 ; Console . Write ( clstNum ( N ) ) ; } }
Count array elements having modular inverse under given prime number P equal to itself | C # program to implement the above approach ; Function to get the count of elements that satisfy the given condition . ; Stores count of elements that satisfy the condition ; Traverse the given array . ; If square of current element is equal to 1 ; Driver Code
using System ; class GFG { static int equvInverse ( int [ ] arr , int N , int P ) { int cntElem = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] * arr [ i ] ) % P == 1 ) { cntElem ++ ; } } return cntElem ; } public static void Main ( ) { int [ ] arr = { 1 , 6 , 4 , 5 } ; int N = arr . Length ; int P = 7 ; Console . WriteLine ( equvInverse ( arr , N , P ) ) ; } }
Count ways to split array into K non | C # program to implement the above approach ; Function to get the value of pow ( K , M ) ; Stores value of pow ( K , M ) ; Calculate value of pow ( K , N ) ; If N is odd , update res ; Update M to M / 2 ; Update K ; Function to print total ways to split the array that satisfies the given condition ; Stores total ways that satisfies the given condition ; Stores count of distinct elements in the given arr ; Store distinct elements of the given array ; Traverse the given array ; Insert current element into set st . ; Update M ; Update cntways ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int power ( int K , int M ) { int res = 1 ; while ( M > 0 ) { if ( ( M & 1 ) == 1 ) { res = ( res * K ) ; } M = M >> 1 ; K = ( K * K ) ; } return res ; } static int cntWays ( int [ ] arr , int N , int K ) { int cntways = 0 ; int M = 0 ; HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . Add ( arr [ i ] ) ; } M = st . Count ; cntways = power ( K , M ) ; return cntways ; } public static void Main ( ) { int [ ] arr = { 2 , 3 } ; int N = arr . Length ; int K = 2 ; Console . WriteLine ( cntWays ( arr , N , K ) ) ; } }
Minimize product of first N | C # program for the above approach ; Function to find the minimum product of 1 to N - 1 after performing the given operations ; Initialize ans with 1 ; Multiply ans with N - 2 ( ( N - 4 ) / 2 ) times ; Multiply ans with N - 1 and N - 2 once ; Print ans ; Driver Code ; Given Number N ; Function Call
using System ; class GFG { static int mod = ( int ) 1e9 + 7 ; static void minProduct ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= ( n - 4 ) / 2 ; i ++ ) { ans = ( int ) ( 1L * ans * ( n - 2 ) ) % mod ; } ans = ( int ) ( 1L * ans * ( n - 2 ) * ( n - 1 ) ) % mod ; Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 8 ; minProduct ( N ) ; } }
Bitwise XOR of all unordered pairs from a given array | C # program to implement the above approach ; Function to get bitwise XOR of all possible pairs of the given array ; Stores bitwise XOR of all possible pairs ; Generate all possible pairs and calculate bitwise XOR of all possible pairs ; Calculate bitwise XOR of each pair ; Driver code
using System ; class GFG { public static int TotalXorPair ( int [ ] arr , int N ) { int totalXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalXOR ^= arr [ i ] ^ arr [ j ] ; } } return totalXOR ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . Write ( TotalXorPair ( arr , N ) ) ; } }
Count permutations of all integers upto N that can form an acyclic graph based on given conditions | C # implementation of the above approach ; Find the count of possible graphs ; Driver Code
using System ; class GFG { static void possibleAcyclicGraph ( int N ) { Console . Write ( ( int ) Math . Pow ( 2 , N - 1 ) ) ; return ; } public static void Main ( ) { int N = 4 ; possibleAcyclicGraph ( N ) ; } }
Minimize positive product of two given numbers by at most N decrements | C # program to implement the above approach ; Function to minimize the product of two numbers ; Reducing X , N times , minimizes the product ; Reduce X to 1 and reduce remaining N from Y ; Reducing Y , N times , minimizes the product ; Reduce Y to 1 and reduce remaining N from X ; Driver Code
using System ; class GFG { static int minProd ( int X , int Y , int N ) { if ( X <= Y ) { if ( N < X ) return ( X - N ) * Y ; else { return Math . Max ( Y - ( N - X + 1 ) , 1 ) ; } } if ( Y >= N ) return ( Y - N ) * X ; return Math . Max ( X - ( N - Y + 1 ) , 1 ) ; } public static void Main ( String [ ] args ) { int X = 47 , Y = 42 , N = 167 ; Console . WriteLine ( minProd ( X , Y , N ) ) ; } }
Count pairs from an array having product of their sum and difference equal to 1 | C # program for the above approach ; Function to count the desired number of pairs ; Initialize oneCount ; Initialize the desiredPair ; Traverse the given array ; If 1 is encountered ; If 0 is encountered ; Update count of pairs ; Return the readonly count ; Driver Code ; Given array [ ] arr ; Function call
using System ; class GFG { static int countPairs ( int [ ] arr , int n ) { int oneCount = 0 ; int desiredPair = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { oneCount ++ ; } if ( arr [ i ] == 0 ) { desiredPair += oneCount ; } } return desiredPair ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 1 , 0 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }
Smallest missing non | C # program to implement the above approach ; Function to print the smallest missing non - negative integer up to every array indices ; Stores the smallest missing non - negative integers between start index to current index ; Store the boolean value to check smNonNeg present between start index to each index of the array ; Traverse the array ; Since output always lies in the range [ 0 , N - 1 ] ; Check if smNonNeg is present between start index and current index or not ; Print smallest missing non - negative integer ; Driver Code
using System ; class GFG { static void smlstNonNeg ( int [ ] arr , int N ) { int smNonNeg = 0 ; bool [ ] hash = new bool [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ i ] = false ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i ] < N ) { hash [ arr [ i ] ] = true ; } while ( hash [ smNonNeg ] ) { smNonNeg ++ ; } Console . Write ( smNonNeg + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 0 , 1 , 2 , 3 , 5 } ; int N = arr . Length ; smlstNonNeg ( arr , N ) ; } }
Split array into two subsequences having minimum count of pairs with sum equal to X | C # program for the above approach ; Function to split the array into two subsequences ; Stores the two subsequences ; Flag to set / reset to split arrays elements alternately into two arrays ; Traverse the given array ; If 2 * arr [ i ] is less than X ; Push element into the first array ; If 2 * arr [ i ] is greater than X ; Push element into the second array ; If 2 * arr [ i ] is equal to X ; Alternatively place the elements into the two arrays ; Print both the arrays ; Driver Code ; Size of the array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void solve ( int [ ] arr , int N , int X ) { List < int > A = new List < int > ( ) , B = new List < int > ( ) ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( 2 * arr [ i ] ) < X ) { A . Add ( arr [ i ] ) ; } else if ( ( 2 * arr [ i ] ) > X ) { B . Add ( arr [ i ] ) ; } else { if ( c % 2 == 0 ) { A . Add ( arr [ i ] ) ; } else { B . Add ( arr [ i ] ) ; } c ++ ; } } Console . Write ( " The ▁ First ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < A . Count ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; Console . Write ( " The ▁ Second ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < B . Count ; i ++ ) { Console . Write ( B [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 } ; int X = 7 ; int N = arr . Length ; solve ( arr , N , X ) ; } }
Split array into minimum number of subsets having maximum pair sum at most K | C # program to implement the above approach ; Function to get the minimum count of subsets that satisfy the given condition ; Store the minimum count of subsets that satisfy the given condition ; Stores start index of the sorted array . ; Stores end index of the sorted array ; Sort the given array ; Traverse the array ; If only two elements of sorted array left ; If only one elements left in the array ; Driver Code
using System ; class GFG { static int cntMinSub ( int [ ] arr , int N , int K ) { int res = 0 ; int start = 0 ; int end = N - 1 ; Array . Sort ( arr ) ; while ( end - start > 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { start ++ ; } else { res ++ ; end -- ; } } if ( end - start == 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { res ++ ; start ++ ; end -- ; } else { res ++ ; end -- ; } } if ( start == end ) { res ++ ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 6 , 8 , 10 , 20 , 25 } ; int N = arr . Length ; int K = 26 ; Console . Write ( cntMinSub ( arr , N , K ) ) ; } }
Count possible values of K such that X | C # program for the above approach ; Function to count integers K satisfying given equation ; Calculate the absoluter difference between a and b ; Iterate till sqrt of the difference ; Return the count ; Driver Code
using System ; class GFG { static int condition ( int a , int b ) { int d = Math . Abs ( a - b ) , count = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( d ) ; i ++ ) { if ( d % i == 0 ) { if ( d / i == i ) count += 1 ; else count += 2 ; } } return count ; } public static void Main ( String [ ] args ) { int x = 2 , y = 6 ; Console . WriteLine ( condition ( x , y ) ) ; } }
Count pairs in an array whose product is composite number | C # Program to implement the above approach ; Function to check if a number is prime or not ; Check if N is multiple of i or not . ; If N is multiple of i . ; Function to get the count of pairs whose product is a composite number . ; Stores the count of pairs whose product is a composite number ; Generate all possible pairs ; Stores the product of element of current pair ; If prod is a composite number ; Driver Code
using System ; class GFG { static bool isComposite ( int N ) { for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return true ; } } return false ; } static int compositePair ( int [ ] arr , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int prod = arr [ i ] * arr [ j ] ; if ( isComposite ( prod ) ) { res ++ ; } } } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 2 , 8 } ; int N = arr . Length ; Console . WriteLine ( compositePair ( arr , N ) ) ; } }
Count N | C # program for the above approach ; Function for calculate ( x ^ y ) % mod in O ( log y ) ; Base Condition ; Transition state of power Function ; Function for counting total numbers that can be formed such that digits X , Y are present in each number ; Calculate the given expression ; Return the readonly answer ; Driver Code
using System ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static long power ( int x , int y ) { if ( y == 0 ) return 1 ; int p = ( int ) ( power ( x , y / 2 ) % mod ) ; p = ( p * p ) % mod ; if ( y % 2 == 1 ) { p = ( x * p ) % mod ; } return p ; } static int TotalNumber ( int N ) { int ans = ( int ) ( ( power ( 10 , N ) - 2 * power ( 9 , N ) + power ( 8 , N ) + 2 * mod ) % mod ) ; return ans ; } public static void Main ( String [ ] args ) { int N = 10 ; Console . Write ( TotalNumber ( N ) + " STRNEWLINE " ) ; } }
Array obtained by repeatedly reversing array after every insertion from given array | C # program of the above approach ; Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void generateArray ( int [ ] arr , int n ) { List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) != 0 ) ans . Insert ( 0 , arr [ i ] ) ; else ans . Add ( arr [ i ] ) ; } if ( ( n & 1 ) != 0 ) { ans . Reverse ( ) ; } foreach ( int x in ans ) { Console . Write ( x + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 1 , 2 , 3 , 4 } ; generateArray ( arr , n ) ; } }
Find the greater number closest to N having at most one non | C # program to implement the above approach ; Function to get closest greater number with at most non zero digit ; Stores the closest greater number with at most one non - zero digit ; Stores length of str ; Append 10 to the end of resultant string ; Append n - 1 times '0' to the end of resultant string ; Driver code
using System ; class GFG { public static string closestgtNum ( string str ) { string res = " " ; int n = str . Length ; if ( str [ 0 ] < '9' ) { res = res + ( char ) ( str [ 0 ] + 1 ) ; } else { res = res + '1' ; res = res + '0' ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { res = res + '0' ; } return res ; } static void Main ( ) { string str = "120" ; Console . WriteLine ( closestgtNum ( str ) ) ; } }
Count maximum non | C # program for the above approach ; Function to count maximum number of non - overlapping subarrays with sum equals to the target ; Stores the readonly count ; Next subarray should start from index >= availIdx ; Tracks the prefix sum ; Map to store the prefix sum for respective indices ; Check if cur_sum - target is present in the array or not ; Update the index of current prefix sum ; Return the count of subarrays ; Driver Code ; Given array [ ] arr ; Given sum target ; Function call
using System ; using System . Collections . Generic ; class GFG { static int maximumSubarrays ( int [ ] arr , int N , int target ) { int ans = 0 ; int availIdx = - 1 ; int cur_sum = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; mp . Add ( 0 , 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { cur_sum += arr [ i ] ; if ( mp . ContainsKey ( cur_sum - target ) && mp [ cur_sum - target ] >= availIdx ) { ans ++ ; availIdx = i ; } if ( mp . ContainsKey ( cur_sum ) ) mp [ cur_sum ] = i ; else mp . Add ( cur_sum , i ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , - 1 , 4 , 3 , 6 , 4 , 5 , 1 } ; int N = arr . Length ; int target = 6 ; Console . Write ( maximumSubarrays ( arr , N , target ) ) ; } }
Maximize array sum by replacing equal adjacent pairs by their sum and X respectively | C # program for the above approach ; Function to calculate x ^ y ; Base Case ; Find the value in temp ; If y is even ; Function that find the maximum possible sum of the array ; Print the result using the formula ; Driver Code ; Function Call
using System ; class GFG { static int power ( int x , int y ) { int temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } public static void maximumPossibleSum ( int N , int X ) { Console . WriteLine ( X * ( power ( 2 , N ) - 1 ) ) ; } public static void Main ( String [ ] args ) { int N = 3 , X = 5 ; maximumPossibleSum ( N , X ) ; } }