codeparrot/xlcost-text-to-code huggingface dataset:
Maximum product of two non | C # program to find maximum product of two non - intersecting paths ; Returns maximum length path in subtree rooted at u after removing edge connecting u and v ; To find lengths of first and second maximum in subtrees . currMax is to store overall maximum . ; Loop through all neighbors of u ; If neighbor is v , then skip it ; Call recursively with current neighbor as root ; Get max from one side and update ; Store total length by adding max and second max ; Update current max by adding 1 , i . e . current node is included ; Method returns maximum product of length of two non - intersecting paths ; One by one removing all edges and calling dfs on both subtrees ; Calling dfs on subtree rooted at g [ i , j ] , excluding edge from g [ i , j ] to i . ; Calling dfs on subtree rooted at i , edge from i to g [ i , j ] ; Utility function to add an undirected edge ( u , v ) ; Driver code ; There are N edges , so + 1 for nodes and + 1 for 1 - based indexing
using System ; using System . Collections . Generic ; public class GFG { static int curMax ; static int dfs ( List < int > [ ] g , int u , int v ) { int max1 = 0 , max2 = 0 , total = 0 ; for ( int i = 0 ; i < g [ u ] . Count ; i ++ ) { if ( g [ u ] [ i ] == v ) continue ; total = Math . Max ( total , dfs ( g , g [ u ] [ i ] , u ) ) ; if ( curMax > max1 ) { max2 = max1 ; max1 = curMax ; } else max2 = Math . Max ( max2 , curMax ) ; } total = Math . Max ( total , max1 + max2 ) ; curMax = max1 + 1 ; return total ; } static int maxProductOfTwoPaths ( List < int > [ ] g , int N ) { int res = int . MinValue ; int path1 , path2 ; for ( int i = 1 ; i < N + 2 ; i ++ ) { for ( int j = 0 ; j < g [ i ] . Count ; j ++ ) { curMax = 0 ; path1 = dfs ( g , g [ i ] [ j ] , i ) ; curMax = 0 ; path2 = dfs ( g , i , g [ i ] [ j ] ) ; res = Math . Max ( res , path1 * path2 ) ; } } return res ; } static void addEdge ( List < int > [ ] g , int u , int v ) { g [ u ] . Add ( v ) ; g [ v ] . Add ( u ) ; } public static void Main ( String [ ] args ) { int [ , ] edges = { { 1 , 8 } , { 2 , 6 } , { 3 , 1 } , { 5 , 3 } , { 7 , 8 } , { 8 , 4 } , { 8 , 6 } } ; int N = edges . GetLength ( 0 ) ; List < int > [ ] g = new List < int > [ N + 2 ] ; for ( int i = 0 ; i < g . Length ; i ++ ) g [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) addEdge ( g , edges [ i , 0 ] , edges [ i , 1 ] ) ; Console . Write ( maxProductOfTwoPaths ( g , N ) + " STRNEWLINE " ) ; } }
Minimum steps to reach N from 1 by multiplying each step by 2 , 3 , 4 or 5 | C # implementation to find minimum number of steps to reach N from 1 ; Function to find a minimum number of steps to reach N from 1 ; Check until N is greater than 1 and operations can be applied ; Condition to choose the operations greedily ; Driver code
using System ; class GFG { static int Minsteps ( int n ) { int ans = 0 ; while ( n > 1 ) { if ( n % 5 == 0 ) { ans ++ ; n = n / 5 ; continue ; } else if ( n % 4 == 0 ) { ans ++ ; n = n / 4 ; continue ; } else if ( n % 3 == 0 ) { ans ++ ; n = n / 3 ; continue ; } else if ( n % 2 == 0 ) { ans ++ ; n = n / 2 ; continue ; } return - 1 ; } return ans ; } public static void Main ( ) { int n = 10 ; Console . Write ( Minsteps ( n ) ) ; } }
Find the maximum difference after applying the given operations two times on a number | C # program to find the maximum difference after two given operations on a number ; Function that returns the maximum difference after two given operations on a number ; Initialize the strings to make operations ; Store length of the string ; Make the maximum number using the first operation ; Find the first digit which is not equal to '9' ; Store the digit for the given operation ; Replace all the selected ' digit ' with '9' ; Break after the operation is completed ; Check if first digit is equal to '1' ; Find the first digit which is greater than '1' ; Store the digit for the given operation ; Replace all the selected ' digit ' with '0' ; Break after the operation is completed ; Select the first digit for the given operation ; Replace all the selected ' digit ' with '1' ; Return the difference after converting into integers ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int minDifference ( int num ) { char [ ] maximum = ( num . ToString ( ) ) . ToCharArray ( ) ; char [ ] minimum = ( num . ToString ( ) ) . ToCharArray ( ) ; int n = maximum . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( maximum [ i ] != '9' ) { char digit = maximum [ i ] ; for ( int j = i ; j < n ; j ++ ) { if ( maximum [ j ] == digit ) maximum [ j ] = '9' ; } break ; } } if ( minimum [ 0 ] == '1' ) { for ( int i = 1 ; i < n ; i ++ ) { if ( minimum [ i ] - '0' > 1 ) { char digit = minimum [ i ] ; for ( int j = i ; j < n ; j ++ ) { if ( digit == minimum [ j ] ) minimum [ j ] = '0' ; } break ; } } } else { char digit = minimum [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( minimum [ i ] == digit ) minimum [ i ] = '1' ; } } return Convert . ToInt32 ( string . Join ( " " , maximum ) ) - Convert . ToInt32 ( string . Join ( " " , minimum ) ) ; } static void Main ( ) { int N = 1101157 ; Console . Write ( minDifference ( N ) ) ; } }
Check if the square of a number is divisible by K or not | C # implementation to check if the square of X is divisible by K ; Function to return if square of X is divisible by K ; Finding gcd of x and k ; Dividing k by their gcd ; Check for divisibility of X by reduced K ; Driver Code
using System ; class GFG { static void checkDivisible ( int x , int k ) { int g = __gcd ( x , k ) ; k /= g ; if ( x % k == 0 ) { Console . Write ( " YES STRNEWLINE " ) ; } else { Console . Write ( " NO STRNEWLINE " ) ; } } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int x = 6 , k = 9 ; checkDivisible ( x , k ) ; } }
Find the minimum value of the given expression over all pairs of the array | C # program to find the minimum value of the given expression over all pairs of the array ; Function to find the minimum value of the expression ; The expression simplifies to finding the minimum xor value pair Sort given array ; Calculate min xor of consecutive pairs ; Driver Code
using System ; class GFG { static int MinimumValue ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int minXor = Int32 . MaxValue ; int val = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { val = arr [ i ] ^ arr [ i + 1 ] ; minXor = Math . Min ( minXor , val ) ; } return minXor ; } public static void Main ( ) { int N = 6 ; int [ ] A = new int [ ] { 12 , 3 , 14 , 5 , 9 , 8 } ; Console . Write ( MinimumValue ( A , N ) ) ; } }
Find length of longest substring with at most K normal characters | C # implementation to Find length of longest subString with at most K normal characters ; Function to find maximum length of normal subStrings ; keeps count of normal characters ; indexes of subString ; maintain length of longest subString with at most K normal characters ; get position of character ; check if current character is normal ; check if normal characters count exceeds K ; update answer with subString length ; get position of character ; check if character is normal then decrement count ; Driver code ; initialise the String
using System ; public class GFG { static int maxNormalSubString ( char [ ] P , char [ ] Q , int K , int N ) { if ( K == 0 ) return 0 ; int count = 0 ; int left = 0 , right = 0 ; int ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { int pos = P [ right ] - ' a ' ; if ( Q [ pos ] == '0' ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = Math . Max ( ans , right - left ) ; } while ( left < right ) { int pos = P [ left ] - ' a ' ; left ++ ; if ( Q [ pos ] == '0' ) count -- ; if ( count < K ) break ; } } return ans ; } public static void Main ( String [ ] args ) { String P = " giraffe " , Q = "01111001111111111011111111" ; int K = 2 ; int N = P . Length ; Console . Write ( maxNormalSubString ( P . ToCharArray ( ) , Q . ToCharArray ( ) , K , N ) ) ; } }
Make all the elements of array even with given operations | C # program to make all array element even ; Function to count the total number of operations needed to make all array element even ; Traverse the given array ; If an odd element occurs then increment that element and next adjacent element by 1 ; Traverse the array if any odd element occurs then return - 1 ; Returns the count of operations ; Driver code
using System ; class GFG { static int countOperations ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { arr [ i ] ++ ; arr [ i + 1 ] ++ ; count += 2 ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) return - 1 ; } return count ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; Console . Write ( countOperations ( arr , n ) ) ; } }
Count of pairs with difference at most K with no element repeating | C # implementation to count the number of pairs whose difference is atmost K in an array ; Function to count the number of pairs whose difference is atmost K in an array ; Sorting the Array ; Variable to store the count of pairs whose difference is atmost K ; Loop to consider the consecutive pairs of the array ; if Pair found increment the index by 2 ; Driver Code ; Function Call
using System ; class GFG { static int countPairs ( int [ ] arr , int k ) { Array . Sort ( arr ) ; int pair = 0 ; int index = 0 ; while ( index < arr . Length - 1 ) { if ( arr [ index + 1 ] - arr [ index ] <= k ) { pair += 1 ; index += 2 ; } else { index += 1 ; } } return pair ; } public static void Main ( ) { int [ ] arr = { 1 , 4 , 3 , 7 , 5 } ; int k = 2 ; int count = countPairs ( arr , k ) ; Console . WriteLine ( count ) ; } }
Maximum profit by selling N items at two markets | C # implementation of the approach ; Function to calculate max profit ; Prefix sum array for profitA [ ] ; Suffix sum array for profitB [ ] ; If all the items are sold in market A ; Find the maximum profit when the first i items are sold in market A and the rest of the items are sold in market B for all possible values of i ; If all the items are sold in market B ; Driver code ; Function to calculate max profit
using System ; class GFG { static int maxProfit ( int [ ] profitA , int [ ] profitB , int n ) { int [ ] preSum = new int [ n ] ; preSum [ 0 ] = profitA [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { preSum [ i ] = preSum [ i - 1 ] + profitA [ i ] ; } int [ ] suffSum = new int [ n ] ; suffSum [ n - 1 ] = profitB [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffSum [ i ] = suffSum [ i + 1 ] + profitB [ i ] ; } int res = preSum [ n - 1 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { res = Math . Max ( res , preSum [ i ] + suffSum [ i + 1 ] ) ; } res = Math . Max ( res , suffSum [ 0 ] ) ; return res ; } public static void Main ( String [ ] args ) { int [ ] profitA = { 2 , 3 , 2 } ; int [ ] profitB = { 10 , 30 , 40 } ; int n = profitA . Length ; Console . WriteLine ( maxProfit ( profitA , profitB , n ) ) ; } }
Minimum number of edges that need to be added to form a triangle | C # implementation of the approach ; Pair class ; Function to return the minimum number of edges that need to be added to the given graph such that it contains at least one triangle ; adj is the adjacency matrix such that adj [ i , j ] = 1 when there is an edge between i and j ; As the graph is undirected so there will be an edge between ( i , j ) and ( j , i ) ; To store the required count of edges ; For every possible vertex triplet ; If the vertices form a triangle ; If no edges are present ; If only 1 edge is required ; Two edges are required ; Driver Code ; Number of nodes ; Storing the edges in a vector of pairs
using System ; using System . Collections . Generic ; class Pair { public int first ; public int second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } class GFG { static int minEdges ( List < Pair > v , int n ) { int [ , ] adj = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < v . Count ; i ++ ) { adj [ v [ i ] . first , v [ i ] . second ] = 1 ; adj [ v [ i ] . second , v [ i ] . first ] = 1 ; } int edgesNeeded = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { for ( int k = j + 1 ; k <= n ; k ++ ) { if ( adj [ i , j ] == 1 && adj [ j , k ] == 1 && adj [ k , i ] == 1 ) return 0 ; if ( ! ( adj [ i , j ] == 1 adj [ j , k ] == 1 adj [ k , i ] == 1 ) ) edgesNeeded = Math . Min ( edgesNeeded , 3 ) ; else { if ( ( adj [ i , j ] == 1 && adj [ j , k ] == 1 ) || ( adj [ j , k ] == 1 && adj [ k , i ] == 1 ) || ( adj [ k , i ] == 1 && adj [ i , j ] == 1 ) ) { edgesNeeded = 1 ; } else edgesNeeded = Math . Min ( edgesNeeded , 2 ) ; } } } } return edgesNeeded ; } public static void Main ( String [ ] args ) { int n = 3 ; List < Pair > v = new List < Pair > ( ) ; v . Add ( new Pair ( 1 , 2 ) ) ; v . Add ( new Pair ( 1 , 3 ) ) ; Console . WriteLine ( minEdges ( v , n ) ) ; } }
Minimum operations required to make every element greater than or equal to K | C # implementation of above approach ; Function to calculate gcd of two numbers ; C # function to get minimum operation needed ; The priority queue holds a minimum element in the top position ; push value one by one from the given array ; store count of minimum operation needed ; All elements are now >= k ; It is impossible to make as there are no sufficient elements available ; Take two smallest elements and replace them by their LCM first smallest element ; Second smallest element ; Increment the count ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int FindMinOperation ( int [ ] a , int n , int k ) { List < int > Q = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) Q . Add ( a [ i ] ) ; Q . Sort ( ) ; int ans = 0 ; while ( true ) { if ( Q [ 0 ] >= k ) break ; if ( Q . Count < 2 ) return - 1 ; int x = Q [ 0 ] ; Q . RemoveAt ( 0 ) ; int y = Q [ 0 ] ; Q . RemoveAt ( 0 ) ; int z = ( x * y ) / gcd ( x , y ) ; Q . Add ( z ) ; Q . Sort ( ) ; ans ++ ; } return ans ; } static void Main ( ) { int [ ] a = { 3 , 5 , 7 , 6 , 8 } ; int k = 8 ; int n = a . Length ; Console . WriteLine ( FindMinOperation ( a , n , k ) ) ; } }
Check if it is possible to make two matrices strictly increasing by swapping corresponding values only | C # implementation of the above approach ; Function to check whether the matrices can be made strictly increasing with the given operation ; Swap only when a [ i ] [ j ] > b [ i ] [ j ] ; Check if rows are strictly increasing ; Check if columns are strictly increasing ; Driver Code
using System ; using System . Collections ; class GfG { static string Check ( int [ , ] a , int [ , ] b , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i , j ] > b [ i , j ] ) { int tmp = a [ i , j ] ; a [ i , j ] = b [ i , j ] ; b [ i , j ] = tmp ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m - 1 ; j ++ ) if ( a [ i , j ] >= a [ i , j + 1 ] b [ i , j ] >= b [ i , j + 1 ] ) return " No " ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i , j ] >= a [ i + 1 , j ] b [ i , j ] >= b [ i + 1 , j ] ) return " No " ; return " Yes " ; } public static void Main ( string [ ] arg ) { int n = 2 , m = 2 ; int [ , ] a = { { 2 , 10 } , { 11 , 5 } } ; int [ , ] b = { { 9 , 4 } , { 3 , 12 } } ; Console . Write ( Check ( a , b , n , m ) ) ; } }
Find the lexicographically smallest string which satisfies the given condition | C # implementation of the approach ; Function to return the required string ; First character will always be ' a ' ; To store the resultant string ; Since length of the string should be greater than 0 and first element of array should be 1 ; Check one by one all element of given prefix array ; If the difference between any two consecutive elements of the prefix array is greater than 1 then there will be no such string possible that satisfies the given array Also , string cannot have more than 26 distinct characters ; If difference is 0 then the ( i + 1 ) th character will be same as the ith character ; If difference is 1 then the ( i + 1 ) th character will be different from the ith character ; Return the resultant string ; Driver code
using System ; class GFG { static string smallestString ( int N , int [ ] A ) { char ch = ' a ' ; string S = " " ; if ( N < 1 A [ 0 ] != 1 ) { S = " - 1" ; return S ; } S += ch ; ch ++ ; for ( int i = 1 ; i < N ; i ++ ) { int diff = A [ i ] - A [ i - 1 ] ; if ( diff > 1 diff < 0 A [ i ] > 26 ) { S = " - 1" ; return S ; } else if ( diff == 0 ) S += ' a ' ; else { S += ch ; ch ++ ; } } return S ; } static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 3 , 3 } ; int n = arr . Length ; Console . WriteLine ( smallestString ( n , arr ) ) ; } }
Count of alphabets whose ASCII values can be formed with the digits of N | C # implementation of the approach ; Function that returns true if num can be formed with the digits in digits [ ] array ; Copy of the digits array ; Get last digit ; If digit array doesn 't  contain current digit ; One occurrence is used ; Remove the last digit ; Function to return the count of required alphabets ; To store the occurrences of digits ( 0 - 9 ) ; Get last digit ; Update the occurrence of the digit ; Remove the last digit ; If any lowercase character can be picked from the current digits ; If any uppercase character can be picked from the current digits ; Return the required count of alphabets ; Driver code
using System ; class GFG { static bool canBePicked ( int [ ] digits , int num ) { int [ ] copyDigits = ( int [ ] ) digits . Clone ( ) ; while ( num > 0 ) { int digit = num % 10 ; if ( copyDigits [ digit ] == 0 ) return false ; else copyDigits [ digit ] -- ; num /= 10 ; } return true ; } static int countAlphabets ( int n ) { int i , count = 0 ; int [ ] digits = new int [ 10 ] ; while ( n > 0 ) { int digit = n % 10 ; digits [ digit ] ++ ; n /= 10 ; } for ( i = ' a ' ; i <= ' z ' ; i ++ ) if ( canBePicked ( digits , i ) ) count ++ ; for ( i = ' A ' ; i <= ' Z ' ; i ++ ) if ( canBePicked ( digits , i ) ) count ++ ; return count ; } public static void Main ( ) { int n = 1623455078 ; Console . WriteLine ( countAlphabets ( n ) ) ; } }
Largest number less than X having at most K set bits | C # implementation of the above approach ; Function to get no of set bits in binary representation of positive integer n ; Function to return the greatest number <= X having at most K set bits . ; Remove rightmost set bits one by one until we count becomes k ; Return the required number ; Driver code
using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int greatestKBits ( int X , int K ) { int set_bit_count = countSetBits ( X ) ; if ( set_bit_count <= K ) return X ; int diff = set_bit_count - K ; for ( int i = 0 ; i < diff ; i ++ ) X &= ( X - 1 ) ; return X ; } public static void Main ( ) { int X = 21 , K = 2 ; Console . WriteLine ( greatestKBits ( X , K ) ) ; } }
Find two numbers whose sum and GCD are given | C # program to find two numbers whose sum and GCD is given ; function to find gcd of two numbers ; Function to find two numbers whose sum and gcd is given ; sum != gcd checks that both the numbers are positive or not ; Driver code
using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static void findTwoNumbers ( int sum , int gcd ) { if ( __gcd ( gcd , sum - gcd ) == gcd && sum != gcd ) Console . WriteLine ( " a ▁ = ▁ " + Math . Min ( gcd , sum - gcd ) + " , ▁ b ▁ = ▁ " + ( int ) ( sum - Math . Min ( gcd , sum - gcd ) ) ) ; else Console . WriteLine ( - 1 ) ; } public static void Main ( ) { int sum = 8 ; int gcd = 2 ; findTwoNumbers ( sum , gcd ) ; } }
Find maximum distance between any city and station | C # program to calculate the maximum distance between any city and its nearest station ; Function to calculate the maximum distance between any city and its nearest station ; Initialize boolean list ; Assign True to cities containing station ; Driver code
using System ; class GFG { static int findMaxDistance ( int numOfCities , int [ ] station , int n ) { bool [ ] hasStation = new bool [ numOfCities + 1 ] ; for ( int city = 0 ; city < n ; city ++ ) { hasStation [ station [ city ] ] = true ; } int dist = 0 ; int maxDist = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { maxDist = Math . Min ( station [ i ] , maxDist ) ; } for ( int city = 0 ; city < numOfCities ; city ++ ) { if ( hasStation [ city ] == true ) { maxDist = Math . Max ( ( dist + 1 ) / 2 , maxDist ) ; dist = 0 ; } else dist += 1 ; } return Math . Max ( maxDist , dist ) ; } public static void Main ( String [ ] args ) { int numOfCities = 6 ; int [ ] station = { 3 , 1 } ; int n = station . Length ; Console . WriteLine ( " Max ▁ Distance : " + findMaxDistance ( numOfCities , station , n ) ) ; } }
Split the number into N parts such that difference between the smallest and the largest part is minimum | C # implementation of the approach ; Function that prints the required sequence ; If we cannot split the number into exactly ' N ' parts ; If x % n == 0 then the minimum difference is 0 and all numbers are x / n ; upto n - ( x % n ) the values will be x / n after that the values will be x / n + 1 ; Driver code
using System ; public class GFG { static void split ( int x , int n ) { if ( x < n ) Console . WriteLine ( " - 1 ▁ " ) ; else if ( x % n == 0 ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( ( x / n ) + " ▁ " ) ; } else { int zp = n - ( x % n ) ; int pp = x / n ; for ( int i = 0 ; i < n ; i ++ ) { if ( i >= zp ) Console . Write ( ( pp + 1 ) + " ▁ " ) ; else Console . Write ( pp + " ▁ " ) ; } } } static public void Main ( ) { int x = 5 ; int n = 3 ; split ( x , n ) ; } }
Minimum time to reach a point with + t and | C # implementation of the above approach ; returns the minimum time required to reach ' X ' ; Stores the minimum time ; increment ' t ' by 1 ; update the sum ; Driver code
using System ; public class GFG { static long cal_minimum_time ( long X ) { long t = 0 ; long sum = 0 ; while ( sum < X ) { t ++ ; sum = sum + t ; } return t ; } static public void Main ( ) { long n = 6 ; long ans = cal_minimum_time ( n ) ; Console . WriteLine ( " The ▁ minimum ▁ time ▁ required ▁ is ▁ : ▁ " + ans ) ; } }
Check if a string can be rearranged to form special palindrome | C # implementation of the above approach ; Driver code ; creating a array which stores the frequency of each character ; Checking if a character is uppercase or not ; Increasing by 1 if uppercase ; Decreasing by 1 if lower case ; Storing the sum of positive numbers in the frequency array ; Storing the sum of negative numbers in the frequency array ; If all character balances out then its Yes ; If there is only 1 character which does not balances then also it is Yes
using System ; class GFG { public static void Main ( ) { string s = " ABCdcba " ; int [ ] u = new int [ 26 ] ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( Char . IsUpper ( s [ i ] ) ) { u [ s [ i ] - 65 ] += 1 ; } else { u [ s [ i ] - 97 ] -= 1 ; } } int po = 0 ; int ne = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( u [ i ] > 0 ) po += u [ i ] ; if ( u [ i ] < 0 ) ne += u [ i ] ; } if ( po == 0 && ne == 0 ) Console . Write ( " YES " + " STRNEWLINE " ) ; else if ( po == 1 && ne == 0 ) Console . Write ( " YES " + " STRNEWLINE " ) ; else if ( po == 0 && ne == - 1 ) Console . Write ( " YES " + " STRNEWLINE " ) ; else Console . Write ( " NO " + " STRNEWLINE " ) ; } }