codeparrot/xlcost-text-to-code huggingface dataset:
Search an element in a sorted array formed by reversing subarrays from a random index | C # program for the above approach ; Function to search an element in a sorted array formed by reversing subarrays from a random index ; Set the boundaries for binary search ; Apply binary search ; Initialize the middle element ; If element found ; Random point is on right side of mid ; From l to mid arr is reverse sorted ; Random point is on the left side of mid ; From mid to h arr is reverse sorted ; Return Not Found ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int find ( List < int > arr , int N , int key ) { int l = 0 ; int h = N - 1 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( arr [ l ] >= arr [ mid ] ) { if ( arr [ l ] >= key && key >= arr [ mid ] ) h = mid - 1 ; else l = mid + 1 ; } else { if ( arr [ mid ] >= key && key >= arr [ h ] ) l = mid + 1 ; else h = mid - 1 ; } } return - 1 ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 10 , 8 , 6 , 5 , 2 , 1 , 13 , 12 } ; int N = arr . Count ; int key = 8 ; int ans = find ( arr , N , key ) ; Console . Write ( ans ) ; } }
Maximum elements that can be removed from front of two arrays such that their sum is at most K | C # program for the above approach ; Function to find the maximum number of items that can be removed from both the arrays ; Stores the maximum item count ; Stores the prefix sum of the cost of items ; Insert the item cost 0 at the front of the arrays ; Build the prefix sum for the array A [ ] ; Update the value of A [ i ] ; Build the prefix sum for the array B [ ] ; Update the value of B [ i ] ; Iterate through each item of the array A [ ] ; If A [ i ] exceeds K ; Store the remaining amount after taking top i elements from the array A ; Store the number of items possible to take from the array B [ ] ; Store low and high bounds for binary search ; Binary search to find number of item that can be taken from stack B in rem amount ; Calculate the mid value ; Update the value of j and lo ; Update the value of the hi ; Store the maximum of total item count ; Print the result ; Driver code
using System ; class GFG { static void maxItems ( int n , int m , int [ ] a , int [ ] b , int K ) { int count = 0 ; int [ ] A = new int [ n + 1 ] ; int [ ] B = new int [ m + 1 ] ; A [ 0 ] = 0 ; B [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { A [ i ] = a [ i - 1 ] + A [ i - 1 ] ; } for ( int i = 1 ; i <= m ; i ++ ) { B [ i ] = b [ i - 1 ] + B [ i - 1 ] ; } for ( int i = 0 ; i <= n ; i ++ ) { if ( A [ i ] > K ) break ; int rem = K - A [ i ] ; int j = 0 ; int lo = 0 , hi = m ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; if ( B [ mid ] <= rem ) { j = mid ; lo = mid + 1 ; } else { hi = mid - 1 ; } } count = Math . Max ( j + i , count ) ; } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int n = 4 , m = 5 , K = 7 ; int [ ] A = { 2 , 4 , 7 , 3 } ; int [ ] B = { 1 , 9 , 3 , 4 , 5 } ; maxItems ( n , m , A , B , K ) ; } }
Number which is co | C # program for the above approach ; Function to check whether the given number N is prime or not ; Base Case ; If N has more than one factor , then return false ; Otherwise , return true ; Function to find X which is co - prime with the integers from the range [ L , R ] ; Store the resultant number ; Check for prime integers greater than R ; If the current number is prime , then update coPrime and break out of loop ; Print the resultant number ; Driver Code
using System ; class GFG { static bool isPrime ( int N ) { if ( N == 1 ) return false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } static int findCoPrime ( int L , int R ) { int coPrime ; for ( int i = R + 1 ; ; i ++ ) { if ( isPrime ( i ) ) { coPrime = i ; break ; } } return coPrime ; } public static void Main ( string [ ] args ) { int L = 16 , R = 17 ; Console . WriteLine ( findCoPrime ( L , R ) ) ; } }
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | C # program for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the required result ; Traverse over all the pairs ( i , j ) ; Calculate the value of the expression and update the overall maximum value ; Print the result ; Driver code
using System ; class GFG { static void maximumValue ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { ans = Math . Max ( ans , arr [ i ] + arr [ j ] + i - j ) ; } } Console . Write ( ans ) ; } static void Main ( ) { int [ ] arr = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . Length ; maximumValue ( arr , N ) ; } }
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | C # program for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the maximum value of ( arr [ i ] + i ) ; Stores the required result ; Traverse the array arr [ ] ; Calculate for current pair and update maximum value ; Update maxValue if ( arr [ i ] + I ) is greater than maxValue ; Print the result ; Driver code
using System ; class GFG { static void maximumValue ( int [ ] arr , int n ) { int maxvalue = arr [ 0 ] ; int result = 0 ; for ( int i = 1 ; i < n ; i ++ ) { result = Math . Max ( result , maxvalue + arr [ i ] - i ) ; maxvalue = Math . Max ( maxvalue , arr [ i ] + i ) ; } Console . Write ( result ) ; } static void Main ( ) { int [ ] arr = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . Length ; maximumValue ( arr , N ) ; } }
Minimum sum of absolute differences of pairs in a triplet from three arrays | C # program for the above approach ; Lower_bound function ; Function to find the value closest to K in the array A [ ] ; Initialize close value as the end element ; Find lower bound of the array ; If lower_bound is found ; If lower_bound is not the first array element ; If * ( it - 1 ) is closer to k ; Return closest value of k ; Function to find the minimum sum of abs ( arr [ i ] - brr [ j ] ) and abs ( brr [ j ] - crr [ k ] ) ; Sort the vectors arr and crr ; Initialize minimum as INT_MAX ; Traverse the array brr [ ] ; Stores the element closest to val from the array arr [ ] ; Stores the element closest to val from the array crr [ ] ; If sum of differences is minimum ; Update the minimum ; Print the minimum absolute difference possible ; Driver Code ; Function Call
using System ; class GFG { public static int lower_bound ( int [ ] arr , int key ) { int low = 0 ; int high = arr . Length - 1 ; while ( low < high ) { int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] >= key ) { high = mid ; } else { low = mid + 1 ; } } return low ; } static int closestValue ( int [ ] A , int k ) { int close = A [ A . Length - 1 ] ; int it = lower_bound ( A , k ) ; if ( it != A . Length ) { close = A [ it ] ; if ( it != 0 ) { if ( ( k - A [ it - 1 ] ) < ( close - k ) ) { close = A [ it - 1 ] ; } } } return close ; } static void minPossible ( int [ ] arr , int [ ] brr , int [ ] crr ) { Array . Sort ( arr ) ; Array . Sort ( crr ) ; int minimum = Int32 . MaxValue ; foreach ( int val in brr ) { int arr_close = closestValue ( arr , val ) ; int crr_close = closestValue ( crr , val ) ; if ( Math . Abs ( val - arr_close ) + Math . Abs ( val - crr_close ) < minimum ) minimum = Math . Abs ( val - arr_close ) + Math . Abs ( val - crr_close ) ; } Console . WriteLine ( minimum ) ; } static void Main ( ) { int [ ] a = { 1 , 8 , 5 } ; int [ ] b = { 2 , 9 } ; int [ ] c = { 5 , 4 } ; minPossible ( a , b , c ) ; } }
Minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively | C # program for the above approach ; Function to find the minimum number of swaps required to make the first and the last elements the largest and smallest element in the array ; Stores the count of swaps ; Stores the maximum element ; Stores the minimum element ; If the array contains a single distinct element ; Stores the indices of the maximum and minimum elements ; If the first index of the maximum element is found ; If current index has the minimum element ; Update the count of operations to place largest element at the first ; Update the count of operations to place largest element at the last ; If smallest element is present before the largest element initially ; Driver Code
using System ; using System . Linq ; class GFG { static int minimum_swaps ( int [ ] arr , int n ) { int count = 0 ; int max_el = arr . Max ( ) ; int min_el = arr . Min ( ) ; if ( min_el == max_el ) return 0 ; int index_max = - 1 ; int index_min = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == max_el && index_max == - 1 ) { index_max = i ; } if ( arr [ i ] == min_el ) { index_min = i ; } } count += index_max ; count += ( n - 1 - index_min ) ; if ( index_min < index_max ) count -= 1 ; return count ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 1 , 6 , 5 } ; int N = arr . Length ; Console . WriteLine ( minimum_swaps ( arr , N ) ) ; } }
Maximum difference between node and its ancestor in a Directed Acyclic Graph ( DAG ) | C # program for the above approach ; Function to perform DFS Traversal on the given graph ; Update the value of ans ; Update the currentMin and currentMax ; Traverse the adjacency list of the node src ; Recursively call for the child node ; Function to calculate maximum absolute difference between a node and its ancestor ; Stores the adjacency list of graph ; Create Adjacency list ; Add a directed edge ; Perform DFS Traversal ; Print the maximum absolute difference ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int ans ; static void DFS ( int src , List < List < int > > Adj , int [ ] arr , int currentMin , int currentMax ) { ans = Math . Max ( ans , Math . Max ( Math . Abs ( currentMax - arr [ src - 1 ] ) , Math . Abs ( currentMin - arr [ src - 1 ] ) ) ) ; currentMin = Math . Min ( currentMin , arr [ src - 1 ] ) ; currentMax = Math . Min ( currentMax , arr [ src - 1 ] ) ; foreach ( int child in Adj [ src ] ) { DFS ( child , Adj , arr , currentMin , currentMax ) ; } } static void getMaximumDifference ( int [ , ] Edges , int [ ] arr , int N , int M ) { ans = 0 ; List < List < int > > Adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) Adj . Add ( new List < int > ( ) ) ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; Adj [ u ] . Add ( v ) ; } DFS ( 1 , Adj , arr , arr [ 0 ] , arr [ 0 ] ) ; Console . WriteLine ( ans ) ; } static void Main ( ) { int N = 5 , M = 4 ; int [ , ] Edges = { { 1 , 2 } , { 2 , 3 } , { 4 , 5 } , { 1 , 3 } } ; int [ ] arr = { 13 , 8 , 3 , 15 , 18 } ; getMaximumDifference ( Edges , arr , N , M ) ; } }
Check if a given string is a comment or not | C # program for the above approach ; Function to check if the given string is a comment or not ; If two continuous slashes preceeds the comment ; Driver Code ; Given string ; Function call to check whether then given string is a comment or not
using System ; using System . Collections . Generic ; class GFG { static void isComment ( string S ) { char [ ] line = S . ToCharArray ( ) ; if ( line [ 0 ] == ' / ' && line [ 1 ] == ' / ' && line [ 2 ] != ' / ' ) { Console . WriteLine ( " It ▁ is ▁ a ▁ single - line ▁ comment " ) ; return ; } if ( line [ line . Length - 2 ] == ' * ' && line [ line . Length - 1 ] == ' / ' && line [ 0 ] == ' / ' && line [ 1 ] == ' * ' ) { Console . WriteLine ( " It ▁ is ▁ a ▁ multi - line ▁ comment " ) ; return ; } Console . WriteLine ( " It ▁ is ▁ not ▁ a ▁ comment " ) ; } static public void Main ( ) { string line = " GeeksForGeeks ▁ GeeksForGeeks " ; isComment ( line ) ; } }
Print digits for each array element that does not divide any digit of that element | C # program for the above approach ; Function to find digits for each array element that doesn 't divide any digit  of the that element ; Traverse the array arr [ ] ; Iterate over the range [ 2 , 9 ] ; Stores if there exists any digit in arr [ i ] which is divisible by j ; If any digit of the number is divisible by j ; If the digit j doesn 't  divide any digit of arr[i] ; Driver Code
using System ; class GFG { static void indivisibleDigits ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " : ▁ " ) ; for ( int j = 2 ; j < 10 ; j ++ ) { int temp = arr [ i ] ; bool flag = true ; while ( temp > 0 ) { if ( ( temp % 10 ) != 0 && ( temp % 10 ) % j == 0 ) { flag = false ; break ; } temp /= 10 ; } if ( flag ) { Console . Write ( j + " ▁ " ) ; } } Console . WriteLine ( ) ; } } public static void Main ( ) { int [ ] arr = { 4162 , 1152 , 99842 } ; int N = arr . Length ; indivisibleDigits ( arr , N ) ; } }
Length of longest non | C # program for the above approach ; Function to find the longest non - decreasing subsequence with difference between adjacent elements exactly equal to 1 ; Base case ; Sort the array in ascending order ; Stores the maximum length ; Traverse the array ; If difference between current pair of adjacent elements is 1 or 0 ; Extend the current sequence Update len and max_len ; Otherwise , start a new subsequence ; Print the maximum length ; Driver Code ; Given array ; Size of the array ; Function call to find the longest subsequence
using System ; public class GFG { static void longestSequence ( int [ ] arr , int N ) { if ( N == 0 ) { Console . WriteLine ( 0 ) ; return ; } Array . Sort ( arr ) ; int maxLen = 1 ; int len = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i - 1 ] + 1 ) { len ++ ; maxLen = Math . Max ( maxLen , len ) ; } else { len = 1 ; } } Console . WriteLine ( maxLen ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 8 , 5 , 4 , 8 , 4 } ; int N = arr . Length ; longestSequence ( arr , N ) ; } }
Queries to find the minimum array sum possible by removing elements from either end | C # program for the above approach ; Function to find the minimum sum for each query after removing elements from either ends ; Traverse the query array ; Traverse the array from the front ; If element equals val , then break out of loop ; Traverse the array from rear ; If element equals val , break ; Print the minimum of the two as the answer ; Driver Code ; Function Call
using System ; class GFG { static void minSum ( int [ ] arr , int N , int [ ] Q , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int val = Q [ i ] ; int front = 0 , rear = 0 ; for ( int j = 0 ; j < N ; j ++ ) { front += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } for ( int j = N - 1 ; j >= 0 ; j -- ) { rear += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } Console . Write ( Math . Min ( front , rear ) + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 2 , 3 , 6 , 7 , 4 , 5 , 1 } ; int N = arr . Length ; int [ ] Q = { 7 , 6 } ; int M = Q . Length ; minSum ( arr , N , Q , M ) ; } }
Frequency of lexicographically Kth smallest character in the a string | C # program to implement the above approach ; Function to find the frequency of the lexicographically Kth smallest character ; Convert the string to array of characters ; Sort the array in ascending order ; Store the Kth character ; Store the frequency of the K - th character ; Count the frequency of the K - th character ; Print the count ; Driver Code
using System ; class GFG { public static void KthCharacter ( string S , int N , int K ) { char [ ] strarray = S . ToCharArray ( ) ; Array . Sort ( strarray ) ; char ch = strarray [ K - 1 ] ; int count = 0 ; foreach ( char c in strarray ) { if ( c == ch ) count ++ ; } Console . Write ( count ) ; } public static void Main ( ) { string S = " geeksforgeeks " ; int N = S . Length ; int K = 3 ; KthCharacter ( S , N , K ) ; } }
Longest substring with no pair of adjacent characters are adjacent English alphabets | C # program for the above approach ; Function to find the longest substring satisfying the given condition ; Stores all temporary substrings ; Stores the longest substring ; Stores the length of the substring T ; Stores the first character of string S ; Traverse the string ; If the absolute difference is 1 ; Update the length of substring T ; Update the longest substring ; Otherwise , stores the current character ; Again checking for longest substring and update accordingly ; Print the longest substring ; Driver Code ; Given string ; Function call to find the longest substring satisfying given condition
using System ; public class GFG { static void findSubstring ( string S ) { string T = " " ; string ans = " " ; int l = 0 ; T += S [ 0 ] ; for ( int i = 1 ; i < S . Length ; i ++ ) { if ( Math . Abs ( S [ i ] - S [ i - 1 ] ) == 1 ) { l = T . Length ; if ( l > ans . Length ) { ans = T ; } T = " " ; T += S [ i ] ; } else { T += S [ i ] ; } } l = T . Length ; if ( l > ans . Length ) { ans = T ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { string S = " aabdml " ; findSubstring ( S ) ; } }
Minimize length of an array by removing similar subarrays from both ends | C # program for the above approach ; Function to minimize length of the array by removing similar subarrays from both ends of the array ; Initialize two pointers ; Stores the current integer ; Check if the elements at both ends are same or not ; Move the front pointer ; Move the rear pointer ; Print the minimized length of the array ; Driver Code ; Input ; Function call to find the minimized length of the array
using System ; public class GFG { static void findMinLength ( int [ ] arr , int N ) { int front = 0 , back = N - 1 ; while ( front < back ) { int x = arr [ front ] ; if ( arr [ front ] != arr [ back ] ) break ; while ( arr [ front ] == x && front <= back ) front ++ ; while ( arr [ back ] == x && front <= back ) back -- ; } Console . WriteLine ( back - front + 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 } ; int N = arr . Length ; findMinLength ( arr , N ) ; } }
Digits whose alphabetic representations are jumbled in a given string | C # program for the above approach ; Function to convert the jumbled string into digits ; Initialize vector ; Initialize answer ; Size of the string ; Traverse the string ; Update the elements of the vector ; Print the digits into their original format ; Return answer ; Driver Code
using System ; public class GFG { static string finddigits ( string s ) { int [ ] arr = new int [ 10 ] ; string ans = " " ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' z ' ) arr [ 0 ] ++ ; if ( s [ i ] == ' w ' ) arr [ 2 ] ++ ; if ( s [ i ] == ' g ' ) arr [ 8 ] ++ ; if ( s [ i ] == ' x ' ) arr [ 6 ] ++ ; if ( s [ i ] == ' v ' ) arr [ 5 ] ++ ; if ( s [ i ] == ' o ' ) arr [ 1 ] ++ ; if ( s [ i ] == ' s ' ) arr [ 7 ] ++ ; if ( s [ i ] == ' f ' ) arr [ 4 ] ++ ; if ( s [ i ] == ' h ' ) arr [ 3 ] ++ ; if ( s [ i ] == ' i ' ) arr [ 9 ] ++ ; } arr [ 7 ] -= arr [ 6 ] ; arr [ 5 ] -= arr [ 7 ] ; arr [ 4 ] -= arr [ 5 ] ; arr [ 1 ] -= ( arr [ 2 ] + arr [ 4 ] + arr [ 0 ] ) ; arr [ 3 ] -= arr [ 8 ] ; arr [ 9 ] -= ( arr [ 5 ] + arr [ 6 ] + arr [ 8 ] ) ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) { ans += ( char ) ( i + '0' ) ; } } return ans ; } static public void Main ( ) { string s = " owoftnuoer " ; Console . WriteLine ( finddigits ( s ) ) ; } }
Check if two binary strings can be made equal by swapping pairs of unequal characters | C # program to implement of above approach ; Function to check if a string s1 can be converted into s2 ; Count of '0' in strings in s1 and s2 ; Iterate both the strings and count the number of occurrences of ; Count is not equal ; Iterating over both the arrays and count the number of occurrences of '0' ; If the count of occurrences of '0' in S2 exceeds that in S1 ; Driver code
using System ; class GFG { static void check ( string s1 , string s2 ) { int s1_0 = 0 , s2_0 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] == '0' ) { s1_0 ++ ; } if ( s2 [ i ] == '0' ) { s2_0 ++ ; } } if ( s1_0 != s2_0 ) { Console . WriteLine ( " NO " ) ; return ; } else { int Count1 = 0 , Count2 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] == '0' ) { Count1 ++ ; } if ( s2 [ i ] == '0' ) { Count2 ++ ; } if ( Count1 < Count2 ) { Console . WriteLine ( " NO " ) ; return ; } } Console . WriteLine ( " YES " ) ; } } static void Main ( ) { string s1 = "100111" ; string s2 = "111010" ; check ( s1 , s2 ) ; s1 = "110100" ; s2 = "010101" ; check ( s1 , s2 ) ; } }
Check if two binary strings can be made equal by swapping 1 s occurring before 0 s | C # program for the above approach ; Function to check if it is possible to make two binary strings equal by given operations ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str1 ; Stores count of 1 ' s ▁ and ▁ 0' s of the string str2 ; Stores current count of 1 's  presenty in the string str1 ; Count the number of 1 ' s ▁ and ▁ 0' s present in the strings str1 and str2 ; If the number of 1 ' s ▁ and ▁ 0' s are not same of the strings str1 and str2 then print not possible ; Traversing through the strings str1 and str2 ; If the str1 character not equals to str2 character ; Swaps 0 with 1 of the string str1 ; Breaks the loop as the count of 1 's is zero. Hence, no swaps  possible ; Swaps 1 with 0 in the string str1 ; Print not possible ; Driver Code ; Given Strings ; Function Call
using System ; using System . Text ; class GFG { static void isBinaryStringsEqual ( string str1 , string str2 ) { int str1Zeros = 0 , str1Ones = 0 ; int str2Zeros = 0 , str2Ones = 0 ; int flag = 0 ; int curStr1Ones = 0 ; for ( int i = 0 ; i < str1 . Length ; i ++ ) { if ( str1 [ i ] == '1' ) { str1Ones ++ ; } else if ( str1 [ i ] == '0' ) { str1Zeros ++ ; } if ( str2 [ i ] == '1' ) { str2Ones ++ ; } else if ( str2 [ i ] == '0' ) { str2Zeros ++ ; } } if ( str1Zeros != str2Zeros && str1Ones != str2Ones ) { Console . WriteLine ( " Not ▁ Possible " ) ; } else { for ( int i = 0 ; i < str1 . Length ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( str1 [ i ] == '0' && curStr1Ones > 0 ) { StringBuilder sb = new StringBuilder ( str1 ) ; sb [ i ] = '1' ; str1 = sb . ToString ( ) ; curStr1Ones -- ; } if ( str1 [ i ] == '0' && curStr1Ones == 0 ) { flag ++ ; break ; } if ( str1 [ i ] == '1' && str2 [ i ] == '0' ) { StringBuilder sb = new StringBuilder ( str1 ) ; sb [ i ] = '0' ; str1 = sb . ToString ( ) ; curStr1Ones ++ ; } } } if ( flag == 0 ) { Console . WriteLine ( " Possible " ) ; } else { Console . WriteLine ( " Not ▁ Possible " ) ; } } } static public void Main ( ) { string str1 = "0110" ; string str2 = "0011" ; isBinaryStringsEqual ( str1 , str2 ) ; } }
Count array elements whose all distinct digits appear in K | C # program to implement the above approach ; Function to the count of array elements whose distinct digits are a subset of the digits of K ; Stores distinct digits of K ; Iterate over all the digits of K ; Insert current digit into set ; Update K ; Stores the count of array elements whose distinct digits are a subset of the digits of K ; Traverse the array , [ ] arr ; Stores current element ; Check if all the digits of arr [ i ] are present in K or not ; Iterate over all the digits of arr [ i ] ; Stores current digit ; If digit not present in the set ; Update flag ; Update no ; If all the digits of arr [ i ] present in set ; Update count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int noOfValidKbers ( int K , int [ ] arr ) { HashSet < int > set = new HashSet < int > ( ) ; while ( K != 0 ) { set . Add ( K % 10 ) ; K = K / 10 ; } int count = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int no = arr [ i ] ; bool flag = true ; while ( no != 0 ) { int digit = no % 10 ; if ( ! set . Contains ( digit ) ) { flag = false ; break ; } no = no / 10 ; } if ( flag == true ) { count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int K = 12 ; int [ ] arr = { 1 , 12 , 1222 , 13 , 2 } ; Console . WriteLine ( noOfValidKbers ( K , arr ) ) ; } }