codeparrot/xlcost-text-to-code huggingface dataset:
Sum of all Perfect numbers lying in the range [ L , R ] | C # implementation to find the sum of all perfect numbers lying in the range [ L , R ] ; Array to store the sum ; Function to check if a number is a perfect number or not ; Iterating till the square root of the number and checking if the sum of divisors is equal to the number or not ; If it is a perfect number , then return the number ; Else , return 0 ; Function to precompute the sum of perfect squares and store then in an array
using System ; public class GFG { static int [ ] pref = new int [ 10000 ] ; static int isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return n ; return 0 ; } static void precomputation ( ) { for ( int i = 1 ; i < 10000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfect ( i ) ; } } public static void Main ( String [ ] args ) { int L = 6 , R = 28 ; precomputation ( ) ; Console . WriteLine ( pref [ R ] - pref [ L - 1 ] ) ; } }
Ternary number system or Base 3 numbers | C # program to convert a ternary number to decimal number ; Function to convert a ternary number to a decimal number ; If the number is greater than 0 , compute the decimal representation of the number ; Loop to iterate through the number ; Computing the decimal digit ; Driver code
using System ; class GFG { static void convertToDecimal ( int N ) { Console . Write ( " Decimal ▁ number ▁ of ▁ " + N + " ▁ is : ▁ " ) ; if ( N != 0 ) { int decimalNumber = 0 ; int i = 0 , remainder ; while ( N != 0 ) { remainder = N % 10 ; N /= 10 ; decimalNumber += remainder * ( int ) Math . Pow ( 3 , i ) ; ++ i ; } Console . Write ( decimalNumber + " STRNEWLINE " ) ; } else Console . Write ( "0" + " STRNEWLINE " ) ; } public static void Main ( ) { int Ternary = 10202202 ; convertToDecimal ( Ternary ) ; } }
Check if a given pair of Numbers are Betrothed numbers or not | C # program for the above approach ; Function to check whether N is perfect square or not ; Find sqrt ; Function to check whether the given pairs of numbers is Betrothed numbers or not ; For finding the sum of all the divisors of first number n ; For finding the sum of all the divisors of second number m ; Driver Code ; Function Call
using System ; class GFG { static bool isPerfectSquare ( int N ) { double sr = Math . Sqrt ( N ) ; return ( sr - Math . Floor ( sr ) ) == 0 ; } static void BetrothedNumbers ( int n , int m ) { int Sum1 = 1 ; int Sum2 = 1 ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { Sum1 += i + ( isPerfectSquare ( n ) ? 0 : n / i ) ; } } for ( int i = 2 ; i <= Math . Sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { Sum2 += i + ( isPerfectSquare ( m ) ? 0 : m / i ) ; } } if ( ( n + 1 == Sum2 ) && ( m + 1 == Sum1 ) ) { Console . Write ( " YES " + " STRNEWLINE " ) ; } else { Console . Write ( " NO " + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int N = 9504 ; int M = 20734 ; BetrothedNumbers ( N , M ) ; } }
Count of numbers upto M with GCD equals to K when paired with M | C # program to Count of numbers between 0 to M which have GCD with M equals to K . ; Function to calculate GCD using euler totient function ; Finding the prime factors of limit to calculate it 's  euler totient function ; Calculating the euler totient function of ( m / k ) ; Function print the count of numbers whose GCD with M equals to K ; GCD of m with any integer cannot be equal to k ; 0 and m itself will be the only valid integers ; Finding the number upto which coefficient of k can come ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int EulerTotientFunction ( int limit ) { int copy = limit ; List < int > primes = new List < int > ( ) ; for ( int i = 2 ; i * i <= limit ; i ++ ) { if ( limit % i == 0 ) { while ( limit % i == 0 ) { limit /= i ; } primes . Add ( i ) ; } } if ( limit >= 2 ) { primes . Add ( limit ) ; } int ans = copy ; foreach ( int it in primes ) { ans = ( ans / it ) * ( it - 1 ) ; } return ans ; } static void CountGCD ( int m , int k ) { if ( m % k != 0 ) { Console . Write ( 0 + " STRNEWLINE " ) ; return ; } if ( m == k ) { Console . Write ( 2 + " STRNEWLINE " ) ; return ; } int limit = m / k ; int ans = EulerTotientFunction ( limit ) ; Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int M = 9 ; int K = 1 ; CountGCD ( M , K ) ; } }
Find the product of sum of two diagonals of a square Matrix | C # program to find the product of the sum of diagonals . ; Function to find the product of the sum of diagonals . ; Initialize sums of diagonals ; Return the answer ; Driven code ; Function call
using System ; class GFG { static long product ( int [ , ] mat , int n ) { long d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { d1 += mat [ i , i ] ; d2 += mat [ i , n - i - 1 ] ; } return 1L * d1 * d2 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 5 , 8 , 1 } , { 5 , 10 , 3 } , { - 6 , 17 , - 9 } } ; int n = mat . GetLength ( 0 ) ; Console . Write ( product ( mat , n ) ) ; } }
Program to calculate Percentile of a student based on rank | C # program to calculate Percentile of a student based on rank ; Program to calculate the percentile ; flat variable to store the result ; calculate and return the percentile ; Driver Code
using System ; class GFG { static float getPercentile ( int rank , int students ) { float result = ( float ) ( students - rank ) / students * 100 ; return result ; } public static void Main ( String [ ] args ) { int your_rank = 805 ; int total_students = 97481 ; Console . Write ( getPercentile ( your_rank , total_students ) ) ; } }
Sum of numbers in the Kth level of a Fibonacci triangle | C # implementation to find the Sum of numbers in the Kth level of a Fibonacci triangle ; Function to return the nth Fibonacci number ; Function to return the required sum of the array ; Using our deduced result ; Function to return the sum of fibonacci in the Kth array ; Count of fibonacci which are in the arrays from 1 to k - 1 ; Driver code
using System ; class GFG { static int fib ( int n ) { double phi = ( 1 + Math . Sqrt ( 5 ) ) / 2 ; return ( int ) Math . Round ( Math . Pow ( phi , n ) / Math . Sqrt ( 5 ) ) ; } static int calculateSum ( int l , int r ) { int sum = fib ( r + 2 ) - fib ( l + 1 ) ; return sum ; } static int sumFibonacci ( int k ) { int l = ( k * ( k - 1 ) ) / 2 ; int r = l + k ; int sum = calculateSum ( l , r - 1 ) ; return sum ; } public static void Main ( ) { int k = 3 ; Console . Write ( sumFibonacci ( k ) ) ; } }
Print the sequence of size N in which every term is sum of previous K terms | C # implementation to find the series in which every term is sum of previous K terms ; Function to generate the series in the form of array ; Pick a starting point ; Find the sum of all elements till count < K ; Find the value of sum at i position ; Driver Code
using System ; class Sum { void sumOfPrevK ( int N , int K ) { int [ ] arr = new int [ N ] ; arr [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { int j = i - 1 , count = 0 , sum = 0 ; while ( j >= 0 && count < K ) { sum += arr [ j ] ; j -- ; count ++ ; } arr [ i ] = sum ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { Sum s = new Sum ( ) ; int N = 10 , K = 4 ; s . sumOfPrevK ( N , K ) ; } }
Print the sequence of size N in which every term is sum of previous K terms | C # implementation to find the series in which every term is sum of previous K terms ; Function to generate the series in the form of array ; Pick a starting point ; Computing the previous sum ; Loop to print the series ; Driver code
using System ; public class Sum { void sumOfPrevK ( int N , int K ) { int [ ] arr = new int [ N ] ; int prevsum = 0 ; arr [ 0 ] = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( i < K ) { arr [ i + 1 ] = arr [ i ] + prevsum ; prevsum = arr [ i + 1 ] ; } else { arr [ i + 1 ] = arr [ i ] + prevsum - arr [ i + 1 - K ] ; prevsum = arr [ i + 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { Sum s = new Sum ( ) ; int N = 8 , K = 3 ; s . sumOfPrevK ( N , K ) ; } }
Maximum GCD of all subarrays of length at least 2 | C # program for the above approach ; Function to find GCD ; To store the maximum GCD ; Traverse the array ; Find GCD of the consecutive element ; If calculated GCD > maxGCD then update it ; Print the maximum GCD ; Driver Code ; Function call
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } static void findMaxGCD ( int [ ] arr , int n ) { int maxGCD = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int val = gcd ( arr [ i ] , arr [ i + 1 ] ) ; if ( val > maxGCD ) { maxGCD = val ; } } Console . Write ( maxGCD + " STRNEWLINE " ) ; } public static void Main ( ) { int [ ] arr = { 3 , 18 , 9 , 9 , 5 , 15 , 8 , 7 , 6 , 9 } ; int n = arr . Length ; findMaxGCD ( arr , n ) ; } }
Sum of elements of an AP in the given range | C # program to find the sum of elements of an AP in the given range ; Function to find sum in the given range ; Find the value of k ; Find the common difference ; Find the sum ; Driver code
using System ; class GFG { static int findSum ( int [ ] arr , int n , int left , int right ) { int k = right - left ; int d = arr [ 1 ] - arr [ 0 ] ; int ans = arr [ left - 1 ] * ( k + 1 ) ; ans = ans + ( d * ( k * ( k + 1 ) ) ) / 2 ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 } ; int queries = 3 ; int [ , ] q = { { 2 , 4 } , { 2 , 6 } , { 5 , 6 } } ; int n = arr . Length ; for ( int i = 0 ; i < queries ; i ++ ) Console . Write ( findSum ( arr , n , q [ i , 0 ] , q [ i , 1 ] ) + " STRNEWLINE " ) ; } }
Check if a subarray exists with sum greater than the given Array | C # program to check if a subarray exists with sum greater than the given Array ; Function to check whether there exists a subarray whose sum is greater than or equal to sum of given array elements ; Initialize sum with 0 ; Checking possible prefix subarrays . If sum of them is less than or equal to zero , then return 1 ; again reset sum to zero ; Checking possible suffix subarrays . If sum of them is less than or equal to zero , then return 1 ; Otherwise return 0 ; Driver Code
using System ; class GFG { static bool subarrayPossible ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum <= 0 ) return true ; } sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; if ( sum <= 0 ) return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , - 12 , 7 , - 10 , 20 , 30 , - 10 , 50 , 60 } ; int size = arr . Length ; if ( subarrayPossible ( arr , size ) ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } }
Finding Integreand using Weedle 's Rule | C # program to Implement Weedle 's Rule ; A sample function f ( x ) = 1 / ( 1 + x ^ 2 ) ; Function to find the integral value of f ( x ) with step size h , with initial lower limit and upper limit a and b ; Find step size h ; To store the final sum ; Find sum using Weedle 's Formula ; Return the final sum ; Driver Code ; lower limit and upper limit ; Function Call
using System ; class GFG { static float y ( float x ) { float num = 1 ; float denom = ( float ) 1.0 + x * x ; return num / denom ; } static float WeedleRule ( float a , float b ) { float h = ( b - a ) / 6 ; float sum = 0 ; sum = sum + ( ( ( 3 * h ) / 10 ) * ( y ( a ) + y ( a + 2 * h ) + 5 * y ( a + h ) + 6 * y ( a + 3 * h ) + y ( a + 4 * h ) + 5 * y ( a + 5 * h ) + y ( a + 6 * h ) ) ) ; return sum ; } public static void Main ( ) { float a = 0 , b = 6 ; float num = WeedleRule ( a , b ) ; Console . Write ( " f ( x ) ▁ = ▁ " + num ) ; } }
Find the minimum number to be added to N to make it a prime number | C # program to find the minimum number to be added to N to make it a prime number ; Function to check if a given number is a prime or not ; Base cases ; This is checked so that we can skip middle five numbers in below loop ; For all the remaining numbers , check if any number is a factor if the number or not ; If none of the above numbers are the factors for the number , then the given number is prime ; Function to return the smallest number to be added to make a number prime ; Base case ; Loop continuously until isPrime returns true for a number greater than n ; If the number is not a prime , then increment the number by 1 and the counter which stores the number to be added ; 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 int findSmallest ( int N ) { if ( N == 0 ) return 2 ; if ( N == 1 ) return 1 ; int prime = N , counter = 0 ; bool found = false ; while ( ! found ) { if ( isPrime ( prime ) ) found = true ; else { prime ++ ; counter ++ ; } } return counter ; } public static void Main ( ) { int N = 10 ; Console . Write ( findSmallest ( N ) ) ; } }
Find the position of the given Prime Number | C # program to find the position of the given prime number ; Function to precompute the position of every prime number using Sieve ; 0 and 1 are not prime numbers ; Variable to store the position ; Incrementing the position for every prime number ; Driver code
using System ; class GFG { static readonly int limit = 1000000 ; static int [ ] position = new int [ limit + 1 ] ; static void sieve ( ) { position [ 0 ] = - 1 ; position [ 1 ] = - 1 ; int pos = 0 ; for ( int i = 2 ; i <= limit ; i ++ ) { if ( position [ i ] == 0 ) { position [ i ] = ++ pos ; for ( int j = i * 2 ; j <= limit ; j += i ) position [ j ] = - 1 ; } } } public static void Main ( String [ ] args ) { sieve ( ) ; int n = 11 ; Console . Write ( position [ n ] ) ; } }
Minimum possible sum of array elements after performing the given operation | C # program to find the minimum possible sum of the array elements after performing the given operation ; Function to find the maximum sum of the sub array ; max_so_far represents the maximum sum found till now and max_ending_here represents the maximum sum ending at a specific index ; Iterating through the array to find the maximum sum of the subarray ; If the maximum sum ending at a specific index becomes less than 0 , then making it equal to 0. ; Function to find the minimum possible sum of the array elements after performing the given operation ; Finding the sum of the array ; Computing the minimum sum of the array ; Driver code
using System ; class GFG { static double maxSubArraySum ( double [ ] a , int size ) { double max_so_far = Int32 . 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 void minPossibleSum ( double [ ] a , int n , double x ) { double mxSum = maxSubArraySum ( a , n ) ; double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } sum = sum - mxSum + mxSum / x ; Console . Write ( sum + " STRNEWLINE " ) ; } public static void Main ( ) { int N = 3 ; double X = 2 ; double [ ] A = { 1 , - 2 , 3 } ; minPossibleSum ( A , N , X ) ; } }
Print the two possible permutations from a given sequence | C # program to print two permutations from a given sequence ; Function to check if the sequence is concatenation of two permutations or not ; Computing the sum of all the elements in the array ; Computing the prefix sum for all the elements in the array ; Iterating through the i from lengths 1 to n - 1 ; Sum of first i + 1 elements ; Sum of remaining n - i - 1 elements ; Lengths of the 2 permutations ; Checking if the sums satisfy the formula or not ; Function to print the two permutations ; Print the first permutation ; Print the second permutation ; Function to find the two permutations from the given sequence ; If the sequence is not a concatenation of two permutations ; Find the largest element in the array and set the lengths of the permutations accordingly ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static bool checkPermutation ( int [ ] arr , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int [ ] prefix = new int [ n + 1 ] ; prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long lsum = prefix [ i ] ; long rsum = sum - prefix [ i ] ; long l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } static void printPermutations ( int [ ] arr , int n , int l1 , int l2 ) { for ( int i = 0 ; i < l1 ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; for ( int i = l1 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static void findPermutations ( int [ ] arr , int n ) { if ( ! checkPermutation ( arr , n ) ) { Console . Write ( " Not ▁ Possible " ) ; return ; } int l1 = 0 , l2 = 0 ; l1 = arr . Max ( ) ; l2 = n - l1 ; HashSet < int > s1 = new HashSet < int > ( ) , s2 = new HashSet < int > ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) s1 . Add ( arr [ i ] ) ; for ( int i = l1 ; i < n ; i ++ ) s2 . Add ( arr [ i ] ) ; if ( s1 . Count == l1 && s2 . Count == l2 ) printPermutations ( arr , n , l1 , l2 ) ; else { l1 = l1 + l2 ; l2 = l1 - l2 ; l1 = l1 - l2 ; printPermutations ( arr , n , l1 , l2 ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 , 10 , 2 } ; int n = arr . Length ; findPermutations ( arr , n ) ; } }
Count pairs in array such that one element is reverse of another | C # program to count the pairs in array such that one element is reverse of another ; Function to reverse the digits of the number ; Loop to iterate till the number is greater than 0 ; Extract the last digit and keep multiplying it by 10 to get the reverse of the number ; Function to find the pairs from the such that one number is reverse of the other ; Iterate through all pairs ; Increment count if one is the reverse of other ; Driver code
using System ; class GFG { static int reverse ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static int countReverse ( int [ ] arr , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( reverse ( arr [ i ] ) == arr [ j ] ) { res ++ ; } return res ; } public static void Main ( String [ ] arr ) { int [ ] a = { 16 , 61 , 12 , 21 , 25 } ; int n = a . Length ; Console . Write ( countReverse ( a , n ) ) ; } }
Count pairs in array such that one element is reverse of another | C # program to count the pairs in array such that one element is reverse of another ; Function to reverse the digits of the number ; Loop to iterate till the number is greater than 0 ; Extract the last digit and keep multiplying it by 10 to get the reverse of the number ; Function to find the pairs from the array such that one number is reverse of the other ; Iterate over every element in the array and increase the frequency of the element in hash map ; Iterate over every element in the array ; Remove the current element from the hash map by decreasing the frequency to avoid counting when the number is a palindrome or when we visit its reverse ; Increment the count by the frequency of reverse of the number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int reverse ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static int countReverse ( 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 res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] -- ; } else { freq . Add ( arr [ i ] , - 1 ) ; } if ( freq . ContainsKey ( reverse ( arr [ i ] ) ) ) { res += freq [ reverse ( arr [ i ] ) ] ; } } return res ; } static void Main ( ) { int [ ] a = { 16 , 61 , 12 , 21 , 25 } ; int n = a . Length ; Console . WriteLine ( countReverse ( a , n ) ) ; } }
Sum of i * countDigits ( i ) ^ countDigits ( i ) for all i in range [ L , R ] | C # program to find the required sum ; Function to return the required sum ; Iterating for all the number of digits from 1 to 10 ; If the range is valid ; Sum of AP ; Computing the next minimum and maximum numbers by for the ( i + 1 ) - th digit ; Driver code
using System ; class GFG { static readonly int MOD = 1000000007 ; static int rangeSum ( int l , int r ) { int a = 1 , b = 9 , res = 0 ; for ( int i = 1 ; i <= 10 ; i ++ ) { int L = Math . Max ( l , a ) ; int R = Math . Min ( r , b ) ; if ( L <= R ) { int sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += ( int ) Math . Pow ( i , i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } public static void Main ( String [ ] args ) { int l = 98 , r = 102 ; Console . Write ( rangeSum ( l , r ) ) ; } }
Trial division Algorithm for Prime Factorization | C # implementation of Trial Division Algorithm ; Function to check if a number is a prime number or not ; Initializing with the value 2 from where the number is checked ; Computing the square root of the number N ; While loop till the square root of N ; If any of the numbers between [ 2 , sqrt ( N ) ] is a factor of N Then the number is composite ; If none of the numbers is a factor , then it is a prime number ; Driver Code ; To check if a number is a prime or not
using System ; class GFG { static int TrialDivision ( int N ) { int i = 2 ; int k = ( int ) Math . Ceiling ( Math . Sqrt ( N ) ) ; while ( i <= k ) { if ( N % i == 0 ) return 0 ; i += 1 ; } return 1 ; } public static void Main ( ) { int N = 49 ; int p = TrialDivision ( N ) ; if ( p != 0 ) Console . Write ( " Prime " ) ; else Console . Write ( " Composite " ) ; } }
Find if it is possible to choose subarray that it contains exactly K even integers | C # program to check if it is possible to choose a subarray that contains exactly K even integers ; Function to check if it is possible to choose a subarray that contains exactly K even integers ; Variable to store the count of even numbers ; If we have to select 0 even numbers but there is all odd numbers in the array ; If the count of even numbers is greater than or equal to K then we can select a subarray with exactly K even integers ; If the count of even numbers is less than K then we cannot select any subarray with exactly K even integers ; Driver Code
using System ; class GFG { static void isPossible ( int [ ] A , int n , int k ) { int countOfTwo = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] % 2 == 0 ) { countOfTwo ++ ; } } if ( k == 0 && countOfTwo == n ) Console . Write ( " NO " ) ; else if ( countOfTwo >= k ) { Console . Write ( " Yes " ) ; } else Console . Write ( " No " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 } ; int K = 2 ; int n = arr . Length ; isPossible ( arr , n , K ) ; } }
Count of pairs ( A , B ) in range 1 to N such that last digit of A is equal to the first digit of B | C # program to implement the above approach ; Function to Count of pairs ( A , B ) in range 1 to N ; count C i , j ; Calculate number of pairs ; Driver code ; Function call
using System ; class GFG { static int pairs ( int n ) { int [ , ] c = new int [ 10 , 10 ] ; int tmp = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i >= tmp * 10 ) tmp *= 10 ; c [ i / tmp , i % 10 ] ++ ; } int ans = 0 ; for ( int i = 1 ; i < 10 ; i ++ ) for ( int j = 1 ; j < 10 ; j ++ ) ans += c [ i , j ] * c [ j , i ] ; return ans ; } public static void Main ( String [ ] args ) { int n = 25 ; Console . Write ( pairs ( n ) ) ; } }