codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs with average present in the same array | C # implementation of the approach ; Function to return the count of valid pairs ; Frequency array Twice the original size to hold negative elements as well ; Update the frequency of each of the array element ; If say x = - 1000 then we will place the frequency of - 1000 at ( - 1000 + 1000 = 0 ) a [ 0 ] index ; To store the count of valid pairs ; Remember we will check only for ( even , even ) or ( odd , odd ) pairs of indexes as the average of two consecutive elements is a floating point number ; Driver code
using System ; class GFG { static int N = 1000 ; static int countPairs ( int [ ] arr , int n ) { int size = ( 2 * N ) + 1 ; int [ ] freq = new int [ size ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; freq [ x + N ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( freq [ i ] > 0 ) { ans += ( ( freq [ i ] ) * ( freq [ i ] - 1 ) ) / 2 ; for ( int j = i + 2 ; j < 2001 ; j += 2 ) { if ( freq [ j ] > 0 && ( freq [ ( i + j ) / 2 ] > 0 ) ) { ans += ( freq [ i ] * freq [ j ] ) ; } } } } return ans ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 5 , 1 , 3 , 5 } ; int n = arr . Length ; Console . WriteLine ( countPairs ( arr , n ) ) ; } }
Smallest and Largest sum of two n | C # implementation of the approach ; Function to return the smallest sum of 2 n - digit numbers ; Function to return the largest sum of 2 n - digit numbers ; Driver code
using System ; class GFG { static int smallestSum ( int n ) { if ( n == 1 ) return 0 ; return ( 2 * ( int ) Math . Pow ( 10 , n - 1 ) ) ; } static int largestSum ( int n ) { return ( 2 * ( ( int ) Math . Pow ( 10 , n ) - 1 ) ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( " Largest ▁ = ▁ " + largestSum ( n ) ) ; Console . Write ( " Smallest ▁ = ▁ " + smallestSum ( n ) ) ; } }
Given two arrays count all pairs whose sum is an odd number | C # program to implement the above approach ; Function that returns the number of pairs ; Count of odd and even numbers ; Traverse in the first array and count the number of odd and evene numbers in them ; Traverse in the second array and count the number of odd and evene numbers in them ; Count the number of pairs ; Return the number of pairs ; Driver code
using System ; class GFG { static int count_pairs ( int [ ] a , int [ ] b , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { odd1 ++ ; } else { even1 ++ ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) { odd2 ++ ; } else { even2 ++ ; } } int pairs = Math . Min ( odd1 , even2 ) + Math . Min ( odd2 , even1 ) ; return pairs ; } static public void Main ( ) { int [ ] a = { 9 , 14 , 6 , 2 , 11 } ; int [ ] b = { 8 , 4 , 7 , 20 } ; int n = a . Length ; int m = b . Length ; Console . WriteLine ( count_pairs ( a , b , n , m ) ) ; } }
Print steps to make a number in form of 2 ^ X | C # program to implement the above approach ; Function to find the leftmost unset bit in a number . ; Function that perform the step ; Find the leftmost unset bit ; If the number has no bit unset , it means it is in form 2 ^ x - 1 ; Count the steps ; Iterate till number is of form 2 ^ x - 1 ; At even step increase by 1 ; Odd step xor with any 2 ^ m - 1 ; Find the leftmost unset bit ; 2 ^ m - 1 ; Perform the step ; Increase the steps ; Driver code
using System ; class GFG { static int find_leftmost_unsetbit ( int n ) { int ind = - 1 ; int i = 1 ; while ( n > 0 ) { if ( ( n % 2 ) != 1 ) { ind = i ; } i ++ ; n >>= 1 ; } return ind ; } static void perform_steps ( int n ) { int left = find_leftmost_unsetbit ( n ) ; if ( left == - 1 ) { Console . Write ( " No ▁ steps ▁ required " ) ; return ; } int step = 1 ; while ( find_leftmost_unsetbit ( n ) != - 1 ) { if ( step % 2 == 0 ) { n += 1 ; Console . WriteLine ( " Step " + step + " : ▁ Increase ▁ by ▁ 1" ) ; } else { int m = find_leftmost_unsetbit ( n ) ; int num = ( int ) ( Math . Pow ( 2 , m ) - 1 ) ; n = n ^ num ; Console . WriteLine ( " Step " + step + " : ▁ Xor ▁ with ▁ " + num ) ; } step += 1 ; } } static public void Main ( ) { int n = 39 ; perform_steps ( n ) ; } }
Determine the position of the third person on regular N sided polygon | C # implementation of the approach ; Function to find out the number of that vertices ; Another person can 't stand on  vertex on which 2 children stand. ; calculating minimum jumps from each vertex . ; Calculate sum of jumps . ; Driver code ; Calling function
using System ; using System . Collections . Generic ; class GFG { static int vertices ( int N , int A , int B ) { int position = 0 ; int minisum = int . MaxValue ; int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i == A i == B ) continue ; else { int x = Math . Abs ( i - A ) ; int y = Math . Abs ( i - B ) ; sum = x + y ; if ( sum < minisum ) { minisum = sum ; position = i ; } } } return position ; } public static void Main ( String [ ] args ) { int N = 3 , A = 1 , B = 2 ; Console . WriteLine ( " Vertex ▁ = ▁ " + vertices ( N , A , B ) ) ; } }
Find sum of factorials in an array | C # implementation of the approach ; Function to return the factorial of n ; Function to return the sum of factorials of the array elements ; To store the required sum ; Add factorial of all the elements ; Driver Code
using System ; class GFG { static int factorial ( int n ) { int f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { f *= i ; } return f ; } static int sumFactorial ( int [ ] arr , int n ) { int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += factorial ( arr [ i ] ) ; } return s ; } public static void Main ( ) { int [ ] arr = { 7 , 3 , 5 , 4 , 8 } ; int n = arr . Length ; Console . WriteLine ( sumFactorial ( arr , n ) ) ; } }
Minimum steps to color the tree with given colors | C # implementation of the approach ; To store the required answer ; To store the graph ; Function to add edges ; Dfs function ; When there is difference in colors ; For all it 's child nodes ; Driver code ; Here zero is for parent of node 1 ; Adding edges in the graph ; Dfs call ; Required answer
using System ; using System . Collections . Generic ; class GFG { static int ans = 0 ; static List < List < int > > gr = new List < List < int > > ( ) ; static void Add_Edge ( int u , int v ) { gr [ u ] . Add ( v ) ; gr [ v ] . Add ( u ) ; } static void dfs ( int child , int par , int [ ] color ) { if ( color [ child ] != color [ par ] ) ans ++ ; for ( int i = 0 ; i < gr [ child ] . Count ; i ++ ) { if ( gr [ child ] [ i ] == par ) continue ; dfs ( gr [ child ] [ i ] , child , color ) ; } } public static void Main ( String [ ] args ) { for ( int i = 0 ; i <= 10 ; i ++ ) gr . Add ( new List < int > ( ) ) ; int [ ] color = { 0 , 1 , 2 , 3 , 2 , 2 , 3 } ; Add_Edge ( 1 , 2 ) ; Add_Edge ( 1 , 3 ) ; Add_Edge ( 2 , 4 ) ; Add_Edge ( 2 , 5 ) ; Add_Edge ( 3 , 6 ) ; dfs ( 1 , 0 , color ) ; Console . WriteLine ( ans ) ; } }
Highest power of 2 that divides a number represented in binary | C # implementation of the approach ; Function to return the highest power of 2 which divides the given binary number ; To store the highest required power of 2 ; Counting number of consecutive zeros from the end in the given binary string ; Driver code
using System ; class GFG { static int highestPower ( String str , int len ) { int ans = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '0' ) ans ++ ; else break ; } return ans ; } public static void Main ( String [ ] args ) { String str = "100100" ; int len = str . Length ; Console . WriteLine ( highestPower ( str , len ) ) ; } }
Number of ways to arrange K different objects taking N objects at a time | C # implementation of the approach ; Function to return n ! % p ; long res = 1 ; Initialize result ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Returns n ^ ( - 1 ) mod p ; Returns nCr % p using Fermat 's little theorem. ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the number of ways to arrange K different objects taking N objects at a time ; Driver Code ; Function call
using System ; class GFG { static long mod = ( long ) ( 1e9 + 7 ) ; static long factorial ( long n , long p ) { for ( int i = 2 ; i <= n ; i ++ ) res = ( res * i ) % p ; return res ; } static long power ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long n , long p ) { return power ( n , p - 2 , p ) ; } static long nCrModP ( long n , long r , long p ) { if ( r == 0 ) return 1 ; long [ ] fac = new long [ ( int ) n + 1 ] ; fac [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ ( int ) n ] * modInverse ( fac [ ( int ) r ] , p ) % p * modInverse ( fac [ ( int ) n - ( int ) r ] , p ) % p ) % p ; } static long countArrangements ( long n , long k , long p ) { return ( factorial ( n , p ) * nCrModP ( k , n , p ) ) % p ; } public static void Main ( ) { long N = 5 , K = 8 ; Console . WriteLine ( countArrangements ( N , K , mod ) ) ; } }
Find maximum product of digits among numbers less than or equal to N | C # implementation of the approach ; Function that returns the maximum product of digits among numbers less than or equal to N ; Driver code
using System ; class GFG { static int maxProd ( int N ) { if ( N == 0 ) return 1 ; if ( N < 10 ) return N ; return Math . Max ( maxProd ( N / 10 ) * ( N % 10 ) , maxProd ( N / 10 - 1 ) * 9 ) ; } static public void Main ( ) { int N = 390 ; Console . WriteLine ( maxProd ( N ) ) ; } }
Check if a number from every row can be selected such that xor of the numbers is greater than zero | C # program to implement the above approach ; Function to check if a number from every row can be selected such that xor of the numbers is greater than zero ; Find the xor of first column for every row ; If Xorr is 0 ; Traverse in the matrix ; Check is atleast 2 distinct elements ; Driver code
using System ; class GFG { static int N = 2 ; static int M = 3 ; static bool check ( int [ , ] mat ) { int xorr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xorr ^= mat [ i , 0 ] ; } if ( xorr != 0 ) return true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( mat [ i , j ] != mat [ i , 0 ] ) return true ; } } return false ; } static void Main ( ) { int [ , ] mat = { { 7 , 7 , 7 } , { 10 , 10 , 7 } } ; if ( check ( mat ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Sum of the series 1 , 2 , 4 , 3 , 5 , 7 , 9 , 6 , 8 , 10 , 11 , 13. . till N | C # program to implement the above approach ; Function to find the sum of first N odd numbers ; Function to find the sum of first N even numbers ; Function to overall find the sum of series ; Initial odd numbers ; Initial even numbers ; First power of 2 ; Check for parity for odd / even ; Counts the sum ; Get the minimum out of remaining num or power of 2 ; Decrease that much numbers from num ; If the segment has odd numbers ; Summate the odd numbers By exclusion ; Increase number of odd numbers ; If the segment has even numbers ; Summate the even numbers By exclusion ; Increase number of even numbers ; Next set of numbers ; Change parity for odd / even ; Driver code
using System ; class GFG { static int sumodd ( int n ) { return ( n * n ) ; } static int sumeven ( int n ) { return ( n * ( n + 1 ) ) ; } static int findSum ( int num ) { int sumo = 0 ; int sume = 0 ; int x = 1 ; int cur = 0 ; int ans = 0 ; while ( num > 0 ) { int inc = Math . Min ( x , num ) ; num -= inc ; if ( cur == 0 ) { ans = ans + sumodd ( sumo + inc ) - sumodd ( sumo ) ; sumo += inc ; } else { ans = ans + sumeven ( sume + inc ) - sumeven ( sume ) ; sume += inc ; } x *= 2 ; cur ^= 1 ; } return ans ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . WriteLine ( findSum ( n ) ) ; } }
Find the nth term of the given series | C # implementation of the approach ; Function to return the nth term of the given series ; Driver code
using System ; class GFG { static int oddTriangularNumber ( int N ) { return ( N * ( ( 2 * N ) - 1 ) ) ; } public static void Main ( String [ ] args ) { int N = 3 ; Console . WriteLine ( oddTriangularNumber ( N ) ) ; } }
Check if given two straight lines are identical or not | C # program to check if given two straight lines are identical or not ; Function to check if they are identical ; Driver Code
using System ; class GFG { static void idstrt ( double a1 , double b1 , double c1 , double a2 , double b2 , double c2 ) { if ( ( a1 / a2 == b1 / b2 ) && ( a1 / a2 == c1 / c2 ) && ( b1 / b2 == c1 / c2 ) ) Console . WriteLine ( " The ▁ given ▁ straight " + " ▁ lines ▁ are ▁ identical " ) ; else Console . WriteLine ( " The ▁ given ▁ straight " + " ▁ lines ▁ are ▁ not ▁ identical " ) ; } public static void Main ( String [ ] args ) { double a1 = - 2 , b1 = 4 , c1 = 3 , a2 = - 6 , b2 = 12 , c2 = 9 ; idstrt ( a1 , b1 , c1 , a2 , b2 , c2 ) ; } }
Count different numbers possible using all the digits their frequency times | C # program to implement the above approach ; Initialize an array to store factorial values ; Function to calculate and store X ! values ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function that return modular inverse of x under modulo p ; Function that returns the count of different number possible by using along the digits its frequency times ; Preprocess factorial values ; Initialize the result and sum of aint the frequencies ; Calculate the sum of frequencies ; Putting res equal to x ! ; Multiplying res with modular inverse of X0 ! , X1 ! , . . , X9 ! ; Driver Code
using System ; class GFG { static int MAXN = 100000 ; static int MOD = 1000000007 ; static long [ ] fact = new long [ MAXN ] ; static void factorial ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; } static long power ( long x , long y , long p ) { while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long x , long p ) { return power ( x , p - 2 , p ) ; } static long countDifferentNumbers ( long [ ] arr , long P ) { factorial ( ) ; long res = 0 , X = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) X += arr [ i ] ; res = fact [ ( int ) X ] ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( arr [ i ] > 1 ) res = ( res * modInverse ( fact [ ( int ) arr [ i ] ] , P ) ) % P ; } return res ; } public static void Main ( String [ ] args ) { long [ ] arr = { 1 , 0 , 2 , 0 , 0 , 7 , 4 , 0 , 0 , 3 } ; Console . WriteLine ( countDifferentNumbers ( arr , MOD ) ) ; } }
Equation of straight line passing through a given point which bisects it into two equal line segments | C # implementation of the approach ; Function to print the equation of the required line ; Driver code
using System ; class GFG { static void line ( double x0 , double y0 ) { double c = ( int ) ( 2 * y0 * x0 ) ; Console . WriteLine ( y0 + " x " + " ▁ + ▁ " + x0 + " y ▁ = ▁ " + c ) ; } public static void Main ( String [ ] args ) { double x0 = 4 , y0 = 3 ; line ( x0 , y0 ) ; } }
Find the original matrix when largest element in a row and a column are given | C # implementation of the approach ; Function that prints the original matrix ; Iterate in the row ; Iterate in the column ; If previously existed an element ; Driver code
using System ; class GFG { static int N = 3 ; static int M = 7 ; static void printOriginalMatrix ( int [ ] a , int [ ] b , int [ , ] mat ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i , j ] == 1 ) Console . Write ( Math . Min ( a [ i ] , b [ j ] ) + " ▁ " ) ; else Console . Write ( "0" + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { int [ ] a = { 2 , 1 , 3 } ; int [ ] b = { 2 , 3 , 0 , 0 , 2 , 0 , 1 } ; int [ , ] mat = { { 1 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 , 0 } } ; printOriginalMatrix ( a , b , mat ) ; } }
Calculate the loss incurred in selling the given items at discounted price | C # implementation of the approach ; Function to return the x % of n ; Function to return the total loss ; To store the total loss ; Original price of the item ; The price at which the item will be sold ; The discounted price of the item ; Loss incurred ; Driver code ; Total items
using System ; class GFG { static float percent ( int n , int x ) { float p = n * x ; p /= 100 ; return p ; } static float getLoss ( int [ ] price , int [ ] quantity , int [ ] X , int n ) { float loss = 0 ; for ( int i = 0 ; i < n ; i ++ ) { float originalPrice = price [ i ] ; float sellingPrice = originalPrice + percent ( ( int ) originalPrice , X [ i ] ) ; float afterDiscount = sellingPrice - percent ( ( int ) sellingPrice , X [ i ] ) ; loss += ( ( originalPrice - afterDiscount ) * quantity [ i ] ) ; } return loss ; } public static void Main ( ) { int [ ] price = { 20 , 48 , 200 , 100 } ; int [ ] quantity = { 20 , 48 , 1 , 1 } ; int [ ] X = { 0 , 48 , 200 , 5 } ; int n = X . Length ; Console . Write ( getLoss ( price , quantity , X , n ) ) ; } }
Maximum difference between two elements in an Array | C # implementation of the approach ; Function to return the maximum absolute difference between any two elements of the array ; To store the minimum and the maximum elements from the array ; Driver code
using System ; class GFG { static int maxAbsDiff ( int [ ] arr , int n ) { int minEle = arr [ 0 ] ; int maxEle = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { minEle = Math . Min ( minEle , arr [ i ] ) ; maxEle = Math . Max ( maxEle , arr [ i ] ) ; } return ( maxEle - minEle ) ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( maxAbsDiff ( arr , n ) ) ; } }
Maximize the maximum subarray sum after removing atmost one element | C # implementation of the approach ; Function to return the maximum sub - array sum ; Initialized ; Traverse in the array ; Increase the sum ; If sub - array sum is more than the previous ; If sum is negative ; Function that returns the maximum sub - array sum after removing an element from the same sub - array ; Maximum sub - array sum using Kadane 's Algorithm ; Re - apply Kadane 's with minor changes ; Increase the sum ; If sub - array sum is greater than the previous ; If elements are 0 , no removal ; If elements are more , then store the minimum value in the sub - array obtained till now ; If sum is negative ; Re - initialize everything ; Driver code
using System ; class GFG { static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static int maximizeSum ( int [ ] a , int n ) { int cnt = 0 ; int mini = int . MaxValue ; int minSubarray = int . MaxValue ; int sum = maxSubArraySum ( a , n ) ; int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; cnt ++ ; minSubarray = Math . Min ( a [ i ] , minSubarray ) ; if ( sum == max_ending_here ) { if ( cnt == 1 ) mini = Math . Min ( mini , 0 ) ; else mini = Math . Min ( mini , minSubarray ) ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; cnt = 0 ; minSubarray = int . MaxValue ; } } return sum - mini ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , - 2 , 3 } ; int n = a . Length ; Console . WriteLine ( maximizeSum ( a , n ) ) ; } }
3 | C # implementation of the approach ; Function that returns true if n is an Osiris number ; 3 rd digit ; 2 nd digit ; 1 st digit ; Check the required condition ; Driver code
using System ; class GFG { static bool isOsiris ( int n ) { int a = n % 10 ; int b = ( n / 10 ) % 10 ; int c = n / 100 ; int digit_sum = a + b + c ; if ( n == ( 2 * ( digit_sum ) * 11 ) ) { return true ; } return false ; } static void Main ( ) { int n = 132 ; if ( isOsiris ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Dudeney Numbers | C # implementation of the approach ; Function that returns true if n is a Dudeney number ; If n is not a perfect cube ; Last digit ; Update the digit sum ; Remove the last digit ; If cube root of n is not equal to the sum of its digits ; Driver code
using System ; class GFG { static bool isDudeney ( int n ) { int cube_rt = ( int ) ( Math . Round ( ( Math . Pow ( n , 1.0 / 3.0 ) ) ) ) ; if ( cube_rt * cube_rt * cube_rt != n ) return false ; int dig_sum = 0 ; int temp = n ; while ( temp > 0 ) { int rem = temp % 10 ; dig_sum += rem ; temp /= 10 ; } if ( cube_rt != dig_sum ) return false ; return true ; } public static void Main ( ) { int n = 17576 ; if ( isDudeney ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Number of triangles possible with given lengths of sticks which are powers of 2 | C # implementation of the approach ; Function to return the number of positive area triangles ; To store the count of total triangles ; To store the count of pairs of sticks with equal lengths ; Back - traverse and count the number of triangles ; Count the number of pairs ; If we have one remaining stick and we have a pair ; Count 1 triangle ; Reduce one pair ; Count the remaining triangles that can be formed ; Driver code
using System ; class GFG { static int countTriangles ( int [ ] a , int n ) { int cnt = 0 ; int pairs = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { pairs += a [ i ] / 2 ; if ( a [ i ] % 2 == 1 && pairs > 0 ) { cnt += 1 ; pairs -= 1 ; } } cnt += ( 2 * pairs ) / 3 ; return cnt ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 2 , 2 , 2 } ; int n = a . Length ; Console . WriteLine ( countTriangles ( a , n ) ) ; } }
Smallest N digit number which is a multiple of 5 | C # implementation of the approach ; Function to return the smallest n digit number which is a multiple of 5 ; Driver code
using System ; class GFG { static int smallestMultiple ( int n ) { if ( n == 1 ) return 5 ; return ( int ) ( Math . Pow ( 10 , n - 1 ) ) ; } public static void Main ( ) { int n = 4 ; Console . Write ( smallestMultiple ( n ) ) ; } }
Find HCF of two numbers without using recursion or Euclidean algorithm | C # implementation of the approach ; Function to return the HCF of x and y ; Minimum of the two numbers ; If both the numbers are divisible by the minimum of these two then the HCF is equal to the minimum ; Highest number between 2 and minimum / 2 which can divide both the numbers is the required HCF ; If both the numbers are divisible by i ; 1 divides every number ; Driver code
using System ; class GFG { static int getHCF ( int x , int y ) { int minimum = Math . Min ( x , y ) ; if ( x % minimum == 0 && y % minimum == 0 ) return minimum ; for ( int i = minimum / 2 ; i >= 2 ; i -- ) { if ( x % i == 0 && y % i == 0 ) return i ; } return 1 ; } static void Main ( ) { int x = 16 , y = 32 ; Console . WriteLine ( getHCF ( x , y ) ) ; } }
Check if product of first N natural numbers is divisible by their sum | C # implementation of the approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that return true if the product of the first n natural numbers is divisible by the sum of first n natural numbers ; Driver code
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 bool isDivisible ( int n ) { if ( isPrime ( n + 1 ) ) return false ; return true ; } static void Main ( ) { int n = 6 ; if ( isDivisible ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }