codeparrot/xlcost-text-to-code huggingface dataset:
Length of the longest alternating even odd subarray | C # program to find the Length of the longest alternating even odd subarray ; Function to find the longest subarray ; Length of longest alternating subarray ; Iterate in the array ; Increment count if consecutive elements has an odd sum ; Store maximum count in longest ; Reinitialize cnt as 1 consecutive elements does not have an odd sum ; Length of ' longest ' can never be 1 since even odd has to occur in pair or more so return 0 if longest = 1 ; Driver code
using System ; class GFG { static int longestEvenOddSubarray ( int [ ] a , int n ) { int longest = 1 ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] + a [ i + 1 ] ) % 2 == 1 ) { cnt ++ ; } else { longest = Math . Max ( longest , cnt ) ; cnt = 1 ; } } if ( longest == 1 ) return 0 ; return Math . Max ( cnt , longest ) ; } static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 , 7 , 8 } ; int n = a . Length ; Console . WriteLine ( longestEvenOddSubarray ( a , n ) ) ; } }
Check whether given number N is a Moran Number or not | C # implementation to check if the number is Moran number ; Function to calculate digit sum ; Function to check if number is prime ; Function to check if number is moran number ; Calculate digit sum ; Check if n is completely divisible by digit sum ; Calculate the quotient ; Check if the number is prime ; Driver code
using System ; class GFG { static int digSum ( int a ) { int sum = 0 ; while ( a != 0 ) { sum += a % 10 ; a = a / 10 ; } return sum ; } static bool isPrime ( int r ) { bool s = true ; for ( int i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } static void moranNo ( int n ) { int dup = n ; int sum = digSum ( dup ) ; if ( n % sum == 0 ) { int c = n / sum ; if ( isPrime ( c ) ) { Console . Write ( " Yes " ) ; return ; } } Console . Write ( " No " ) ; } public static void Main ( ) { int n = 21 ; moranNo ( n ) ; } }
Program to check if N is a Hendecagonal Number | C # program for the above approach ; Function to check if N is a hendecagonal number ; Condition to check if the number is a hendecagonal number ; Driver code ; Given number ; Function call
using System ; class GFG { public static bool ishendecagonal ( int N ) { double n = ( 7 + Math . Sqrt ( 72 * N + 49 ) ) / 18 ; return ( n - ( int ) n ) == 0 ; } public static void Main ( string [ ] args ) { int N = 11 ; if ( ishendecagonal ( N ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No STRNEWLINE " ) ; } } }
Program to check if N is a Hexadecagonal Number | C # program for the above approach ; Function to check if N is a hexadecagonal number ; Condition to check if the number is a hexadecagonal number ; Driver code ; Given number ; Function call
using System ; class GFG { public static bool ishexadecagonal ( int N ) { double n = ( 12 + Math . Sqrt ( 112 * N + 144 ) ) / 28 ; return ( n - ( int ) n ) == 0 ; } public static void Main ( string [ ] args ) { int N = 16 ; if ( ishexadecagonal ( N ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Program to check if N is a Nonagonal Number | C # program for the above approach ; Function to check if N is a nonagonal number ; Condition to check if the number is a nonagonal number ; Driver code ; Given number ; Function call
using System ; class GFG { public static bool isnonagonal ( int N ) { double n = ( 5 + Math . Sqrt ( 56 * N + 25 ) ) / 14 ; return ( n - ( int ) n ) == 0 ; } public static void Main ( string [ ] args ) { int N = 9 ; if ( isnonagonal ( N ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Count of decreasing pairs formed from numbers 1 to N | C # program to count decreasing pairs formed from numbers 1 to N ; Function to count the possible number of pairs ; if the number is even then the answer in ( N / 2 ) - 1 ; if the number is odd then the answer in N / 2 ; Driver code
using System ; class GFG { static void divParts ( int N ) { if ( N % 2 == 0 ) Console . WriteLine ( ( N / 2 ) - 1 ) ; else Console . WriteLine ( ( N / 2 ) ) ; } public static void Main ( ) { int N = 8 ; divParts ( N ) ; } }
Count the minimum steps to reach 0 from the given integer N | C # program to count the minimum steps to reach 0 from the given integer N ; Function returns min step to reach 0 from N ; Direct possible reduction of value N ; Remaining steps needs to be reduced by 1 ; Summation of both the values ; Return the final answer ; Driver code
using System ; class GFG { static int getMinSteps ( int n , int jump ) { int quotient = n / jump ; int remainder = n % jump ; int steps = quotient + remainder ; return steps ; } public static void Main ( string [ ] args ) { int N = 6 , K = 3 ; Console . Write ( getMinSteps ( N , K ) ) ; } }
Construct an Array of size N whose sum of cube of all elements is a perfect square | C # implementation of the above approach ; Function to construct an array of size N ; Prints the first N natural numbers ; Driver Code
using System ; class GFG { public static void constructArray ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 5 ; constructArray ( N ) ; } }
Program to check if N is a Centered Decagonal Number | C # implementation to check that a number is a centered decagonal number or not ; Function to check that the number is a centered decagonal number ; Condition to check if the number is a centered decagonal number ; Driver Code ; Function call
using System ; class GFG { static bool isCentereddecagonal ( int N ) { double n = ( 5 + Math . Sqrt ( 20 * N + 5 ) ) / 10 ; return ( n - ( int ) n ) == 0 ; } static public void Main ( ) { int n = 11 ; if ( isCentereddecagonal ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Count Sexy Prime Pairs in the given array | C # program to count Sexy Prime pairs in array ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are SPP ( Sexy Prime Pair ) or not ; Function to find SPP ( Sexy Prime Pair ) pairs from the given array ; Iterate through all pairs ; Increment count if SPP ( Sexy Prime Pair ) pair ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) pair
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 += 6 ) { if ( n % i == 0 || n % ( i + 6 ) == 0 ) { return false ; } } return true ; } static bool SexyPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . Abs ( n1 - n2 ) == 6 ) ; } static int countSexyPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( SexyPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 7 , 5 , 11 , 13 } ; int n = arr . Length ; Console . Write ( countSexyPairs ( arr , n ) ) ; } }
Find the K | C # program to Find the kth Permutation Sequence of first n natural numbers ; Function to find the index of number at first position of kth sequence of set of size n ; n_actual_fact = n ! ; First position of the kth sequence will be occupied by the number present at index = k / ( n - 1 ) ! ; Function to find the kth permutation of n numbers ; Store readonly answer ; Insert all natural number upto n in set ; Mark the first position ; Subtract 1 to get 0 based indexing ; itr now points to the number at index in set s ; Remove current number from the set ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findFirstNumIndex ( int k , int n ) { if ( n == 1 ) return 0 ; n -- ; int first_num_index ; int n_partial_fact = n ; while ( k >= n_partial_fact && n > 1 ) { n_partial_fact = n_partial_fact * ( n - 1 ) ; n -- ; } first_num_index = k / n_partial_fact ; k = k % n_partial_fact ; return first_num_index ; } static String findKthPermutation ( int n , int k ) { String ans = " " ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) s . Add ( i ) ; List < int > v = new List < int > ( s ) ; int itr = v [ 0 ] ; k = k - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int index = findFirstNumIndex ( k , n - i ) ; if ( index < v . Count ) { ans += ( ( v [ index ] . ToString ( ) ) ) ; v . RemoveAt ( index ) ; } else ans += String . Join ( " " , itr + 2 ) ; itr = v [ 0 ] ; } return ans ; } public static void Main ( String [ ] args ) { int n = 3 , k = 4 ; String kth_perm_seq = findKthPermutation ( n , k ) ; Console . Write ( kth_perm_seq + " STRNEWLINE " ) ; } }
Find the Largest N digit perfect square number in Base B | C # implementation to find largest N digit perfect square number in base B ; Function to find the largest N digit number ; Largest n - digit perfect square ; Print the result ; Driver code
using System ; class GFG { static double nDigitPerfectSquares ( int n , int b ) { double largest = Math . Pow ( Math . Ceiling ( Math . Sqrt ( Math . Pow ( b , n ) ) ) - 1 , 2 ) ; return largest ; } public static void Main ( String [ ] args ) { int N = 1 , B = 8 ; Console . WriteLine ( nDigitPerfectSquares ( N , B ) ) ; } }
Find Cube root of a number using Log function | C # program to find cube root of a number using logarithm ; Function to find the cube root ; Calculate the cube root ; Return the readonly answer ; Driver code
using System ; class GFG { static double cubeRoot ( double n ) { double ans = Math . Pow ( 3 , ( ( 1.0 / 3 ) * ( Math . Log ( n ) / Math . Log ( 3 ) ) ) ) ; return ans ; } public static void Main ( String [ ] args ) { double N = 8 ; Console . Write ( " { 0 : F2 } " , cubeRoot ( N ) ) ; } }
Find the maximum possible value for the given periodic function | C # program to find the maximum possible value for the given function ; Function to return the maximum value of f ( x ) ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int floorMax ( int A , int B , int N ) { int x = Math . Min ( B - 1 , N ) ; return ( A * x ) / B ; } public static void Main ( string [ ] args ) { int A = 11 , B = 10 , N = 9 ; Console . Write ( floorMax ( A , B , N ) ) ; } }
Minimum moves taken to move coin of each cell to any one cell of Matrix | C # program to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; Function to find the minimum number of moves taken to move the element of each cell to any one cell of the square matrix of odd length ; Initializing count to 0 ; Number of layers that are around the centre element ; Iterating over ranger of layers ; Increase the value of count by 8 * k * k ; Driver code
using System ; class GFG { public static int calculateMoves ( int n ) { int count = 0 ; int layers = n / 2 ; for ( int k = 1 ; k < layers + 1 ; k ++ ) { count += 8 * k * k ; } return count ; } public static void Main ( ) { int N = 5 ; Console . Write ( calculateMoves ( N ) ) ; } }
Check if N can be represented as sum of squares of two consecutive integers | C # implementation to check that a number is sum of squares of 2 consecutive numbers or not ; Function to check that the a number is sum of squares of 2 consecutive numbers or not ; Condition to check if the a number is sum of squares of 2 consecutive numbers or not ; Driver code ; Function call
using System ; class GFG { public static bool isSumSquare ( int N ) { double n = ( 2 + Math . Sqrt ( 8 * N - 4 ) ) / 2 ; return ( n - ( int ) n ) == 0 ; } public static void Main ( String [ ] args ) { int i = 13 ; if ( isSumSquare ( i ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Program to check if N is a Centered heptagonal number | C # implementation to check that a number is a centered heptagonal number or not ; Function to check that the number is a centered heptagonal number ; Condition to check if the number is a centered heptagonal number ; Driver Code ; Function call
using System ; class GFG { static bool isCenteredheptagonal ( int N ) { double n = ( 7 + Math . Sqrt ( 56 * N - 7 ) ) / 14 ; return ( n - ( int ) n ) == 0 ; } static public void Main ( ) { int n = 8 ; if ( isCenteredheptagonal ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Program to check if N is a Centered nonadecagonal number | C # implementation to check that a number is a Centered nonadecagonal number or not ; Function to check that the number is a Centered nonadecagonal number ; Condition to check if the number is a Centered nonadecagonal number ; Driver Code ; Function call
using System ; class GFG { static bool isCenterednonadecagonal ( int N ) { float n = ( float ) ( ( 19 + Math . Sqrt ( 152 * N + 209 ) ) / 38 ) ; return ( n - ( int ) n ) == 0 ; } public static void Main ( ) { int n = 20 ; if ( isCenterednonadecagonal ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Program to check if N is a Centered Octadecagonal number | C # implementation to check that a number is a centered octadecagonal number or not ; Function to check that the number is a centered octadecagonal number ; Implement the formula generated ; Condition to check if the number is a Centered octadecagonal number ; Driver Code ; Function call
using System ; class GFG { static bool isCenteredOctadecagonal ( int N ) { double n = ( 9 + Math . Sqrt ( 36 * N + 45 ) ) / 18 ; return ( n - ( int ) n ) == 0 ; } static public void Main ( ) { int n = 19 ; if ( isCenteredOctadecagonal ( n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Logarithm tricks for Competitive Programming | C # implementation to find Kth root of the number ; Function to find the Kth root of the number ; Driver Code
using System ; class GFG { static double kthRoot ( double n , int k ) { return Math . Pow ( k , ( 1.0 / k ) * ( Math . Log ( n ) / Math . Log ( k ) ) ) ; } public static void Main ( ) { double N = 8.0 ; int K = 3 ; Console . Write ( kthRoot ( N , K ) ) ; } }
Logarithm tricks for Competitive Programming | C # implementation to check if the number is power of K ; Function to count the number of digits in a number ; Logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Driver Code
using System ; class GFG { static bool isPower ( int N , int K ) { int res1 = ( int ) ( Math . Log ( N ) / Math . Log ( K ) ) ; double res2 = Math . Log ( N ) / Math . Log ( K ) ; return ( res1 == res2 ) ; } public static void Main ( ) { int N = 8 ; int K = 2 ; if ( isPower ( N , K ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Count of subarrays which start and end with the same element | C # program to Count total sub - array which start and end with same element ; function to find total sub - array which start and end with same element ; initialize result with 0 ; array to count frequency of 1 to N ; update frequency of A [ i ] ; update result with sub - array contributed by number i ; print the result ; Driver code
using System ; class GFG { public static void cntArray ( int [ ] A , int N ) { int result = 0 ; int [ ] frequency = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { frequency [ A [ i ] ] ++ ; } for ( int i = 1 ; i <= N ; i ++ ) { int frequency_of_i = frequency [ i ] ; result += ( ( frequency_of_i ) * ( frequency_of_i + 1 ) ) / 2 ; } Console . Write ( result ) ; } public static void Main ( ) { int [ ] A = { 1 , 5 , 6 , 1 , 9 , 5 , 8 , 10 , 8 , 9 } ; int N = A . Length ; cntArray ( A , N ) ; } }
Length of array pair formed where one contains all distinct elements and other all same elements | C # implementation to Divide the array into two arrays having same size , one with distinct elements and other with same elements ; Function to find the max size possible ; Counting the maximum frequency ; Counting total distinct elements ; Find max of both the answer ; Driver code
using System ; using System . Collections ; class GFG { static int findMaxSize ( int [ ] a , int n ) { ArrayList frq = new ArrayList ( n + 1 ) ; for ( int i = 0 ; i <= n ; i ++ ) frq . Add ( 0 ) ; for ( int i = 0 ; i < n ; ++ i ) frq [ a [ i ] ] = ( int ) frq [ a [ i ] ] + 1 ; int maxfrq = int . MinValue ; for ( int i = 0 ; i < frq . Count ; i ++ ) { if ( maxfrq < ( int ) frq [ i ] ) { maxfrq = ( int ) frq [ i ] ; } } int dist = n + 1 ; for ( int i = 0 ; i < frq . Count ; i ++ ) { if ( ( int ) frq [ i ] == 0 ) { dist -- ; } } int ans1 = Math . Min ( maxfrq - 1 , dist ) ; int ans2 = Math . Min ( maxfrq , dist - 1 ) ; int ans = Math . Max ( ans1 , ans2 ) ; return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 2 , 4 , 1 , 4 , 3 , 4 } ; int n = arr . Length ; Console . Write ( findMaxSize ( arr , n ) ) ; } }
Longest subarray with all elements same | C # program to find largest subarray with all equal elements . ; Function to find largest sub array with all equal elements . ; Traverse the array ; If element is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
using System ; class GFG { static int subarray ( int [ ] arr , int n ) { int ans = 1 , temp = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { ++ temp ; } else { ans = Math . Max ( ans , temp ) ; temp = 1 ; } } ans = Math . Max ( ans , temp ) ; return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 1 , 1 , 2 , 2 , 2 , 3 , 3 } ; int n = arr . Length ; Console . Write ( subarray ( arr , n ) ) ; } }
Min and max length subarray having adjacent element difference atmost K | C # program to find the minimum and maximum length subarray such that difference between adjacent elements is atmost K ; Function to find the maximum and minimum length subarray ; Initialise minimum and maximum size of subarray in worst case ; Left will scan the size of possible subarray in left of selected element ; Right will scan the size of possible subarray in right of selected element ; Temp will store size of group associateed with every element ; Loop to find size of subarray for every element of array ; Left will move in left direction and compare difference between itself and element left to it ; Right will move in right direction and compare difference between itself and element right to it ; If subarray of much lesser or much greater is found than yet known then update ; Print minimum and maximum possible size of subarray ; Driver code ; Function call to find maximum and minimum possible sub array
using System ; class GFG { static void findMaxMinSubArray ( int [ ] arr , int K , int n ) { int min = n ; int max = 0 ; int left ; int right ; int tmp ; for ( int i = 0 ; i < n ; i ++ ) { tmp = 1 ; left = i ; while ( left - 1 >= 0 && Math . Abs ( arr [ left ] - arr [ left - 1 ] ) <= K ) { left -- ; tmp ++ ; } right = i ; while ( right + 1 <= n - 1 && Math . Abs ( arr [ right ] - arr [ right + 1 ] ) <= K ) { right ++ ; tmp ++ ; } if ( min > tmp ) min = tmp ; if ( max < tmp ) max = tmp ; } Console . Write ( min ) ; Console . Write ( " , ▁ " ) ; Console . Write ( max ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 5 , 6 , 7 } ; int K = 2 ; int n = arr . Length ; findMaxMinSubArray ( arr , K , n ) ; } }
Count of elements which is the sum of a subarray of the given Array | C # implementation to count the elements such that their exist a subarray whose sum is equal to this element ; Function to count the elements such that their exist a subarray whose sum is equal to this element ; Loop to count the frequency ; Loop to iterate over every possible subarray of the array ; Driver Code
using System ; class GFG { static int countElement ( int [ ] arr , int n ) { int [ ] freq = new int [ n + 1 ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { int tmpsum = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { tmpsum += arr [ j ] ; if ( tmpsum <= n ) { ans += freq [ tmpsum ] ; freq [ tmpsum ] = 0 ; } } } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . Length ; Console . WriteLine ( countElement ( arr , n ) ) ; } }
Nth root of a number using log | C # implementation to find the Kth root of a number using log ; Function to find the Kth root of the number using log function ; Driver Code
using System ; class GFG { static double kthRoot ( double n , int k ) { return Math . Pow ( k , ( ( 1.0 / k ) * ( Math . Log ( n ) / Math . Log ( k ) ) ) ) ; } public static void Main ( String [ ] args ) { double n = 81 ; int k = 4 ; Console . Write ( " { 0 : F6 } " , kthRoot ( n , k ) ) ; } }
Make A , B and C equal by adding total value N to them | C # program to distribute integer N among A , B , C such that they become equal ; Find maximum among the three elements ; Summation of three elements ; Check if difference is divisible by 3 ; Driver code
using System ; class GFG { static void distributeN ( int A , int B , int C , int n ) { int max1 = Math . Max ( A , B ) ; int max2 = Math . Max ( B , C ) ; int maximum = Math . Max ( max1 , max2 ) ; int sum = A + B + C ; int p = ( 3 * maximum ) - sum ; int diff = n - p ; if ( diff < 0 diff % 3 == 0 ) Console . Write ( " No " ) ; else Console . Write ( " Yes " ) ; } public static void Main ( String [ ] args ) { int A = 10 , B = 20 ; int C = 15 , n = 14 ; distributeN ( A , B , C , n ) ; } }
Represent N as sum of K odd numbers with repetitions allowed | C # implementation to represent N as sum of K even numbers ; Function to print the representation ; N must be greater than equal to 2 * K and must be odd ; Driver Code
using System ; class GFG { static void sumOddNumbers ( int N , int K ) { int check = N - ( K - 1 ) ; if ( check > 0 && check % 2 == 1 ) { for ( int i = 0 ; i < K - 1 ; i ++ ) { Console . Write ( "1 ▁ " ) ; } Console . Write ( + check ) ; } else Console . WriteLine ( " - 1 ▁ " ) ; } public static void Main ( ) { int N = 5 ; int K = 3 ; sumOddNumbers ( N , K ) ; } }
Number of continuous reductions of A from B or B from A to make them ( 1 , 1 ) | C # implementation to find the minimum number of operations required to reach ( 1 , 1 ) ; Function to find the minimum number of steps required ; Condition to check if it is not possible to reach ; Condition to check if the pair is reached to 1 , 1 ; Condition to change the A as the maximum element ; Driver Code
using System ; class GFG { static int minimumSteps ( int a , int b , int c ) { if ( a < 1 b < 1 ) return - 1 ; if ( a == 1 && b == 1 ) return c ; if ( a < b ) { a = a + b ; b = a - b ; a = a - b ; } return minimumSteps ( a - b , b , c + 1 ) ; } public static void Main ( ) { int a = 75 ; int b = 17 ; Console . WriteLine ( minimumSteps ( a , b , 0 ) ) ; } }