codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of MEX values of each node in an N | C # program for the above approach ; Function to create an N - ary Tree ; Traverse the edges ; Add edges ; Function to get the maximum sum of MEX values of tree rooted at 1 ; Initialize mex ; Iterate through all children of node ; Recursively find maximum sum of MEX values of each node in tree rooted at u ; Store the maximum sum of MEX of among all subtrees ; Increase the size of tree rooted at current node ; Resulting MEX for the current node of the recursive call ; Driver Code ; Given N nodes ; Given N - 1 edges ; Stores the tree ; Generates the tree ; Returns maximum sum of MEX values of each node
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void makeTree ( List < int > [ ] tree , pair [ ] edges , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = edges [ i ] . first ; int v = edges [ i ] . second ; tree [ u ] . Add ( v ) ; } } static pair dfs ( int node , List < int > [ ] tree ) { int mex = 0 ; int size = 1 ; foreach ( int u in tree [ node ] ) { pair temp = dfs ( u , tree ) ; mex = Math . Max ( mex , temp . first ) ; size += temp . second ; } return new pair ( mex + size , size ) ; } public static void Main ( String [ ] args ) { int N = 7 ; pair [ ] edges = { new pair ( 1 , 4 ) , new pair ( 1 , 5 ) , new pair ( 5 , 2 ) , new pair ( 5 , 3 ) , new pair ( 4 , 7 ) , new pair ( 7 , 6 ) } ; List < int > [ ] tree = new List < int > [ N + 1 ] ; for ( int i = 0 ; i < tree . Length ; i ++ ) tree [ i ] = new List < int > ( ) ; makeTree ( tree , edges , N ) ; Console . Write ( ( dfs ( 1 , tree ) . first ) ) ; } }
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | C # program for the above approach ; Function to count divisors of n ; Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Divisor count of x ; Iterate from x + 1 and check for each element ; Driver Code ; Given X ; Function call
using System ; class GFG { static int divisorCount ( int n ) { int x = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == n / i ) x ++ ; else x += 2 ; } } return x ; } static int minvalue_y ( int x ) { int a = divisorCount ( x ) ; int y = x + 1 ; while ( ( a & 1 ) == ( divisorCount ( y ) & 1 ) ) y ++ ; return y ; } public static void Main ( ) { int x = 5 ; Console . WriteLine ( minvalue_y ( x ) ) ; } }
Minimum value exceeding X whose count of divisors has different parity with count of divisors of X | C # program for the above approach ; Function to find the minimum value exceeding x whose count of divisors has different parity with count of divisors of X ; Check if x is perfect square ; Driver Code
using System ; class GFG { static int minvalue_y ( int x ) { int n = ( int ) Math . Sqrt ( x ) ; if ( n * n == x ) return x + 1 ; return ( int ) Math . Pow ( n + 1 , 2 ) ; } public static void Main ( ) { int x = 5 ; Console . WriteLine ( minvalue_y ( x ) ) ; } }
Sum of first N natural numbers with alternate signs | C # program to implement the above approach ; Function to find the sum of first N natural numbers with alternate signs ; Stores sum of alternate sign of first N natural numbers ; If N is an even number ; Update alternateSum ; If N is an odd number ; Update alternateSum ; Driver Code
using System ; class GFG { static int alternatingSumOfFirst_N ( int N ) { int alternateSum = 0 ; if ( N % 2 == 0 ) { alternateSum = ( - N ) / 2 ; } else { alternateSum = ( N + 1 ) / 2 ; } return alternateSum ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( alternatingSumOfFirst_N ( N ) ) ; } }
Minimum value to be added to the prefix sums at each array indices to make them positive | C # program for the above approach ; Function to find minimum startValue for positive prefix sum at each index ; Store the minimum prefix sum ; Stores prefix sum at each index ; Traverse over the array ; Update the prefix sum ; Update the minValue ; Return the positive start value ; Driver Code ; Function Call
using System ; class GFG { static int minStartValue ( int [ ] nums ) { int minValue = 0 ; int sum = 0 ; foreach ( int n in nums ) { sum += n ; minValue = Math . Min ( minValue , sum ) ; } int startValue = 1 - minValue ; return startValue ; } public static void Main ( ) { int [ ] nums = { - 3 , 2 , - 3 , 4 , 2 } ; Console . WriteLine ( minStartValue ( nums ) ) ; } }
Count ways to split array into two equal sum subarrays by replacing each array element to 0 once | C # program for the above approach ; Function to find number of ways to split array into 2 subarrays having equal sum by changing element to 0 once ; Stores the count of elements in prefix and suffix of array elements ; Stores the sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto index i ; Stores sum of suffix of index i ; Stores the desired result ; 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 ; Count number of ways to split the array at index i such that subarray sums are equal ; Update the final result ; Return the result ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int countSubArrayRemove ( int [ ] arr , int N ) { Dictionary < int , int > prefix_element_count = new Dictionary < int , int > ( ) ; Dictionary < int , int > suffix_element_count = new Dictionary < int , int > ( ) ; int total_sum_of_elements = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { total_sum_of_elements += arr [ i ] ; if ( ! suffix_element_count . ContainsKey ( arr [ i ] ) ) suffix_element_count [ arr [ i ] ] = 1 ; else suffix_element_count [ arr [ i ] ] ++ ; } int prefix_sum = 0 ; int suffix_sum = 0 ; int count_subarray_equal_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { prefix_sum += arr [ i ] ; if ( ! prefix_element_count . ContainsKey ( arr [ i ] ) ) prefix_element_count [ arr [ i ] ] = 1 ; else prefix_element_count [ arr [ i ] ] ++ ; suffix_sum = total_sum_of_elements - prefix_sum ; if ( ! suffix_element_count . ContainsKey ( arr [ i ] ) ) suffix_element_count [ arr [ i ] ] = 0 ; else suffix_element_count [ arr [ i ] ] -= 1 ; int difference = prefix_sum - suffix_sum ; int number_of_subarray_at_i_split = 0 ; if ( prefix_element_count . ContainsKey ( difference ) ) number_of_subarray_at_i_split = prefix_element_count [ difference ] ; if ( suffix_element_count . ContainsKey ( - difference ) ) number_of_subarray_at_i_split += suffix_element_count [ - difference ] ; count_subarray_equal_sum += number_of_subarray_at_i_split ; } return count_subarray_equal_sum ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 1 , 3 , 1 } ; int N = arr . Length ; Console . Write ( countSubArrayRemove ( arr , N ) ) ; } }
Count set bits in Bitwise XOR of all adjacent elements upto N | C # program to implement the above approach ; Function to count of set bits in Bitwise XOR of adjacent elements up to N ; Stores count of set bits by Bitwise XOR on adjacent elements of [ 0 , N ] ; Stores all possible values on right most set bit over [ 0 , N ] ; Iterate over the range [ 0 , N ] ; Update N ; Update bit_Position ; Driver Code
using System ; class GFG { static int countXORSetBitsAdjElemRange1_N ( int N ) { int total_set_bits = 0 ; int bit_Position = 1 ; while ( N != 0 ) { total_set_bits += ( ( N + 1 ) / 2 * bit_Position ) ; N -= ( N + 1 ) / 2 ; bit_Position ++ ; } return total_set_bits ; } public static void Main ( ) { int N = 4 ; Console . Write ( countXORSetBitsAdjElemRange1_N ( N ) ) ; } }
Check if a right | C # program to implement the above approach ; Function to check if N is a perfect square number or not ; If N is a non positive integer ; Stores square root of N ; Check for perfect square ; If N is not a perfect square number ; Function to check if given two sides of a triangle forms a right - angled triangle ; If the value of ( A * A + B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( A * A - B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( B * B - A * A ) is a perfect square number ; Update checkTriangle ; Driver Code ; If the given two sides of a triangle forms a right - angled triangle ; Otherwise
using System ; class GFG { static int checkPerfectSquare ( int N ) { if ( N <= 0 ) { return 0 ; } double sq = Math . Sqrt ( N ) ; if ( Math . Floor ( sq ) == Math . Ceiling ( sq ) ) { return 1 ; } return 0 ; } static bool checktwoSidesareRighTriangle ( int A , int B ) { bool checkTriangle = false ; if ( checkPerfectSquare ( A * A + B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( A * A - B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( B * B - A * A ) != 0 ) { checkTriangle = true ; } return checkTriangle ; } public static void Main ( ) { int A = 3 , B = 4 ; if ( checktwoSidesareRighTriangle ( A , B ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Minimize increments or decrements required to make sum and product of array elements non | C # program for the above approach ; Function to find the sum of array ; Return the sum ; Function that counts the minimum operations required to make the sum and product of array non - zero ; Stores count of zero elements ; Iterate over the array to count zero elements ; Sum of elements of the array ; Print the result ; Driver Code ; Given array arr [ ] ; Size of array ; Function call
using System ; class GFG { static int array_sum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int countOperations ( int [ ] arr , int N ) { int count_zeros = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count_zeros ++ ; } int sum = array_sum ( arr , N ) ; if ( count_zeros != 0 ) return count_zeros ; if ( sum == 0 ) return 1 ; return 0 ; } public static void Main ( ) { int [ ] arr = { - 1 , - 1 , 0 , 0 } ; int N = arr . Length ; Console . Write ( countOperations ( arr , N ) ) ; } }
Bitwise OR of all unordered pairs from a given array | C # program to implement the above approach ; Function to find the bitwise OR of all possible pairs of the array ; Stores bitwise OR of all possible pairs of [ ] arr ; Traverse the array [ ] arr ; Update totalOR ; Return bitwise OR of all possible pairs of [ ] arr ; Driver Code
using System ; class GFG { static int TotalBitwiseORPair ( int [ ] arr , int N ) { int totalOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalOR |= arr [ i ] ; } return totalOR ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 12 , 15 } ; int N = arr . Length ; Console . Write ( TotalBitwiseORPair ( arr , N ) ) ; } }
Difference between lexicographical ranks of two given permutations | C # program for the above approach ; Function the print the difference between the lexicographical ranks ; Store the permutations in lexicographic order ; Intital permutation ; Initial variables ; Check permutation ; Initialize second permutation ; Check permutation ; Print difference ; Driver Code ; Given array P [ ] ; Given array Q [ ] ; Given size ; Function call
using System ; class GFG { static void findDifference ( int [ ] p , int [ ] q , int N ) { int [ ] A = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = i + 1 ; bool IsCorrect ; int a = 1 , b = 1 ; do { IsCorrect = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != p [ i ] ) { IsCorrect = false ; break ; } } if ( IsCorrect ) break ; a ++ ; } while ( next_permutation ( A ) ) ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = i + 1 ; do { IsCorrect = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != q [ i ] ) { IsCorrect = false ; break ; } } if ( IsCorrect ) break ; b ++ ; } while ( next_permutation ( A ) ) ; Console . Write ( Math . Abs ( a - b ) + " STRNEWLINE " ) ; } static bool next_permutation ( int [ ] p ) { for ( int a = p . Length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . Length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . Length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] p = { 1 , 3 , 2 } ; int [ ] q = { 3 , 1 , 2 } ; int n = p . Length ; findDifference ( p , q , n ) ; } }
Count of packets placed in each box after performing given operations | C # program for the above approach ; Function to print final array after performing aint the operations ; Initialize variables ; Traverse through aint operations ; Operation Type ; Move left ; Move right ; Pick a packet ; Drop a packet ; Exit ; Print final array ; Driver Code ; Given capacity ; Given array with initial values ; Array size ; Operations ; Number of operations ; Function call
using System ; class GFG { static void printFinalArray ( int [ ] a , int n , int [ ] operations , int p , int capacity ) { int i , curr = 0 ; bool picked = false ; for ( i = 0 ; i < p ; i ++ ) { int s = operations [ i ] ; bool flag = false ; switch ( s ) { case 1 : if ( curr != 0 ) curr -- ; break ; case 2 : if ( curr != n - 1 ) curr ++ ; break ; case 3 : if ( picked == false && a [ curr ] != 0 ) { picked = true ; a [ curr ] -- ; } break ; case 4 : if ( picked == true && a [ curr ] != capacity ) { picked = false ; a [ curr ] ++ ; } break ; default : flag = true ; break ; } if ( flag == true ) break ; } for ( i = 0 ; i < n ; i ++ ) { Console . Write ( a [ i ] + " ▁ " ) ; } } public static void Main ( ) { int capacity = 5 ; int [ ] a = { 2 , 5 , 2 } ; int N = a . Length ; int [ ] operations = { 3 , 2 , 4 , 1 , 4 , 5 } ; int M = operations . Length ; printFinalArray ( a , N , operations , M , capacity ) ; } }
Check if sum of count of digits of array elements is Prime or not | C # program for the above approach ; Function to check whether a number is prime or not ; Corner cases ; If given number is a multiple of 2 or 3 ; Function to check if sum of count of digits of all array elements is prime or not ; Initialize sum with 0 ; Traverse over the array ; Convert array element to string ; Add the count of digits to sum ; Print the result ; Drive Code ; Function call
using System ; class GFG { static 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 ; } static void CheckSumPrime ( int [ ] A , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { String s = A [ i ] . ToString ( ) ; sum += s . Length ; } if ( isPrime ( sum ) == true ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { int [ ] A = { 1 , 11 , 12 } ; int N = A . Length ; CheckSumPrime ( A , N ) ; } }
Probability of obtaining Prime Numbers as product of values obtained by throwing N dices | C # program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the probability of obtaining a prime number as the product of N thrown dices ; Stores count of favorable outcomes ; Stores count of sample space ; Print the required probability ; Driver code
using System ; class GFG { static int power ( int x , int N ) { int res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } static void probablityPrimeprod ( int N ) { int N_E = 3 * N ; int N_S = power ( 6 , N ) ; Console . Write ( N_E + " ▁ / ▁ " + N_S ) ; } public static void Main ( String [ ] args ) { int N = 2 ; probablityPrimeprod ( N ) ; } }
Smaller palindromic number closest to N | C # program to implement the above approach ; Function to check whether a number is palindrome or not ; Stores reverse of N ; Stores the value of N ; Calculate reverse of N ; Update rev ; Update N ; Update N ; If N is equal to rev of N ; Function to find the closest smaller palindromic number to N ; Calculate closest smaller palindromic number to N ; Update N ; Driver Code
using System ; class GFG { static bool checkPalindrome ( int N ) { int rev = 0 ; int temp = N ; while ( N != 0 ) { rev = rev * 10 + N % 10 ; N = N / 10 ; } N = temp ; if ( N == rev ) { return true ; } return false ; } static int closestSmallerPalindrome ( int N ) { do { N -- ; } while ( N >= 0 && ! checkPalindrome ( N ) ) ; return N ; } public static void Main ( ) { int N = 4000 ; Console . WriteLine ( closestSmallerPalindrome ( N ) ) ; } }
Minimize increments or decrements by 2 to convert given value to a perfect square | C # program to implement the above approach ; Function to find the minimum number of operations required to make N a perfect square ; Stores count of operations by performing decrements ; Stores value of N ; Decrement the value of temp ; Stores square root of temp ; If temp is a perfect square ; Update temp ; Store count of operations by performing increments ; Increment the value of N ; Stores sqrt of N ; If N is a perfect square ; Update temp ; Return the minimum count ; Driver code
using System ; class GFG { static int MinimumOperationReq ( int N ) { int cntDecr = 0 ; int temp = N ; while ( temp > 0 ) { int X = ( int ) Math . Sqrt ( temp ) ; if ( X * X == temp ) { break ; } temp = temp - 2 ; cntDecr += 1 ; } int cntIncr = 0 ; while ( true ) { int X = ( int ) Math . Sqrt ( N ) ; if ( X * X == N ) { break ; } N = N + 2 ; cntIncr += 1 ; } return Math . Min ( cntIncr , cntDecr ) ; } public static void Main ( String [ ] args ) { int N = 15 ; Console . Write ( MinimumOperationReq ( N ) ) ; } }
First term from given Nth term of the equation F ( N ) = ( 2 * F ( N | C # program to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the value of F ( 1 ) ; Stores power ( 2 , N - 1 ) ; Stores modulo multiplicative inverse of P_2 under modulo M ; Stores the value of F ( 1 ) ; Update res ; Driver code
using System ; class GFG { static readonly int M = 1000000007 ; static long power ( long x , long N ) { long res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) % M ; } x = ( x * x ) % M ; N = N >> 1 ; } return res ; } static long moduloInverse ( long X ) { return power ( X , M - 2 ) ; } static long F_1 ( long N , long F_N ) { long P_2 = power ( 2 , N - 1 ) ; long modInv = moduloInverse ( P_2 ) ; long res ; res = ( ( modInv % M ) * ( F_N % M ) ) % M ; return res ; } public static void Main ( String [ ] args ) { long N = 3 ; long F_N = 6 ; Console . Write ( F_1 ( N , F_N ) ) ; } }
Queries to replace every array element by its XOR with a given value with updates | C # program of the above approach ; Function to insert an element into the array ; Function to update every array element a [ i ] by a [ i ] ^ x ; Function to compute the final results after the operations ; Driver Code ; Queries ; Function Call
using System ; using System . Collections . Generic ; class GFG { static List < int > arr = new List < int > ( ) ; static int effect ; static void add ( int x ) { arr . Add ( x ) ; } static void update ( int x ) { effect = effect ^ x ; } static void computeResults ( ) { for ( int i = 0 ; i < arr . Count ; i ++ ) { arr [ i ] = arr [ i ] ^ effect ; Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { arr = new List < int > ( ) ; arr . Add ( 0 ) ; effect = 0 ; add ( 5 ) ; update ( 2 ) ; computeResults ( ) ; } }
Check if a subarray of size K exists whose elements form a number divisible by 3 | C # implementation of the above approach ; Function to find the K size subarray ; Check if the first K elements forms a number which is divisible by 3 ; Using Sliding window technique ; Calculate sum of next K size subarray ; Check if sum is divisible by 3 ; Update the indices of the subarray ; If no such subarray is found ; Print the subarray ; Driver code ; Given array and K ; Function call
using System ; using System . Collections . Generic ; class GFG { class Point { public int x , y ; public Point ( int first , int second ) { this . x = first ; this . y = second ; } } public static void findSubArray ( List < int > arr , int k ) { Point ans = new Point ( 0 , 0 ) ; int i , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } int found = 0 ; if ( sum % 3 == 0 ) { ans = new Point ( 0 , i - 1 ) ; found = 1 ; } for ( int j = i ; j < arr . Count ; j ++ ) { if ( found == 1 ) break ; sum = sum + arr [ j ] - arr [ j - k ] ; if ( sum % 3 == 0 ) { ans = new Point ( j - k + 1 , j ) ; found = 1 ; } } if ( found == 0 ) ans = new Point ( - 1 , 0 ) ; if ( ans . x == - 1 ) { Console . Write ( - 1 ) ; } else { for ( i = ans . x ; i <= ans . y ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { List < int > arr = new List < int > ( ) ; arr . Add ( 84 ) ; arr . Add ( 23 ) ; arr . Add ( 45 ) ; arr . Add ( 12 ) ; arr . Add ( 56 ) ; arr . Add ( 82 ) ; int K = 3 ; findSubArray ( arr , K ) ; } }
Count permutations of given array that generates the same Binary Search Tree ( BST ) | C # program to implement the above approach ; Function to precompute the factorial of 1 to N ; Function to get the value of nCr ; nCr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; Function to count the number of ways to rearrange the array to obtain same BST ; Store the size of the array ; Base case ; Store the elements of the left subtree of BST ; Store the elements of the right subtree of BST ; Store the root node ; Push all the elements of the left subtree ; Push all the elements of the right subtree ; Store the size of leftSubTree ; Store the size of rightSubTree ; Recurrence relation ; Driver Code ; Store the size of arr ; Store the factorial up to N ; Precompute the factorial up to N
using System ; using System . Collections . Generic ; class GFG { static void calculateFact ( int [ ] fact , int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } } static int nCr ( int [ ] fact , int N , int R ) { if ( R > N ) return 0 ; int res = fact [ N ] / fact [ R ] ; res /= fact [ N - R ] ; return res ; } static int countWays ( List < int > arr , int [ ] fact ) { int N = arr . Count ; if ( N <= 2 ) { return 1 ; } List < int > leftSubTree = new List < int > ( ) ; List < int > rightSubTree = new List < int > ( ) ; int root = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] < root ) { leftSubTree . Add ( arr [ i ] ) ; } else { rightSubTree . Add ( arr [ i ] ) ; } } int N1 = leftSubTree . Count ; int N2 = rightSubTree . Count ; int countLeft = countWays ( leftSubTree , fact ) ; int countRight = countWays ( rightSubTree , fact ) ; return nCr ( fact , N - 1 , N1 ) * countLeft * countRight ; } public static void Main ( String [ ] args ) { int [ ] a = { 3 , 4 , 5 , 1 , 2 } ; List < int > arr = new List < int > ( ) ; foreach ( int i in a ) arr . Add ( i ) ; int N = a . Length ; int [ ] fact = new int [ N ] ; calculateFact ( fact , N ) ; Console . Write ( countWays ( arr , fact ) ) ; } }
Sum of all ordered pair | C # program to implement the above approach ; Function to calculate the sum of all pair - products ; Stores sum of array ; Update sum of the array ; Driver code
using System ; class GFG { static int sumOfProd ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } return sum * sum ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 1 , 5 , 4 } ; int N = arr . Length ; Console . WriteLine ( sumOfProd ( arr , N ) ) ; } }