codeparrot/xlcost-text-to-code huggingface dataset:
Minimize K to let Person A consume at least ceil ( N / ( M + 1 ) ) candies based on given rules | C # program for the above approach ; Function to find minimum value of K such that the first person gets at least ( N / ( M + 1 ) ) candies ; Find the minimum required value of candies for the first person ; Iterate K from [ 1 , n ] ; Total number of candies ; Candies taken by Person 1 ; Candies taken by 1 st person is minimum of K and candies left ; Traverse the array arr [ ] ; Amount consumed by the person j ; Update the number of candies ; Good share of candies achieved ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void minimumK ( List < int > arr , int M , int N ) { int good = ( int ) ( ( N * 1.0 ) / ( ( M + 1 ) * 1.0 ) ) + 1 ; for ( int i = 1 ; i <= N ; i ++ ) { int K = i ; int candies = N ; int taken = 0 ; while ( candies > 0 ) { taken += Math . Min ( K , candies ) ; candies -= Math . Min ( K , candies ) ; for ( int j = 0 ; j < M ; j ++ ) { int consume = ( arr [ j ] * candies ) / 100 ; candies -= consume ; } } if ( taken >= good ) { Console . Write ( i ) ; return ; } } } public static void Main ( ) { int N = 13 , M = 1 ; List < int > arr = new List < int > ( ) ; arr . Add ( 50 ) ; minimumK ( arr , M , N ) ; } }
Total time required to travel a path denoted by a given string | C # program for the above approach ; Function to calculate time taken to travel the path ; Stores total time ; Initial position ; Stores visited segments ; Check whether segment is present in the set ; Increment the value of time by 2 ; Insert segment into the set ; Print the value of time ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void calcTotalTime ( string path ) { int time = 0 ; int x = 0 , y = 0 ; HashSet < string > s = new HashSet < string > ( ) ; for ( int i = 0 ; i < path . Length ; i ++ ) { int p = x ; int q = y ; if ( path [ i ] == ' N ' ) y ++ ; else if ( path [ i ] == ' S ' ) y -- ; else if ( path [ i ] == ' E ' ) x ++ ; else if ( path [ i ] == ' W ' ) x -- ; string o = ( p + x ) + " ▁ " + ( q + y ) ; if ( s . Contains ( o ) == false ) { time += 2 ; s . Add ( o ) ; } else time += 1 ; } Console . Write ( time ) ; } public static void Main ( ) { string path = " NSE " ; calcTotalTime ( path ) ; } }
Cost required to make all array elements equal to 1 | C # program for the above approach ; Function to calculate the cost required to make all array elements equal to 1 ; Stores the total cost ; Traverse the array arr [ ] ; If current element is 0 ; Convert 0 to 1 ; Add the cost ; Return the total cost ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int findCost ( int [ ] A , int N ) { int totalCost = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 0 ) { A [ i ] = 1 ; totalCost += i ; } } return totalCost ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 1 , 0 , 1 , 0 } ; int N = arr . Length ; Console . Write ( findCost ( arr , N ) ) ; } }
Find the peak index of a given array | C # program for the above approach ; Function to find the peak index for the given array ; Base Case ; Check for strictly increasing array ; If the strictly increasing condition is violated , then break ; Stores the value of i , which is a potential peak index ; Second traversal , for strictly decreasing array ; When the strictly decreasing condition is violated , then break ; If i = N - 1 , it means that ans is the peak index ; Otherwise , peak index doesn 't exist ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int peakIndex ( int [ ] arr ) { int N = arr . Length ; if ( arr . Length < 3 ) return - 1 ; int i = 0 ; while ( i + 1 < N ) { if ( arr [ i + 1 ] < arr [ i ] arr [ i ] == arr [ i + 1 ] ) break ; i ++ ; } if ( i == 0 i == N - 1 ) return - 1 ; int ans = i ; while ( i < N - 1 ) { if ( arr [ i ] < arr [ i + 1 ] arr [ i ] == arr [ i + 1 ] ) break ; i ++ ; } if ( i == N - 1 ) return ans ; return - 1 ; } static public void Main ( ) { int [ ] arr = { 0 , 1 , 0 } ; Console . WriteLine ( peakIndex ( arr ) ) ; } }
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | Set 2 | C # program for the above approach ; Function to check if the array has 2 elements whose sum is equal to the given value ; Sort the array in increasing order ; Traverse the array , nums [ ] ; Store the required number to be found ; Perform binary search ; Store the mid value ; If nums [ mid ] is greater than x , then update high to mid - 1 ; If nums [ mid ] is less than x , then update low to mid + 1 ; Otherwise ; If mid is equal i , check mid - 1 and mid + 1 ; Otherwise , print the pair and return ; If no such pair is found , then print - 1 ; Driver Code ; Function Call
using System ; public class GFG { static void hasArrayTwoPairs ( int [ ] nums , int n , int target ) { Array . Sort ( nums ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = target - nums [ i ] ; int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = low + ( ( high - low ) / 2 ) ; if ( nums [ mid ] > x ) { high = mid - 1 ; } else if ( nums [ mid ] < x ) { low = mid + 1 ; } else { if ( mid == i ) { if ( ( mid - 1 >= 0 ) && nums [ mid - 1 ] == x ) { Console . Write ( nums [ i ] + " , ▁ " ) ; Console . Write ( nums [ mid - 1 ] ) ; return ; } if ( ( mid + 1 < n ) && nums [ mid + 1 ] == x ) { Console . Write ( nums [ i ] + " , ▁ " ) ; Console . Write ( nums [ mid + 1 ] ) ; return ; } break ; } else { Console . Write ( nums [ i ] + " , ▁ " ) ; Console . Write ( nums [ mid ] ) ; return ; } } } } Console . Write ( - 1 ) ; } static public void Main ( ) { int [ ] A = { 0 , - 1 , 2 , - 3 , 1 } ; int X = - 2 ; int N = A . Length ; hasArrayTwoPairs ( A , N , X ) ; } }
Smallest divisor of N closest to X | C # program for the above approach ; Function to find the divisor of N closest to the target ; Iterate till square root of N ; Check if divisors are equal ; Check if i is the closest ; Check if i is the closest ; Check if n / i is the closest ; Print the closest value ; Driver code ; Given N & X ; Function Call
using System ; class GFG { static void findClosest ( int N , int target ) { int closest = - 1 ; int diff = Int32 . MaxValue ; for ( int i = 1 ; i <= Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( N / i == i ) { if ( Math . Abs ( target - i ) < diff ) { diff = Math . Abs ( target - i ) ; closest = i ; } } else { if ( Math . Abs ( target - i ) < diff ) { diff = Math . Abs ( target - i ) ; closest = i ; } if ( Math . Abs ( target - N / i ) < diff ) { diff = Math . Abs ( target - N / i ) ; closest = N / i ; } } } } Console . Write ( closest ) ; } static void Main ( ) { int N = 16 , X = 5 ; findClosest ( N , X ) ; } }
Find a pair ( a , b ) such that Aa + Bb = N | C # program for the above approach ; Function to calculate the minimum power of A and B greater than N ; Stores the power of A which is greater than N ; Increment count by 1 ; Divide N by A ; Function to find a pair ( a , b ) such that A ^ a + B ^ b = N ; Calculate the minimum power of A greater than N ; Calculate the minimum power of B greater than N ; Make copy of A and B ; Traverse for every pair ( i , j ) ; Check if B ^ j + A ^ i = N To overcome the overflow problem use B = N - A rather than B + A = N ; Increment power B by 1 ; Increment power A by 1 ; Finally print - 1 if no pair is found ; Driver Code ; Given A , B and N ; Function Call
using System ; class GFG { static int power ( int A , int N ) { int count = 0 ; if ( A == 1 ) return 0 ; while ( N > 0 ) { count ++ ; N /= A ; } return count ; } static void Pairs ( int N , int A , int B ) { int powerA , powerB ; powerA = power ( A , N ) ; powerB = power ( B , N ) ; int intialB = B , intialA = A ; A = 1 ; for ( int i = 0 ; i <= powerA ; i ++ ) { B = 1 ; for ( int j = 0 ; j <= powerB ; j ++ ) { if ( B == N - A ) { Console . WriteLine ( i + " ▁ " + j ) ; return ; } B *= intialB ; } A *= intialA ; } Console . WriteLine ( " - 1" ) ; return ; } public static void Main ( String [ ] args ) { int N = 106 , A = 3 , B = 5 ; Pairs ( N , A , B ) ; } }
Count numbers from a given range that are not divisible by any of the array elements | C # program for the above approach ; Function to find the non - multiples till k ; Stores all unique multiples ; Iterate the array ; For finding duplicates only once ; Inserting all multiples into the set ; Returning only the count of numbers that are not divisible by any of the array elements ; Function to count the total values in the range [ L , R ] ; Count all values in the range using exclusion principle ; Driver code ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static int findNonMultiples ( int [ ] arr , int n , int k ) { HashSet < int > multiples = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( ! multiples . Contains ( arr [ i ] ) ) { for ( int j = 1 ; j <= k / arr [ i ] ; j ++ ) { multiples . Add ( arr [ i ] * j ) ; } } } return k - multiples . Count ; } public static int countValues ( int [ ] arr , int N , int L , int R ) { return findNonMultiples ( arr , N , R ) - findNonMultiples ( arr , N , L - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int N = arr . Length ; int L = 1 ; int R = 20 ; Console . WriteLine ( countValues ( arr , N , L , R ) ) ; } }
Minimum number of coins to be collected per hour to empty N piles in at most H hours | C # program for the above approach ; Function to find the minimum number of coins to be collected per hour to empty N piles in H hours ; Stores the minimum coins to be removed per hour ; Find the maximum array element ; Perform Binary Search ; Store the mid value of the range in K ; Find the total time taken to empty N piles by removing K coins per hour ; If total time does not exceed H ; Otherwise ; Print the required result ; Driver Code ; Function Call
using System ; using System . Collections ; class GFG { static void minCollectingSpeed ( int [ ] piles , int H ) { int ans = - 1 ; int low = 1 , high ; Array . Sort ( piles ) ; high = piles [ piles . Length - 1 ] ; while ( low <= high ) { int K = low + ( high - low ) / 2 ; int time = 0 ; foreach ( int ai in piles ) { time += ( ai + K - 1 ) / K ; } if ( time <= H ) { ans = K ; high = K - 1 ; } else { low = K + 1 ; } } Console . Write ( ans ) ; } static public void Main ( string [ ] args ) { int [ ] arr = { 3 , 6 , 7 , 11 } ; int H = 8 ; minCollectingSpeed ( arr , H ) ; } }
Count distinct pairs with given sum | C # program to implement the above approach ; Function to count distinct pairs in array whose sum equal to K ; Stores count of distinct pairs whose sum equal to K ; Sort the array ; Stores index of the left pointer ; Stores index of the right pointer ; Calculate count of distinct pairs whose sum equal to K ; If sum of current pair is equal to K ; Remove consecutive duplicate array elements ; Update i ; Remove consecutive duplicate array elements ; Update j ; Update cntPairs ; Update i ; Update j ; If sum of current pair less than K ; Update i ; Update j ; Driver Code
using System ; class GFG { static int cntDisPairs ( int [ ] arr , int N , int K ) { int cntPairs = 0 ; Array . Sort ( arr ) ; int i = 0 ; int j = N - 1 ; while ( i < j ) { if ( arr [ i ] + arr [ j ] == K ) { while ( i < j && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } while ( i < j && arr [ j ] == arr [ j - 1 ] ) { j -- ; } cntPairs += 1 ; i ++ ; j -- ; } else if ( arr [ i ] + arr [ j ] < K ) { i ++ ; } else { j -- ; } } return cntPairs ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 5 , 7 , 7 , 8 } ; int N = arr . Length ; int K = 13 ; Console . WriteLine ( cntDisPairs ( arr , N , K ) ) ; } }
Count distinct pairs with given sum | C # program to implement the above approach ; Function to count distinct pairs in array whose sum equal to K ; Stores count of distinct pairs whose sum equal to K ; Store frequency of each distinct element of the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key value of the map ; If i is the half of K ; If frequency of i greater than 1 ; Update cntPairs ; Update cntPairs ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int cntDisPairs ( int [ ] arr , int N , int K ) { int cntPairs = 0 ; Dictionary < int , int > cntFre = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( cntFre . ContainsKey ( arr [ i ] ) ) cntFre [ arr [ i ] ] = cntFre [ arr [ i ] ] + 1 ; else cntFre . Add ( arr [ i ] , 1 ) ; } foreach ( KeyValuePair < int , int > it in cntFre ) { int i = it . Key ; if ( 2 * i == K ) { if ( cntFre [ i ] > 1 ) cntPairs += 2 ; } else { if ( cntFre . ContainsKey ( K - i ) ) { cntPairs += 1 ; } } } cntPairs = cntPairs / 2 ; return cntPairs ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 5 , 7 , 7 , 8 } ; int N = arr . Length ; int K = 13 ; Console . Write ( cntDisPairs ( arr , N , K ) ) ; } }
Queries to find longest subsequence having no similar adjacent elements with updates | C # program for the above approach ; Function to find the length of the longest subsequence such that no two adjacent elements are equal ; Replace element at index x with y ; Since x is 1 - indexed , decrement x by 1 ; Keep track of number of elements in subsequence ; If previous element is not same as current element ; Print the desired count ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void longestSubsequence ( int N , int Q , int [ ] arr , int [ , ] Queries ) { for ( int i = 0 ; i < Q ; i ++ ) { int x = Queries [ i , 0 ] ; int y = Queries [ i , 1 ] ; arr [ x - 1 ] = y ; int count = 1 ; for ( int j = 1 ; j < N ; j ++ ) { if ( arr [ j ] != arr [ j - 1 ] ) { count += 1 ; } } Console . Write ( count + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 5 , 2 } ; int N = arr . Length ; int Q = 2 ; int [ , ] Queries = { { 1 , 3 } , { 4 , 2 } } ; longestSubsequence ( N , Q , arr , Queries ) ; } }
Queries to find longest subsequence having no similar adjacent elements with updates | C # program for the above approach ; Traverse the array arr [ ] ; If previous element is not same as current element ; Traverse the queries ; Replace element at index x with y ; Recalculate for index x ; Subtract contribution of element at index x ; Add contribution of y ; Recalculate for index x + 1 ; Subtract contribution of element at index x + 1 ; Adds contribution of y ; Replace the element ; Driver Code ; Function Call
using System ; class GFG { static void longestSubsequence ( int N , int Q , int [ ] arr , int [ , ] Queries ) { int count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count += 1 ; } } for ( int i = 0 ; i < Q ; i ++ ) { int x = Queries [ i , 0 ] ; int y = Queries [ i , 1 ] ; if ( x > 1 ) { if ( arr [ x - 1 ] != arr [ x - 2 ] ) { count -= 1 ; } if ( arr [ x - 2 ] != y ) { count += 1 ; } } if ( x < N ) { if ( arr [ x ] != arr [ x - 1 ] ) { count -= 1 ; } if ( y != arr [ x ] ) { count += 1 ; } } Console . Write ( count + " ▁ " ) ; arr [ x - 1 ] = y ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 5 , 2 } ; int N = arr . Length ; int Q = 2 ; int [ , ] Queries = { { 1 , 3 } , { 4 , 2 } } ; longestSubsequence ( N , Q , arr , Queries ) ; } }
Sum of absolute differences of indices of occurrences of each array element | C # program for the above approach ; Function to find sum of differences of indices of occurrences of each unique array element ; Stores indices of each array element ; Store the indices ; Stores the sums ; Traverse the array ; Find sum for each element ; Iterate over the Map ; Calculate sum of occurrences of arr [ i ] ; Store sum for current element ; Print answer for each element ; Driver Code ; Given array ; Given size ; Function call
using System ; using System . Collections . Generic ; class GFG { static void sum ( int [ ] arr , int n ) { Dictionary < int , List < int > > mp = new Dictionary < int , List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { List < int > v = new List < int > ( ) ; v . Add ( i ) ; if ( mp . ContainsKey ( arr [ i ] ) ) v . AddRange ( mp [ arr [ i ] ] ) ; mp [ arr [ i ] ] = v ; } int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; foreach ( int it in mp [ arr [ i ] ] ) { sum += Math . Abs ( it - i ) ; } ans [ i ] = sum ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } return ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 1 , 2 } ; int n = arr . Length ; sum ( arr , n ) ; } }
Convert vowels into upper case character in a given string | C # program to implement the above approach ; Function to convert vowels into uppercase ; Stores the length of str ; Driver Code
using System ; class GFG { static void conVowUpp ( char [ ] str ) { int N = str . Length ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' a ' str [ i ] == ' e ' str [ i ] == ' i ' str [ i ] == ' o ' str [ i ] == ' u ' ) { char c = char . ToUpperInvariant ( str [ i ] ) ; str [ i ] = c ; } } foreach ( char c in str ) Console . Write ( c ) ; } public static void Main ( String [ ] args ) { String str = " eutopia " ; conVowUpp ( str . ToCharArray ( ) ) ; } }
Maximize number of days for which P chocolates can be distributed consecutively to N people | C # program to implement the above approach ; Stores the frequency of each type of chocolate ; Function to check if chocolates can be eaten for ' mid ' no . of days ; If cnt exceeds N , return true ; Function to find the maximum number of days for which chocolates can be eaten ; Store the frequency of each type of chocolate ; Initialize start and end with 0 and P respectively ; Calculate mid ; Check if chocolates can be distributed for mid days ; Check if chocolates can be distributed for more than mid consecutive days ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; static int N , P ; static bool helper ( int mid ) { int cnt = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { int temp = i . Value ; while ( temp >= mid ) { temp -= mid ; cnt ++ ; } } return cnt >= N ; } static int findMaximumDays ( int [ ] arr ) { for ( int i = 0 ; i < P ; i ++ ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 ; } else { mp . Add ( arr [ i ] , 1 ) ; } } int start = 0 , end = P , ans = 0 ; while ( start <= end ) { int mid = start + ( ( end - start ) / 2 ) ; if ( mid != 0 && helper ( mid ) ) { ans = mid ; start = mid + 1 ; } else if ( mid == 0 ) { start = mid + 1 ; } else { end = mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { N = 3 ; P = 10 ; int [ ] arr = { 1 , 2 , 2 , 1 , 1 , 3 , 3 , 3 , 2 , 4 } ; Console . Write ( findMaximumDays ( arr ) ) ; } }
Count subarrays having sum modulo K same as the length of the subarray | C # program for the above approach ; Function that counts the subarrays having sum modulo k equal to the length of subarray ; Stores the count of subarrays ; Stores prefix sum of the array ; Calculate prefix sum array ; Generate all the subarrays ; Check if this subarray is a valid subarray or not ; Total count of subarrays ; Driver Code ; Given arr [ ] ; Size of the array ; Given K ; Function call
using System ; using System . Collections . Generic ; class GFG { static void countSubarrays ( int [ ] a , int n , int k ) { int ans = 0 ; List < int > pref = new List < int > ( ) ; pref . Add ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) pref . Add ( ( a [ i ] + pref [ i ] ) % k ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { if ( ( pref [ j ] - pref [ i - 1 ] + k ) % k == j - i + 1 ) { ans ++ ; } } } Console . WriteLine ( ans ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 3 , 1 , 5 } ; int N = arr . Length ; int K = 4 ; countSubarrays ( arr , N , K ) ; } }
Count subarrays having sum modulo K same as the length of the subarray | C # program for the above approach ; Function that counts the subarrays having sum modulo k equal to the length of subarray ; Stores the count of ( pref [ i ] - i ) % k ; Stores the count of subarray ; Stores prefix sum of the array ; Find prefix sum array ; Base Condition ; Remove the index at present after K indices from the current index ; Update the answer for subarrays ending at the i - th index ; Add the calculated value of current index to count ; Print the count of subarrays ; Driver Code ; Given [ ] arr ; Size of the array ; Given K ; Function call
using System ; using System . Collections . Generic ; class GFG { static void countSubarrays ( int [ ] a , int n , int k ) { Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; long ans = 0 ; List < int > pref = new List < int > ( ) ; pref . Add ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) pref . Add ( ( a [ i ] + pref [ i ] ) % k ) ; cnt . Add ( 0 , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { int remIdx = i - k ; if ( remIdx >= 0 ) { if ( cnt . ContainsKey ( ( pref [ remIdx ] - remIdx % k + k ) % k ) ) cnt [ ( pref [ remIdx ] - remIdx % k + k ) % k ] = cnt [ ( pref [ remIdx ] - remIdx % k + k ) % k ] - 1 ; else cnt . Add ( ( pref [ remIdx ] - remIdx % k + k ) % k , - 1 ) ; } if ( cnt . ContainsKey ( ( pref [ i ] - i % k + k ) % k ) ) ans += cnt [ ( pref [ i ] - i % k + k ) % k ] ; if ( cnt . ContainsKey ( ( pref [ i ] - i % k + k ) % k ) ) cnt [ ( pref [ i ] - i % k + k ) % k ] = cnt [ ( pref [ i ] - i % k + k ) % k ] + 1 ; else cnt . Add ( ( pref [ i ] - i % k + k ) % k , 1 ) ; } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 3 , 1 , 5 } ; int N = arr . Length ; int K = 4 ; countSubarrays ( arr , N , K ) ; } }
Check if all substrings of length K of a Binary String has equal count of 0 s and 1 s | C # program for the above approach ; Function to check if the substring of length K has equal 0 and 1 ; Traverse the String ; Check if every K - th character is the same or not ; Traverse subString of length K ; If current character is 0 ; Increment count ; Otherwise ; Decrement count ; Check for equal 0 s and 1 s ; Driver code
using System ; class GFG { static bool check ( String s , int k ) { int n = s . Length ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i ; j < n ; j += k ) { if ( s [ i ] != s [ j ] ) return false ; } } int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( s [ i ] == '0' ) c ++ ; else c -- ; } if ( c == 0 ) return true ; else return false ; } public static void Main ( String [ ] args ) { String s = "101010" ; int k = 2 ; if ( check ( s , k ) ) Console . Write ( " Yes " + " STRNEWLINE " ) ; else Console . Write ( " No " + " STRNEWLINE " ) ; } }
Check if characters of a given string can be used to form any N equal strings | C # program for the above approach ; Function to check if the freq of any character is divisible by N ; Stores the frequency of characters ; If frequency of a character is not divisible by n ; If no character has frequency at least N ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool isSame ( String str , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( mp . ContainsKey ( str [ i ] - ' a ' ) ) { mp [ str [ i ] - ' a ' ] = mp [ str [ i ] - ' a ' ] + 1 ; } else { mp . Add ( str [ i ] - ' a ' , 1 ) ; } } foreach ( KeyValuePair < int , int > it in mp ) { if ( ( it . Value ) >= n ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { String str = " ccabcba " ; int n = 4 ; if ( isSame ( str , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }