codeparrot/xlcost-text-to-code huggingface dataset:
Logarithm tricks for Competitive Programming | C # implementation to find the previous and next power of K ; Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Driver Code
using System ; class GFG { static int prevPowerofK ( int n , int k ) { int p = ( int ) ( Math . Log ( n ) / Math . Log ( k ) ) ; return ( int ) Math . Pow ( k , p ) ; } static int nextPowerOfK ( int n , int k ) { return prevPowerofK ( n , k ) * k ; } public static void Main ( ) { int N = 7 ; int K = 2 ; Console . Write ( prevPowerofK ( N , K ) + " ▁ " ) ; Console . Write ( nextPowerOfK ( N , K ) ) ; } }
Sum of all composite numbers lying in the range [ L , R ] for Q queries | C # implementation to find the sum of all Composite numbers in the given range ; Prefix array to precompute the sum of all Composite number ; Function that return number num if num is Composite else return 0 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to precompute the sum of all Composite numbers upto 100000 ; CheckComposite ( ) return the number i if i is Composite else return 0 ; Function to print the sum for each query ; Function to print sum of all Composite numbers between [ L , R ] ; Function that pre computes the sum of all Composite numbers ; Iterate over all Queries to print the sum ; Driver code ; Queries ; Function that print the the sum of all Composite number in Range [ L , R ]
using System ; public class GFG { static int [ ] pref = new int [ 100001 ] ; static int isComposite ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 n % 3 == 0 ) return n ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return n ; return 0 ; } static void preCompute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isComposite ( i ) ; } } static void printSum ( int L , int R ) { Console . Write ( pref [ R ] - pref [ L - 1 ] + " STRNEWLINE " ) ; } static void printSumComposite ( int [ , ] arr , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i , 0 ] , arr [ i , 1 ] ) ; } } public static void Main ( String [ ] args ) { int Q = 2 ; int [ , ] arr = { { 10 , 13 } , { 12 , 21 } } ; printSumComposite ( arr , Q ) ; } }
Probability of getting a perfect square when a random number is chosen in a given range | C # implementation to find the probability of getting a perfect square number ; Function to return the probability of getting a perfect square number in a range ; Count of perfect squares ; Total numbers in range l to r ; Calculating probability ; Driver Code ; Function Call
using System ; class GFG { static float findProb ( int l , int r ) { float countOfPS = ( float ) ( Math . Floor ( Math . Sqrt ( r ) ) - Math . Ceiling ( Math . Sqrt ( l ) ) + 1 ) ; float total = r - l + 1 ; float prob = ( float ) countOfPS / ( float ) total ; return prob ; } public static void Main ( String [ ] args ) { int L = 16 , R = 25 ; Console . Write ( findProb ( L , R ) ) ; } }
Count of groups having largest size while grouping according to sum of its digits | C # implementation to Count the number of groups having the largest size where groups are according to the sum of its digits ; Function to return sum of digits of i ; Create the dictionary of unique sum ; dictionary that contain unique sum count ; Calculate the sum of its digits ; Function to find the largest size of group ; Count of largest size group ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int sumDigits ( int n ) { int sum = 0 ; while ( n != 0 ) { sum += n % 10 ; n /= 10 ; } return sum ; } static Dictionary < int , int > constDict ( int n ) { Dictionary < int , int > d = new Dictionary < int , int > ( ) ; for ( int i = 1 ; i < n + 1 ; ++ i ) { int sum1 = sumDigits ( i ) ; if ( ! d . ContainsKey ( sum1 ) ) d . Add ( sum1 , 1 ) ; else d [ sum1 ] += 1 ; } return d ; } static int countLargest ( int n ) { Dictionary < int , int > d = constDict ( n ) ; int size = 0 ; int count = 0 ; foreach ( KeyValuePair < int , int > it in d ) { int k = it . Key ; int val = it . Value ; if ( val > size ) { size = val ; count = 1 ; } else if ( val == size ) count += 1 ; } return count ; } static void Main ( ) { int n = 13 ; int group = countLargest ( n ) ; Console . WriteLine ( group ) ; } }
Find the length of factorial of a number in any given base | A optimised C # program to find the number of digits in a factorial in base b ; Returns the number of digits present in n ! in base b Since the result can be large long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code ; calling findDigits ( Number , Base )
using System ; class GFG { static long findDigits ( int n , int b ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double M_PI = 3.141592 ; double M_E = 2.7182 ; double x = ( ( n * Math . Log10 ( n / M_E ) + Math . Log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( Math . Log10 ( b ) ) ; return ( long ) ( Math . Floor ( x ) + 1 ) ; } public static void Main ( string [ ] args ) { Console . WriteLine ( findDigits ( 4 , 16 ) ) ; Console . WriteLine ( findDigits ( 5 , 8 ) ) ; Console . WriteLine ( findDigits ( 12 , 16 ) ) ; Console . WriteLine ( findDigits ( 19 , 13 ) ) ; } }
Sum of all Non | C # implementation to find the sum of all non - fibonacci numbers in a range from L to R ; Array to precompute the sum of non - fibonacci numbers ; Function to find if a number is a perfect square ; Function that returns N if N is non - fibonacci number ; N is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both are perferct square ; Function to precompute sum of non - fibonacci Numbers ; Function to find the sum of all non - fibonacci numbers in a range ; Driver Code ; Pre - computation ; Loop to find the sum for each query
using System ; class GFG { static long [ ] pref = new long [ 100010 ] ; static bool isPerfectSquare ( int x ) { int s = ( int ) Math . Sqrt ( x ) ; return ( s * s == x ) ; } static int isNonFibonacci ( int n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 0 ; else return n ; } static void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) ; } } static void printSum ( int L , int R ) { int sum = ( int ) ( pref [ R ] - pref [ L - 1 ] ) ; Console . Write ( sum + " ▁ " ) ; } public static void Main ( String [ ] args ) { compute ( ) ; int Q = 2 ; int [ , ] arr = { { 1 , 5 } , { 6 , 10 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i , 0 ] , arr [ i , 1 ] ) ; } } }
Count twin prime pairs in an Array | C # program to count Twin 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 Twin Primes or not ; Function to find Twin Prime pairs from the given array ; Iterate through all pairs ; Increment count if twin prime pair ; Driver 's code ; Function call to find Twin Primes 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 + 2 ) == 0 ) { return false ; } } return true ; } static bool twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . Abs ( n1 - n2 ) == 2 ) ; } static int countTwinPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( twinPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 11 } ; int n = arr . Length ; Console . Write ( countTwinPairs ( arr , n ) ) ; } }
Fill the missing numbers in the array of N natural numbers such that arr [ i ] not equal to i | C # implementation of above approach ; Function to fill the position with arr [ i ] = 0 ; Inserting all elements in missing [ ] set from 1 to N ; Inserting unfilled positions ; Removing allocated_elements ; Loop for filling the positions with arr [ i ] != i ; Checking for any arr [ i ] = i ; Finding the suitable position in the array to swap with found i for which arr [ i ] = i ; Checking if the position is present in unfilled_position ; Swapping arr [ i ] & arr [ pos ] ( arr [ pos ] = pos ) ; Function to Print the array ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void solve ( int [ ] arr , int n ) { HashSet < int > unfilled_indices = new HashSet < int > ( ) ; HashSet < int > missing = new HashSet < int > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { missing . Add ( i ) ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { unfilled_indices . Add ( i ) ; } else { missing . Remove ( arr [ i ] ) ; } } int [ ] mi = new int [ missing . Count ] ; int l = 0 ; foreach ( int x in missing ) { mi [ l ++ ] = x ; } int m = missing . Count ; foreach ( int it in unfilled_indices ) { arr [ it ] = mi [ m - 1 ] ; m -- ; } int pos = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == i ) { pos = i ; } } if ( pos != 0 ) { for ( int i = 1 ; i < n ; i ++ ) { if ( pos != i ) { if ( unfilled_indices . Contains ( i ) ) { int x = arr [ i ] ; arr [ i ] = pos ; arr [ pos ] = x ; break ; } } } } printArray ( arr , n ) ; } static void printArray ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 } ; int n = arr . Length ; solve ( arr , n ) ; } }
Find Kth number from sorted array formed by multiplying any two numbers in the array | C # implementation to find the Kth number in the list formed from product of any two numbers in the array and sorting them ; Function to find number of pairs ; Negative and Negative ; Add Possible Pairs ; Positive and Positive ; Add Possible pairs ; Negative and Positive ; Add Possible pairs ; Function to find the kth element in the list ; Separate Positive and Negative elements ; Sort the Elements ; Binary search ; Return the required answer ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static bool check ( int x , List < int > pos , List < int > neg , int k ) { int pairs = 0 ; int p = neg . Count - 1 ; int nn = neg . Count - 1 ; int pp = pos . Count - 1 ; for ( int i = 0 ; i < neg . Count ; i ++ ) { while ( ( p >= 0 ) && ( ( int ) neg [ i ] * ( int ) neg [ p ] <= x ) ) p -- ; pairs += Math . Min ( nn - p , nn - i ) ; } p = 0 ; for ( int i = pos . Count - 1 ; i >= 0 ; i -- ) { while ( ( p < pos . Count ) && ( ( int ) pos [ i ] * ( int ) pos [ p ] <= x ) ) p ++ ; pairs += Math . Min ( p , i ) ; } p = pos . Count - 1 ; for ( int i = neg . Count - 1 ; i >= 0 ; i -- ) { while ( ( p >= 0 ) && ( ( int ) neg [ i ] * ( int ) pos [ p ] <= x ) ) p -- ; pairs += pp - p ; } return ( pairs >= k ) ; } static int kth_element ( int [ ] a , int n , int k ) { List < int > pos = new List < int > ( ) ; List < int > neg = new List < int > ( ) ; ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) pos . Add ( a [ i ] ) ; else neg . Add ( a [ i ] ) ; } sort ( pos . begin ( ) , pos . end ( ) ) ; sort ( neg . begin ( ) , neg . end ( ) ) ; pos . Sort ( ) ; neg . Sort ( ) ; int l = ( int ) - 1e8 , ans = 0 , r = ( int ) 1e8 ; while ( l <= r ) { int mid = ( l + r ) >> 1 ; if ( check ( mid , pos , neg , k ) ) { ans = mid ; r = mid - 1 ; } else l = mid + 1 ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { - 4 , - 2 , 3 , 3 } ; int k = 3 ; int n = a . Length ; Console . WriteLine ( kth_element ( a , n , k ) ) ; } }
Find root of a number using Newton 's method | C # implementation of the approach ; Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; The closed guess will be stored in the root ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code
using System ; class GFG { static double squareRoot ( double n , double l ) { double x = n ; double root ; int count = 0 ; while ( true ) { count ++ ; root = 0.5 * ( x + ( n / x ) ) ; if ( Math . Abs ( root - x ) < l ) break ; x = root ; } return root ; } public static void Main ( ) { double n = 327 ; double l = 0.00001 ; Console . WriteLine ( squareRoot ( n , l ) ) ; } }
Count the occurrence of Nth term in first N terms of Van Eck 's sequence | C # program to count the occurrence of nth term in first n terms of Van Eck 's sequence ; Utility function to compute Van Eck 's sequence ; Initialize sequence array ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Get nth term of the sequence ; Count the occurrence of nth term in first n terms of the sequence ; Return count ; Driver code ; Pre - compute Van Eck 's sequence ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
using System ; class GFG { static int MAX = 100000 ; static int [ ] sequence = new int [ MAX + 1 ] ; static void vanEckSequence ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { sequence [ i ] = 0 ; } for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( sequence [ j ] == sequence [ i ] ) { sequence [ i + 1 ] = i - j ; break ; } } } } static int getCount ( int n ) { int nthTerm = sequence [ n - 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( sequence [ i ] == nthTerm ) count ++ ; } return count ; } public static void Main ( ) { vanEckSequence ( ) ; int n = 5 ; Console . WriteLine ( getCount ( n ) ) ; n = 11 ; Console . WriteLine ( getCount ( n ) ) ; } }
Count the occurrence of Nth term in first N terms of Van Eck 's sequence | C # program to count the occurrence of nth term in first n terms of Van Eck 's sequence ; Utility function to compute Van Eck 's sequence ; Initialize sequence array ; Loop to generate sequence ; Check if sequence [ i ] has occured previously or is new to sequence ; If occurrence found then the next term will be how far back this last term occured previously ; Utility function to count the occurrence of nth term in first n terms of the sequence ; Initialize count as 1 ; Increment count if ( i + 1 ) th term is non - zero ; Previous occurrence of sequence [ i ] will be it ( i - sequence [ i + 1 ] ) th position ; Return the count of occurrence ; Driver code ; Pre - compute Van Eck 's sequence ; Print count of the occurrence of nth term in first n terms of the sequence ; Print count of the occurrence of nth term in first n terms of the sequence
using System ; class GFG { static int MAX = 100000 ; static int [ ] sequence = new int [ MAX + 1 ] ; static void vanEckSequence ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { sequence [ i ] = 0 ; } for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( sequence [ j ] == sequence [ i ] ) { sequence [ i + 1 ] = i - j ; break ; } } } } static int getCount ( int n ) { int count = 1 ; int i = n - 1 ; while ( sequence [ i + 1 ] != 0 ) { count ++ ; i = i - sequence [ i + 1 ] ; } return count ; } public static void Main ( string [ ] args ) { vanEckSequence ( ) ; int n = 5 ; Console . WriteLine ( getCount ( n ) ) ; n = 11 ; Console . WriteLine ( getCount ( n ) ) ; } }
Program to find the time remaining for the day to complete | C # program to Number of pairs of lines having integer intersection points ; Function to find the remaining time ; Formula for total remaining minutes = 1440 - 60 h - m ; Remaining hours ; Remaining minutes ; Driver code ; Current time ; Get the remaining time
using System ; class GFG { static void remainingTime ( int h , int m ) { int totalMin , hoursRemaining , minRemaining ; totalMin = 1440 - 60 * h - m ; hoursRemaining = totalMin / 60 ; minRemaining = totalMin % 60 ; Console . WriteLine ( hoursRemaining + " : : " + minRemaining ) ; } public static void Main ( ) { int h = 0 , m = 1 ; remainingTime ( h , m ) ; } }
Goldbach 's Weak Conjecture for Odd numbers | Function to check if a number can be represent as as a sum of 3 prime ; Driver code
using System ; class GFG { static void check ( int n ) { if ( n % 2 == 1 && n > 5 ) { Console . Write ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } public static void Main ( String [ ] args ) { int a = 3 ; int b = 7 ; check ( a ) ; check ( b ) ; } }
Number of ways to reach ( X , Y ) in a matrix starting from the origin | C # implementation of the approach ; To store the factorial and factorial mod inverse of the numbers ; Function to find ( a ^ m1 ) % mod ; Function to find the factorial of all the numbers ; Function to find the factorial modinverse of all the numbers ; Function to return nCr ; Function to return the number of ways to reach ( X , Y ) in a matrix with the given moves starting from the origin ; Driver code
using System . Collections . Generic ; using System ; class GFG { static long [ ] factorial = new long [ 1000005 ] ; static long [ ] modinverse = new long [ 1000005 ] ; static long mod = 1000000007 ; static int N = 1000005 ; static long power ( long a , long m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( a * a ) % mod ; else if ( ( m1 & 1 ) != 0 ) return ( a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; } static void modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } static long binomial ( int n , int r ) { if ( r > n ) return 0 ; long a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( a * modinverse [ r ] ) % mod ; return a ; } static long ways ( long x , long y ) { factorialfun ( ) ; modinversefun ( ) ; if ( ( 2 * x - y ) % 3 == 0 && ( 2 * y - x ) % 3 == 0 ) { long m = ( 2 * x - y ) / 3 ; long n = ( 2 * y - x ) / 3 ; System . out . println ( n + m + " ▁ " + n ) ; return binomial ( ( int ) ( n + m ) , ( int ) n ) ; } return 0 ; } public static void Main ( ) { long x = 3 , y = 3 ; Console . WriteLine ( ways ( x , y ) ) ; } }
Find Nth even length palindromic number formed using digits X and Y | C # program to find nth even palindromic number of only even length composing of 4 ' s ▁ and ▁ 5' s . ; Utility function to compute n 'th palindrome number  ; Calculate the length from above formula as discussed above ; Calculate rank for length L ; Mask to check if i 't bit   is set or not  ; If bit is set append '5' else append '4' ;  ; Driver Code
using System ; class GFG { static String solve ( int n , char x , char y ) { int length = ( int ) Math . Ceiling ( Math . Log ( n + 2 ) / Math . Log ( 2 ) ) - 1 ; int rank = n - ( 1 << length ) + 1 ; String left = " " , right = " " ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int mask = ( 1 << i ) ; int bit = mask & rank ; if ( bit > 0 ) { left += y ; right += y ; } else { left += x ; right += x ; } } right = reverse ( right ) ; String res = left + right ; return res ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { int n = 23 ; char x = '4' , y = '5' ; String ans = solve ( n , x , y ) ; Console . WriteLine ( ans ) ; } }
Maximum of all the integers in the given level of Pascal triangle | C # implementation of the approach ; Function for the binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the maximum value in the nth level of the Pascal 's triangle ; Driver code
using System ; class GFG { static int binomialCoeff ( int n , int k ) { int [ , ] C = new int [ n + 1 , k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } return C [ n , k ] ; } static int findMax ( int n ) { return binomialCoeff ( n , n / 2 ) ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( findMax ( n ) ) ; } }
Find two co | C # implementation of the approach ;  ; Function to find the required numbers ; GCD of the given numbers ; Printing the required numbers ; Driver code
using System ; class GFG { public static int findGCD ( int a , int b ) { if ( b == 0 ) return a ; else return findGCD ( b , a % b ) ; } static void findNumbers ( int a , int b ) { int gcd = findGCD ( a , b ) ; Console . Write ( ( a / gcd ) + " ▁ " + ( b / gcd ) ) ; } static public void Main ( ) { int a = 12 , b = 16 ; findNumbers ( a , b ) ; } }
Find the remaining balance after the transaction | C # program to find the remaining balance ; Function to find the balance ; Check if the transaction can be successful or not ; Transaction is successful ; Transaction is unsuccessful ; Driver Code
using System ; class GFG { static void findBalance ( int x , float bal ) { if ( x % 10 == 0 && ( ( float ) x + 1.50 ) <= bal ) { Console . Write ( " { 0 : F2 } STRNEWLINE " , bal - x - 1.50 ) ; } else { Console . Write ( " { 0 : F2 } STRNEWLINE " , bal ) ; } } public static void Main ( String [ ] args ) { int x = 50 ; float bal = ( float ) 100.50 ; findBalance ( x , bal ) ; } }
Count number of pairs in array having sum divisible by K | SET 2 | C # Program to count pairs whose sum divisible by ' K ' ; Program to count pairs whose sum divisible by ' K ' ; Create a frequency array to count occurrences of all remainders when divided by K ; To store count of pairs . ; Traverse the array , compute the remainder and add k - remainder value hash count to ans ; Count number of ( A [ i ] , ( K - rem ) % K ) pairs ; Increment count of remainder in hash map ; Driver code
using System ; class GFG { static int countKdivPairs ( int [ ] A , int n , int K ) { int [ ] freq = new int [ K ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int rem = A [ i ] % K ; ans += freq [ ( K - rem ) % K ] ; freq [ rem ] ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 2 , 1 , 7 , 5 , 3 } ; int n = A . Length ; int K = 4 ; Console . WriteLine ( countKdivPairs ( A , n , K ) ) ; } }
Minimum number of Bottles visible when a bottle can be enclosed inside another Bottle | C # code for the above approach ; Driver code ; Find the solution
using System ; using System . Collections . Generic ; class GFG { static void min_visible_bottles ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int ans = 0 ; 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 ) ; } ans = Math . Max ( ans , mp [ arr [ i ] ] ) ; } Console . Write ( " Minimum ▁ number ▁ of ▁ " + " Visible ▁ Bottles ▁ are : ▁ " + ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int n = 8 ; int [ ] arr = { 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 } ; min_visible_bottles ( arr , n ) ; } }