codeparrot/xlcost-text-to-code huggingface dataset:
Minimum length of the shortest path of a triangle | C # program to illustrate the above problem ; function to get the minimum length of the shorter side of the triangle ; traversing through each points on the plane ; if sum of a points is greater than the previous one , the maximum gets replaced ; print the length ; Driver code ; initialize the number of points ; points on the plane
using System ; class GFG { static void shortestLength ( int n , int [ ] x , int [ ] y ) { int answer = 0 ; int i = 0 ; while ( n != 0 && i < x . Length ) { if ( x [ i ] + y [ i ] > answer ) answer = x [ i ] + y [ i ] ; i ++ ; } Console . WriteLine ( " Length ▁ - > ▁ " + answer ) ; Console . WriteLine ( " Path ▁ - > ▁ " + " ( ▁ 1 , ▁ " + answer + " ▁ ) " + " and ▁ ( ▁ " + answer + " , ▁ 1 ▁ ) " ) ; } static public void Main ( ) { int n = 4 ; int [ ] x = new int [ ] { 1 , 4 , 2 , 1 } ; int [ ] y = new int [ ] { 4 , 1 , 1 , 2 } ; shortestLength ( n , x , y ) ; } }
Intersecting rectangle when bottom | C # program to find intersection rectangle of given two rectangles . ; function to find intersection rectangle of given two rectangles . ; gives bottom - left point of intersection rectangle ; gives top - right point of intersection rectangle ; no intersection ; gives top - left point of intersection rectangle ; gives bottom - right point of intersection rectangle ; Driver code ; bottom - left and top - right corners of first rectangle ; bottom - left and top - right corners of first rectangle ; function call
using System ; class GFG { static void FindPoints ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x4 , int y4 ) { int x5 = Math . Max ( x1 , x3 ) ; int y5 = Math . Max ( y1 , y3 ) ; int x6 = Math . Min ( x2 , x4 ) ; int y6 = Math . Min ( y2 , y4 ) ; if ( x5 > x6 y5 > y6 ) { Console . WriteLine ( " No ▁ intersection " ) ; return ; } Console . Write ( " ( " + x5 + " , ▁ " + y5 + " ) ▁ " ) ; Console . Write ( " ( " + x6 + " , ▁ " + y6 + " ) ▁ " ) ; int x7 = x5 ; int y7 = y6 ; Console . Write ( " ( " + x7 + " , ▁ " + y7 + " ) ▁ " ) ; int x8 = x6 ; int y8 = y5 ; Console . Write ( " ( " + x8 + " , ▁ " + y8 + " ) ▁ " ) ; } public static void Main ( ) { int x1 = 0 , y1 = 0 , x2 = 10 , y2 = 8 ; int x3 = 2 , y3 = 3 , x4 = 7 , y4 = 9 ; FindPoints ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) ; } }
Find Corners of Rectangle using mid points | C # program to find corner points of a rectangle using given length and middle points . ; Structure to represent a co - ordinate point ; This function receives two points and length of the side of rectangle and prints the 4 corner points of the rectangle ; horizontal rectangle ; vertical rectangle ; slanted rectangle ; calculate slope of the side ; calculate displacements along axes ; Driver code
using System ; class GFG { public class Point { public float x , y ; public Point ( ) { x = y = 0 ; } public Point ( float a , float b ) { x = a ; y = b ; } } ; static void printCorners ( Point p , Point q , float l ) { Point a = new Point ( ) , b = new Point ( ) , c = new Point ( ) , d = new Point ( ) ; if ( p . x == q . x ) { a . x = ( float ) ( p . x - ( l / 2.0 ) ) ; a . y = p . y ; d . x = ( float ) ( p . x + ( l / 2.0 ) ) ; d . y = p . y ; b . x = ( float ) ( q . x - ( l / 2.0 ) ) ; b . y = q . y ; c . x = ( float ) ( q . x + ( l / 2.0 ) ) ; c . y = q . y ; } else if ( p . y == q . y ) { a . y = ( float ) ( p . y - ( l / 2.0 ) ) ; a . x = p . x ; d . y = ( float ) ( p . y + ( l / 2.0 ) ) ; d . x = p . x ; b . y = ( float ) ( q . y - ( l / 2.0 ) ) ; b . x = q . x ; c . y = ( float ) ( q . y + ( l / 2.0 ) ) ; c . x = q . x ; } else { float m = ( p . x - q . x ) / ( q . y - p . y ) ; float dx = ( float ) ( ( l / Math . Sqrt ( 1 + ( m * m ) ) ) * 0.5 ) ; float dy = m * dx ; a . x = p . x - dx ; a . y = p . y - dy ; d . x = p . x + dx ; d . y = p . y + dy ; b . x = q . x - dx ; b . y = q . y - dy ; c . x = q . x + dx ; c . y = q . y + dy ; } Console . Write ( ( int ) a . x + " , ▁ " + ( int ) a . y + " ▁ STRNEWLINE " + ( int ) b . x + " , ▁ " + ( int ) b . y + " STRNEWLINE " + ( int ) c . x + " , ▁ " + ( int ) c . y + " ▁ STRNEWLINE " + ( int ) d . x + " , ▁ " + ( int ) d . y + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { Point p1 = new Point ( 1 , 0 ) , q1 = new Point ( 1 , 2 ) ; printCorners ( p1 , q1 , 2 ) ; Point p = new Point ( 1 , 1 ) , q = new Point ( - 1 , - 1 ) ; printCorners ( p , q , ( float ) ( 2 * Math . Sqrt ( 2 ) ) ) ; } }
Minimize cost to modify the Array such that even indices have even elements and vice versa | C # program for the above approach ; Function to find the minimum cost to modify the array according to the given criteria ; Count of wrong positioned odd and even elements ; Odd Count ; Even Count ; Swapping Cost ; Decrementing cost after swapping ; Only decrementing cost ; Return the minimum cost of the two cases ; Driver Code
using System ; public class GFG { public static int minimumCost ( int [ ] arr , int N , int X , int Y ) { int even_count = 0 , odd_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) > 0 && ( i % 2 == 0 ) ) { odd_count ++ ; } if ( ( arr [ i ] % 2 ) == 0 && ( i & 1 ) > 0 ) { even_count ++ ; } } int cost1 = X * Math . Min ( odd_count , even_count ) ; int cost2 = Y * ( Math . Max ( odd_count , even_count ) - Math . Min ( odd_count , even_count ) ) ; int cost3 = ( odd_count + even_count ) * Y ; return Math . Min ( cost1 + cost2 , cost3 ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 3 , 7 , 2 , 1 } ; int X = 10 , Y = 2 ; int N = arr . Length ; Console . WriteLine ( minimumCost ( arr , N , X , Y ) ) ; } }
Minimum product of maximum and minimum element over all possible subarrays | C # program for the above approach ; Function to find the minimum product of the minimum and maximum among all the possible subarrays ; Stores resultant minimum product ; Traverse the given array arr [ ] ; Min of product of all two pair of consecutive elements ; Return the resultant value ; Driver Code
using System ; public class GFG { static int findMinMax ( int [ ] a ) { int min_val = 1000000000 ; for ( int i = 1 ; i < a . Length ; ++ i ) { min_val = Math . Min ( min_val , a [ i ] * a [ i - 1 ] ) ; } return min_val ; } public static void Main ( string [ ] args ) { int [ ] arr = { 6 , 4 , 5 , 6 , 2 , 4 , 1 } ; Console . WriteLine ( findMinMax ( arr ) ) ; } }
Sum of all nodes with smaller values at a distance K from a given node in a BST | C # program for the above approach ; Structure of Tree ; Constructor ; Function to add the node to the sum below the target node ; Base Case ; If Kth distant node is reached ; Recur for the left and the right subtrees ; Function to find the K distant nodes from target node , it returns - 1 if target node is not present in tree ; Base Case 1 ; If target is same as root . ; Recurr for the left subtree ; Tree is BST so reduce the search space ; Check if target node was found in left subtree ; If root is at distance k from the target ; Node less than target will be present in left ; When node is not present in the left subtree ; If Kth distant node is reached ; Node less than target at k distance maybe present in the left tree ; If target was not present in the left nor in right subtree ; Function to insert a node in BST ; If root is null ; Insert the data in right half ; Insert the data in left half ; Return the root node ; Function to find the sum of K distant nodes from the target node having value less than target node ; Stores the sum of nodes having values < target at K distance ; Print the resultant sum ; Driver Code ; Create the Tree
using System ; public class GFG { static int sum ; public class TreeNode { public int data ; public TreeNode left ; public TreeNode right ; public TreeNode ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } ; static void kDistanceDownSum ( TreeNode root , int k ) { if ( root == null k < 0 ) return ; if ( k == 0 ) { sum += root . data ; return ; } kDistanceDownSum ( root . left , k - 1 ) ; kDistanceDownSum ( root . right , k - 1 ) ; } static int kDistanceSum ( TreeNode root , int target , int k ) { if ( root == null ) return - 1 ; if ( root . data == target ) { kDistanceDownSum ( root . left , k - 1 ) ; return 0 ; } int dl = - 1 ; if ( target < root . data ) { dl = kDistanceSum ( root . left , target , k ) ; } if ( dl != - 1 ) { if ( dl + 1 == k ) sum += root . data ; return - 1 ; } int dr = - 1 ; if ( target > root . data ) { dr = kDistanceSum ( root . right , target , k ) ; } if ( dr != - 1 ) { if ( dr + 1 == k ) sum += root . data ; else kDistanceDownSum ( root . left , k - dr - 2 ) ; return 1 + dr ; } return - 1 ; } static TreeNode insertNode ( int data , TreeNode root ) { if ( root == null ) { TreeNode node = new TreeNode ( data ) ; return node ; } else if ( data > root . data ) { root . right = insertNode ( data , root . right ) ; } else if ( data <= root . data ) { root . left = insertNode ( data , root . left ) ; } return root ; } static void findSum ( TreeNode root , int target , int K ) { sum = 0 ; kDistanceSum ( root , target , K ) ; Console . Write ( sum ) ; } public static void Main ( String [ ] args ) { TreeNode root = null ; int N = 11 ; int [ ] tree = { 3 , 1 , 7 , 0 , 2 , 5 , 10 , 4 , 6 , 9 , 8 } ; for ( int i = 0 ; i < N ; i ++ ) { root = insertNode ( tree [ i ] , root ) ; } int target = 7 ; int K = 2 ; findSum ( root , target , K ) ; } }
Find Nth item distributed from infinite items of infinite types based on given conditions | C # code for the above approach ; Function to find the type of the item given out according to the given rules ; Stores the count of item given out at each step ; Iterate over the days from 1 ; Iterate over type of item on that day ; Count of items given out should exceed n ; Driver Code
using System ; public class GFG { static int itemType ( int n ) { int count = 0 ; for ( int day = 1 ; ; day ++ ) { for ( int type = day ; type > 0 ; type -- ) { count += type ; if ( count >= n ) return type ; } } } static public void Main ( ) { int N = 10 ; Console . WriteLine ( itemType ( N ) ) ; } }
Find the sum of all array elements that are equidistant from two consecutive powers of 2 | C # program for the above approach ; Function to print the sum of array elements that are equidistant from two consecutive powers of 2 ; Stores the resultant sum of the array elements ; Traverse the array arr [ ] ; Stores the power of 2 of the number arr [ i ] ; Stores the number which is power of 2 and lesser than or equal to arr [ i ] ; Stores the number which is power of 2 and greater than or equal to arr [ i ] ; If arr [ i ] - LesserValue is the same as LargerValue - arr [ i ] ; Increment res by arr [ i ] ; Return the resultant sum res ; Driver Code
using System ; class GFG { static int FindSum ( int [ ] arr , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int power = ( int ) ( Math . Log ( arr [ i ] ) / Math . Log ( 2 ) ) ; int LesserValue = ( int ) Math . Pow ( 2 , power ) ; int LargerValue = ( int ) Math . Pow ( 2 , power + 1 ) ; if ( ( arr [ i ] - LesserValue ) == ( LargerValue - arr [ i ] ) ) { res += arr [ i ] ; } } return res ; } public static void Main ( ) { int [ ] arr = { 10 , 24 , 17 , 3 , 8 } ; int N = arr . Length ; Console . WriteLine ( FindSum ( arr , N ) ) ; } }
Find the person who will finish last | C # program for the above approach ; Function to find the person who will finish last ; To keep track of rows and columns having 1 ; Available rows and columns ; Minimum number of choices we have ; If number of choices are odd ; P1 will finish last ; Otherwise , P2 will finish last ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findLast ( int [ , ] mat ) { int m = 3 ; int n = 3 ; HashSet < int > rows = new HashSet < int > ( ) ; HashSet < int > cols = new HashSet < int > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( mat [ i , j ] > 0 ) ) { rows . Add ( i ) ; cols . Add ( j ) ; } } } int avRows = m - rows . Count ; int avCols = n - cols . Count ; int choices = Math . Min ( avRows , avCols ) ; if ( ( choices & 1 ) != 0 ) Console . WriteLine ( " P1" ) ; else Console . WriteLine ( " P2" ) ; } static public void Main ( ) { int [ , ] mat = { { 1 , 0 , 0 } , { 0 , 0 , 0 } , { 0 , 0 , 1 } } ; findLast ( mat ) ; } }
Sum of decimals that are binary representations of first N natural numbers | C # program for the above approach ; Function to find the sum of first N natural numbers represented in binary representation ; Stores the resultant sum ; Iterate until the value of N is greater than 0 ; If N is less than 2 ; Store the MSB position of N ; Iterate in the range [ 1 , x ] and add the contribution of the numbers from 1 to ( 2 ^ x - 1 ) ; Update the value of the cur and add ; Add the cur to ans ; Store the remaining numbers ; Add the contribution by MSB by the remaining numbers ; The next iteration will be repeated for 2 ^ x - 1 ; Print the result ; Driver Code
using System ; class GFG { const int MOD = 1000000007 ; static void sumOfBinaryNumbers ( int n ) { int ans = 0 ; int one = 1 ; while ( true ) { if ( n <= 1 ) { ans = ( ans + n ) % MOD ; break ; } int x = ( int ) Math . Log ( n , 2 ) ; int cur = 0 ; int add = ( one << ( x - 1 ) ) ; for ( int i = 1 ; i <= x ; i ++ ) { cur = ( cur + add ) % MOD ; add = ( add * 10 % MOD ) ; } ans = ( ans + cur ) % MOD ; int rem = n - ( one << x ) + 1 ; int p = ( int ) Math . Pow ( 10 , x ) ; p = ( p * ( rem % MOD ) ) % MOD ; ans = ( ans + p ) % MOD ; n = rem - 1 ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { int N = 3 ; sumOfBinaryNumbers ( N ) ; } }
Nearest Fibonacci Number to N | C # program for the above approach ; Function to find the Fibonacci number which is nearest to N ; Base Case ; Initialize the first & second terms of the Fibonacci series ; Store the third term ; Iterate until the third term is less than or equal to num ; Update the first ; Update the second ; Update the third ; Store the Fibonacci number having smaller difference with N ; Print the result ; Driver Code
using System ; class GFG { static void nearestFibonacci ( int num ) { if ( num == 0 ) { Console . Write ( 0 ) ; return ; } int first = 0 , second = 1 ; int third = first + second ; while ( third <= num ) { first = second ; second = third ; third = first + second ; } int ans = ( Math . Abs ( third - num ) >= Math . Abs ( second - num ) ) ? second : third ; Console . Write ( ans ) ; } public static void Main ( string [ ] args ) { int N = 17 ; nearestFibonacci ( N ) ; } }
Permutation of first N natural numbers having given array as the prefix maximum array | C # program for the above approach ; Function to check if the maximum prefix array of ans [ ] is equal to array arr [ ] ; Initialize a variable , Max ; Traverse the array , ans [ ] ; Store the maximum value upto index i ; If it is not equal to a [ i ] , then return false ; Otherwise return false ; Function to find the permutation of the array whose prefix maximum array is same as the given array a [ ] ; Stores the required permutation ; Stores the index of first occurrence of elements ; Traverse the array a [ ] ; If a [ i ] is not present in um , then store it in um ; Update the ans [ i ] to a [ i ] ; Stores the unvisited numbers ; Fill the array , v [ ] ; Store the index ; Traverse the array , ans [ ] ; Fill v [ j ] at places where ans [ i ] is 0 ; Check if the current permutation maximum prefix array is same as the given array a [ ] ; If true , the print the permutation ; Otherwise , print - 1 ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool checkPermutation ( int [ ] ans , int [ ] a , int n ) { int Max = Int32 . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { Max = Math . Max ( Max , ans [ i ] ) ; if ( Max != a [ i ] ) return false ; } return true ; } static void findPermutation ( int [ ] a , int n ) { int [ ] ans = new int [ n ] ; Dictionary < int , int > um = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! um . ContainsKey ( a [ i ] ) ) { ans [ i ] = a [ i ] ; um [ a [ i ] ] = i ; } } List < int > v = new List < int > ( ) ; int j = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ! um . ContainsKey ( i ) ) { v . Add ( i ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( ans [ i ] == 0 ) { ans [ i ] = v [ j ] ; j ++ ; } } if ( checkPermutation ( ans , a , n ) ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } else Console . Write ( " - 1" ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 4 , 5 , 5 } ; int N = arr . Length ; findPermutation ( arr , N ) ; } }
Count pairs of equal elements possible by excluding each array element once | C # program for the above approach ; Function to count the number of required pairs for every array element ; Initialize a map ; Update the frequency of every element ; Stores the count of pairs ; Traverse the map ; Count the number of ways to select pairs consisting of equal elements only ; Traverse the array ; Print the count for every array element ; Driver code ; Given array ; Size of the array
using System ; using System . Collections . Generic ; class GFG { static void countEqualElementPairs ( int [ ] arr , int N ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! map . ContainsKey ( arr [ i ] ) ) map [ arr [ i ] ] = 1 ; else map [ arr [ i ] ] ++ ; } int total = 0 ; foreach ( KeyValuePair < int , int > e in map ) { total += ( e . Value * ( e . Value - 1 ) ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( total - ( map [ arr [ i ] ] - 1 ) + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 1 , 2 } ; int N = 5 ; countEqualElementPairs ( arr , N ) ; } }
Count of Octal numbers upto N digits | C # program to find the count of natural octal numbers upto N digits ; Function to return the count of natural octal numbers upto N digits ; Loop to iterate from 1 to N and calculating number of octal numbers for every ' i ' th digit . ; Driver code
using System ; class GFG { static int count ( int N ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( int ) ( 7 * Math . Pow ( 8 , i - 1 ) ) ; } return sum ; } public static void Main ( ) { int N = 4 ; Console . WriteLine ( count ( N ) ) ; } }
Palindromic divisors of a number | C # program to find all the palindromic divisors of a number ; Function to check is num is palindromic or not ; Convert n to String str ; Starting and ending index of String str ; If char at s and e are not equals then return false ; Function to find palindromic divisors ; To sore the palindromic divisors of number n ; If n is divisible by i ; Check if number is a perfect square ; Check divisor is palindromic , then store it ; Check if divisors are palindrome ; Check if n / divisors is palindromic or not ; Print all palindromic divisors in sorted order ; Driver code ; Function call to find all palindromic divisors
using System ; using System . Collections . Generic ; class GFG { static bool isPalindrome ( int n ) { String str = String . Join ( " " , n ) ; int s = 0 , e = str . Length - 1 ; while ( s < e ) { if ( str [ s ] != str [ e ] ) { return false ; } s ++ ; e -- ; } return true ; } static void palindromicDivisors ( int n ) { List < int > PalindromDivisors = new List < int > ( ) ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( isPalindrome ( i ) ) { PalindromDivisors . Add ( i ) ; } } else { if ( isPalindrome ( i ) ) { PalindromDivisors . Add ( i ) ; } if ( isPalindrome ( n / i ) ) { PalindromDivisors . Add ( n / i ) ; } } } } PalindromDivisors . Sort ( ) ; for ( int i = 0 ; i < PalindromDivisors . Count ; i ++ ) { Console . Write ( PalindromDivisors [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int n = 66 ; palindromicDivisors ( n ) ; } }
Remove minimum numbers from the array to get minimum OR value | C # implementation of the approach ; Function to return the minimum deletions to get minimum OR ; To store the minimum element ; Find the minimum element from the array ; To store the frequency of the minimum element ; Find the frequency of the minimum element ; Return the readonly answer ; Driver code
using System ; class GFG { static int findMinDel ( int [ ] arr , int n ) { int min_num = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) min_num = Math . Min ( arr [ i ] , min_num ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == min_num ) cnt ++ ; return n - cnt ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 3 , 2 } ; int n = arr . Length ; Console . Write ( findMinDel ( arr , n ) ) ; } }
Number of subarrays with GCD equal to 1 | C # implementation of the approach ; Function to return the required count ; To store the final answer ; To store the GCD starting from index ' i ' ; Loop to find the gcd of each subarray from arr [ i ] to arr [ i ... n - 1 ] ; Increment the count if curr_gcd = 1 ; Return the final answer ; Driver code
using System ; class GFG { static int cntSubArr ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_gcd = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_gcd = __gcd ( curr_gcd , arr [ j ] ) ; ans += ( curr_gcd == 1 ) ? 1 : 0 ; } } return ans ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( cntSubArr ( arr , n ) ) ; } }
Program to print prime numbers from 1 to N . | C # program to display first N Prime numbers ; Function to print first N prime numbers ; Declare the variables ; Print display message ; Traverse each number from 1 to N with the help of for loop ; Skip 0 and 1 as they are neither prime nor composite ; flag variable to tell if i is prime or not ; flag = 1 means i is prime and flag = 0 means i is not prime ; Driver code
using System ; class GFG { static void print_primes_till_N ( int N ) { int i , j , flag ; Console . Write ( " Prime ▁ numbers ▁ between ▁ 1 ▁ and ▁ " + N + " ▁ are : STRNEWLINE " ) ; for ( i = 1 ; i <= N ; i ++ ) { if ( i == 1 i == 0 ) continue ; flag = 1 ; for ( j = 2 ; j <= i / 2 ; ++ j ) { if ( i % j == 0 ) { flag = 0 ; break ; } } if ( flag == 1 ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 100 ; print_primes_till_N ( N ) ; } }
Maximize the expression ( A AND X ) * ( B AND X ) | Bit Manipulation | C # implementation of the approach ; Function to find X according to the given conditions ; int can have 32 bits ; Temporary ith bit ; Compute ith bit of X according to given conditions Expression below is the direct conclusion from the illustration we had taken earlier ; Add the ith bit of X to X ; Driver code
using System ; class GFG { static int MAX = 32 ; static int findX ( int A , int B ) { int X = 0 ; for ( int bit = 0 ; bit < MAX ; bit ++ ) { int tempBit = 1 << bit ; int bitOfX = A & B & tempBit ; X += bitOfX ; } return X ; } public static void Main ( String [ ] args ) { int A = 11 , B = 13 ; Console . WriteLine ( findX ( A , B ) ) ; } }
Number of subsets whose mean is maximum | C # implementation of the approach ; Function to return the count of subsets with the maximum mean ; Maximum value from the array ; To store the number of times maximum element appears in the array ; Return the count of valid subsets ; Driver code
using System ; using System . Linq ; class GFG { static int cntSubSets ( int [ ] arr , int n ) { int maxVal = arr . Max ( ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == maxVal ) cnt ++ ; } return ( int ) ( Math . Pow ( 2 , cnt ) - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 2 } ; int n = arr . Length ; Console . WriteLine ( cntSubSets ( arr , n ) ) ; } }