codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of steps required to place all 1 s at a single index | C # Program to implement the above approach ; Function to print minimum steps required to shift all 1 s to a single index in a binary array ; Size of array ; Used to store cumulative sum ; Initialize count ; Traverse the array in forward direction ; Steps needed to store all previous ones to ith index ; Count number of 1 s present till i - th index ; Initialize count ; Traverse the array in backward direction ; Steps needed to store all 1 s to the right of i at current index ; Count number of 1 s present after i - th index ; Print the number of steps required ; Driver Code
using System ; class GFG { static void minsteps ( int [ ] A ) { int n = A . Length ; int [ ] left = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { left [ i ] = 0 ; } int [ ] right = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { right [ i ] = 0 ; } int [ ] res = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { res [ i ] = 0 ; } int count = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { left [ i ] = left [ i - 1 ] + count ; count += A [ i ] ; } count = A [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] + count ; count += A [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { res [ i ] = left [ i ] + right [ i ] ; Console . Write ( res [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 0 , 1 , 0 } ; minsteps ( A ) ; } }
Minimize flips on K | C # program for the above approach ; Function to find the minimum number K - length subarrays required to be flipped to make all array elements 1 ; Stores whether an element can be flipped or not ; Store the required number of flips ; Traverse the array , A [ ] ; Find the prefix sum for the indices i > 0 ; Check if the current element is required to be flipped ; If subarray of size K is not possible , then print - 1 and return ; Increment ans by 1 ; Change the current state of the element ; Decrement isFlipped [ i + K ] ; If subarray of size K is not possible , then print - 1 and return ; Increment ans by 1 ; Change the current state of the element ; Decrement isFlipped [ i + K ] ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void minimumOperations ( List < int > A , int K ) { List < int > isflipped = new List < int > ( ) ; for ( int i = 0 ; i < A . Count + 1 ; i ++ ) isflipped . Add ( 0 ) ; int ans = 0 ; for ( int i = 0 ; i < A . Count ; i ++ ) { if ( i > 0 ) { isflipped [ i ] += isflipped [ i - 1 ] ; isflipped [ i ] %= 2 ; } if ( A [ i ] == 0 && isflipped [ i ] == 0 ) { if ( ( A . Count - i + 1 ) <= K ) { Console . Write ( - 1 ) ; return ; } ans += 1 ; isflipped [ i ] += 1 ; isflipped [ i + K ] -= 1 ; } else if ( A [ i ] == 1 && isflipped [ i ] != 0 ) { if ( ( A . Count - i + 1 ) <= K ) { Console . Write ( - 1 ) ; return ; } ans += 1 ; isflipped [ i ] += 1 ; isflipped [ i + K ] -= 1 ; } } Console . WriteLine ( ans ) ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 0 , 1 , 0 } ; int K = 1 ; minimumOperations ( arr , K ) ; } }
Calculate sum of scores after N days based on given conditions | C # Program to implement the above approach ; Function to c sum of calculate sum of scores after n days ; Store the required sum ; Store the score on previous monday and current day respectively ; Iterate over the range [ 1 , n ] ; If the current day is monday ; Increment score of prev_monday by 1 ; Update score of current day ; Add score of current day and increment score for next day ; Print the result ; Driver Code
using System ; class GFG { static void findScoreSum ( int n ) { int total = 0 ; int prev_monday = 0 , curr_day = 0 ; for ( int day = 1 ; day <= n ; day ++ ) { if ( day % 7 == 1 ) { prev_monday ++ ; curr_day = prev_monday ; } total += curr_day ++ ; } Console . Write ( total ) ; } public static void Main ( String [ ] args ) { int N = 8 ; findScoreSum ( N ) ; } }
Calculate sum of scores after N days based on given conditions | C # program for the above approach ; Function to calculate sum of scores after n days ; Store the number of full weeks ; Stores the remaining days in the last week ; Store the sum of scores in the first F full weeks ; Store the sum of scores in the last week ; Print the result ; Driver Code
using System ; class GFG { static void findScoreSum ( int n ) { int F = n / 7 ; int D = n % 7 ; int fullWeekScore = ( 49 + 7 * F ) * F / 2 ; int lastNonFullWeekScore = ( 2 * F + D + 1 ) * D / 2 ; Console . WriteLine ( fullWeekScore + lastNonFullWeekScore ) ; } static public void Main ( ) { int N = 8 ; findScoreSum ( N ) ; } }
Largest number up to N whose modulus with X is equal to Y modulo X | C # program for the above approach ; Function to print the largest number upto N whose modulus with X is same as Y % X ; Stores the required number ; Update num as the result ; Return the resultant number ; Driver Code
using System ; class GFG { static long maximumNum ( long X , long Y , long N ) { long num = 0 ; if ( N - N % X + Y <= N ) { num = N - N % X + Y ; } else { num = N - N % X - ( X - Y ) ; } return num ; } public static void Main ( string [ ] args ) { long X = 10 ; long Y = 5 ; long N = 15 ; Console . WriteLine ( maximumNum ( X , Y , N ) ) ; } }
Generate an N | C # program for the above approach ; Function to calculate GCD of two integers ; Function to calculate GCD of a given array ; Utility function to check for all the possible combinations ; If an N - length sequence is obtained ; If GCD of the sequence is K ; Otherwise ; Add an element from the first array ; Recursively proceed further ; If combination satisfies the necessary condition ; Remove the element from the combination ; Add an element from the second array ; Recursive proceed further ; If combination satisfies the necessary condition ; Remove the element from the combination ; Function to check all the possible combinations ; Stores the subsequence ; If GCD is not equal to K for any combination ; Driver Code ; Given arrays ; Given value of K ; Function call to generate the required subsequence
using System ; using System . Collections . Generic ; class GFG { static int GCD ( int a , int b ) { if ( b < 1 ) return a ; return GCD ( b , a % b ) ; } static int GCDArr ( List < int > a ) { int ans = a [ 0 ] ; foreach ( int i in a ) ans = GCD ( ans , i ) ; return ans ; } static bool findSubseqUtil ( List < int > a , List < int > b , List < int > ans , int k , int i ) { if ( ans . Count == a . Count ) { if ( GCDArr ( ans ) == k ) { Console . Write ( " [ " ) ; int m = ans . Count ; for ( int j = 0 ; j < m - 1 ; j ++ ) Console . Write ( ans [ j ] + " , ▁ " ) ; Console . Write ( ans [ m - 1 ] + " ] " ) ; return true ; } else return false ; } ans . Add ( a [ i ] ) ; bool temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp ) return true ; ans . RemoveAt ( ans . Count - 1 ) ; ans . Add ( b [ i ] ) ; temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp ) return true ; ans . RemoveAt ( ans . Count - 1 ) ; return false ; } static void findSubseq ( List < int > A , List < int > B , int K , int i ) { List < int > ans = new List < int > ( ) ; findSubseqUtil ( A , B , ans , K , i ) ; if ( ans . Count < 1 ) Console . WriteLine ( - 1 ) ; } public static void Main ( ) { List < int > A = new List < int > { 5 , 3 , 6 , 2 , 9 } ; List < int > B = new List < int > { 21 , 7 , 14 , 12 , 28 } ; int K = 3 ; findSubseq ( A , B , K , 0 ) ; } }
Minimize count of swaps of adjacent elements required to make an array increasing | C # program for the above approach ; Function to count minimum number of operations required to obtain an increasing array from given array A [ ] ; Store the required result ; Traverse the array A [ ] ; If the current element is not in its correct position ; Check if it is present at index i - 1 ; Check if it is present at index i - 2 ; Otherwise , print - 1 ( Since A [ i ] can not be swapped more than twice ) ; Print the result ; Driver code ; Given array ; Store the size of the array
using System ; class GFG { static void minimumOperations ( int [ ] A , int n ) { int cnt = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != ( i + 1 ) ) { if ( ( ( i - 1 ) >= 0 ) && A [ i - 1 ] == ( i + 1 ) ) { cnt ++ ; int t = A [ i ] ; A [ i ] = A [ i - 1 ] ; A [ i - 1 ] = t ; } else if ( ( ( i - 2 ) >= 0 ) && A [ i - 2 ] == ( i + 1 ) ) { cnt += 2 ; A [ i - 2 ] = A [ i - 1 ] ; A [ i - 1 ] = A [ i ] ; A [ i ] = i + 1 ; } else { Console . WriteLine ( - 1 ) ; return ; } } } Console . WriteLine ( cnt ) ; } public static void Main ( ) { int [ ] A = { 7 , 3 , 2 , 1 , 4 } ; int n = A . Length ; minimumOperations ( A , n ) ; } }
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | Set | C # program for the above approach ; Function to count the occurrences of X in the generated square matrix ; Store the required result ; Iterate over the range [ 1 , N ] ; Check if x is a multiple of i or not ; Check if the other multiple exists in the range or not ; Print the result ; Driver Code
using System ; class GFG { static void countOccurrences ( int n , int x ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( x % i == 0 ) { if ( x / i <= n ) count ++ ; } } Console . WriteLine ( count ) ; } public static void Main ( String [ ] args ) { int N = 7 , X = 12 ; countOccurrences ( N , X ) ; } }
Reduce all array elements to zero by performing given operations thrice | C # program for the above approach ; Function to reduce all array elements to zero ; If size of array is 1 ; First operation ; 2 nd Operation ; 3 rd Operation ; Otherwise ; 1 st Operation ; 2 nd Operation ; 3 rd Operation ; Driver Code ; Input ; Function call to make all array elements equal to 0
using System ; class GFG { static void ConvertArray ( int [ ] arr , int N ) { if ( N == 1 ) { Console . WriteLine ( " Operation ▁ 1 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; Console . WriteLine ( " Added ▁ elements : ▁ " + - 1 * arr [ 0 ] ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Operation ▁ 2 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; Console . WriteLine ( " Added ▁ elements : ▁ " + 1 * arr [ 0 ] ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Operation ▁ 3 ▁ : ▁ " + 1 + " ▁ " + 1 ) ; Console . WriteLine ( " Added ▁ elements : ▁ " + - 1 * arr [ 0 ] ) ; } else { Console . WriteLine ( " Operation ▁ 1 ▁ : ▁ " + 1 + " ▁ " + N ) ; Console . Write ( " Added ▁ elements : ▁ " ) ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( - 1 * arr [ i ] * N + " ▁ " ) ; } Console . WriteLine ( ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Operation ▁ 2 ▁ : ▁ " + 1 + " ▁ " + ( N - 1 ) ) ; Console . Write ( " Added ▁ elements : ▁ " ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { Console . Write ( arr [ i ] * ( N - 1 ) + " ▁ " ) ; } Console . WriteLine ( ) ; Console . WriteLine ( ) ; Console . WriteLine ( " Operation ▁ 3 ▁ : ▁ " + N + " ▁ " + N ) ; Console . Write ( " Added ▁ elements : ▁ " ) ; Console . WriteLine ( arr [ N - 1 ] * ( N - 1 ) ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 4 } ; int N = arr . Length ; ConvertArray ( arr , N ) ; } }
Count pairs from an array with even product of count of distinct prime factors | C # program for the above approach ; Function to calculate count of distinct prime factors of a number ; Sieve of Eratosthenes ; Function to count pairs with even product of distinct prime factors ; Stores count of distinct prime factors ; Stores the count of numbers with even prime factors in B [ ] ; Stores the count of numbers with odd prime factors in B [ ] ; Even Product Pairs ; Traverse the array B [ ] ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; Increment oddCount by 1 ; Increment evenCount by 1 ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; odd * even = even ; If count of prime factors is even ; even * odd = even even * even = even ; Driver Code
using System ; public class GFG { static int MAX = 1000000 ; static void countOfPrimefactors ( int [ ] CountDistinct ) { bool [ ] prime = new bool [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } } static int CountEvenPair ( int [ ] A , int [ ] B , int N , int M ) { int [ ] countDistinct = new int [ ( MAX + 1 ) ] ; countOfPrimefactors ( countDistinct ) ; int evenCount = 0 ; int oddCount = 0 ; int evenPairs = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( countDistinct [ B [ i ] ] == 0 ) continue ; if ( ( countDistinct [ B [ i ] ] & 1 ) != 0 ) { oddCount ++ ; } else { evenCount ++ ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( countDistinct [ A [ i ] ] == 0 ) continue ; if ( ( countDistinct [ A [ i ] ] & 1 ) != 0 ) { evenPairs += ( evenCount ) ; } else { evenPairs += evenCount + oddCount ; } } return evenPairs ; } public static void Main ( string [ ] args ) { int [ ] A = { 1 , 2 , 3 } ; int [ ] B = { 4 , 5 , 6 } ; int N = A . Length ; int M = B . Length ; Console . WriteLine ( CountEvenPair ( A , B , N , M ) ) ; } }
Minimum array elements required to be subtracted from either end to reduce K to 0 | C # program for the above approach ; Function to find the length of longest subarray having sum K ; Stores the index of the prefix sum ; Traverse the given array ; Update sum ; If the subarray starts from index 0 ; Add the current prefix sum with index if it is not present in the map um ; Check if sum - K is present in Map um or not ; Update the maxLength ; Return the required maximum length ; Function to find the minimum removal of array elements required to reduce K to 0 ; Stores the sum of the array ; Traverse the array arr [ ] ; Update sum of the array ; Find maxLen ; If the subarray with sum doesn 't exist ; Otherwise , print the required maximum length ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int longestSubarray ( int [ ] arr , int N , int K ) { Dictionary < int , int > um = new Dictionary < int , int > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; if ( sum == K ) maxLen = i + 1 ; if ( ! um . ContainsKey ( sum ) ) um [ sum ] = i ; if ( um . ContainsKey ( sum - K ) ) { if ( maxLen < ( i - um [ sum - K ] ) ) maxLen = i - um [ sum - K ] ; } } return maxLen ; } static void minRequiredOperation ( int [ ] arr , int N , int K ) { int TotalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) TotalSum += arr [ i ] ; int maxLen = longestSubarray ( arr , N , TotalSum - K ) ; if ( maxLen == - 1 ) { Console . WriteLine ( - 1 ) ; } else Console . WriteLine ( N - maxLen ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 1 , 1 , 2 } ; int K = 4 ; int N = arr . Length ; minRequiredOperation ( arr , N , K ) ; } }
Minimum number of digits required to be removed to make a number divisible by 4 | C # program for the above approach ; Function to count the minimum number of digits required to be removed to make a given number divisible by 4 ; Store the size of the string ; Stores the required result ; Check for every pair of digits if the number formed by them is divisible by 4 or not ; Store s [ i ] in a variable ; If it is divisible by 2 ; Store the number formed by s [ j ] and s [ i ] ; Check if it is divisible by 4 ; Store the number of digits required to be deleted ; Update ans ; If value of ans is unchanged , then check if any s [ i ] is divisible by 4 ; If true , update ans to n - 1 ; Print the result ; Driver Code
using System ; class GFG { static void minimumDeletions ( string s ) { int n = s . Length ; int ans = n ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int t = s [ i ] - '0' ; if ( t % 2 == 0 ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { int num = ( s [ j ] - '0' ) * 10 + t ; if ( num % 4 == 0 ) { int k1 = i - j - 1 ; int k2 = n - i - 1 ; ans = Math . Min ( ans , k1 + k2 ) ; } } } } if ( ans == n ) { for ( int i = 0 ; i < n ; i ++ ) { int num = s [ i ] - '0' ; if ( num % 4 == 0 ) { ans = n - 1 ; } } } Console . WriteLine ( ans ) ; } static public void Main ( ) { string str = "12367" ; minimumDeletions ( str ) ; } }
Generate a sequence such that float division of array elements is maximized | C # program for the above approach ; Function to place the parenthesis such that the result is maximized ; Store the required string ; Add the first integer to string ; If the size of array is 1 ; If the size of array is 2 , print the 1 st integer followed by / operator followed by the next integer ; If size of array is exceeds two , print 1 st integer concatenated with operators ' / ' , ' ( ' and next integers with the operator ' / ' ; Add parenthesis at the end ; Print the final expression ; Driver Code
using System ; class GFG { static void generateSequence ( int [ ] arr , int n ) { string ans = " " ; ans = arr [ 0 ] . ToString ( ) ; if ( n == 1 ) Console . WriteLine ( ans ) ; else if ( n == 2 ) { Console . WriteLine ( ans + " / " + arr [ 1 ] . ToString ( ) ) ; } else { ans += " / ( " + arr [ 1 ] . ToString ( ) ; for ( int i = 2 ; i < n ; i ++ ) { ans += " / " + arr [ i ] . ToString ( ) ; } ans += " ) " ; Console . WriteLine ( ans ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 1000 , 100 , 10 , 2 } ; int N = arr . Length ; generateSequence ( arr , N ) ; } }
Count of pairs having even and odd LCM from an array | C # program for the above approach ; Function to find count of distinct pairs having even LCM and odd LCM ; Store the total number of pairs ; Stores the count of odd numbers in the array ; Traverse the array arr [ ] ; Update the count of pairs with odd LCM ; Print the count of required pairs ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void LCMPairs ( int [ ] arr , int N ) { int total_pairs = ( N * ( N - 1 ) ) / 2 ; int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; } odd = ( odd * ( odd - 1 ) ) / 2 ; Console . Write ( " Even ▁ = ▁ " + ( total_pairs - odd ) + " , ▁ Odd ▁ = ▁ " + odd ) ; } static void Main ( ) { int [ ] arr = { 3 , 6 , 5 , 4 } ; int N = arr . Length ; LCMPairs ( arr , N ) ; } }
Value required to be added to N to obtain the sum of first M multiples of K | C # program for the above approach ; Function to print the value to be added to N to obtain sum of first M multiples of K ; Store the sum of the first M multiples of K ; Store the value to be added to obtain N ; Driver code ; Input
using System ; class GFG { static int printNumber ( int N , int K , int M ) { int sum = K * ( M * ( M + 1 ) / 2 ) ; return sum - N ; } public static void Main ( String [ ] args ) { int N = 17 ; int K = 3 ; int M = 4 ; Console . Write ( printNumber ( N , K , M ) ) ; } }
Count even and odd Bitwise XORs of consecutive numbers in a range [ L , R ] starting from L | C # program for the above approach ; Print count of even and odd numbers of XOR value from L to R ; Store the number of elements between L and R ; Count of even XOR values ; If L is odd and range % 4 = 3 ; Increment even by 1 ; If L is even and range % 4 != 0 ; Increment even by 1 ; Print the answer ; Driver code
using System ; class GFG { static void countEvenOdd ( int L , int R ) { int range = R - L + 1 ; int even = ( range / 4 ) * 2 ; if ( ( L & 1 ) != 0 && ( range % 4 == 3 ) ) { even ++ ; } else if ( ( L & 1 ) == 0 && ( range % 4 != 0 ) ) { even ++ ; } Console . Write ( " Even ▁ = ▁ " + even + " , ▁ Odd ▁ = ▁ " + ( range - even ) ) ; } static void Main ( ) { int L = 2 , R = 7 ; countEvenOdd ( L , R ) ; } }
Minimize swaps of same | C # program for the above approach ; Function to count the number of swaps required to make the sum of ASCII values of the characters of both strings odd ; Initialize alphabets with value ; Initialize values for each alphabet ; Size of the string ; Sum of string S ; Sum of string T ; Stores whether there is any index i such that S [ i ] and T [ i ] have different parities ; Traverse the strings ; Update sum1 and sum2 ; If S [ i ] and T [ i ] have different parities ; If sum1 and sum2 are both odd ; If sum1 and sum2 are both even ; If exists print 1 ; Otherwise ; If sum1 and sum2 are of different parities ; Driver Code ; Function Call
using System ; public class GFG { static void countSwaps ( string S , string T ) { int [ ] value = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) value [ i ] = i + 1 ; int N = S . Length ; int sum1 = 0 ; int sum2 = 0 ; bool flag = false ; for ( int i = 0 ; i < N ; i ++ ) { sum1 += value [ S [ i ] - ' a ' ] ; sum2 += value [ T [ i ] - ' a ' ] ; if ( ( value [ S [ i ] - ' a ' ] % 2 == 0 && value [ T [ i ] - ' a ' ] % 2 == 1 ) || ( value [ S [ i ] - ' a ' ] % 2 == 1 && value [ T [ i ] - ' a ' ] % 2 == 0 ) ) flag = false ; } if ( sum1 % 2 == 1 && sum2 % 2 == 1 ) Console . Write ( "0 STRNEWLINE " ) ; else if ( sum1 % 2 == 0 && sum2 % 2 == 0 ) { if ( flag ) Console . Write ( "1" ) ; else Console . Write ( " - 1" ) ; } else { Console . Write ( " - 1" ) ; } } public static void Main ( String [ ] args ) { string S = " acd " ; string T = " dbf " ; countSwaps ( S , T ) ; } }
Count distinct prime triplets up to N such that sum of two primes is equal to the third prime | C # program for the above approach ; Function to check if a number is a prime or not ; Function to count the number of valid prime triplets ; Stores the count of prime triplets ; Iterate from 2 to N and check for each p , whether p & ( p - 2 ) are prime or not ; Print the count obtained ; Driver Code
using System ; public class GFG { static bool isPrime ( int N ) { if ( N <= 1 ) return false ; for ( int i = 2 ; i <= Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } static void countPrimeTuples ( int N ) { int count = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime ( i ) && isPrime ( i - 2 ) ) count ++ ; } Console . WriteLine ( count ) ; } static public void Main ( ) { int N = 6 ; countPrimeTuples ( N ) ; } }
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | C # program to implement the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Store the required answer ; Check for all possible pairs ; If the condition satisfy then increment count by 1 ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static void countPairs ( int [ ] A , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( A [ i ] A [ j ] ) > ( A [ i ] & A [ j ] ) ) { count ++ ; } } Console . Write ( count ) ; } public static void Main ( ) { int [ ] A = { 1 , 4 , 7 } ; int N = A . Length ; countPairs ( A , N ) ; } }
Count maximum number of consumable candies | C # implememtation of the above approach ; Function to find the count of maximum consumable candies ; Store the count of total candies ; Stores the count of maximum consumable candies ; Checks if it is safe to counsume all candies ; Traverse the array arr [ ] ; If A [ i ] + M is greater than B [ i ] ; Mark all_safe as false ; Update ans ; Update ans ; Increment total by A [ i ] ; If all_safe is true ; Otherwise , ; Driver code ; Function call to find maximum consumable candies
using System ; class GFG { static int maximumCandy ( int [ ] candies , int [ ] safety , int N , int M ) { int total = 0 ; int ans = Int32 . MaxValue ; bool all_safe = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( candies [ i ] + M > safety [ i ] ) { all_safe = false ; ans = Math . Min ( ans , safety [ i ] ) ; } else { ans = Math . Min ( ans , candies [ i ] + M ) ; } total += candies [ i ] ; } if ( all_safe ) return total ; else return ans ; } static void Main ( ) { int [ ] A = { 4 , 5 , 2 , 3 } ; int [ ] B = { 8 , 13 , 6 , 4 } ; int M = 5 ; int N = A . Length ; Console . WriteLine ( maximumCandy ( A , B , N , M ) ) ; } }