codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find the minimum index in a range [ L , R ] having at least value X with updates | C # program to implement the above approach ; Stores nodes value of the Tree ; Function to build segment tree ; Base Case ; Find the value of mid ; Update for left subtree ; Update for right subtree ; Update the value at the current index ; Function for finding the index of the first element at least x ; If current range does not lie in query range ; If current range is inside of query range ; Maximum value in this range is less than x ; Finding index of first value in this range ; Update the value of the minimum index ; Find mid of the current range ; Left subtree ; If it does not lie in left subtree ; Function for updating segment tree ; Update the value , we reached leaf node ; Find the mid ; If pos lies in the left subtree ; pos lies in the right subtree ; Update the maximum value in the range ; Function to print the answer for the given queries ; Build segment tree ; Find index of first value atleast 2 in range [ 0 , n - 1 ] ; Update value at index 2 to 5 ; Find index of first value atleast 4 in range [ 0 , n - 1 ] ; Find index of first value atleast 0 in range [ 0 , n - 1 ] ; Driver code ; Function Call
using System ; class GFG { static int maxN = 100 ; static int [ ] Tree = new int [ 4 * maxN ] ; static void build ( int [ ] arr , int index , int s , int e ) { if ( s == e ) Tree [ index ] = arr [ s ] ; else { int m = ( s + e ) / 2 ; build ( arr , 2 * index , s , m ) ; build ( arr , 2 * index + 1 , m + 1 , e ) ; Tree [ index ] = Math . Max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } static int atleast_x ( int index , int s , int e , int ql , int qr , int x ) { if ( ql > e qr < s ) return - 1 ; if ( s <= ql && e <= qr ) { if ( Tree [ index ] < x ) return - 1 ; while ( s != e ) { int m = ( s + e ) / 2 ; if ( Tree [ 2 * index ] >= x ) { e = m ; index = 2 * index ; } else { s = m + 1 ; index = 2 * index + 1 ; } } return s ; } int mm = ( s + e ) / 2 ; int val = atleast_x ( 2 * index , s , mm , ql , qr , x ) ; if ( val != - 1 ) return val ; return atleast_x ( 2 * index + 1 , mm + 1 , e , ql , qr , x ) ; } static void update ( int index , int s , int e , int new_val , int pos ) { if ( s == e ) Tree [ index ] = new_val ; else { int m = ( s + e ) / 2 ; if ( pos <= m ) { update ( 2 * index , s , m , new_val , pos ) ; } else { update ( 2 * index + 1 , m + 1 , e , new_val , pos ) ; } Tree [ index ] = Math . Max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } static void printAnswer ( int [ ] arr , int n ) { build ( arr , 1 , 0 , n - 1 ) ; Console . WriteLine ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 2 ) ) ; arr [ 2 ] = 5 ; update ( 1 , 0 , n - 1 , 5 , 2 ) ; Console . WriteLine ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 4 ) ) ; Console . WriteLine ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 0 ) ) ; } static void Main ( ) { int [ ] arr = { 1 , 3 , 2 , 4 , 6 } ; int N = arr . Length ; printAnswer ( arr , N ) ; } }
Smallest Subtree with all the Deepest Nodes | C # program to implement the above approach ; Structure of a Node ; Function to return depth of the Tree from root ; If current node is a leaf node ; Function to find the root of the smallest subtree consisting of all deepest nodes ; Stores height of left subtree ; Stores height of right subtree ; If height of left subtree exceeds that of the right subtree ; Traverse left subtree ; If height of right subtree exceeds that of the left subtree ; Otherwise ; Return current node ; Driver Code
using System ; class GFG { public class TreeNode { public int val ; public TreeNode left ; public TreeNode right ; public TreeNode ( int data ) { this . val = data ; left = null ; right = null ; } } ; static int find_ht ( TreeNode root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return 1 ; return Math . Max ( find_ht ( root . left ) , find_ht ( root . right ) ) + 1 ; } static TreeNode find_node ( TreeNode root , TreeNode req_node ) { if ( root == null ) return req_node ; int left_ht = find_ht ( root . left ) ; int right_ht = find_ht ( root . right ) ; if ( left_ht > right_ht ) { req_node = find_node ( root . left , req_node ) ; } else if ( right_ht > left_ht ) { req_node = find_node ( root . right , req_node ) ; } else { req_node = root ; return req_node ; } return req_node ; } public static void Main ( String [ ] args ) { TreeNode root = new TreeNode ( 1 ) ; root . left = new TreeNode ( 2 ) ; root . right = new TreeNode ( 3 ) ; TreeNode req_node = null ; req_node = find_node ( root , req_node ) ; Console . Write ( req_node . val ) ; } }
Maximum even numbers present in any subarray of size K | C # program to implement the above approach ; Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the maximum count of even numbers from all the subarrays of size K ; Generate all subarrays of size K ; Store count of even numbers in current subarray of size K ; Traverse the current subarray ; If current element is an even number ; Update the answer ; Return answer ; Driver Code ; Size of the input array
using System ; class GFG { static int maxEvenIntegers ( int [ ] arr , int N , int M ) { int ans = 0 ; for ( int i = 0 ; i <= N - M ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( arr [ i + j ] % 2 == 0 ) cnt ++ ; } ans = Math . Max ( ans , cnt ) ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 4 , 7 , 6 } ; int K = 3 ; int N = arr . Length ; Console . WriteLine ( maxEvenIntegers ( arr , N , K ) ) ; } }
Count maximum concatenation of pairs from given array that are divisible by 3 | C # program to implement the above approach ; Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count of array elements whose remainder is 0 by taking modulo by 3 ; Stores count of array elements whose remainder is 1 by taking modulo by 3 ; Stores count of array elements whose remainder is 2 by taking modulo by 3 ; Traverse the array ; Stores sum of digits of arr [ i ] ; Update digitSum ; If remainder of digitSum by by taking modulo 3 is 0 ; Update rem0 ; If remainder of digitSum by by taking modulo 3 is 1 ; Update rem1 ; Update rem2 ; Driver code ; To display the result
using System ; using System . Collections . Generic ; class GFG { static int countDiv ( int [ ] arr ) { int rem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; foreach ( int i in arr ) { int digitSum = 0 ; digitSum += i ; if ( digitSum % 3 == 0 ) { rem0 += 1 ; } else if ( digitSum % 3 == 1 ) { rem1 += 1 ; } else { rem2 += 1 ; } } return ( rem0 / 2 + Math . Min ( rem1 , rem2 ) ) ; } static void Main ( ) { int [ ] arr = { 5 , 3 , 2 , 8 , 7 } ; Console . Write ( countDiv ( arr ) ) ; } }
Maximum Sum Subsequence | C # program to implement the above approach ; Function to print the maximum non - emepty subsequence sum ; Stores the maximum non - emepty subsequence sum in an array ; Stores the largest element in the array ; Traverse the array ; If a [ i ] is greater than 0 ; Update sum ; Driver Code
using System ; class GFG { static int MaxNonEmpSubSeq ( int [ ] a , int n ) { int sum = 0 ; int max = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( max < a [ i ] ) { max = a [ i ] ; } } if ( max <= 0 ) { return max ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { sum += a [ i ] ; } } return sum ; } static void Main ( ) { int [ ] arr = { - 2 , 11 , - 4 , 2 , - 3 , - 10 } ; int N = arr . Length ; Console . WriteLine ( MaxNonEmpSubSeq ( arr , N ) ) ; } }
Count numbers up to N having Kth bit set | C # program for above approach ; Function to return the count of number of 1 's at ith bit in a range [1, n - 1] ; Store count till nearest power of 2 less than N ; If K - th bit is set in N ; Add to result the nearest power of 2 less than N ; Return result ; Driver Code ; Function Call
using System ; class GFG { static long getcount ( long n , int k ) { long res = ( n >> ( k + 1 ) ) << k ; if ( ( ( n >> k ) & 1 ) != 0 ) res += n & ( ( 1 << k ) - 1 ) ; return res ; } static void Main ( ) { long N = 14 ; int K = 2 ; Console . WriteLine ( getcount ( N + 1 , K ) ) ; } }
Check if a string can be split into two substrings such that one substring is a substring of the other | C # program to implement the above approach ; Function to check if a string can be divided into two substrings such that one substring is substring of the other ; Store the last character of S ; Traverse the characters at indices [ 0 , N - 2 ] ; Check if the current character is equal to the last character ; If true , set f = 1 ; Break out of the loop ; Driver code ; Given string , S ; Store the size of S ; Function Call
using System ; class GFG { static void splitString ( string S , int N ) { char c = S [ N - 1 ] ; int f = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S [ i ] == c ) { f = 1 ; break ; } } if ( f != 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( ) { string S = " abcdab " ; int N = S . Length ; splitString ( S , N ) ; } }
Find the light bulb with maximum glowing time | C # program for the above approach ; Function to find the bulb having maximum glow ; Initialize variables ; Traverse the array consisting of glowing time of the bulbs ; For 1 st bulb ; Calculate the glowing time ; Update the maximum glow ; Find lexicographically largest bulb ; Bulb with maximum time ; Return the resultant bulb ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static char longestLastingBulb ( List < int > onTime , string s ) { char ans ; int n = onTime . Count ; int maxDur = Int32 . MinValue ; int maxPos = Int32 . MinValue ; int currentDiff = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { currentDiff = onTime [ i ] ; maxDur = currentDiff ; maxPos = i ; } else { currentDiff = onTime [ i ] - onTime [ i - 1 ] ; if ( maxDur < currentDiff ) { maxDur = currentDiff ; maxPos = i ; } else { if ( maxDur == currentDiff ) { char one = s [ i ] ; char two = s [ maxPos ] ; if ( one > two ) { maxDur = currentDiff ; maxPos = i ; } } } } } ans = s [ maxPos ] ; return ans ; } static void Main ( ) { string S = " spuda " ; List < int > arr = new List < int > ( new int [ ] { 12 , 23 , 36 , 46 , 62 } ) ; Console . Write ( longestLastingBulb ( arr , S ) ) ; } }
Find the next greater element in a Circular Array | Set 2 | C # program for the above approach ; Function to find the NGE for the given circular array [ ] arr ; Initialize stack and nge [ ] array ; Initialize nge [ ] array to - 1 ; Traverse the array ; If stack is not empty and current element is greater than top element of the stack ; Assign next greater element for the top element of the stack ; Pop the top element of the stack ; Print the nge [ ] array ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printNGE ( int [ ] arr , int n ) { Stack < int > s = new Stack < int > ( ) ; int [ ] nge = new int [ n ] ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { nge [ i ] = - 1 ; } i = 0 ; while ( i < 2 * n ) { while ( s . Count != 0 && arr [ i % n ] > arr [ s . Peek ( ) ] ) { nge [ s . Peek ( ) ] = arr [ i % n ] ; s . Pop ( ) ; } s . Push ( i % n ) ; i ++ ; } for ( i = 0 ; i < n ; i ++ ) { Console . Write ( nge [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , - 2 , 5 , 8 } ; int N = arr . Length ; printNGE ( arr , N ) ; } }
Count array elements whose product of digits is a Composite Number | C # program for the above approach ; Function to generate prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to calculate the product of digits of the given number ; Stores the product of digits ; Extract digits and add to the sum ; Return the product of its digits ; Function to print number of distinct values with digit product as composite ; Initialize set ; Initialize boolean array ; Pre - compute primes ; Traverse array ; Stores the product of digits of the current array element ; If Product of digits is less than or equal to 1 ; If Product of digits is not a prime ; Print the answer ; Driver Code ; Given array ; Given size ; Function call
using System ; using System . Collections . Generic ; class GFG { static int N = 100005 ; static void SieveOfEratosthenes ( bool [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int digitProduct ( int number ) { int res = 1 ; while ( number > 0 ) { res *= ( number % 10 ) ; number /= 10 ; } return res ; } static void DistinctCompositeDigitProduct ( int [ ] arr , int n ) { SortedSet < int > output = new SortedSet < int > ( ) ; bool [ ] prime = new bool [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( prime , N ) ; for ( int i = 0 ; i < n ; i ++ ) { int ans = digitProduct ( arr [ i ] ) ; if ( ans <= 1 ) { continue ; } if ( ! prime [ ans ] ) { output . Add ( ans ) ; } } Console . WriteLine ( output . Count ) ; } public static void Main ( ) { int [ ] arr = { 13 , 55 , 7 , 13 , 11 , 71 , 233 , 233 , 144 , 89 } ; int n = arr . Length ; DistinctCompositeDigitProduct ( arr , n ) ; } }
Count pairs from a given range whose sum is a Prime Number in that range | C # program to implement the above approach ; Function to find all prime numbers in range [ 1 , lmt ] using sieve of Eratosthenes ; segmentedSieve [ i ] : Stores if i is a prime number ( True ) or not ( False ) ; Initialize all elements of segmentedSieve [ ] to false ; Set 0 and 1 as non - prime ; Iterate over the range [ 2 , lmt ] ; If i is a prime number ; Append i into prime ; Set all multiple of i non - prime ; Update Sieve [ j ] ; Function to find all the prime numbers in the range [ low , high ] ; Stores square root of high + 1 ; Stores all the prime numbers in the range [ 1 , lmt ] ; Find all the prime numbers in the range [ 1 , lmt ] ; Stores count of elements in the range [ low , high ] ; segmentedSieve [ i ] : Check if ( i - low ) is a prime number or not ; Traverse the array prime [ ] ; Store smallest multiple of prime [ i ] in the range [ low , high ] ; If lowLim is less than low ; Update lowLim ; Iterate over all multiples of prime [ i ] ; If j not equal to prime [ i ] ; Update segmentedSieve [ j - low ] ; Function to count the number of pairs in the range [ L , R ] whose sum is a prime number in the range [ L , R ] ; segmentedSieve [ i ] : Check if ( i - L ) is a prime number or not ; Stores count of pairs whose sum of elements is a prime and in range [ L , R ] ; Iterate over [ L , R ] ; If ( i - L ) is a prime ; Update cntPairs ; Driver Code
using System ; using System . Collections ; class GFG { static ArrayList simpleSieve ( int lmt , ArrayList prime ) { bool [ ] Sieve = new bool [ lmt + 1 ] ; Array . Fill ( Sieve , true ) ; Sieve [ 0 ] = Sieve [ 1 ] = false ; for ( int i = 2 ; i <= lmt ; ++ i ) { if ( Sieve [ i ] == true ) { prime . Add ( i ) ; for ( int j = i * i ; j <= lmt ; j += i ) { Sieve [ j ] = false ; } } } return prime ; } static bool [ ] SegmentedSieveFn ( int low , int high ) { int lmt = ( int ) ( Math . Sqrt ( high ) ) + 1 ; ArrayList prime = new ArrayList ( ) ; prime = simpleSieve ( lmt , prime ) ; int n = high - low + 1 ; bool [ ] segmentedSieve = new bool [ n + 1 ] ; Array . Fill ( segmentedSieve , true ) ; for ( int i = 0 ; i < prime . Count ; i ++ ) { int lowLim = ( int ) ( low / ( int ) prime [ i ] ) * ( int ) prime [ i ] ; if ( lowLim < low ) { lowLim += ( int ) prime [ i ] ; } for ( int j = lowLim ; j <= high ; j += ( int ) prime [ i ] ) { if ( j != ( int ) prime [ i ] ) { segmentedSieve [ j - low ] = false ; } } } return segmentedSieve ; } static int countPairsWhoseSumPrimeL_R ( int L , int R ) { bool [ ] segmentedSieve = SegmentedSieveFn ( L , R ) ; int cntPairs = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( segmentedSieve [ i - L ] ) { cntPairs += i / 2 ; } } return cntPairs ; } public static void Main ( ) { int L = 1 , R = 5 ; Console . Write ( countPairsWhoseSumPrimeL_R ( L , R ) ) ; } }
Lexicographically smallest permutation of a string that can be reduced to length K by removing K | C # program for the above approach ; Function to count the number of zeroes present in the string ; Traverse the string ; Return the count ; Function to rearrange the string s . t the string can be reduced to a length K as per the given rules ; Distribute the count of 0 s and 1 s in segment of length 2 k ; Store string that are initially have formed lexicographically smallest 2 k length substring ; Store the lexicographically smallest string of length n that satisfy the condition ; Insert temp_str into final_str ( n / 2 k ) times and add ( n % 2 k ) characters of temp_str at end ; Return the final string ; Function to reduce the string to length K that follows the given conditions ; If the string contains either 0 s or 1 s then it always be reduced into a K length string ; If the string contains only 0 s 1 s then it always reduces to a K length string ; If K = 1 ; Check whether the given string is K reducing string or not ; Otherwise recursively find the string ; Driver Code ; Function Call
using System ; class GFG { static int count_zeroes ( int n , string str ) { int cnt = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '0' ) cnt ++ ; } return cnt ; } static string kReducingStringUtil ( int n , int k , string str , int no_of_zeroes ) { int zeroes_in_2k = ( ( no_of_zeroes ) * ( 2 * k ) ) / n ; int ones_in_2k = 2 * k - zeroes_in_2k ; string temp_str = " " ; for ( int i = 0 ; i < ( zeroes_in_2k ) / 2 ; i ++ ) { temp_str += '0' ; } for ( int i = 0 ; i < ones_in_2k ; i ++ ) { temp_str += '1' ; } for ( int i = 0 ; i < ( zeroes_in_2k ) / 2 ; i ++ ) { temp_str += '0' ; } string final_str = " " ; for ( int i = 0 ; i < n / ( 2 * k ) ; i ++ ) { final_str += ( temp_str ) ; } for ( int i = 0 ; i < n % ( 2 * k ) ; i ++ ) { final_str += temp_str [ i ] ; } return final_str ; } static string kReducingString ( int n , int k , string str ) { int no_of_zeroes = count_zeroes ( n , str ) ; int no_of_ones = n - no_of_zeroes ; if ( no_of_zeroes == 0 no_of_zeroes == n ) { return str ; } if ( k == 1 ) { if ( no_of_zeroes == 0 no_of_zeroes == n ) { return str ; } else { return " Not ▁ Possible " ; } } bool check = false ; for ( int i = ( n / k ) ; i < n ; i += ( n / k ) ) { if ( no_of_zeroes == i no_of_ones == i ) { check = true ; break ; } } if ( check == false ) { return " Not ▁ Possible " ; } return kReducingStringUtil ( n , k , str , no_of_zeroes ) ; } public static void Main ( ) { string str = "0000100001100001" ; int K = 4 ; int N = str . Length ; Console . WriteLine ( kReducingString ( N , K , str ) ) ; } }
Count quadruplets with sum K from given array | C # program for the above approach ; Function to return the number of quadruplets having the given sum ; Initialize variables ; Initialize answer ; Store the frequency of sum of first two elements ; Traverse from 0 to N - 1 , where arr [ i ] is the 3 rd element ; All possible 4 th elements ; Sum of last two element ; Frequency of sum of first two elements ; Store frequency of all possible sums of first two elements ; Return the answer ; Driver Code ; Given array [ ] arr ; Given sum S ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int countSum ( int [ ] a , int n , int sum ) { int i , j ; int count = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int temp = a [ i ] + a [ j ] ; if ( temp < sum && m . ContainsKey ( sum - temp ) ) count += m [ sum - temp ] ; } for ( j = 0 ; j < i ; j ++ ) { int temp = a [ i ] + a [ j ] ; if ( temp < sum ) if ( m . ContainsKey ( temp ) ) m [ temp ] ++ ; else m . Add ( temp , 1 ) ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = arr . Length ; Console . Write ( countSum ( arr , N , S ) ) ; } }
Count pairs whose product modulo 10 ^ 9 + 7 is equal to 1 | C # program to implement the above approach ; Iterative Function to calculate ( x ^ y ) % MOD ; Initialize result ; Update x if it exceeds MOD ; If x is divisible by MOD ; If y is odd ; Multiply x with res ; y must be even now ; Function to count number of pairs whose product modulo 1000000007 is 1 ; Stores the count of desired pairs ; Stores the frequencies of each array element ; Traverse the array and update frequencies in hash ; Calculate modular inverse of arr [ i ] under modulo 1000000007 ; Update desired count of pairs ; If arr [ i ] and its modular inverse is equal under modulo MOD ; Updating count of desired pairs ; Return the final count ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MOD = 1000000007 ; static long modPower ( long x , int y ) { long res = 1 ; x = x % MOD ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % MOD ; y = y / 2 ; x = ( x * x ) % MOD ; } return res ; } static int countPairs ( long [ ] arr , int N ) { int pairCount = 0 ; Dictionary < long , int > hash = new Dictionary < long , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( hash . ContainsKey ( arr [ i ] ) ) { hash . Add ( arr [ i ] , hash [ arr [ i ] ] + 1 ) ; } else { hash . Add ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < N ; i ++ ) { long modularInverse = modPower ( arr [ i ] , MOD - 2 ) ; if ( hash . ContainsKey ( modularInverse ) ) pairCount += hash [ modularInverse ] ; if ( arr [ i ] == modularInverse ) { pairCount -- ; } } return pairCount / 2 ; } public static void Main ( ) { long [ ] arr = { 2 , 236426 , 280311812 , 500000004 } ; int N = arr . Length ; Console . WriteLine ( countPairs ( arr , N ) ) ; } }
Check if all subarrays contains at least one unique element | C # program for the above approach ; Function to check if all subarrays of array have at least one unique element ; Stores frequency of subarray elements ; Generate all subarrays ; Insert first element in map ; Update frequency of current subarray in the Dictionary ; Check if at least one element occurs once in current subarray ; If any subarray doesn 't  have unique element ; Clear map for next subarray ; Return Yes if all subarray having at least 1 unique element ; Driver Code ; Given array [ ] arr ; Function Call
using System ; using System . Collections . Generic ; class GFG { static String check ( int [ ] arr , int n ) { Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { hm . Add ( arr [ i ] , 1 ) ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( hm . ContainsKey ( arr [ j ] ) ) hm [ arr [ j ] ] ++ ; else hm . Add ( arr [ j ] , 1 ) ; bool flag = false ; foreach ( int k in hm . Values ) { if ( k == 1 ) { flag = true ; break ; } } if ( ! flag ) return " No " ; } hm . Clear ( ) ; } return " Yes " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 } ; int N = arr . Length ; Console . WriteLine ( check ( arr , N ) ) ; } }
Search insert position of K in a sorted array | C # program for the above approach ; Function to find insert position of K ; Lower and upper bounds ; Traverse the search space ; If K is found ; Return insert position ; Driver Code
using System ; class GFG { static int find_index ( int [ ] arr , int n , int K ) { int start = 0 ; int end = n - 1 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( arr [ mid ] == K ) return mid ; else if ( arr [ mid ] < K ) start = mid + 1 ; else end = mid - 1 ; } return end + 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 6 } ; int n = arr . Length ; int K = 2 ; Console . WriteLine ( find_index ( arr , n , K ) ) ; } }