codeparrot/xlcost-text-to-code huggingface dataset:
Longest Mountain Subarray | C # code for the longest Mountain Subarray ; Function to find the longest mountain subarray ; If the size of array is less than 3 , the array won 't show mountain like  behaviour ; When a new mountain sub - array is found , there is a need to set the variables k , j to - 1 in order to help calculate the length of new mountain sub - array ; j marks the starting index of a new mountain sub - array . So set the value of j to current index i . ; Checks if next element is less than current element ; Checks if starting element exists or not , if the starting element of the mountain sub - array exists then the index of ending element is stored in k ; This condition checks if both starting index and ending index exists or not , if yes , the length is calculated . ; d holds the length of the longest mountain sub - array . If the current length is greater than the calculated length , then value of d is updated . ; Ignore if there is no increase or decrease in the value of the next element ; Checks and calculates the length if last element of the array is the last element of a mountain sub - array ; Driver code
using System ; class GFG { public static int longestMountain ( int [ ] a ) { int i = 0 , j = - 1 , k = - 1 , p = 0 , d = 0 ; if ( a . Length < 3 ) return 0 ; for ( i = 0 ; i < a . Length - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] ) { if ( k != - 1 ) { k = - 1 ; j = - 1 ; } if ( j == - 1 ) j = i ; } else { if ( a [ i + 1 ] < a [ i ] ) { if ( j != - 1 ) k = i + 1 ; if ( k != - 1 && j != - 1 ) { if ( d < k - j + 1 ) d = k - j + 1 ; } } else { k = - 1 ; j = - 1 ; } } } if ( k != - 1 && j != - 1 ) { if ( d < k - j + 1 ) d = k - j + 1 ; } return d ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 3 , 1 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 7 , 6 , 5 } ; Console . WriteLine ( longestMountain ( a ) ) ; } }
Check whether an Array can be made 0 by splitting and merging repeatedly | C # program for the above approach ; Function that finds if it is possible to make the array contain only 1 element i . e . 0 ; Check if element is odd ; According to the logic in above approach ; Driver code
using System ; class GFG { public static string solve ( int [ ] A ) { int i , ctr = 0 ; for ( i = 0 ; i < A . Length ; i ++ ) { if ( A [ i ] % 2 == 1 ) { ctr ++ ; } } if ( ctr % 2 == 1 ) { return " No " ; } else { return " Yes " ; } } public static void Main ( ) { int [ ] arr = { 9 , 17 } ; Console . Write ( solve ( arr ) ) ; } }
Count of perfect squares of given length | C # program to count perfect squares of given length ; Function to check if a number is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to return the count of n digit perfect squares ; Initialize result ; Traverse through all numbers of n digits ; Check if current number ' i ' is perfect square ; Driver code
using System ; class GFG { static bool isPerfectSquare ( double x ) { double sr = Math . Sqrt ( x ) ; return ( ( sr - Math . Floor ( sr ) ) == 0 ) ; } static int countSquares ( int n ) { int cnt = 0 ; for ( int i = ( int ) Math . Pow ( 10 , ( n - 1 ) ) ; i < Math . Pow ( 10 , n ) ; i ++ ) { if ( i != 0 && isPerfectSquare ( i ) ) cnt ++ ; } return cnt ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( countSquares ( n ) ) ; } }
Count of perfect squares of given length | C # Program to count perfect squares of given length ; Function to return the count of n digit perfect squares ; Driver code
using System ; class GFG { static int countSquares ( int n ) { int r = ( int ) Math . Ceiling ( Math . Sqrt ( Math . Pow ( 10 , n ) ) ) ; int l = ( int ) Math . Ceiling ( Math . Sqrt ( Math . Pow ( 10 , n - 1 ) ) ) ; return r - l ; } public static void Main ( ) { int n = 3 ; Console . Write ( countSquares ( n ) ) ; } }
Sum of alternating sign cubes of first N Natural numbers | C # implementation to compute the sum of cubes with alternating sign ; Function to compute sum of the cubes with alternating sign ; Driver code
using System ; class GFG { static int summation ( int N ) { int co = ( N + 1 ) / 2 ; int ce = ( N ) / 2 ; int se = 2 * ( ( ce * ( ce + 1 ) ) * ( ce * ( ce + 1 ) ) ) ; int so = ( co * co ) * ( 2 * ( ( co * co ) ) - 1 ) ; return so - se ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( summation ( n ) ) ; } }
Program to check if N is a Star Number | C # implementation to check that a number is a star number or not ; Function to check that the number is a star number ; Condition to check if the number is a star number ; Driver code ; Function call
using System ; class GFG { static bool isStar ( int N ) { double n = ( 6 + Math . Sqrt ( 24 * N + 12 ) ) / 6 ; return ( n - ( int ) n ) == 0 ; } public static void Main ( ) { int i = 13 ; if ( isStar ( i ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Smallest number to make Array sum at most K by dividing each element | C # program to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Function to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Binary search between 1 and 10 ^ 9 ; Calculating the new sum after dividing every element by mid ; If after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; Else , move mid + 1 to high ; Returning the minimum number ; Driver Code
using System ; class GFG { static int findMinDivisor ( int [ ] arr , int n , int limit ) { int low = 0 , high = 1000000000 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) Math . Ceiling ( ( double ) arr [ i ] / ( double ) mid ) ; } if ( sum <= limit ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 9 } ; int N = arr . Length ; int K = 6 ; Console . WriteLine ( findMinDivisor ( arr , N , K ) ) ; } }
Count of triplets ( a , b , c ) in the Array such that a divides b and b divides c | C # program to find count of triplets ( a , b , c ) in the Array such that a divides b and b divides c ; Function to count triplets ; Iterate for middle element ; Iterate left array for a [ i ] ; Iterate right array for a [ k ] ; return the final result ; Driver code
using System ; class GFG { public static int getCount ( int [ ] arr , int n ) { int count = 0 ; for ( int j = 1 ; j < n - 1 ; j ++ ) { int p = 0 , q = 0 ; for ( int i = 0 ; i < j ; i ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) p ++ ; } for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ k ] % arr [ j ] == 0 ) q ++ ; } count += p * q ; } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 } ; int N = arr . Length ; Console . WriteLine ( getCount ( arr , N ) ) ; } }
Find pair with maximum ratio in an Array | C # implementation to find the maximum pair in the array ; Function to find the maximum pair possible for the array ; Loop to iterate over every possible pair in the array ; Check pair ( x , y ) as well as ( y , x ) for maximum value ; Update the answer ; Driver code
using System ; class GFG { static float computeMaxValue ( float [ ] arr , int n ) { float ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { float val = Math . Max ( arr [ i ] / arr [ j ] , arr [ j ] / arr [ i ] ) ; ans = Math . Max ( ans , val ) ; } } return ans ; } public static void Main ( String [ ] args ) { float [ ] arr = { 15 , 10 , 3 , 2 } ; int N = arr . Length ; Console . WriteLine ( computeMaxValue ( arr , N ) ) ; } }
Find the Kth number which is not divisible by N | C # implementation to find the K 'th non-divisible number of N ; Function to find the Kth not divisible by N ; Driver Code ; Function Call
using System ; class GFG { static int kthNonDivisible ( int N , int K ) { return ( int ) ( K + Math . Floor ( ( double ) ( K - 1 ) / ( N - 1 ) ) ) ; } public static void Main ( String [ ] args ) { int N = 3 ; int K = 6 ; Console . Write ( kthNonDivisible ( N , K ) ) ; } }
Find the maximum sum pair in an Array with even parity | C # implementation to find a pair with even parity and maximum sum ; Function that returns true if count of set bits in given number is even ; Parity will store the count of set bits ; Function to print the elements of the array ; Function to remove all the even parity elements from the given array ; Traverse the array ; If the current element has even parity ; Driver Code ; Function Call
using System ; class GFG { static int sz = ( int ) 1e3 ; static bool isEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x % 2 == 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } static void printArray ( int [ ] arr , int len ) { for ( int i = 0 ; i < len ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static void findPairEvenParity ( int [ ] arr , int len ) { int firstMaximum = Int32 . MinValue ; int secondMaximum = Int32 . MinValue ; for ( int i = 0 ; i < len ; i ++ ) { if ( isEvenParity ( arr [ i ] ) ) { if ( arr [ i ] >= firstMaximum ) { secondMaximum = firstMaximum ; firstMaximum = arr [ i ] ; } else if ( arr [ i ] >= secondMaximum ) { secondMaximum = arr [ i ] ; } } } Console . Write ( firstMaximum + " ▁ " + secondMaximum ) ; } public static void Main ( ) { int [ ] arr = { 18 , 15 , 8 , 9 , 14 } ; int len = arr . Length ; findPairEvenParity ( arr , len ) ; } }
Program to check if N is a Hexagonal Number or not | C # program to check if N is a hexagonal number ; Function to check if number is hexagonal ; Calculate the value for n ; Check if n - floor ( n ) is equal to 0 ; Driver code
using System ; class GFG { static bool isHexagonal ( int N ) { float val = 8 * N + 1 ; float x = 1 + ( float ) Math . Sqrt ( val ) ; float n = ( x ) / 4 ; if ( ( n - ( int ) n ) == 0 ) { return true ; } else { return false ; } } public static void Main ( string [ ] args ) { int N = 14 ; if ( isHexagonal ( N ) == true ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Program to convert Number in characters | C # program to convert number in characters ; To calculate the reverse of the number ; The remainder will give the last digit of the number ; Extract the first digit of the reversed number ; Match it with switch case ; Divide the number by 10 to get the next number ; Driver code
using System ; class GFG { static void NumbertoCharacter ( int n ) { int rev = 0 , r = 0 ; while ( n > 0 ) { r = n % 10 ; rev = rev * 10 + r ; n = n / 10 ; } while ( rev > 0 ) { r = rev % 10 ; switch ( r ) { case 1 : Console . Write ( " one ▁ " ) ; break ; case 2 : Console . Write ( " two ▁ " ) ; break ; case 3 : Console . Write ( " three ▁ " ) ; break ; case 4 : Console . Write ( " four ▁ " ) ; break ; case 5 : Console . Write ( " five ▁ " ) ; break ; case 6 : Console . Write ( " six ▁ " ) ; break ; case 7 : Console . Write ( " seven ▁ " ) ; break ; case 8 : Console . Write ( " eight ▁ " ) ; break ; case 9 : Console . Write ( " nine ▁ " ) ; break ; case 0 : Console . Write ( " zero ▁ " ) ; break ; default : Console . Write ( " UnValid ▁ " ) ; break ; } rev = rev / 10 ; } } public static void Main ( String [ ] args ) { int n = 12345 ; NumbertoCharacter ( n ) ; } }
Count all subarrays whose sum can be split as difference of squares of two Integers | C # program to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; Function to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; Loop to iterate over all the possible subsequences of the array ; Finding the sum of all the possible subsequences ; Condition to check whether the number can be split as difference of squares ; Driver code
using System ; class GFG { static int Solve ( int [ ] arr , int n ) { int temp = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { temp = 0 ; for ( int j = i ; j < n ; j ++ ) { temp += arr [ j ] ; if ( ( temp + 2 ) % 4 != 0 ) count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int N = arr . Length ; Console . Write ( Solve ( arr , N ) ) ; } }
Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M | C # program to find the product of all the combinations of M elements from an array whose index in the sorted order divides M completely ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; If y is odd , multiply x with result ; y must be even now ; Iterative Function to calculate ( nCr ) % p and save in f [ n , r ] C ( n , r ) % p = [ C ( n - 1 , r - 1 ) % p + C ( n - 1 , r ) % p ] % p and C ( n , 0 ) = C ( n , n ) = 1 ; If j > i then C ( i , j ) = 0 ; If i is equal to j then C ( i , j ) = 1 ; C ( i , j ) = ( C ( i - 1 , j ) + C ( i - 1 , j - 1 ) ) % p ; Initialize the answer ; For every element arr [ i ] , x is count of occurrence of arr [ i ] in different set such that index of arr [ i ] in those sets divides m completely . ; Finding the count of arr [ i ] by placing it at the index which divides m completely ; Using fermat 's little theorem ; Multiplying with the count ; Driver code
using System ; class GFG { static int m = 4 ; static long power ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void nCr ( int n , long p , int [ , ] f ) { for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( j > i ) f [ i , j ] = 0 ; else if ( j == 0 j == i ) f [ i , j ] = 1 ; else f [ i , j ] = ( f [ i - 1 , j ] + f [ i - 1 , j - 1 ] ) % ( int ) p ; } } } static void operations ( int [ ] arr , int n , int [ , ] f ) { long p = 1000000007 ; nCr ( n , p - 1 , f ) ; Array . Sort ( arr ) ; long ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { long x = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { if ( m % j == 0 ) x = ( x + ( f [ n - i - 1 , m - j ] * f [ i , j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ; } ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) ; } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 7 , 9 , 3 } ; int n = arr . Length ; int [ , ] f = new int [ n + 1 , m + 1 ] ; operations ( arr , n , f ) ; } }
Check whether a number can be represented by the product of two squares | C # implementation to check whether a number can be represented by the product of two squares ; Function to check if there exist two numbers product of whose squares is n . ; Check whether the product of the square of both numbers is equal to N ; Driver code
using System ; class GFG { static bool prodSquare ( int n ) { for ( long i = 2 ; i * i <= n ; i ++ ) for ( long j = 2 ; j <= n ; j ++ ) if ( i * i * j * j == n ) return true ; return false ; } public static void Main ( String [ ] args ) { int n = 25 ; if ( prodSquare ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check whether a number can be represented by the product of two squares | C # implementation to check whether a number can be represented by the product of two squares ; Function to check if there exist two numbers product of whose squares is n ; Initialize map ; Store square value in hashmap ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool prodSquare ( int n ) { Dictionary < float , float > s = new Dictionary < float , float > ( ) ; for ( int i = 2 ; i * i <= n ; ++ i ) { s . Add ( ( float ) ( i * i ) , ( float ) 1 ) ; if ( s . ContainsKey ( ( float ) n / ( i * i ) ) ) return true ; } return false ; } public static void Main ( String [ ] args ) { int n = 25 ; if ( prodSquare ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Print N distinct numbers following the given operations | C # implementation to print N distinct numbers ; Function to print the required array ; Check if number is a multiple of 4 ; Printing left half of the array ; Printing Right Half of the array ; Driver code
using System ; public class GFG { static void printArr ( int n ) { if ( n % 4 == 0 ) { for ( int i = 1 ; i <= n / 2 ; i ++ ) Console . Write ( i * 2 + " ▁ " ) ; for ( int i = 1 ; i < n / 2 ; i ++ ) Console . Write ( i * 2 - 1 + " ▁ " ) ; Console . WriteLine ( n + n / 2 - 1 ) ; } else Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int n = 22 ; printArr ( n ) ; } }
Sum of product of all subsets formed by only divisors of N | C # program to find the sum of product of all the subsets formed by only divisors of N ; Function to find the sum of product of all the subsets formed by only divisors of N ; Store all the divisors of n ; Loop to find out the divisors of N ; Both ' i ' and ' n / i ' are the divisors of n ; Check if ' i ' and ' n / i ' are equal or not ; Calculating the answer ; Excluding the value of the empty set ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int GetSum ( int n ) { List < int > divisors = new List < int > ( ) ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { divisors . Add ( i ) ; if ( i != n / i ) { divisors . Add ( n / i ) ; } } } int ans = 1 ; foreach ( int i in divisors ) { ans *= ( i + 1 ) ; } ans = ans - 1 ; return ans ; } public static void Main ( ) { int N = 4 ; Console . WriteLine ( GetSum ( N ) ) ; } }
Length of longest Powerful number subsequence in an Array | C # program to find the length of longest Powerful Subsequence in an array ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return false ; Check if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to find the longest subsequence which contain all powerful numbers ; Driver code
using System ; class GFG { static bool isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . Sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static int longestPowerfulSubsequence ( int [ ] arr , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) answer ++ ; } return answer ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 4 , 10 , 13 , 9 , 25 } ; int n = arr . Length ; Console . Write ( longestPowerfulSubsequence ( arr , n ) + " STRNEWLINE " ) ; } }
Maximum OR value of a pair in an Array without using OR operator | C # program for the above approach ; Function to return the maximum bitwise OR for any pair of the given array without using bitwise OR operation ; Find maximum element in the array ; Finding complement will set all unset bits in a number ; Iterate through all other array elements to find maximum AND value ; c will give the maximum value that could be added to max_value to produce maximum OR value ; Driver code
using System ; using System . Linq ; class GFG { static int maxOR ( int [ ] arr , int n ) { int max_value = arr . Max ( ) ; int number_of_bits = ( int ) ( Math . Log ( max_value ) ) + 2 ; int complement = ( ( 1 << number_of_bits ) - 1 ) ^ max_value ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != max_value ) { c = Math . Max ( c , ( complement & arr [ i ] ) ) ; } } return ( max_value + c ) ; } public static void Main ( ) { int [ ] arr = { 3 , 6 , 8 , 16 } ; int n = arr . Length ; Console . Write ( maxOR ( arr , n ) ) ; } }
Find the XOR of the elements in the given range [ L , R ] with the value K for a given set of queries | C # implementation to perform the XOR range updates on an array ; Function to perform the update operation on the given array ; Converting the indices to 0 indexing . ; Saving the XOR of K from the starting index in the range [ L , R ] . ; Saving the XOR of K at the ending index in the given [ L , R ] . ; Function to display the resulting array ; Finding the resultant value in the result array ; Combining the effects of the updates with the original array without changing the initial array . ; Driver code ; Query 1 ; Query 2 ; Query 3
using System ; class GFG { static void update ( int [ ] res , int L , int R , int K ) { L -= 1 ; R -= 1 ; res [ L ] ^= K ; res [ R + 1 ] ^= K ; } static void display ( int [ ] arr , int [ ] res , int n ) { int i ; for ( i = 1 ; i < n ; i ++ ) { res [ i ] = res [ i ] ^ res [ i - 1 ] ; } for ( i = 0 ; i < n ; i ++ ) { Console . Write ( ( arr [ i ] ^ res [ i ] ) + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 8 , 10 } ; int N = arr . Length ; int [ ] res = new int [ N ] ; int L = 1 , R = 3 , K = 2 ; update ( res , L , R , K ) ; L = 2 ; R = 4 ; K = 3 ; update ( res , L , R , K ) ; display ( arr , res , N ) ; } }
Shortest subarray to be removed to make all Array elements unique | C # program to make array elements pairwise distinct by removing at most one subarray of minimum length ; Function to check if elements of Prefix and suffix of each sub array of size K are pairwise distinct or not ; Hash map to store frequencies of elements of prefix and suffix ; Variable to store number of occurrences of an element other than one ; Adding frequency of elements of suffix to hash for subarray starting from first index There is no prefix for this sub array ; Counting extra elements in current Hash map ; If there are no extra elements return true ; Check for remaining sub arrays ; First element of suffix is now part of subarray which is being removed so , check for extra elements ; Decrement frequency of first element of the suffix ; Increment frequency of last element of the prefix ; Check for extra elements ; If there are no extra elements return true ; Function for calculating minimum length of the subarray , which on removing make all elements pairwise distinct ; Possible range of length of subarray ; Binary search to find minimum ans ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool check ( int [ ] a , int n , int k ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int extra = 0 ; for ( int i = k ; i < n ; i ++ ) if ( m . ContainsKey ( a [ i ] ) ) m [ a [ i ] ] = m [ a [ i ] ] + 1 ; else m . Add ( a [ i ] , 1 ) ; foreach ( int x in m . Keys ) extra += m [ x ] - 1 ; if ( extra == 0 ) return true ; for ( int i = 1 ; i + k - 1 < n ; i ++ ) { if ( m [ a [ i + k - 1 ] ] > 1 ) extra -- ; m [ a [ i + k - 1 ] ] = m [ a [ i + k - 1 ] ] - 1 ; m [ a [ i - 1 ] ] = m [ a [ i - 1 ] ] + 1 ; if ( m [ a [ i - 1 ] ] > 1 ) extra ++ ; if ( extra == 0 ) return true ; } return false ; } static int minlength ( int [ ] a , int n ) { int lo = 0 , hi = n + 1 ; int ans = 0 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( check ( a , n , mid ) ) { ans = mid ; hi = mid ; } else lo = mid + 1 ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 1 , 2 , 3 } ; int n = a . Length ; Console . WriteLine ( minlength ( a , n ) ) ; } }
Find N distinct integers with zero sum | C # implementation to print N distinct numbers such that their sum is 0 ; Function to print distinct n numbers such that their sum is 0 ; Print 2 symmetric numbers ; Print a extra 0 if N is odd ; Driver code
using System ; class GFG { static void findNumbers ( int N ) { for ( int i = 1 ; i <= ( N / 2 ) ; i ++ ) { Console . Write ( i + " , ▁ " + - i + " , ▁ " ) ; } if ( N % 2 == 1 ) Console . Write ( 0 ) ; } static void Main ( ) { int N = 10 ; findNumbers ( N ) ; } }