codeparrot/xlcost-text-to-code huggingface dataset:
Count of subarrays having exactly K prime numbers | C # program for the above approach ; 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 & i + 2 then it is not prime ; Function to find number of subarrays with sum exactly equal to k ; STL map to store number of subarrays starting from index zero having particular value of sum . ; To store the sum of element traverse so far ; Add current element to currsum ; If currsum = K , then a new subarray is found ; If currsum > K then find the no . of subarrays with sum currsum - K and exclude those subarrays ; Add currsum to count of different values of sum ; Return the readonly result ; Function to count the subarray with K primes ; Update the array element ; If current element is prime then update the arr [ i ] to 1 ; Else change arr [ i ] to 0 ; Function Call ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool isPrime ( int n ) { int i ; if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static int findSubarraySum ( int [ ] arr , int n , int K ) { Dictionary < int , int > prevSum = new Dictionary < int , int > ( ) ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == K ) { res ++ ; } if ( prevSum . ContainsKey ( currsum - K ) ) { res += ( prevSum [ currsum - K ] ) ; } if ( prevSum . ContainsKey ( currsum ) ) { prevSum [ currsum ] = prevSum [ currsum ] + 1 ; } else { prevSum . Add ( currsum , 1 ) ; } } return res ; } static void countSubarray ( int [ ] arr , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) { arr [ i ] = 1 ; } else { arr [ i ] = 0 ; } } Console . Write ( findSubarraySum ( arr , n , K ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 2 ; int N = arr . Length ; countSubarray ( arr , N , K ) ; } }
Count the elements having frequency equals to its value | C # program to count the elements having frequency equals to its value ; Function to find the count ; Hash map for counting frequency ; Counting freq of each element ; Check if value equals to frequency and increment the count ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int find_maxm ( int [ ] arr , int 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 ) ; } } int ans = 0 ; foreach ( KeyValuePair < int , int > x in mp ) { int value = x . Key ; int freq = x . Value ; if ( value == freq ) { ans ++ ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 2 , 3 , 4 , 3 } ; int n = arr . Length ; Console . Write ( find_maxm ( arr , n ) ) ; } }
Find the Number of Permutations that satisfy the given condition in an array | C # program to find the number of permutations that satisfy the given condition in an array ; Function to calculate x ^ y recursively ; Function to return the number of permutations that satisfy the given condition in an array ; If there is only one element then only one permutation is available ; Sort the array for calculating the number of elements occurring twice ; If the maximum element is occurring twice , then the number of permutations satisfying the condition is 0 ; This variable will store the number of element occurring twice ; Loop to check the number of elements occurring twice ; Check if this element is occurring twice ; If this element is occurring twice then check if this number is occurring more than twice ; If element occurring thrice then no permutation will satisfy the given condition ; Since we have checked the next element as well , then we can increment the loop variable ; Driver code
using System ; class GFG { static int pow ( int x , int y ) { if ( y == 1 ) return x ; if ( y == 0 ) return 1 ; int temp = pow ( x , y / 2 ) ; temp *= temp ; if ( y % 2 == 1 ) temp *= x ; return temp ; } static int noOfPermutations ( int [ ] a , int n ) { if ( n == 1 ) { return 1 ; } Array . Sort ( a ) ; if ( a [ n - 1 ] == a [ n - 2 ] ) { return 0 ; } int x = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { if ( a [ i ] == a [ i + 1 ] ) { if ( a [ i ] == a [ i + 2 ] ) { return 0 ; } x ++ ; i ++ ; } } return pow ( 2 , n - 2 * x - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 2 , 3 , 4 } ; int n = a . Length ; int num = noOfPermutations ( a , n ) ; Console . Write ( num ) ; } }
Length of the Smallest Subarray that must be removed in order to Maximise the GCD | C # program to find the length of the smallest subarray that must be removed in order to maximise the GCD ; Function to find the length of the smallest subarray that must be removed in order to maximise the GCD ; Store the maximum possible GCD of the resulting subarray ; Two pointers initially pointing to the first and last element respectively ; Moving the left pointer to the right if the elements are divisible by the maximum GCD ; Moving the right pointer to the left if the elements are divisible by the maximum GCD ; Return the length of the subarray ; Driver code
using System ; class GFG { static int GetMinSubarrayLength ( int [ ] a , int n ) { int ans = Math . Max ( a [ 0 ] , a [ n - 1 ] ) ; int lo = 0 , hi = n - 1 ; while ( lo < n && a [ lo ] % ans == 0 ) lo ++ ; while ( hi > lo && a [ hi ] % ans == 0 ) hi -- ; return ( hi - lo + 1 ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 8 , 2 , 1 , 4 } ; int N = arr . Length ; int Length = GetMinSubarrayLength ( arr , N ) ; Console . WriteLine ( Length ) ; } }
Maximize the number of indices such that element is greater than element to its left | C # program of the above approach ; Function to find the maximum pairs such that arr [ i + 1 ] > arr [ i ] ; To store the frequency of the element in [ ] arr ; Store the frequency in map M ; To find the maximum frequency store in map M ; Print the maximum number of possible pairs ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] arr , int 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 ) ; } } int maxFreq = 0 ; foreach ( KeyValuePair < int , int > it in mp ) { maxFreq = Math . Max ( maxFreq , it . Value ) ; } Console . Write ( N - maxFreq + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 8 , 5 , 9 , 8 , 8 , 7 , 7 , 5 , 7 , 7 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }
Find minimum value of the expression by choosing K elements from given array | C # program to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Function to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Sorting the array for least k element selection ; Select first k elements from sorted array ; Return value of solved expression ; Driver code ; Function call
using System ; class GFG { static int minimumValue ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int answer = 0 ; for ( int i = 0 ; i < k ; i ++ ) answer += arr [ i ] * arr [ i ] ; return answer * ( 2 * k - 2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 21 , 5 , 3 , 8 } ; int k = 3 ; int n = arr . Length ; Console . Write ( minimumValue ( arr , n , k ) ) ; } }
Transform N to Minimum possible value | C # program to transform N to the minimum value ; Initialising the answer ; Function to find the digitsum ; Iterate over all digits and add them ; Return the digit su , ; Function to transform N to the minimum value ; If the final value is lesser than least value ; If final value is equal to least value then check for lesser number of steps to reach this value ; The value will be obtained in less than 15 steps as proved so applying normal recursive operations ; Driver code ; Function call ; Print the answers
using System ; class GFG { static int min_val = int . MaxValue ; static int min_steps = 0 ; static int sumOfDigits ( int n ) { string s = n . ToString ( ) ; int sum = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { sum += ( s [ i ] - '0' ) ; } return sum ; } static void Transform ( int n , int d , int steps ) { if ( n < min_val ) { min_val = n ; min_steps = steps ; } else if ( n == min_val ) { min_steps = Math . Min ( min_steps , steps ) ; } if ( steps < 15 ) { Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; Transform ( n + d , d , steps + 1 ) ; } } public static void Main ( string [ ] args ) { int N = 9 , D = 3 ; Transform ( N , D , 0 ) ; Console . Write ( min_val + " ▁ " + min_steps ) ; } }
Remove all 1 s from the adjacent left of 0 s in a Binary Array | C # implementation of the above approach ; Function to find the maximum number of 1 's before 0 ; Traverse the array ; If value is 1 ; If consecutive 1 followed by 0 , then update the maxCnt ; Print the maximum consecutive 1 's  followed by 0 ; Driver Code ; Function Call ; Function Call
using System ; public class GFG { static void noOfMoves ( int [ ] arr , int n ) { int cnt = 0 ; int maxCnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { cnt ++ ; } else { if ( cnt != 0 ) { maxCnt = Math . Max ( maxCnt , cnt ) ; cnt = 0 ; } } } Console . Write ( maxCnt + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 } ; int N = arr . Length ; noOfMoves ( arr , N ) ; int [ ] arr1 = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 } ; N = arr1 . Length ; noOfMoves ( arr1 , N ) ; } }
Check if given coins can be used to pay a value of S | C # implementation to check if it is possible to pay a value ; Function to check if it is possible to pay a value ; Loop to add the value of coin A ; Condition to check if it is possible to pay a value of S ; Driver Code
using System ; class GFG { static void knowPair ( int a , int b , int n , int s , int m ) { int i = 0 , rem = 0 ; int count_b = 0 , flag = 0 ; while ( i <= a ) { rem = s - ( n * i ) ; count_b = rem / m ; if ( rem % m == 0 && count_b <= b ) { flag = 1 ; } i ++ ; } if ( flag == 1 ) { Console . Write ( " YES " + " STRNEWLINE " ) ; } else { Console . Write ( " NO " + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int A = 1 ; int B = 2 ; int n = 3 ; int S = 4 ; int m = 2 ; knowPair ( A , B , n , S , m ) ; } }
Sum of all numbers in the given range which are divisible by M | C # program to find the sum of numbers divisible by M in the given range ; Function to find the sum of numbers divisible by M in the given range ; Variable to store the sum ; Running a loop from A to B and check if a number is divisible by i . ; If the number is divisible , then add it to sum ; Return the sum ; Driver code ; A and B define the range M is the dividend ; Printing the result
using System ; class GFG { static int sumDivisibles ( int A , int B , int M ) { int sum = 0 ; for ( int i = A ; i <= B ; i ++ ) if ( i % M == 0 ) sum += i ; return sum ; } public static void Main ( String [ ] args ) { int A = 6 , B = 15 , M = 3 ; Console . Write ( sumDivisibles ( A , B , M ) + " STRNEWLINE " ) ; } }
Sum of all numbers in the given range which are divisible by M | C # program to find the sum of numbers divisible by M in the given range ; Function to find the largest number smaller than or equal to N that is divisible by K ; Finding the remainder when N is divided by K ; If the remainder is 0 , then the number itself is divisible by K ; Else , then the difference between N and remainder is the largest number which is divisible by K ; Function to find the smallest number greater than or equal to N that is divisible by K ; Finding the remainder when N is divided by K ; If the remainder is 0 , then the number itself is divisible by K ; Else , then the difference between N and remainder is the largest number which is divisible by K ; Function to find the sum of numbers divisible by M in the given range ; Variable to store the sum ; To bring the smallest and largest numbers in the range [ A , B ] ; To count the number of terms in the AP ; Sum of n terms of an AP ; Driver code ; A and B define the range , M is the dividend ; Printing the result
using System ; using System . Collections . Generic ; class GFG { static int findSmallNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } static int findLargeNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } static int sumDivisibles ( int A , int B , int M ) { int first = findSmallNum ( A , M ) ; int last = findLargeNum ( B , M ) ; if ( first < A ) first += M ; if ( last > B ) first -= M ; int n = ( B / M ) - ( A - 1 ) / M ; return n * ( first + last ) / 2 ; } public static void Main ( String [ ] args ) { int A = 6 , B = 15 , M = 3 ; Console . Write ( sumDivisibles ( A , B , M ) ) ; } }
Count of all unique substrings with non | C # program to find the count of all unique sub - Strings with non - repeating characters ; Function to count all unique distinct character subStrings ; Hashmap to store all subStrings ; Iterate over all the subStrings ; Boolean array to maintain all characters encountered so far ; Variable to maintain the subString till current position ; Get the position of the character in the String ; Check if the character is encountred ; Add the current character to the subString ; Insert subString in Hashmap ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int distinctSubString ( String P , int N ) { HashSet < String > S = new HashSet < String > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { bool [ ] freq = new bool [ 26 ] ; String s = " " ; for ( int j = i ; j < N ; ++ j ) { int pos = P [ j ] - ' a ' ; if ( freq [ pos ] == true ) break ; freq [ pos ] = true ; s += P [ j ] ; S . Add ( s ) ; } } return S . Count ; } public static void Main ( String [ ] args ) { String S = " abba " ; int N = S . Length ; Console . Write ( distinctSubString ( S , N ) ) ; } }
Sum of all subarrays of size K | C # implementation to find the sum of all subarrays of size K ; Function to find the sum of all subarrays of size K ; Loop to consider every subarray of size K ; Initialize sum = 0 ; Calculate sum of all elements of current subarray ; Print sum of each subarray ; Driver Code ; Function Call
using System ; class GFG { static void calcSum ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i <= n - k ; i ++ ) { int sum = 0 ; for ( int j = i ; j < k + i ; j ++ ) sum += arr [ j ] ; Console . Write ( sum + " ▁ " ) ; } } static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; int k = 3 ; calcSum ( arr , n , k ) ; } }
Sum of all subarrays of size K | C # implementation to find the sum of all subarrays of size K ; Function to find the sum of all subarrays of size K ; Initialize sum = 0 ; Consider first subarray of size k Store the sum of elements ; Print the current sum ; Consider every subarray of size k Remove first element and add current element to the window ; Add the element which enters into the window and subtract the element which pops out from the window of the size K ; Print the sum of subarray ; Drivers Code ; Function Call
using System ; class GFG { static void calcSum ( int [ ] arr , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum += arr [ i ] ; Console . Write ( sum + " ▁ " ) ; for ( int i = k ; i < n ; i ++ ) { sum = ( sum - arr [ i - k ] ) + arr [ i ] ; Console . Write ( sum + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; int k = 3 ; calcSum ( arr , n , k ) ; } }
Number of indices pair such that element pair sum from first Array is greater than second Array | C # program to find the number of indices pair such that pair sum from first Array is greater than second Array ; Function to get the number of pairs of indices { i , j } in the given two arrays A and B such that A [ i ] + A [ j ] > B [ i ] + B [ j ] ; Intitializing the difference array D ; Computing the difference between the elements at every index and storing it in the array D ; Sort the array D ; Variable to store the total number of pairs that satisfy the given condition ; Loop to iterate through the difference array D and find the total number of pairs of indices that follow the given condition ; If the value at the index i is positive , then it remains positive for any pairs with j such that j > i . ; If the value at that index is negative then we need to find the index of the value just greater than - D [ i ] ; Driver code
using System ; using System . Collections . Generic ; class GFG { static long getPairs ( List < int > A , List < int > B , int n ) { int [ ] D = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { D [ i ] = A [ i ] - B [ i ] ; } Array . Sort ( D ) ; long total = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( D [ i ] > 0 ) { total += n - i - 1 ; } else { int k = upper_bound ( D , 0 , D . Length , - D [ i ] ) ; total += n - k ; } } return total ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void Main ( String [ ] args ) { int n = 5 ; List < int > A = new List < int > ( ) ; List < int > B = new List < int > ( ) ; A . Add ( 4 ) ; A . Add ( 8 ) ; A . Add ( 2 ) ; A . Add ( 6 ) ; A . Add ( 2 ) ; B . Add ( 4 ) ; B . Add ( 5 ) ; B . Add ( 4 ) ; B . Add ( 1 ) ; B . Add ( 3 ) ; Console . Write ( getPairs ( A , B , n ) ) ; } }
Number of pairs such that their HCF and LCM is equal | Naive C # program to count number of pairs such that their hcf and lcm are equal ; Function to return HCF of two numbers ; Function to return LCM of two numbers ; Returns the number of valid pairs ; Traversing the array . For each array element , checking if it follow the condition ; Driver function
using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } static int countPairs ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) ans ++ ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int n = arr . Length ; Console . Write ( countPairs ( arr , n ) ) ; } }
Number of pairs such that their HCF and LCM is equal | C # program to count number of pairs such that their hcf and lcm are equal ; Function to count number of pairs such that their hcf and lcm are equal ; Store frequencies of array elements ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] , ; Driver function
using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] a , int n ) { Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( frequency . ContainsKey ( a [ i ] ) ) { var val = frequency [ a [ i ] ] ; frequency . Remove ( a [ i ] ) ; frequency . Add ( a [ i ] , val + 1 ) ; } else { frequency . Add ( a [ i ] , 1 ) ; } } int count = 0 ; foreach ( KeyValuePair < int , int > entry in frequency ) { int f = entry . Value ; count += f * ( f - 1 ) / 2 ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int n = arr . Length ; Console . Write ( countPairs ( arr , n ) ) ; } }
Find the maximum possible value of last element of the Array | C # program to find the maximum possible value of last element of the array ; Function to find the maximum possible value of last element of the array ; Traverse for all element ; Find the distance ; If moves less than distance then we can not move this number to end ; How many number we can move to end ; Take the minimum of both of them ; Increment in the end ; Remove taken moves ; Return the last element ; Driver code ; Function call
using System ; class GFG { static int maxValue ( int [ ] arr , int n , int moves ) { for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { int distance = n - 1 - i ; if ( moves < distance ) break ; int can_take = moves / distance ; int take = Math . Min ( arr [ i ] , can_take ) ; arr [ n - 1 ] += take ; moves -= take * distance ; } } return arr [ n - 1 ] ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 0 , 1 } ; int M = 5 ; int N = arr . Length ; Console . Write ( maxValue ( arr , N , M ) ) ; } }
Minimum number of Factorials whose sum is equal to N | C # program to find minimum number of factorials ; Array to calculate all factorials less than or equal to N Since there can be only 14 factorials till 10 ^ 10 Hence the maximum size of fact [ ] is 14 ; Store the actual size of fact [ ] ; Function to precompute factorials till N ; Precomputing factorials ; Function to find the minimum number of factorials whose sum represents N ; Precompute factorials ; Initialize result ; Traverse through all factorials ; Find factorials ; Print min count ; Print result ; Driver program
using System ; using System . Collections . Generic ; class GFG { static int [ ] fact = new int [ 14 ] ; static int size = 1 ; static void preCompute ( int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; fact [ i - 1 ] <= N ; i ++ ) { fact [ i ] = ( fact [ i - 1 ] * i ) ; size ++ ; } } static void findMin ( int N ) { preCompute ( N ) ; int originalN = N ; List < int > ans = new List < int > ( ) ; for ( int i = size - 1 ; i >= 0 ; i -- ) { while ( N >= fact [ i ] ) { N -= fact [ i ] ; ans . Add ( fact [ i ] ) ; } } Console . Write ( ans . Count + " STRNEWLINE " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 27 ; findMin ( n ) ; } }
Longest increasing sub | C # implementation to find the longest increasing subsequence in repeating element of array ; Function to find the LCS ; Loop to create frequency array ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findLCS ( int [ ] arr , int 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 ) ; } } return mp . Count ; } public static void Main ( String [ ] args ) { int n = 3 ; int [ ] arr = { 3 , 2 , 1 } ; Console . Write ( findLCS ( arr , n ) ) ; } }