codeparrot/xlcost-text-to-code huggingface dataset:
Smallest element present in every subarray of all possible lengths | C # program for above approach ; Function to print the common elements for all subarray lengths ; Function to find and store the minimum element present in all subarrays of all lengths from 1 to n ; Skip lengths for which answer [ i ] is - 1 ; Initialize minimum as the first element where answer [ i ] is not - 1 ; Updating the answer array ; If answer [ i ] is - 1 , then minimum can be substituted in that place ; Find minimum answer ; Function to find the minimum number corresponding to every subarray of length K , for every K from 1 to N ; Stores the minimum common elements for all subarray lengths ; Initialize with - 1. ; Find for every element , the minimum length such that the number is present in every subsequence of that particular length or more ; To store first occurence and gaps between occurences ; To cover the distance between last occurence and the end of the array ; To find the distance between any two occurences ; Update and store the answer ; Print the required answer ; Function to find the smallest element common in all subarrays for every possible subarray lengths ; Initializing indices array ; Store the numbers present in the array ; Push the index in the indices [ A [ i ] ] and also store the numbers in set to get the numbers present in input array ; Function call to calculate length of subarray for which a number is present in every subarray of that length ; Driver code ; Given array ; Size of array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printAnswer ( int [ ] answer , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( answer [ i ] + " ▁ " ) ; } } static void updateAnswerArray ( int [ ] answer , int N ) { int i = 0 ; while ( answer [ i ] == - 1 ) i ++ ; int minimum = answer [ i ] ; while ( i <= N ) { if ( answer [ i ] == - 1 ) answer [ i ] = minimum ; else answer [ i ] = Math . Min ( minimum , answer [ i ] ) ; minimum = Math . Min ( minimum , answer [ i ] ) ; i ++ ; } } static void lengthOfSubarray ( List < List < int > > indices , HashSet < int > st , int N ) { int [ ] answer = new int [ N + 1 ] ; Array . Fill ( answer , - 1 ) ; foreach ( int itr in st ) { int start = - 1 ; int gap = - 1 ; int t = itr ; indices [ t ] . Add ( N ) ; for ( int i = 0 ; i < indices [ t ] . Count ; i ++ ) { gap = Math . Max ( gap , indices [ t ] [ i ] - start ) ; start = indices [ t ] [ i ] ; } if ( answer [ gap ] == - 1 ) answer [ gap ] = t ; } updateAnswerArray ( answer , N ) ; printAnswer ( answer , N ) ; } static void smallestPresentNumber ( int [ ] arr , int N ) { List < List < int > > indices = new List < List < int > > ( ) ; for ( int i = 0 ; i <= N ; i ++ ) indices . Add ( new List < int > ( ) ) ; HashSet < int > elements = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { indices [ arr [ i ] ] . Add ( i ) ; elements . Add ( arr [ i ] ) ; } lengthOfSubarray ( indices , elements , N ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 } ; int N = arr . Length ; smallestPresentNumber ( arr , N ) ; } }
Minimize remaining array element by removing pairs and replacing them by their absolute difference | C # program for above approach ; Function to find the smallest element left in the array by the given operations ; Base Case ; If this subproblem has occurred previously ; Including i - th array element into the first subset ; If i - th array element is not selected ; Update dp [ i , sum ] ; Utility function to find smallest element left in the array by the given operations ; Stores sum of the array elements ; Traverse the array ; Update total ; Stores overlapping subproblems ; Driver function
using System ; public class GFG { static int smallestLeft ( int [ ] arr , int total , int sum , int i , int [ , ] dp ) { if ( i == 0 ) { return Math . Abs ( total - 2 * sum ) ; } if ( dp [ i , sum ] != - 1 ) return dp [ i , sum ] ; int X = smallestLeft ( arr , total , sum + arr [ i - 1 ] , i - 1 , dp ) ; int Y = smallestLeft ( arr , total , sum , i - 1 , dp ) ; return dp [ i , sum ] = Math . Min ( X , Y ) ; } static void UtilSmallestElement ( int [ ] arr , int N ) { int total = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total += arr [ i ] ; } int [ , ] dp = new int [ N + 1 , total ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < total ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( smallestLeft ( arr , total , 0 , N , dp ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 4 , 1 , 8 , 1 } ; int N = arr . Length ; UtilSmallestElement ( arr , N ) ; } }
Minimize remaining array element by removing pairs and replacing them by their absolute difference | C # program to implement the above approach ; Function to find minimize the remaining array element by removing pairs and replacing them by their absolute difference ; Stores sum of array elements ; Traverse the array ; Update totalSum ; Stores half of totalSum ; dp [ i ] : True if sum i can be obtained as a subset sum ; Base case ; Stores closest sum that can be obtained as a subset sum ; Traverse the array ; Iterate over all possible value of sum ; Update dp [ j ] ; If sum i can be obtained from array elements ; Update reach ; Driver Code
using System ; class GFG { static int SmallestElementLeft ( int [ ] arr , int N ) { int totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } int req = totalSum / 2 ; bool [ ] dp = new bool [ req + 1 ] ; for ( int i = 0 ; i < req + 1 ; i ++ ) { dp [ i ] = false ; } dp [ 0 ] = true ; int reach = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = req ; j - arr [ i ] >= 0 ; j -- ) { dp [ j ] = dp [ j ] || dp [ j - arr [ i ] ] ; if ( dp [ j ] ) { reach = Math . Max ( reach , j ) ; } } } return totalSum - ( 2 * reach ) ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 2 } ; int N = arr . Length ; Console . Write ( SmallestElementLeft ( arr , N ) ) ; } }
Replace even | C # program to implement the above approach ; Function to count the minimum number of substrings of str1 such that replacing even - indexed characters of those substrings converts the string str1 to str2 ; Stores length of str1 ; Convert str1 to character array ; Stores minimum count of operations to convert str1 to str2 ; Traverse both the given string ; If current character in both the strings are equal ; Stores current index of the string ; If current character in both the string are not equal ; Replace str1 [ ptr ] by str2 [ ptr ] ; Update ptr ; Update cntOp ; Driver Code
using System ; class GFG { static int min_Operations ( String str1 , String str2 ) { int N = str1 . Length ; char [ ] str = str1 . ToCharArray ( ) ; int cntOp = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == str2 [ i ] ) { continue ; } int ptr = i ; while ( ptr < N && str [ ptr ] != str2 [ ptr ] ) { str [ ptr ] = str2 [ ptr ] ; ptr += 2 ; } cntOp ++ ; } return cntOp ; } public static void Main ( String [ ] args ) { String str1 = " abcdef " ; String str2 = " ffffff " ; Console . WriteLine ( min_Operations ( str1 , str2 ) ) ; } }
Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not | C # Program for the above approach ; Function to check if a number is composite or not ; Corner cases ; Check if the number is divisible by 2 or 3 or not ; Check if n is a multiple of any other prime number ; Function to check if a number is a Perfect Square or not ; Function to check if a number is a Fibonacci number or not ; If 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both are perfect square ; Function to check if GCD of composite numbers from the array [ ] a which are divisible by k is a Fibonacci number or not ; Traverse the array ; If array element is composite and divisible by k ; Calculate GCD of all elements in compositeset ; If GCD is Fibonacci ; Recursive function to return gcd of a and b ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static bool isPerfectSquare ( int x ) { int s = ( int ) Math . Sqrt ( x ) ; return ( s * s == x ) ; } static bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } static void ifgcdFibonacci ( int [ ] a , int n , int k ) { List < int > compositeset = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isComposite ( a [ i ] ) && a [ i ] % k == 0 ) { compositeset . Add ( a [ i ] ) ; } } int gcd = compositeset [ 0 ] ; for ( int i = 1 ; i < compositeset . Count ; i ++ ) { gcd = __gcd ( gcd , compositeset [ i ] ) ; if ( gcd == 1 ) { break ; } } if ( isFibonacci ( gcd ) ) { Console . Write ( " Yes " ) ; return ; } Console . Write ( " No " ) ; return ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 34 , 2 , 4 , 8 , 5 , 7 , 11 } ; int n = arr . Length ; int k = 2 ; ifgcdFibonacci ( arr , n , k ) ; } }
Longest subarray in which all elements are a factor of K | C # program to implement the above approach ; Function to find the length of the longest subarray in which all elements are a factor of K ; Stores length of the longest subarray in which all elements are a factor of K ; Stores length of the current subarray ; Traverse the array arr [ ] ; If A [ i ] is a factor of K ; Update Len ; Update MaxLen ; If A [ i ] is not a factor of K ; Update Len ; Driver Code
using System ; class GFG { static int find_longest_subarray ( int [ ] A , int N , int K ) { int MaxLen = 0 ; int Len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( K % A [ i ] == 0 ) { Len ++ ; MaxLen = Math . Max ( MaxLen , Len ) ; } else { Len = 0 ; } } return MaxLen ; } public static void Main ( string [ ] args ) { int [ ] A = { 2 , 8 , 3 , 10 , 6 , 7 , 4 , 9 } ; int N = A . Length ; int K = 60 ; Console . WriteLine ( find_longest_subarray ( A , N , K ) ) ; } }
Length of smallest meeting that can be attended | C # program to implement the above approach ; Function to find the minimum time to attend exactly one meeting ; Stores minimum time to attend exactly one meeting ; Sort entrance [ ] array ; Sort exit [ ] time ; Traverse meeting [ , ] ; Stores start time of current meeting ; Stores end time of current meeting ; Find just greater value of u in entrance [ ] ; Find just greater or equal value of u in entrance [ ] ; Console . WriteLine ( exit . Count ) ; Stores enter time to attend the current meeting ; Stores exist time after attending the meeting ; Update start lies in range [ 0 , m - 1 ] and end lies in the range [ 0 , p - 1 ] ; Update ans ; Return answer ; vec . Clear ( ) ; ; Driver Code ; Stores interval of meeting ; Stores entrance timings ; Stores exit timings ; Stores total count of meetings ; Stores total entrance timings ; Stores total exit timings ; Minimum time
using System ; using System . Collections . Generic ; class GFG { static List < int > exit = new List < int > ( ) ; static int minTime ( int [ , ] meeting , int n , int [ ] entrance , int m , int p ) { int ans = int . MaxValue ; Array . Sort ( entrance ) ; exit . Sort ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int u = meeting [ i , 0 ] ; int v = meeting [ i , 1 ] ; int it1 = upper_bound ( entrance , 0 , entrance . Length , u ) ; int it2 = lowerBound ( exit , 0 , exit . Count , v ) ; int start = it1 - 1 ; int end = it2 ; if ( start >= 0 && start < m && end >= 0 && end < p ) ans = Math . Min ( ans , exit [ end ] - entrance [ start ] ) ; } return ans >= int . MaxValue ? - 1 : ans ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } static int lowerBound ( List < int > vec , int low , int high , int element ) { int [ ] array = new int [ vec . Count ] ; int k = 0 ; foreach ( int val in vec ) { array [ k ] = val ; k ++ ; } while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } public static void Main ( String [ ] args ) { int [ , ] meeting = { { 15 , 19 } , { 5 , 10 } , { 7 , 25 } } ; int [ ] entrance = { 4 , 13 , 25 , 2 } ; exit . Add ( 10 ) ; exit . Add ( 25 ) ; int n = meeting . GetLength ( 0 ) ; int m = entrance . Length ; int p = exit . Count ; Console . Write ( minTime ( meeting , n , entrance , m , p ) + " STRNEWLINE " ) ; } }
Divide array into two arrays which does not contain any pair with sum K | C # program for the above approach ; Function to split the given array into two separate arrays satisfying given condition ; Stores resultant arrays ; Traverse the array ; If a [ i ] is smaller than or equal to k / 2 ; Print first array ; Print second array ; Driver Code ; Given K ; Given array ; Given size
using System ; using System . Collections . Generic ; class GFG { static void splitArray ( int [ ] a , int n , int k ) { List < int > first = new List < int > ( ) ; List < int > second = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= k / 2 ) first . Add ( a [ i ] ) ; else second . Add ( a [ i ] ) ; } for ( int i = 0 ; i < first . Count ; i ++ ) { Console . Write ( first [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; for ( int i = 0 ; i < second . Count ; i ++ ) { Console . Write ( second [ i ] + " ▁ " ) ; } } public static void Main ( ) { int k = 5 ; int [ ] a = { 0 , 1 , 3 , 2 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = a . Length ; splitArray ( a , n , k ) ; } }
Count numbers in a given range whose count of prime factors is a Prime Number | C # program to implement the above approach ; Function to find the smallest prime factor of all the numbers in range [ 0 , MAX ] ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; No smallest prime factor of 0 and 1 exists ; Traverse all the numbers in the range [ 1 , MAX ] ; Update spf [ i ] ; Update all the numbers whose smallest prime factor is 2 ; Traverse all the numbers in the range [ 1 , sqrt ( MAX ) ] ; Check if i is a prime number ; Update all the numbers whose smallest prime factor is i ; Check if j is a prime number ; Function to find count of prime factor of num ; Stores count of prime factor of num ; Calculate count of prime factor ; Update count ; Update num ; Function to precalculate the count of numbers in the range [ 0 , i ] whose count of prime factors is a prime number ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; Update sum [ 0 ] ; Traverse all the numbers in the range [ 1 , MAX ] ; Stores count of prime factor of i ; If count of prime factor is a prime number ; Update sum [ i ] ; Update sum [ i ] ; Driver code ; Stores smallest prime factor of all the numbers in the range [ 0 , MAX ] ; Stores the sum of all the numbers in the range [ 0 , i ] count of prime factor is a prime number ; int N = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ;
using System ; class GFG { public static int MAX = 1001 ; public static int [ ] sieve ( ) { int [ ] spf = new int [ MAX ] ; spf [ 0 ] = spf [ 1 ] = - 1 ; for ( int i = 2 ; i < MAX ; i ++ ) { spf [ i ] = i ; } for ( int i = 4 ; i < MAX ; i = i + 2 ) { spf [ i ] = 2 ; } for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAX ; j = j + i ) { if ( spf [ j ] == j ) { spf [ j ] = i ; } } } } return spf ; } public static int countFactors ( int [ ] spf , int num ) { int count = 0 ; while ( num > 1 ) { count ++ ; num = num / spf [ num ] ; } return count ; } public static int [ ] precalculateSum ( int [ ] spf ) { int [ ] sum = new int [ MAX ] ; sum [ 0 ] = 0 ; for ( int i = 1 ; i < MAX ; i ++ ) { int prime_factor = countFactors ( spf , i ) ; if ( spf [ prime_factor ] == prime_factor ) { sum [ i ] = sum [ i - 1 ] + 1 ; } else { sum [ i ] = sum [ i - 1 ] ; } } return sum ; } public static void Main ( String [ ] args ) { int [ ] spf = sieve ( ) ; int [ ] sum = precalculateSum ( spf ) ; int [ , ] Q = { { 4 , 8 } , { 30 , 32 } } ; for ( int i = 0 ; i < 2 ; i ++ ) { Console . Write ( ( sum [ Q [ i , 1 ] ] - sum [ Q [ i , 0 ] - 1 ] ) + " ▁ " ) ; } } }
Maximum number of Perfect Numbers present in a subarray of size K | C # program for the above approach ; Function to check a number is Perfect Number or not ; Stores sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to N ; Function to return maximum sum of a subarray of size K ; If k is greater than N ; Compute sum of first window of size K ; Compute sums of remaining windows by removing first element of previous window and adding last element of current window ; return the answer ; Function to find all the perfect numbers in the array ; The given array is converted into binary array ; Driver Code
using System ; class GFG { static int isPerfect ( int N ) { int sum = 1 ; for ( int i = 2 ; i < Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { sum += i ; } else { sum += i + N / i ; } } } if ( sum == N && N != 1 ) return 1 ; return 0 ; } static int maxSum ( int [ ] arr , int N , int K ) { if ( N < K ) { Console . Write ( " Invalid " ) ; return - 1 ; } int res = 0 ; for ( int i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } int curr_sum = res ; for ( int i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = Math . Max ( res , curr_sum ) ; } return res ; } static int max_PerfectNumbers ( int [ ] arr , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = isPerfect ( arr [ i ] ) == 1 ? 1 : 0 ; } return maxSum ( arr , N , K ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 28 , 2 , 3 , 6 , 496 , 99 , 8128 , 24 } ; int K = 4 ; int N = arr . Length ; Console . Write ( max_PerfectNumbers ( arr , N , K ) ) ; } }
Count greater elements on the left side of every array element | C # program to implement the above approach ; Function to print the count of greater elements on left of each array element ; Function to get the count of greater elements on left of each array element ; Store distinct array elements in sorted order ; Stores the count of greater elements on the left side ; Traverse the array ; Insert array elements into the set ; Find previous greater element ; Find the distance between the previous greater element of arr [ i ] and last element of the set ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void display ( int [ ] countLeftGreater , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( countLeftGreater [ i ] + " ▁ " ) ; } } static void countGreater ( int [ ] arr , int N ) { List < int > St = new List < int > ( ) ; int [ ] countLeftGreater = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { St . Add ( arr [ i ] ) ; int it = 0 ; St . Sort ( ) ; foreach ( int itr in St ) { if ( arr [ i ] < itr ) { break ; } it ++ ; } countLeftGreater [ i ] = Math . Abs ( it - St . Count ) ; } display ( countLeftGreater , N ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 1 , 2 , 3 , 0 , 11 , 4 } ; int N = arr . Length ; countGreater ( arr , N ) ; } }
Count characters to be shifted from the start or end of a string to obtain another string | C # program for the above approach ; Function to find the minimum cost to convert string A to string B ; Length of string ; Initialize maxlen as 0 ; Traverse the string A ; Stores the length of substrings of string A ; Traversing string B for each character of A ; Shift i pointer towards right and increment length , if A [ i ] equals B [ j ] ; If traverse till end ; Update maxlen ; Return minimum cost ; Driver Code ; Given two strings A and B ; Function call
using System ; using System . Collections . Generic ; class GFG { static int minCost ( string A , string B ) { int n = A . Length ; int i = 0 ; int maxlen = 0 ; while ( i < n ) { int length = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( A [ i ] == B [ j ] ) { ++ i ; ++ length ; if ( i == n ) break ; } } maxlen = Math . Max ( maxlen , length ) ; } return n - maxlen ; } public static void Main ( ) { string A = " edacb " ; string B = " abcde " ; Console . WriteLine ( minCost ( A , B ) ) ; } }
Lexicographic rank of a string among all its substrings | C # program for the above approach ; Function to find lexicographic rank of String among all its subString ; Length of String ; Traverse the given String and store the indices of each character ; Extract the index ; Store it in the vector ; Traverse the alphaIndex array lesser than the index of first character of given String ; If alphaIndex [ i ] size exceeds 0 ; Traverse over the indices ; Add count of subString equal to n - alphaIndex [ i , j ] ; Store all subStrings in a vector str starting with the first character of the given String ; Insert the current character to subString ; Store the subString formed ; Sort the subString in the lexicographical order ; Find the rank of given String ; increase the rank until the given String is same ; If subString is same as the given String ; Add 1 to rank of the given String ; Driver Code ; Given String ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int lexicographicRank ( char [ ] s ) { int n = s . Length ; List < int > [ ] alphaIndex = new List < int > [ 26 ] ; for ( int i = 0 ; i < alphaIndex . Length ; i ++ ) alphaIndex [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { int x = s [ i ] - ' a ' ; alphaIndex [ x ] . Add ( i ) ; } int rank = 0 ; for ( int i = 0 ; i < 26 && ' a ' + i < s [ 0 ] ; i ++ ) { if ( alphaIndex [ i ] . Count > 0 ) { for ( int j = 0 ; j < alphaIndex [ i ] . Count ; j ++ ) { rank = rank + ( n - alphaIndex [ i ] [ j ] ) ; } } } List < String > str = new List < String > ( ) ; for ( int i = 0 ; i < alphaIndex [ s [ 0 ] - ' a ' ] . Count ; i ++ ) { String subString = " " ; int j = alphaIndex [ s [ 0 ] - ' a ' ] [ i ] ; for ( ; j < n ; j ++ ) { subString += ( s [ j ] ) ; str . Add ( subString ) ; } } str . Sort ( ) ; for ( int i = 0 ; i < str . Count ; i ++ ) { if ( ! str [ i ] . Equals ( String . Join ( " " , s ) ) ) { rank ++ ; } else { break ; } } return rank + 1 ; } public static void Main ( String [ ] args ) { String str = " enren " ; Console . Write ( lexicographicRank ( str . ToCharArray ( ) ) ) ; } }
Find H | C # implementation of the above approach ; Function to find the H - index ; Set the range for binary search ; Check if current citations is possible ; Check to the right of mid ; Update h - index ; Since current value is not possible , check to the left of mid ; Print the h - index ; Driver Code ; citations
using System ; class GFG { static int hIndex ( int [ ] citations , int n ) { int hindex = 0 ; int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( citations [ mid ] >= ( mid + 1 ) ) { low = mid + 1 ; hindex = mid + 1 ; } else { high = mid - 1 ; } } Console . WriteLine ( hindex ) ; return hindex ; } public static void Main ( ) { int n = 5 ; int [ ] citations = { 5 , 3 , 3 , 2 , 2 } ; hIndex ( citations , n ) ; } }
Check if all strings of an array can be made same by interchanging characters | C # program to implement the above approach ; Function to check if all Strings are equal after swap operations ; Stores the frequency of characters ; Stores the length of String ; Traverse the array ; Traverse each String ; Check if frequency of character is divisible by N ; Driver Code
using System ; class GFG { static bool checkEqual ( String [ ] arr , int N ) { int [ ] hash = new int [ 256 ] ; int M = arr [ 0 ] . Length ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { hash [ arr [ i ] [ j ] ] ++ ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( hash [ i ] % N != 0 ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { String [ ] arr = { " fdd " , " fhh " } ; int N = arr . Length ; if ( checkEqual ( arr , N ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }