codeparrot/xlcost-text-to-code huggingface dataset:
Minimize cost of converting all array elements to Fibonacci Numbers | C # program to count frequencies of array items ; Function to find the N - th Fibonacci Number ; Find the value of a , b , and r ; Find the N - th Fibonacci ; Return the result ; Function to find the Fibonacci number which is nearest to X ; Calculate the value of n for X ; Return the nearest Fibonacci Number ; Function to find the minimum cost to convert all array elements to Fibonacci Numbers ; Stores the total minimum cost ; Traverse the given array arr [ ] ; Find the nearest Fibonacci Number ; Add the cost ; Return the final cost ; Driver code
using System ; class GFG { static int nthFibo ( int n ) { double a = ( Math . Pow ( 5 , 0.5 ) + 1 ) / 2 ; double b = ( - 1 * ( Math . Pow ( 5 , 0.5 ) ) + 1 ) / 2 ; double r = Math . Pow ( 5 , 0.5 ) ; double ans = ( Math . Pow ( a , n ) - Math . Pow ( b , n ) ) / r ; return ( int ) ans ; } static int nearFibo ( int X ) { double a = ( Math . Pow ( 5 , 0.5 ) + 1 ) / 2 ; int n = ( int ) ( Math . Log ( ( Math . Pow ( 5 , 0.5 ) ) * X ) / Math . Log ( a ) ) ; int nth = nthFibo ( n ) ; int nplus = nthFibo ( n + 1 ) ; if ( Math . Abs ( X - nth ) < Math . Abs ( X - nplus ) ) return nth ; else return nplus ; } static int getCost ( int [ ] arr , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int fibo = nearFibo ( arr [ i ] ) ; cost += Math . Abs ( arr [ i ] - fibo ) ; } return cost ; } public static void Main ( String [ ] args ) { int [ ] arr = { 56 , 34 , 23 , 98 , 7 } ; int n = arr . Length ; Console . WriteLine ( getCost ( arr , n ) ) ; } }
Maximum sum of pairs that are at least K distance apart in an array | C # program for the above approach ; Function to find the largest sum pair that are K distant apart ; Stores the prefix maximum array ; Base Case ; Traverse the array and update the maximum value upto index i ; Stores the maximum sum of pairs ; Iterate over the range [ K , N ] ; Find the maximum value of the sum of valid pairs ; Return the resultant sum ; Driver Code
using System ; class GFG { static int getMaxPairSum ( int [ ] arr , int N , int K ) { int [ ] preMax = new int [ N ] ; preMax [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { preMax [ i ] = Math . Max ( preMax [ i - 1 ] , arr [ i ] ) ; } int res = Int32 . MinValue ; for ( int i = K ; i < N ; i ++ ) { res = Math . Max ( res , arr [ i ] + preMax [ i - K ] ) ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 8 , 6 , 3 } ; int K = 3 ; int N = arr . Length ; Console . Write ( getMaxPairSum ( arr , N , K ) ) ; } }
Program to calculate sum of an Infinite Arithmetic | C # program for the above approach ; Function to find the sum of the infinite AGP ; Stores the sum of infinite AGP ; Print the required sum ; Driver Code
using System ; class GFG { static void sumOfInfiniteAGP ( double a , double d , double r ) { double ans = a / ( 1 - r ) + ( d * r ) / ( 1 - r * r ) ; Console . Write ( ans ) ; } public static void Main ( ) { double a = 0 , d = 1 , r = 0.5 ; sumOfInfiniteAGP ( a , d , r ) ; } }
Count pairs from an array having GCD equal to the minimum element in the pair | C # program for the above approach ; Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Iterate over the range [ 0 , N - 2 ] ; Iterate over the range [ i + 1 , N ] ; If arr [ i ] % arr [ j ] is 0 or arr [ j ] % arr [ i ] is 0 ; Increment count by 1 ; Return the resultant count ; Driver Code
using System ; public class GFG { static int countPairs ( int [ ] arr , int N ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 arr [ j ] % arr [ i ] == 0 ) { count ++ ; } } } return count ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 1 , 2 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }
Count pairs from an array having GCD equal to the minimum element in the pair | C # program for the above approach ; Function to count pairs from an array having GCD equal to minimum element of that pair ; Stores the resultant count ; Stores the frequency of each array element ; Traverse the array arr [ ] ; Iterate over the Map mp ; Stores the array element ; Stores the count of array element x ; If x is 1 ; Increment res by N - 1 ; Increment res by yC2 ; Iterate over the range [ 2 , sqrt ( x ) ] ; If x is divisible by j ; Increment the value of res by mp [ j ] ; If j is not equal to x / j ; Increment res by mp [ x / j ] ; Return the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int CountPairs ( int [ ] arr , int N ) { int res = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] ++ ; else mp . Add ( arr [ i ] , 1 ) ; } foreach ( KeyValuePair < int , int > kvp in mp ) { int x = kvp . Key ; int y = kvp . Value ; if ( x == 1 ) { res += N - 1 ; continue ; } res += ( y * ( y - 1 ) ) / 2 ; for ( int j = 2 ; j <= Math . Sqrt ( x ) ; j ++ ) { if ( x % j == 0 ) { res += mp [ j ] ; if ( j != x / j ) res += mp [ x / j ] ; } } } return res ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 1 , 2 } ; int N = arr . Length ; Console . Write ( CountPairs ( arr , N ) ) ; } }
Product of count of set bits present in binary representations of elements in an array | C # program for the above approach ; Function to count the set bits in an integer ; Stores the count of set bits ; Iterate while N is not equal to 0 ; Increment count by 1 ; Divide N by 2 ; Return the total count obtained ; Function to find the product of count of set bits present in each element of an array ; Stores the resultant product ; Traverse the array arr [ ] ; Stores the count of set bits of arr [ i ] ; Update the product ; Return the resultant product ; Driver Code
using System ; public class GFG { static int countbits ( int n ) { int count = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) count ++ ; n = n / 2 ; } return count ; } static int BitProduct ( int [ ] arr , int N ) { int product = 1 ; for ( int i = 0 ; i < N ; i ++ ) { int bits = countbits ( arr [ i ] ) ; product *= bits ; } return product ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 2 , 4 , 1 , 5 } ; int N = arr . Length ; Console . Write ( BitProduct ( arr , N ) ) ; } }
Queries to calculate sum of array elements consisting of odd number of divisors | C # program for the above approach ; Function to find the sum of elements having odd number of divisors in index range [ L , R ] for Q queries ; Initialize the dp [ ] array ; Traverse the array , arr [ ] ; If a [ i ] is a perfect square , then update value of DP [ i ] to a [ i ] ; Find the prefix sum of DP [ ] array ; Iterate through all the queries ; Find the sum for each query ; Driver Code
using System ; class GFG { static void OddDivisorsSum ( int n , int q , int [ ] a , int [ , ] Query ) { int [ ] DP = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) ( Math . Sqrt ( a [ i ] ) ) ; if ( x * x == a [ i ] ) DP [ i ] = a [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { DP [ i ] = DP [ i - 1 ] + DP [ i ] ; } for ( int i = 0 ; i < q ; i ++ ) { int l = Query [ i , 0 ] ; int r = Query [ i , 1 ] ; if ( l == 0 ) { Console . Write ( DP [ r ] + " ▁ " ) ; } else { Console . Write ( DP [ r ] - DP [ l - 1 ] + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 2 , 4 , 5 , 6 , 9 } ; int N = arr . Length ; int Q = 3 ; int [ , ] Query = { { 0 , 2 } , { 1 , 3 } , { 1 , 4 } } ; OddDivisorsSum ( N , Q , arr , Query ) ; } }
Smallest subset of maximum sum possible by splitting array into two subsets | C # program for the above approach ; Function to split array elements into two subsets having sum of the smaller subset maximized ; Stores the size of the array ; Stores the frequency of array elements ; Stores the total sum of the array ; Stores the sum of the resultant set ; Stores if it is possible to split the array that satisfies the conditions ; Stores the elements of the first subseta ; Traverse the array arr [ ] ; Increment total sum ; Increment count of arr [ i ] ; Sort the array arr [ ] ; Stores the index of the last element of the array ; Traverse the array arr [ ] ; Stores the frequency of arr [ i ] ; If frq + ans . size ( ) is at most remaining size ; Append arr [ i ] to ans ; Decrement totSum by arr [ i ] ; Increment s by arr [ i ] ; Otherwise , decrement i by frq ; If s is greater than totSum ; Mark flag 1 ; If flag is equal to 1 ; Print the arrList ans ; Otherwise , print " - 1" ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findSubset ( List < int > arr ) { int N = arr . Count ; int i ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int totSum = 0 ; int s = 0 ; int flag = 0 ; List < int > ans = new List < int > ( ) ; for ( i = 0 ; i < arr . Count ; i ++ ) { totSum += arr [ i ] ; if ( mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; else mp . Add ( arr [ i ] , 1 ) ; } arr . Sort ( ) ; i = N - 1 ; while ( i >= 0 ) { int frq = mp [ arr [ i ] ] ; if ( ( frq + ans . Count ) < ( N - ( frq + ans . Count ) ) ) { for ( int k = 0 ; k < frq ; k ++ ) { ans . Add ( arr [ i ] ) ; totSum -= arr [ i ] ; s += arr [ i ] ; i -- ; } } else { i -= frq ; } if ( s > totSum ) { flag = 1 ; break ; } } if ( flag == 1 ) { for ( i = ans . Count - 1 ; i >= 0 ; i -- ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } else { Console . Write ( - 1 ) ; } } public static void Main ( ) { List < int > arr = new List < int > ( ) { 5 , 3 , 2 , 4 , 1 , 2 } ; findSubset ( arr ) ; } }
Minimize Bitwise XOR of array elements with 1 required to make sum of array at least K | C # program for the above approach ; Function to find minimum number of Bitwise XOR of array elements with 1 required to make sum of the array at least K ; Stores the count of even array elements ; Stores sum of the array ; Traverse the array arr [ ] ; Increment sum ; If array element is even ; Increase count of even ; If S is at least K ; If S + E is less than K ; Otherwise , moves = K - S ; Driver Code
using System ; class GFG { static int minStepK ( int [ ] arr , int N , int K ) { int E = 0 ; int S = 0 ; for ( int i = 0 ; i < N ; i ++ ) { S += arr [ i ] ; if ( arr [ i ] % 2 == 0 ) E += 1 ; } if ( S >= K ) return 0 ; else if ( S + E < K ) return - 1 ; else return K - S ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 1 , 0 , 1 } ; int N = arr . Length ; int K = 4 ; Console . WriteLine ( minStepK ( arr , N , K ) ) ; } }
Maximize difference between odd and even | C # program for the above approach ; Function to find maximum and minimum value of a number that can be obtained by rotating bits ; Stores the value of N ; Stores the maximum value ; Stores the minimum value ; If temp is odd ; Update the maximum and the minimum value ; If flag is 1 , then return the maximum value ; Otherwise , return the maximum value ; Function to find the maximum difference between the sum of odd and even - indexed array elements possible by rotating bits ; Stores the maximum difference ; Stores the sum of elements present at odd indices ; Stores the sum of elements present at even indices ; Traverse the given array ; If the index is even ; Update the caseOne ; Stores the maximum difference ; Stores the sum of elements placed at odd positions ; Stores the sum of elements placed at even positions ; Traverse the array ; If the index is even ; Update the caseTwo ; Return the maximum of caseOne and caseTwo ; Driver Code
using System ; public class GFG { static int Rotate ( int n , int f ) { int temp = n ; int maxi = n ; int mini = n ; for ( int idx = 0 ; idx < 7 ; idx ++ ) { if ( temp % 2 == 1 ) { temp >>= 1 ; temp += ( int ) Math . Pow ( 2 , 7 ) ; } else temp >>= 1 ; mini = Math . Min ( mini , temp ) ; maxi = Math . Max ( maxi , temp ) ; } if ( f == 1 ) return ( maxi ) ; else return ( mini ) ; } static int calcMinDiff ( int [ ] arr , int n ) { int caseOne = 0 ; int sumOfodd = 0 ; int sumOfeven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) sumOfodd += Rotate ( arr [ i ] , 0 ) ; else sumOfeven += Rotate ( arr [ i ] , 1 ) ; } caseOne = Math . Abs ( sumOfodd - sumOfeven ) ; int caseTwo = 0 ; sumOfodd = 0 ; sumOfeven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) sumOfodd += Rotate ( arr [ i ] , 1 ) ; else sumOfeven += Rotate ( arr [ i ] , 0 ) ; } caseTwo = Math . Abs ( sumOfodd - sumOfeven ) ; return Math . Max ( caseOne , caseTwo ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 123 , 86 , 234 , 189 } ; int n = arr . Length ; Console . WriteLine ( ( calcMinDiff ( arr , n ) ) ) ; } }
Array element with minimum sum of absolute differences | Set 2 | C # program for the above approach ; Function to find the element with minimum sum of differences between any elements in the array ; Stores the required X and sum of absolute differences ; Calculate sum of array elements ; The sum of absolute differences can 't be greater than sum ; Update res that gives the minimum sum ; If the current difference is less than the previous difference ; Update min_diff and res ; Print the resultant value ; Driver Code
using System ; class GFG { static void minimumDiff ( int [ ] arr , int N ) { int res = arr [ 0 ] , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; int min_diff = sum ; for ( int i = 0 ; i < N ; i ++ ) { if ( Math . Abs ( sum - ( arr [ i ] * N ) ) < min_diff ) { min_diff = Math . Abs ( sum - ( arr [ i ] * N ) ) ; res = arr [ i ] ; } } Console . Write ( res ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; minimumDiff ( arr , N ) ; } }
Sum of array elements possible by appending arr [ i ] / K to the end of the array K times for array elements divisible by K | C # program for the above approach ; Function to calculate sum of array elements after adding arr [ i ] / K to the end of the array if arr [ i ] is divisible by K ; Stores the sum of the array ; Stores the array elements ; Traverse the array ; Stores if the operation should be formed or not ; Traverse the vector V ; If flag is false and if v [ i ] is divisible by K ; Otherwise , set flag as true ; Increment the sum by v [ i % N ] ; Return the resultant sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int sum ( int [ ] arr , int N , int K ) { int sum = 0 ; List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { v . Add ( arr [ i ] ) ; } bool flag = false ; for ( int i = 0 ; i < v . Count ; i ++ ) { if ( ! flag && v [ i ] % K == 0 ) v . Add ( v [ i ] / K ) ; else { flag = true ; } sum = sum + v [ i % N ] ; } return sum ; } static void Main ( ) { int [ ] arr = { 4 , 6 , 8 , 2 } ; int K = 2 ; int N = arr . Length ; Console . WriteLine ( sum ( arr , N , K ) ) ; } }
Count array elements whose count of divisors is a prime number | C # program for the above approach ; Function to count the array elements whose count of divisors is prime ; Stores the maximum element ; Find the maximum element ; Store if i - th element is prime ( 0 ) or non - prime ( 1 ) ; Base Case ; If i is a prime number ; Mark all multiples of i as non - prime ; Stores the count of divisors ; Base Case ; Iterate to count factors ; Stores the count of array elements whose count of divisors is a prime number ; Traverse the array arr [ ] ; If count of divisors is prime ; Return the resultant count ; Driver Code
using System ; class GFG { static int primeDivisors ( int [ ] arr , int N ) { int K = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { K = Math . Max ( K , arr [ i ] ) ; } int [ ] prime = new int [ K + 1 ] ; prime [ 0 ] = 1 ; prime [ 1 ] = 1 ; for ( int i = 2 ; i < K + 1 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j < K + 1 ; j += i ) { prime [ j ] = 1 ; } } } int [ ] factor = new int [ K + 1 ] ; factor [ 0 ] = 0 ; factor [ 1 ] = 1 ; for ( int i = 2 ; i < K + 1 ; i ++ ) { factor [ i ] += 1 ; for ( int j = i ; j < K + 1 ; j += i ) { factor [ j ] += 1 ; } } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( prime [ factor [ arr [ i ] ] ] == 0 ) count ++ ; } return count ; } public static void Main ( ) { int [ ] arr = { 10 , 13 , 17 , 25 } ; int N = arr . Length ; Console . WriteLine ( primeDivisors ( arr , N ) ) ; } }
Count prime numbers up to N that can be represented as a sum of two prime numbers | C # program for the above approach ; Function to store all prime numbers up to N using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is prime ; Set all multiples of p as non - prime ; Function to count prime numbers up to N that can be represented as the sum of two prime numbers ; Stores all the prime numbers ; Update the prime array ; Create a dp array of size n + 1 ; Update dp [ 1 ] = 0 ; Iterate over the range [ 2 , N ] ; Add the previous count value ; Increment dp [ i ] by 1 if i and ( i - 2 ) are both prime ; Print the result ; Driver code
using System ; class GFG { static void SieveOfEratosthenes ( int n , bool [ ] prime ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) { prime [ i ] = false ; } } } } static void countPrime ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( n , prime ) ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) { dp [ i ] = 0 ; } dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] += dp [ i - 1 ] ; if ( prime [ i ] == true && prime [ i - 2 ] == true ) { dp [ i ] ++ ; } } Console . Write ( dp [ n ] ) ; } static void Main ( ) { int N = 6 ; countPrime ( N ) ; } }
Minimize sum of an array having Bitwise AND of all its pairs present in a given matrix | C # program for the above approach ; Function to find the minimum sum of the array such that Bitwise AND of arr [ i ] ana arr [ j ] is mat [ i ] [ j ] ; Stores the minimum possible sum ; Traverse the range [ 0 , N - 1 ] ; Stores the Bitwise OR of all the element of a row ; Traverse the range [ 0 , N - 1 ] ; If i not equal to j ; Update the value of res ; Increment the sum by res ; Return minimum possible sum ; Driver Code
using System ; class GFG { static int findMinSum ( int [ , ] mat , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int res = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( i != j ) { res |= mat [ i , j ] ; } } sum += res ; } return sum ; } public static void Main ( string [ ] args ) { int [ , ] mat = { { - 1 , 2 , 3 } , { 9 , - 1 , 7 } , { 4 , 5 , - 1 } } ; int N = mat . GetLength ( 0 ) ; Console . WriteLine ( findMinSum ( mat , N ) ) ; } }
LCM of unique elements present in an array | C # program for the above approach ; Function to find GCD of two numbers ; Base Case ; Recursively find the GCD ; Function to find LCM of two numbers ; Function to find LCM of unique elements present in the array ; Stores the frequency of each number of the array ; Store the frequency of each element of the array ; Store the required result ; Traverse the map freq ; If the frequency of the current element is 1 , then update ans ; If there is no unique element , set lcm to - 1 ; Print the result ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int findGCD ( int a , int b ) { if ( b == 0 ) return a ; return findGCD ( b , a % b ) ; } static int findLCM ( int a , int b ) { return ( a * b ) / findGCD ( a , b ) ; } static void uniqueElementsLCM ( int [ ] arr , int N ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) freq [ arr [ i ] ] ++ ; else freq . Add ( arr [ i ] , 1 ) ; } int lcm = 1 ; foreach ( KeyValuePair < int , int > kvp in freq ) { if ( kvp . Value == 1 ) { lcm = findLCM ( lcm , kvp . Key ) ; } } if ( lcm == 1 ) lcm = - 1 ; Console . Write ( lcm ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 1 , 3 , 3 , 4 } ; int N = arr . Length ; uniqueElementsLCM ( arr , N ) ; } }
Minimize maximum difference between adjacent elements possible by removing a single array element | C # program to implement the above approach ; Function to find maximum difference between adjacent array elements ; Store the maximum difference ; Traverse the array ; Update maximum difference ; Function to calculate the minimum of maximum difference between adjacent array elements possible by removing a single array element ; Stores the required minimum ; Stores the updated array ; Skip the i - th element ; Update MinValue ; Return MinValue ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maxAdjacentDifference ( List < int > A ) { int diff = 0 ; for ( int i = 1 ; i < A . Count ; i ++ ) { diff = Math . Max ( diff , A [ i ] - A [ i - 1 ] ) ; } return diff ; } static int MinimumValue ( int [ ] arr , int N ) { int MinValue = Int32 . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { List < int > new_arr = new List < int > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) continue ; new_arr . Add ( arr [ j ] ) ; } MinValue = Math . Min ( MinValue , maxAdjacentDifference ( new_arr ) ) ; } return MinValue ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 7 , 8 } ; int N = arr . Length ; Console . WriteLine ( MinimumValue ( arr , N ) ) ; } }
Number of Antisymmetric Relations on a set of N elements | C # program for the above approach ; Function to calculate the value of x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of antisymmetric relations in a set consisting of N elements ; Print the count of antisymmetric relations ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int antisymmetricRelation ( int N ) { return ( power ( 2 , N ) * power ( 3 , ( N * N - N ) / 2 ) ) % mod ; } public static void Main ( ) { int N = 2 ; Console . Write ( antisymmetricRelation ( N ) ) ; } }
Number of Asymmetric Relations on a set of N elements | C # program for the above approach ; Function to calculate x ^ y modulo ( 10 ^ 9 + 7 ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the final value of x ^ y ; Function to count the number of asymmetric relations in a set consisting of N elements ; Return the resultant count ; Driver Code
using System ; class GFG { const int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int asymmetricRelation ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } public static void Main ( string [ ] args ) { int N = 2 ; Console . WriteLine ( asymmetricRelation ( N ) ) ; } }