codeparrot/xlcost-text-to-code huggingface dataset:
Two Balls Reachability Game | C # program to Find is it possible to have X white and Y black balls at the end . ; Recursive function to return gcd of a and b ; Function returns if it 's possible to have X white and Y black balls or not. ; Finding gcd of ( x , y ) and ( a , b ) ; If gcd is same , it 's always  possible to reach (x, y) ; Here it 's never possible  if gcd is not same ; Driver code
using System ; using System . Linq ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void IsPossible ( int a , int b , int x , int y ) { int g = gcd ( x , y ) ; int initial = gcd ( a , b ) ; if ( initial == g ) { Console . Write ( " POSSIBLE STRNEWLINE " ) ; } else { Console . Write ( " NOT ▁ POSSIBLE STRNEWLINE " ) ; } } static public void Main ( ) { int A = 1 , B = 2 ; int X = 4 , Y = 11 ; IsPossible ( A , B , X , Y ) ; A = 2 ; B = 2 ; X = 3 ; Y = 6 ; IsPossible ( A , B , X , Y ) ; } }
Number of ways to color N | C # program for the above approach ; Function to count the ways to color block ; For storing powers of 2 ; For storing binomial coefficient values ; Calculating binomial coefficient using DP ; Calculating powers of 2 ; Sort the indices to calculate length of each section ; Initialise answer to 1 ; Find the length of each section ; Merge this section ; Return the readonly count ; Driver Code ; Number of blocks ; Number of colored blocks ; Function call
using System ; class GFG { static int mod = 1000000007 ; static int waysToColor ( int [ ] arr , int n , int k ) { int [ ] powOf2 = new int [ 500 ] ; int [ , ] c = new int [ 500 , 500 ] ; for ( int i = 0 ; i <= n ; i ++ ) { c [ i , 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { c [ i , j ] = ( c [ i - 1 , j ] + c [ i - 1 , j - 1 ] ) % mod ; } } powOf2 [ 0 ] = powOf2 [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { powOf2 [ i ] = powOf2 [ i - 1 ] * 2 % mod ; } int rem = n - k ; arr [ k ++ ] = n + 1 ; Array . Sort ( arr ) ; int answer = 1 ; for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ i ] - ( i - 1 >= 0 ? arr [ i - 1 ] : 0 ) - 1 ; answer *= c [ rem , x ] % mod * ( i != 0 && i != k - 1 ? powOf2 [ x ] : 1 ) % mod ; rem -= x ; } return answer ; } public static void Main ( String [ ] args ) { int N = 6 ; int K = 3 ; int [ ] arr = { 1 , 2 , 6 , 0 } ; Console . Write ( waysToColor ( arr , N , K ) ) ; } }
Rearrange array such that difference of adjacent elements is in descending order | C # implementation to rearrange array such that difference of adjacent elements is in descending order ; Function to print array in given order ; Sort the array ; Check elements till the middle index ; Check if length is odd print the middle index at last ; Print the remaining elements in the described order ; Driver code ; Array declaration ; Size of array
using System ; class GFG { static void printArray ( int [ ] a , int n ) { Array . Sort ( a ) ; int i = 0 ; int j = n - 1 ; while ( i <= j ) { if ( i == j ) { Console . Write ( a [ i ] + " ▁ " ) ; } else { Console . Write ( a [ j ] + " ▁ " ) ; Console . Write ( a [ i ] + " ▁ " ) ; } i = i + 1 ; j = j - 1 ; } Console . WriteLine ( ) ; } public static void Main ( string [ ] args ) { int [ ] arr1 = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n1 = arr1 . Length ; printArray ( arr1 , n1 ) ; } }
Find the Smallest number that divides X ^ X | C # implementation of above approach ; Function to find the required smallest number ; Finding smallest number that divides n ; i divides n and return this value immediately ; If n is a prime number then answer should be n , as we can 't take 1 as our answer. ; Driver code
using System ; class GFG { static int SmallestDiv ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return i ; } } return n ; } public static void Main ( String [ ] args ) { int X = 385 ; int ans = SmallestDiv ( X ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }
Find an array of size N that satisfies the given conditions | C # implementation of the approach ; Utility function to print the contents of an array ; Function to generate and print the required array ; Initially all the positions are empty ; To store the count of positions i such that arr [ i ] = s ; To store the final array elements ; Set arr [ i ] = s and the gap between them is exactly 2 so in for loop we use i += 2 ; Mark the i 'th position as visited  as we put arr[i] = s ; Increment the count ; Finding the next odd number after s ; If the i 'th position is not visited  it means we did not put any value  at position i so we put 1 now ; Print the final array ; Driver code
using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void findArray ( int n , int k , int s ) { int [ ] vis = new int [ n ] ; int cnt = 0 ; int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n && cnt < k ; i += 2 ) { arr [ i ] = s ; vis [ i ] = 1 ; cnt ++ ; } int val = s ; if ( s % 2 == 0 ) val ++ ; else val = val + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] == 0 ) { arr [ i ] = val ; } } printArr ( arr , n ) ; } public static void Main ( ) { int n = 8 , k = 3 , s = 12 ; findArray ( n , k , s ) ; } }
Sum of numbers in a range [ L , R ] whose count of divisors is prime | C # implementation of the approach ; prime [ i ] stores 1 if i is prime ; divi [ i ] stores the count of divisors of i ; sum [ i ] will store the sum of all the integers from 0 to i whose count of divisors is prime ; Function for Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be 0 if i is Not a prime , else true . ; 0 and 1 is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to count the divisors ; For each number i we will go to each of the multiple of i and update the count of divisor of its multiple j as i is one of the factor of j ; Function for pre - computation ; If count of divisors of i is prime ; taking prefix sum ; Driver code ; Find all the prime numbers till N ; Update the count of divisors of all the numbers till N ; Precomputation for the prefix sum array ; Perform query
using System ; using System . Collections . Generic ; class GFG { static int N = 100000 ; static int [ ] prime = new int [ N ] ; static int [ ] divi = new int [ N ] ; static int [ ] sum = new int [ N ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < N ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( prime [ p ] == 1 ) { for ( int i = p * p ; i < N ; i += p ) prime [ i ] = 0 ; } } } static void DivisorCount ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { divi [ j ] ++ ; } } } static void pre ( ) { for ( int i = 1 ; i < N ; i ++ ) { if ( prime [ divi [ i ] ] == 1 ) { sum [ i ] = i ; } } for ( int i = 1 ; i < N ; i ++ ) sum [ i ] += sum [ i - 1 ] ; } public static void Main ( String [ ] args ) { int l = 5 , r = 8 ; SieveOfEratosthenes ( ) ; DivisorCount ( ) ; pre ( ) ; Console . WriteLine ( sum [ r ] - sum [ l - 1 ] ) ; } }
Count total number of even sum sequences | C # implementation of the above approach ; Iterative function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is greater than or equal to p ; If y is odd then multiply x with the result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to return n ^ ( - 1 ) mod p ; Function to return ( nCr % p ) using Fermat 's little theorem ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the count of odd numbers from 1 to n ; Function to return the count of even numbers from 1 to n ; Function to return the count of the required sequences ; Take i even and n - i odd numbers ; Number of odd numbers must be even ; Total ways of placing n - i odd numbers in the sequence of n numbers ; Add this number to the final answer ; Driver code
using System ; class GFG { static readonly int M = 1000000007 ; static long power ( long x , int y , int p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long n , int p ) { return power ( n , p - 2 , p ) ; } static long nCrModPFermat ( long n , int r , int p ) { if ( r == 0 ) return 1 ; long [ ] fac = new long [ ( int ) n + 1 ] ; fac [ 0 ] = 1 ; int i ; for ( i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ ( int ) n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ ( int ) n - r ] , p ) % p ) % p ; } static long countOdd ( long n ) { long x = n / 2 ; if ( n % 2 == 1 ) x ++ ; return x ; } static long counteEven ( long n ) { long x = n / 2 ; return x ; } static long CountEvenSumSequences ( long n ) { long count = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { int even = i , odd = ( int ) n - i ; if ( odd % 2 == 1 ) continue ; long tot = ( power ( countOdd ( n ) , odd , M ) * nCrModPFermat ( n , odd , M ) ) % M ; tot = ( tot * power ( counteEven ( n ) , i , M ) ) % M ; count += tot ; count %= M ; } return count ; } public static void Main ( String [ ] args ) { long n = 5 ; Console . WriteLine ( CountEvenSumSequences ( n ) ) ; } }
Find the sum of prime numbers in the Kth array | C # mplementation of the approach ; To store whether a number is prime or not ; Function for Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to return the sum of primes in the Kth array ; Update vector v to store all the prime numbers upto MAX ; To store the sum of primes in the kth array ; Count of primes which are in the arrays from 1 to k - 1 ; k is the number of primes in the kth array ; A prime has been added to the sum ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 1000000 ; static bool [ ] prime = new bool [ MAX ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } static int sumPrime ( int k ) { SieveOfEratosthenes ( ) ; List < int > v = new List < int > ( ) ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] ) v . Add ( i ) ; } int sum = 0 ; int skip = ( k * ( k - 1 ) ) / 2 ; while ( k > 0 ) { sum += v [ skip ] ; skip ++ ; k -- ; } return sum ; } public static void Main ( String [ ] args ) { int k = 3 ; Console . WriteLine ( sumPrime ( k ) ) ; } }
Count all substrings having character K | C # implementation of the approach ; Function to return the index of the next occurrence of character ch in str starting from the given index ; Return the index of the first occurrence of ch ; No occurrence found ; Function to return the count of all the substrings of str which contain the character ch at least one ; To store the count of valid substrings ; Index of the first occurrence of ch in str ; No occurrence of ch after index i in str ; Substrings starting at index i and ending at indices j , j + 1 , ... , n - 1 are all valid substring ; Driver code
using System ; class GFG { static int nextOccurrence ( String str , int n , int start , char ch ) { for ( int i = start ; i < n ; i ++ ) { if ( str [ i ] == ch ) return i ; } return - 1 ; } static int countSubStr ( String str , int n , char ch ) { int cnt = 0 ; int j = nextOccurrence ( str , n , 0 , ch ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j != - 1 && j < i ) { j = nextOccurrence ( str , n , j + 1 , ch ) ; } if ( j == - 1 ) break ; cnt += ( n - j ) ; } return cnt ; } static public void Main ( ) { String str = " geeksforgeeks " ; int n = str . Length ; char ch = ' k ' ; Console . WriteLine ( countSubStr ( str , n , ch ) ) ; } }
Check whether bitwise AND of N numbers is Even or Odd | C # implementation of the approach ; Function to check if the bitwise AND of the array elements is even or odd ; If at least an even element is present then the bitwise AND of the array elements will be even ; Driver code
using System ; class GFG { static void checkEvenOdd ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { Console . Write ( " Even " ) ; return ; } } Console . Write ( " Odd " ) ; } static public void Main ( ) { int [ ] arr = { 2 , 12 , 20 , 36 , 38 } ; int n = arr . Length ; checkEvenOdd ( arr , n ) ; } }
Find number of magical pairs of string of length L | C # implementation of the approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; Y must be even now y = y >> 1 ; y = y / 2 ; Driver Code
using System ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } public static void Main ( ) { int L = 2 ; int P = ( int ) Math . Pow ( 10 , 9 ) ; int ans = power ( 325 , L , P ) ; Console . WriteLine ( ans ) ; } }
Longest substring of only 4 's from the first N characters of the infinite string | C # implementation of the approach ; Function to return the length of longest contiguous string containing only 4 's from the first N characters of the string ; Initialize result ; Initialize prefix sum array of characters and product variable ; Preprocessing of prefix sum array ; Initialize variable to store the string length where N belongs to ; Finding the string length where N belongs to ; Driver Code
using System ; class GFG { static int MAXN = 30 ; static int countMaxLength ( int N ) { int res ; int [ ] pre = new int [ MAXN ] ; int p = 1 ; pre [ 0 ] = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { p *= 2 ; pre [ i ] = pre [ i - 1 ] + i * p ; } int ind = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { if ( pre [ i ] >= N ) { ind = i ; break ; } } int x = N - pre [ ind - 1 ] ; int y = 2 * ind - 1 ; if ( x >= y ) res = Math . Min ( x , y ) ; else res = Math . Max ( x , 2 * ( ind - 2 ) + 1 ) ; return res ; } public static void Main ( ) { int N = 25 ; Console . WriteLine ( countMaxLength ( N ) ) ; } }
Difference between Recursion and Iteration | C # program to find factorial of given number ; -- -- - Recursion -- -- - method to find factorial of given number ; recursion call ; -- -- - Iteration -- -- - Method to find the factorial of a given number ; using iteration ; Driver Code
using System ; class GFG { static int factorialUsingRecursion ( int n ) { if ( n == 0 ) return 1 ; return n * factorialUsingRecursion ( n - 1 ) ; } static int factorialUsingIteration ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } public static void Main ( String [ ] args ) { int num = 5 ; Console . WriteLine ( " Factorial ▁ of ▁ " + num + " ▁ using ▁ Recursion ▁ is : ▁ " + factorialUsingRecursion ( 5 ) ) ; Console . WriteLine ( " Factorial ▁ of ▁ " + num + " ▁ using ▁ Iteration ▁ is : ▁ " + factorialUsingIteration ( 5 ) ) ; } }
In | An Not in - place C # program to reverse an array ; Function to reverse arr [ ] from start to end ; Create a copy array and store reversed elements ; Now copy reversed elements back to arr [ ] ; Utility function to print an array ; Driver code
using System ; class GFG { public static void revereseArray ( int [ ] arr , int n ) { int [ ] rev = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) rev [ n - i - 1 ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = rev [ i ] ; } public static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printArray ( arr , n ) ; revereseArray ( arr , n ) ; Console . WriteLine ( " Reversed ▁ array ▁ is " ) ; printArray ( arr , n ) ; } }
In | An in - place C # program to reverse an array ; Function to reverse arr [ ] from start to end ; Utility function to print an array ; Driver code
using System ; class GFG { public static int __ ( int x , int y ) { return x ; } public static void revereseArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n / 2 ; i ++ ) arr [ i ] = __ ( arr [ n - i - 1 ] , arr [ n - i - 1 ] = arr [ i ] ) ; } public static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printArray ( arr , n ) ; revereseArray ( arr , n ) ; Console . WriteLine ( " Reversed ▁ array ▁ is " ) ; printArray ( arr , n ) ; } }
Find Binary string by converting all 01 or 10 to 11 after M iterations | C # program for the above approach . ; Function to find the modified binary string after M iterations ; Set the value of M to the minimum of N or M . ; Declaration of current string state ; Loop over M iterations ; Set the current state as null before each iteration ; Check if this zero has exactly one 1 as neighbour ; Flip the zero ; If there is no change , then no need for further iterations . ; Set the current state as the new previous state str = s1 ; ; Driver Code ; Given String ; Number of Iterations ; Function Call
using System ; class GFG { static void findString ( string str , int M ) { int N = str . Length ; M = Math . Min ( M , N ) ; string s1 = " " ; while ( M != 0 ) { s1 = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == '0' ) { if ( ( ( i > 0 && str [ i - 1 ] == '1' ) && ( i < N - 1 && str [ i + 1 ] != '1' ) ) || ( ( i > 0 && str [ i - 1 ] != '1' ) && ( i < N - 1 && str [ i + 1 ] == '1' ) ) ) { s1 += '1' ; } else { if ( i == 0 i == N - 1 ) { s1 += '1' ; } else { s1 += '0' ; } } } else { s1 += '1' ; } } if ( str == s1 ) break ; M -- ; } Console . WriteLine ( s1 ) ; } static void Main ( ) { string str = "0110100" ; int M = 3 ; findString ( str , M ) ; } }
Count of subsequences with a sum in range [ L , R ] and difference between max and min element at least X | C # program for the above approach ; Function to find the number of subsequences of the given array with a sum in range [ L , R ] and the difference between the maximum and minimum element is at least X ; Initialize answer as 0 ; Creating mask from [ 0 , 2 ^ n - 1 ] ; Stores the count and sum of selected elements respectively ; Variables to store the value of Minimum and maximum element ; Traverse the array ; If the jth bit of the ith mask is on ; Add the selected element ; Update maxVal and minVal value ; Check if the given conditions are true , increment ans by 1. ; Driver Code ; Given input ; Function Call
using System ; class GFG { static int numberofSubsequences ( int [ ] a , int L , int R , int X , int n ) { int ans = 0 ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int cnt = 0 , sum = 0 ; int minVal = Int32 . MaxValue , maxVal = Int32 . MinValue ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) == 0 ) { cnt += 1 ; sum += a [ j ] ; maxVal = Math . Max ( maxVal , a [ j ] ) ; minVal = Math . Min ( minVal , a [ j ] ) ; } } if ( cnt >= 2 && sum >= L && sum <= R && ( maxVal - minVal >= X ) ) { ans += 1 ; } } return ans ; } static public void Main ( ) { int [ ] a = { 10 , 20 , 30 , 25 } ; int L = 40 , R = 50 , X = 10 ; int N = a . Length ; Console . Write ( numberofSubsequences ( a , L , R , X , N ) ) ; } }
Minimum count of numbers needed from 1 to N that yields the sum as K | C # program for the above approach ; Function to find minimum number of elements required to obtain sum K ; Stores the maximum sum that can be obtained ; If K is greater than the Maximum sum ; If K is less than or equal to to N ; Stores the sum ; Stores the count of numbers needed ; Iterate until N is greater than or equal to 1 and sum is less than K ; Increment count by 1 ; Increment sum by N ; Update the sum ; Finally , return the count ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int Minimum ( int N , int K ) { int sum = N * ( N + 1 ) / 2 ; if ( K > sum ) return - 1 ; if ( K <= N ) return 1 ; sum = 0 ; int count = 0 ; while ( N >= 1 && sum < K ) { count += 1 ; sum += N ; N -= 1 ; } return count ; } static public void Main ( ) { int N = 5 , K = 10 ; Console . Write ( Minimum ( N , K ) ) ; } }
Maximize X such that sum of numbers in range [ 1 , X ] is at most K | C # program for above approach ; Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores the result ; Iterate until low is less than or equal to high ; Stores the sum of first mid natural numbers ; If sum is less than or equal to K ; Update res and low ; Otherwise , ; Update ; Return res ; Driver Code ; Input ; Function call
using System ; class GFG { static int Count ( int N , int K ) { if ( K == 0 ) return 0 ; int res = 0 ; int low = 1 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * mid + mid ) / 2 ; if ( sum <= K ) { res = Math . Max ( res , mid ) ; low = mid + 1 ; } else { high = mid - 1 ; } } return res ; } public static void Main ( String [ ] args ) { int N = 6 , K = 14 ; Console . Write ( Count ( N , K ) ) ; } }
Largest number having both positive and negative values present in the array | C # program for the above approach ; Function to find the largest number k such that both k and - k are present in the array ; Stores the array elements ; Initialize a variable res as 0 to store maximum element while traversing the array ; Iterate through array arr ; Add the current element into the set ; Check if the negative of this element is also present in the set or not ; Return the resultant element ; Drive Code
using System ; using System . Collections . Generic ; public class GFG { public static int largestNum ( int [ ] arr ) { HashSet < int > set = new HashSet < int > ( ) ; int res = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { set . Add ( arr [ i ] ) ; if ( set . Contains ( - 1 * arr [ i ] ) ) { res = Math . Max ( res , Math . Abs ( arr [ i ] ) ) ; } } return res ; } static public void Main ( ) { int [ ] arr = { 3 , 2 , - 2 , 5 , - 3 } ; Console . WriteLine ( largestNum ( arr ) ) ; } }
Check if an element is present in an array using at most floor ( N / 2 ) + 2 comparisons | C # program for the above approach ; Function to check whether X is present in the array A [ ] ; Initialise a pointer ; Store the number of comparisons ; Variable to store product ; Check is N is odd ; Update i and T ; Traverse the array ; Check if i < N ; Update T ; Check if T is equal to 0 ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void findElement ( int [ ] A , int N , int X ) { int i = 0 ; int Comparisons = 0 ; int T = 1 ; Comparisons ++ ; if ( N % 2 == 1 ) { i = 1 ; T *= ( A [ 0 ] - X ) ; } for ( ; i < N ; i += 2 ) { Comparisons += 1 ; T *= ( A [ i ] - X ) ; T *= ( A [ i + 1 ] - X ) ; } Comparions += 1 ; if ( T == 0 ) { Console . Write ( " Yes ▁ " + Comparisons ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { int [ ] A = { - 3 , 5 , 11 , 3 , 100 , 2 , 88 , 22 , 7 , 900 , 23 , 4 , 1 } ; int N = A . Length ; int X = 1 ; findElement ( A , N , X ) ; } }