codeparrot/xlcost-text-to-code huggingface dataset:
Count Pronic numbers from a given range | C # program for the above approach ; Function to count pronic numbers in the range [ A , B ] ; Check upto sqrt N ; If product of consecutive numbers are less than equal to num ; Return N - 1 ; Function to count pronic numbers in the range [ A , B ] ; Subtract the count of pronic numbers which are <= ( A - 1 ) from the count f pronic numbers which are <= B ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]
using System ; public class GFG { static int pronic ( int num ) { int N = ( int ) Math . Sqrt ( num ) ; if ( N * ( N + 1 ) <= num ) { return N ; } return N - 1 ; } static int countPronic ( int A , int B ) { return pronic ( B ) - pronic ( A - 1 ) ; } public static void Main ( String [ ] args ) { int A = 3 ; int B = 20 ; Console . Write ( countPronic ( A , B ) ) ; } }
Count integers from a given range with no odd divisors | C # program to implement the above approach ; Function to count integers in the range 1 to N having no odd divisors ; Traverse the array ; Stores the nearest power of two less than arr [ i ] ; Stores the count of integers with no odd divisor for each query ; Iterate until powerOfTwo is less then or equal to arr [ i ] ; Print the answer for the current element ; Driver Code ; Given array ; Size of the array
using System ; class GFG { static void oddDivisors ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int powerOfTwo = 2 ; int count = 0 ; while ( powerOfTwo <= arr [ i ] ) { count ++ ; powerOfTwo = 2 * powerOfTwo ; } Console . Write ( count + " ▁ " ) ; } return ; } public static void Main ( String [ ] args ) { int [ ] arr = { 15 , 16 , 20 , 35 } ; int N = arr . Length ; oddDivisors ( arr , N ) ; } }
Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum | C # program to implement the above approach ; Function to check if it is possible to convert the array ; Traverse the array range [ 1 , N - 1 ] ; If arr [ i ] < arr [ i - 1 ] ; Otherwise ; Decrement arr [ i ] by arr [ i - 1 ] ; If arr [ n - 1 ] is not equal to zero ; Otherwise ; Driver Code ; Function Call
using System ; class GFG { static void checkPossible ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { Console . Write ( " No STRNEWLINE " ) ; return ; } else { arr [ i ] -= arr [ i - 1 ] ; arr [ i - 1 ] = 0 ; } } if ( arr [ n - 1 ] == 0 ) { Console . Write ( " Yes STRNEWLINE " ) ; } else { Console . Write ( " No STRNEWLINE " ) ; } } public static void Main ( ) { int [ ] arr = { 2 , 3 , 3 , 4 , 2 } ; int N = arr . Length ; checkPossible ( arr , N ) ; } }
Nearest smaller power of 2 for every digit of a number | C # program to implement the above approach ; Function to find the nearest power of two for every digit of a given number ; Converting number to string ; Traverse the array ; Calculate log base 2 of the current digit s [ i ] ; Highest power of 2 <= s [ i ] ; ASCII conversion ; Driver Code
using System ; class GFG { static void highestPowerOfTwo ( int num ) { String s = num . ToString ( ) ; for ( int i = 0 ; i < ( int ) s . Length ; i ++ ) { if ( s [ i ] == '0' ) { Console . Write ( "0" ) ; continue ; } int lg = ( int ) ( Math . Log ( s [ i ] - '0' ) / Math . Log ( 2 ) ) ; int p = ( int ) Math . Pow ( 2 , lg ) ; Console . Write ( ( char ) ( p + 48 ) ) ; } } public static void Main ( ) { int num = 4317 ; highestPowerOfTwo ( num ) ; } }
Maximum prefix sum after K reversals of a given array | C # program for the above approach ; Function to find the maximum prefix sum after K reversals of the array ; Stores the required sum ; If K is odd , reverse the array ; Store current prefix sum of array ; Traverse the array arr [ ] ; Add arr [ i ] to currsum ; Update maximum prefix sum till now ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static void maxSumAfterKReverse ( int [ ] arr , int K , int N ) { int sum = Int32 . MinValue ; if ( K % 2 != 0 ) Array . Reverse ( arr ) ; int currsum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { currsum += arr [ i ] ; sum = Math . Max ( sum , currsum ) ; } Console . Write ( sum ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 5 , 8 , 9 , 11 , 2 } ; int K = 1 ; int N = arr . Length ; maxSumAfterKReverse ( arr , K , N ) ; } }
Print all submasks of a given mask | C # program for the above approach ; Function to print the submasks of N ; Driver Code
using System ; class GFG { static void SubMasks ( int N ) { for ( int S = N ; S > 0 ; S = ( S - 1 ) & N ) { Console . Write ( S + " ▁ " ) ; } } static public void Main ( ) { int N = 25 ; SubMasks ( N ) ; } }
Count pairs from a given range having equal Bitwise OR and XOR values | C # program for the above approach ; Function to calculate ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; The upper bound is 2 ^ N ; Stores the count of pairs ; Generate all possible pairs ; Find XOR of both integers ; Find OR of both integers ; If both are equal ; Increment count ; Print count % MOD ; Driver Code ; Function Call
using System ; 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 void countPairs ( int N ) { int high = power ( 2 , N ) ; int count = 0 ; for ( int i = 0 ; i < high ; i ++ ) { for ( int j = 0 ; j < high ; j ++ ) { int X = ( i ^ j ) ; int Y = ( i j ) ; if ( X == Y ) { count ++ ; } } } Console . WriteLine ( count % MOD ) ; } static public void Main ( ) { int N = 10 ; countPairs ( N ) ; } }
Count pairs from a given range having equal Bitwise OR and XOR values | C # program for the above approach ; Function to find the value of ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now , then update y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; Finding 3 ^ N % 10 ^ 9 + 7 ; Driver Code ; Function Call
using System ; public class GFG { static readonly int MOD = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % MOD ; y = y >> 1 ; x = ( x * x ) % MOD ; } return res ; } static void countPairs ( int N ) { Console . Write ( power ( 3 , N ) ) ; } public static void Main ( String [ ] args ) { int N = 10 ; countPairs ( N ) ; } }
Minimize swaps required to place largest and smallest array elements at first and last array indices | C # program for the above approach ; Function to find the minimum count of adjacent swaps to move largest and smallest element at the first and the last index of the array , respectively ; Stores the smallest array element ; Stores the smallest array element ; Stores the last occurrence of the smallest array element ; Stores the first occurrence of the largest array element ; Traverse the array [ ] arr ; If a [ i ] is less than min_element ; Update min_element ; Update min_ind ; If a [ i ] is greater than max_element ; Update max_element ; Update max_ind ; If max_ind is equal to min_ind ; Return 0 ; If max_ind is greater than min_ind ; Otherwise ; Driver Code ; Input ; Print the result
using System ; public class GFG { static int minimumMoves ( int [ ] a , int n ) { int min_element = int . MaxValue ; int max_element = int . MinValue ; int min_ind = - 1 ; int max_ind = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= min_element ) { min_element = a [ i ] ; min_ind = i ; } if ( a [ i ] > max_element ) { max_element = a [ i ] ; max_ind = i ; } } if ( max_ind == min_ind ) { return 0 ; } else if ( max_ind > min_ind ) { return max_ind + ( n - min_ind - 2 ) ; } else { return max_ind + n - min_ind - 1 ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 35 , 46 , 17 , 23 } ; int N = arr . Length ; Console . Write ( minimumMoves ( arr , N ) + " STRNEWLINE " ) ; } }
Find the index of the smallest element to be removed to make sum of array divisible by K | C # program to implement the above approach ; Function to find index of the smallest array element required to be removed to make sum divisible by K ; Stores sum of array elements ; Stores index of the smallest element removed from the array to make sum divisible by K ; Stores the smallest element removed from the array to make sum divisible by K ; Traverse the array , arr [ ] ; Update sum ; Traverse the array arr [ ] ; Calculate remaining sum ; If sum is divisible by K ; If res == - 1 or mini is greater than arr [ i ] ; Update res and mini ; Driver code
using System ; class GFG { static int findIndex ( int [ ] arr , int n , int K ) { int sum = 0 ; int res = - 1 ; int mini = ( int ) 1e9 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { int temp = sum - arr [ i ] ; if ( temp % K == 0 ) { if ( res == - 1 mini > arr [ i ] ) { res = i + 1 ; mini = arr [ i ] ; } } } return res ; } static void Main ( ) { int [ ] arr = { 14 , 7 , 8 , 2 , 4 } ; int K = 7 ; int N = arr . Length ; Console . WriteLine ( findIndex ( arr , N , K ) ) ; } }
Count subarrays having a single distinct element that can be obtained from a given array | C # program for the above approach ; Function to count subarrays of single distinct element into which given array can be split ; Stores the count ; Stores frequency of array elements ; Traverse the array ; Traverse the map ; Increase count of subarrays by ( frequency - 1 ) ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void divisionalArrays ( int [ ] arr , int N ) { int sum = N ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > x in mp ) { if ( ( int ) x . Value > 1 ) { sum += ( int ) x . Value - 1 ; } } Console . WriteLine ( sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 3 } ; int N = arr . Length ; divisionalArrays ( arr , N ) ; } }
Count inversions in a sequence generated by appending given array K times | C # program to implement the above approach ; Function to count the number of inversions in K copies of given array ; Stores count of inversions in the given array ; Stores the count of pairs of distinct array elements ; Traverse the array ; Generate each pair ; Check for each pair , if the condition is satisfied or not ; If pairs consist of distinct elements ; Count inversiosn in the sequence ; Print the answer ; Driver Code ; Given array ; Given K ; Size of the array
using System ; class GFG { static void totalInversions ( int [ ] arr , int K , int N ) { int inv = 0 ; int X = 0 ; int i , j ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] && i < j ) inv ++ ; if ( arr [ i ] > arr [ j ] ) X ++ ; } } int totalInv = X * K * ( K - 1 ) / 2 + inv * K ; Console . WriteLine ( totalInv ) ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 3 } ; int K = 3 ; int N = arr . Length ; totalInversions ( arr , K , N ) ; } }
Count ways to generate an array having distinct elements at M consecutive indices | C # program for the above approach ; Modular function to calculate factorial ; Stores factorial of N ; Iterate over the range [ 1 , N ] ; Update result ; Function to count ways to replace array elements having 0 s with non - zero elements such that any M consecutive elements are distinct ; Store m consecutive distinct elements such that arr [ i ] is equal to B [ i % M ] ; Stores frequency of array elements ; Traverse the array arr [ ] ; If arr [ i ] is non - zero ; If B [ i % M ] is equal to 0 ; Update B [ i % M ] ; Update frequency of arr [ i ] ; If a duplicate element found in M consecutive elements ; Handling the case of inequality ; Stores count of 0 s in B [ ] ; Traverse the array , B [ ] ; If B [ i ] is 0 ; Update cnt ; Calculate factorial ; Driver Code ; Given M ; Given array ; Size of the array ; Function Call
using System ; class GFG { static int Fact ( int N ) { int result = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { result = ( result * i ) ; } return result ; } static void numberOfWays ( int M , int [ ] arr , int N ) { int [ ] B = new int [ M ] ; int [ ] counter = new int [ M + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != 0 ) { if ( B [ i % M ] == 0 ) { B [ i % M ] = arr [ i ] ; counter [ arr [ i ] ] ++ ; if ( counter [ arr [ i ] ] > 1 ) { Console . WriteLine ( 0 ) ; return ; } } else if ( B [ i % M ] != arr [ i ] ) { Console . WriteLine ( 0 ) ; return ; } } } int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( B [ i ] == 0 ) { cnt ++ ; } } Console . WriteLine ( Fact ( cnt ) ) ; } static public void Main ( ) { int M = 4 ; int [ ] arr = new int [ ] { 1 , 0 , 3 , 0 , 0 } ; int N = arr . Length ; numberOfWays ( M , arr , N ) ; } }
Maximize sum by traversing diagonally from each cell of a given Matrix | C # program for the above approach ; Function to find the maximum sum ; Loop to traverse through the upper triangular matrix and update the maximum sum to ans ; Traverse through the lower triangular matrix ; Driver Code ; Given matrix ; Given dimension
using System ; class GFG { static int MaximumSum ( int [ , ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int x = 0 , y = i , sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ x ++ , y ++ ] ; } if ( sum > ans ) ans = sum ; } for ( int i = 1 ; i < n ; i ++ ) { int x = i , y = 0 , sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ x ++ , y ++ ] ; } if ( sum > ans ) ans = sum ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 } , { 3 , 5 , 10 } , { 1 , 3 , 5 } } ; int n = arr . GetLength ( 0 ) ; Console . Write ( MaximumSum ( arr , n ) ) ; } }
Count array elements exceeding all previous elements as well as the next array element | C # program for the above approach ; Function to count array elements satisfying the given condition ; If there is only one array element ; Traverse the array ; Update the maximum element encountered so far ; Count the number of array elements strictly greater than all previous and immediately next elements ; Print the count ; Driver Code ; Given array ; Size of the array
using System ; class GFG { static void numberOfIntegers ( int [ ] arr , int N ) { int cur_max = 0 , count = 0 ; if ( N == 1 ) { count = 1 ; } else { for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] > cur_max ) { cur_max = arr [ i ] ; if ( arr [ i ] > arr [ i + 1 ] ) { count ++ ; } } } if ( arr [ N - 1 ] > cur_max ) count ++ ; } Console . WriteLine ( count ) ; } static public void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 0 , 7 , 2 , 0 , 2 , 0 } ; int N = arr . Length ; numberOfIntegers ( arr , N ) ; } }
Count ways to represent N as sum of powers of 2 | C # program to implement the above approach ; Base Cases ; Check if 2 ^ k can be used as one of the numbers or not ; Otherwise ; Count number of ways to N using 2 ^ k - 1 ; Driver code
using System ; class GFG { static int numberOfWays ( int n , int k ) { if ( n == 0 ) return 1 ; if ( k == 0 ) return 1 ; if ( n >= ( int ) Math . Pow ( 2 , k ) ) { int curr_val = ( int ) Math . Pow ( 2 , k ) ; return numberOfWays ( n - curr_val , k ) + numberOfWays ( n , k - 1 ) ; } else return numberOfWays ( n , k - 1 ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int k = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; Console . WriteLine ( numberOfWays ( n , k ) ) ; } }
Count ways to obtain triplets with positive product consisting of at most one negative element | C # Program to implement the above approach ; Function to calculate possible number of triplets ; counting frequency of positive numbers in array ; If current array element is positive ; Increment frequency ; Select a triplet from freq elements such that i < j < k . ; Driver Code
using System ; public class GFG { static int possibleTriplets ( int [ ] arr , int N ) { int freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { freq ++ ; } } return ( int ) ( ( freq * 1L * ( freq - 1 ) * ( freq - 2 ) ) / 6 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , - 9 , - 3 , 6 } ; int N = arr . Length ; Console . Write ( possibleTriplets ( arr , N ) ) ; } }
Maximize difference between sum of even and odd | C # program to implement the above approach ; Function to find the maximum possible difference between sum of even and odd indices ; Convert [ ] arr into 1 - based indexing ; Reverse the array ; Convert [ ] arr into 1 based index ; Reverse the array ; Stores maximum difference between sum of even and odd indexed elements ; Traverse the array ; If arr [ i ] is local maxima ; Update maxDiff ; If arr [ i ] is local minima ; Update maxDiff ; Driver Code ; Size of array ; Function Call
using System ; using System . Collections . Generic ; public class GFG { static void maxPossibleDiff ( List < int > arr , int N ) { arr . Add ( - 1 ) ; arr . Reverse ( ) ; arr . Add ( - 1 ) ; arr . Reverse ( ) ; int maxDiff = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { maxDiff += arr [ i ] ; } if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { maxDiff -= arr [ i ] ; } } Console . Write ( maxDiff ) ; } public static void Main ( String [ ] args ) { int [ ] array = { 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 } ; List < int > v = new List < int > ( ) ; foreach ( int i in array ) { v . Add ( i ) ; } int N = v . Count ; maxPossibleDiff ( v , N ) ; } }
Reverse all elements of given circular array starting from index K | C # program for the above approach ; Function to print the array [ ] arr ; Print the array ; Function to reverse elements of given circular array starting from index k ; Initialize two variables as start = k and end = k - 1 ; Initialize count = N / 2 ; Loop while count > 0 ; Swap the elements at index ( start % N ) and ( end % N ) ; Update the start and end ; If end equals to - 1 set end = N - 1 ; Print the circular array ; Driver Code ; Function Call
using System ; class GFG { static void printArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static void reverseCircularArray ( int [ ] arr , int N , int K ) { int start = K , end = K - 1 ; int count = N / 2 ; while ( count != 0 ) { int temp = arr [ start % N ] ; arr [ start % N ] = arr [ end % N ] ; arr [ end % N ] = temp ; start ++ ; end -- ; if ( end == - 1 ) { end = N - 1 ; } count -= 1 ; } printArray ( arr , N ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 2 , 4 , 1 } ; int K = 2 ; int N = arr . Length ; reverseCircularArray ( arr , N , K ) ; } }
Generate a Matrix such that given Matrix elements are equal to Bitwise OR of all corresponding row and column elements of generated Matrix | C # program for the above approach ; Function to find the matrix , A [ ] [ ] satisfying the given conditions ; Store the final matrix ; Initialize all the elements of the matrix A with 1 ; Traverse the matrix B [ ] [ ] row - wise ; If B [ i ] [ j ] is equal to 0 ; Mark all the elements of ith row of A [ ] [ ] as 0 ; Mark all the elements of jth column of A [ ] [ ] as 0 ; Check if the matrix B [ ] [ ] can be made using matrix A [ ] [ ] ; Store the bitwise OR of all elements of A [ ] [ ] in ith row and jth column ; Traverse through ith row ; Traverse through jth column ; If B [ i ] [ j ] is not equal to c , print " Not ▁ Possible " ; Print the final matrix A [ ] [ ] ; Driver Code ; Function Call
using System ; class GFG { static void findOriginalMatrix ( int [ , ] B , int N , int M ) { int [ , ] A = new int [ N , M ] ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { A [ i , j ] = 1 ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { if ( B [ i , j ] == 0 ) { for ( int k = 0 ; k < M ; ++ k ) { A [ i , k ] = 0 ; } for ( int k = 0 ; k < N ; ++ k ) { A [ k , j ] = 0 ; } } } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { int c = 0 ; for ( int k = 0 ; k < M ; ++ k ) { if ( c == 1 ) break ; c += A [ i , k ] ; } for ( int k = 0 ; k < N ; ++ k ) { if ( c == 1 ) break ; c += A [ k , j ] ; } if ( c != B [ i , j ] ) { Console . WriteLine ( " Not ▁ Possible " ) ; return ; } } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { Console . Write ( A [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int [ , ] B = new int [ , ] { { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int N = B . GetLength ( 0 ) ; int M = B . GetLength ( 1 ) ; findOriginalMatrix ( B , N , M ) ; } }
Minimize cost to make X and Y equal by given increments | C # program for the above approach ; Function to find gcd of x and y ; Function to find lcm of x and y ; Function to find minimum Cost ; Subtracted initial cost of x ; Subtracted initial cost of y ; Driver Code ; Returns the minimum cost required
using System ; class GFG { static int gcd ( int x , int y ) { if ( y == 0 ) return x ; return gcd ( y , x % y ) ; } static int lcm ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } static int minimumCost ( int x , int y ) { int lcm_ = lcm ( x , y ) ; int costx = ( lcm_ - x ) / x ; int costy = ( lcm_ - y ) / y ; return costx + costy ; } public static void Main ( String [ ] args ) { int x = 5 , y = 17 ; Console . Write ( minimumCost ( x , y ) + " STRNEWLINE " ) ; } }
Find GCD between the sum of two given integers raised to the power of N and their difference | C # program for the above approach ; Function to find the value of ( a ^ n ) % d ; Stores the value of ( a ^ n ) % d ; Calculate the value of ( a ^ n ) % d ; If n is odd ; Update res ; Update a ; Update n ; Function to find the GCD of ( p ^ n + q ^ n ) and p - q mod d ; If p and q are equal ; Stores GCD of ( p ^ n + q ^ n ) and ( p - q ) mod d ; Stores the value of ( p - q ) ; Stores square root of num ; Find the divisors of num . ; If i divides num ; Stores power of ( p ^ n ) mod i ; Stores power of ( q ^ n ) mod i ; Stores power of ( p ^ n + q ^ n ) mod i ; If ( p ^ n + q ^ n ) is divisible by i ; Calculate the largest divisor . ; If i divides num , ( num / i ) also divides num . Hence , calculate temp . ; If ( p ^ n + q ^ n ) is divisible by ( num / i ) ; Calculate the largest divisor . ; Driver code ; Given p , q and n ; Function Call
using System ; class GFG { static int mod = 1000000007 ; static int power ( int a , int n , int d ) { int res = 1 ; while ( n != 0 ) { if ( ( n % 2 ) != 0 ) { res = ( ( res % d ) * ( a % d ) ) % d ; } a = ( ( a % d ) * ( a % d ) ) % d ; n /= 2 ; } return res ; } static int gcd ( int p , int q , int n ) { if ( p == q ) { return ( power ( p , n , mod ) + power ( q , n , mod ) ) % mod ; } int candidate = 1 ; int num = p - q ; int sq = ( int ) Math . Sqrt ( num ) ; for ( int i = 1 ; i <= sq ; ++ i ) { if ( num % i == 0 ) { int X = power ( p , n , i ) ; int Y = power ( q , n , i ) ; int temp = ( X + Y ) % i ; if ( temp == 0 ) { candidate = Math . Max ( candidate , i ) ; } temp = ( power ( p , n , num / i ) + power ( q , n , num / i ) ) % ( num / i ) ; if ( temp == 0 ) { candidate = Math . Max ( candidate , num / i ) ; } } } return candidate % mod ; } static public void Main ( ) { int p , q , n ; p = 10 ; q = 6 ; n = 5 ; Console . WriteLine ( gcd ( p , q , n ) ) ; } }
Split given isosceles triangle of height H into N equal parts | C # Code for above approach ; Function to divide the isosceles triangle in equal parts by making N - 1 cuts parallel to the base ; Iterate over the range [ 1 , n - 1 ] ; Driver code ; Given N ; Given H ; Function call
using System ; class GFG { static void findPoint ( int n , int h ) { for ( int i = 1 ; i < n ; i ++ ) Console . Write ( " { 0 : F2 } ▁ " , Math . Sqrt ( i / ( n * 1.0 ) ) * h ) ; } public static void Main ( String [ ] args ) { int n = 3 ; int h = 2 ; findPoint ( n , h ) ; } }