codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of reversals to reach node 0 from every other node | C # program for the above approach ; Function to find minimum reversals ; Add all adjacent nodes to the node in the graph ; Insert edges in the graph ; Insert edges in the reversed graph ; Create array visited to mark all the visited nodes ; Stores the number of edges to be reversed ; BFS Traversal ; Pop the current node from the queue ; mark the current node visited ; Intitialize count of edges need to be reversed to 0 ; Enqueue adjacent nodes in the reversed graph to the queue , if not visited ; Enqueue adjacent nodes in graph to the queue , if not visited count the number of nodes added to the queue ; Update the reverse edge to the final count ; Return the result ; Driver Code ; Given edges to the graph ; Number of nodes ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int minRev ( ArrayList edges , int n ) { Dictionary < int , ArrayList > graph = new Dictionary < int , ArrayList > ( ) ; Dictionary < int , ArrayList > graph_rev = new Dictionary < int , ArrayList > ( ) ; for ( int i = 0 ; i < edges . Count ; i ++ ) { int x = ( int ) ( ( ArrayList ) edges [ i ] ) [ 0 ] ; int y = ( int ) ( ( ArrayList ) edges [ i ] ) [ 1 ] ; if ( ! graph . ContainsKey ( x ) ) { graph [ x ] = new ArrayList ( ) ; } graph [ x ] . Add ( y ) ; if ( ! graph_rev . ContainsKey ( y ) ) { graph_rev [ y ] = new ArrayList ( ) ; } graph_rev [ y ] . Add ( x ) ; } Queue q = new Queue ( ) ; ArrayList visited = new ArrayList ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { visited . Add ( false ) ; } q . Enqueue ( 0 ) ; int ans = 0 ; while ( q . Count != 0 ) { int curr = ( int ) q . Peek ( ) ; visited [ curr ] = true ; int count = 0 ; q . Dequeue ( ) ; if ( graph_rev . ContainsKey ( curr ) ) { for ( int i = 0 ; i < graph_rev [ curr ] . Count ; i ++ ) { if ( ! ( bool ) visited [ ( int ) ( ( ArrayList ) graph_rev [ curr ] ) [ i ] ] ) { q . Enqueue ( ( int ) ( ( ArrayList ) graph_rev [ curr ] ) [ i ] ) ; } } } if ( graph . ContainsKey ( curr ) ) { for ( int i = 0 ; i < ( ( ArrayList ) graph [ curr ] ) . Count ; i ++ ) { if ( ! ( bool ) visited [ ( int ) ( ( ArrayList ) graph [ curr ] ) [ i ] ] ) { q . Enqueue ( ( int ) ( ( ArrayList ) graph [ curr ] ) [ i ] ) ; count ++ ; } } } ans += count ; } return ans ; } public static void Main ( string [ ] args ) { ArrayList edges = new ArrayList ( ) { new ArrayList ( ) { 0 , 1 } , new ArrayList ( ) { 1 , 3 } , new ArrayList ( ) { 2 , 3 } , new ArrayList ( ) { 4 , 0 } , new ArrayList ( ) { 4 , 5 } } ; int n = 6 ; Console . Write ( minRev ( edges , n ) ) ; } }
Partition a set into two subsets such that difference between max of one and min of other is minimized | C # program for the above approach ; Function to split the array ; Sort the array in increasing order ; Calculating the max difference between consecutive elements ; Return the final minimum difference ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
using System ; class GFG { static int splitArray ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int result = Int32 . MaxValue ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . Min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . Length ; Console . Write ( splitArray ( arr , N ) ) ; } }
Find the element at R ' th ▁ row ▁ and ▁ C ' th column in given a 2D pattern | C # implementation to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; Function to compute the R ' th ▁ row ▁ and ▁ C ' th column of the given pattern ; First element of a given row ; Element in the given column ; Driver code ; Function call
using System ; class GFG { static int findValue ( int R , int C ) { int k = ( R * ( R - 1 ) ) / 2 + 1 ; int diff = R + 1 ; for ( int i = 1 ; i < C ; i ++ ) { k = ( k + diff ) ; diff ++ ; } return k ; } public static void Main ( ) { int R = 4 ; int C = 4 ; int k = findValue ( R , C ) ; Console . Write ( k ) ; } }
Maximize number of groups formed with size not smaller than its largest element | C # implementation of above approach ; Function that prints the number of maximum groups ; Store the number of occurrence of elements ; Make all groups of similar elements and store the left numbers ; Condition for finding first leftover element ; Condition for current leftover element ; Condition if group size is equal to or more than current element ; Printing maximum number of groups ; Driver Code
using System ; class GFG { static void makeGroups ( int [ ] a , int n ) { int [ ] v = new int [ n + 1 ] ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { v [ a [ i ] ] ++ ; } int no_of_groups = 0 ; for ( i = 1 ; i <= n ; i ++ ) { no_of_groups += v [ i ] / i ; v [ i ] = v [ i ] % i ; } i = 1 ; int total = 0 ; for ( i = 1 ; i <= n ; i ++ ) { if ( v [ i ] != 0 ) { total = v [ i ] ; break ; } } i ++ ; while ( i <= n ) { if ( v [ i ] != 0 ) { total += v [ i ] ; if ( total >= i ) { int rem = total - i ; no_of_groups ++ ; total = rem ; } } i ++ ; } Console . Write ( no_of_groups + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 1 , 2 , 2 } ; int size = arr . Length ; makeGroups ( arr , size ) ; } }
Leftmost Column with atleast one 1 in a row | C # program to calculate leftmost column having at least 1 ; Function return leftmost column having at least a 1 ; If current element is 1 decrement column by 1 ; If current element is 0 increment row by 1 ; Driver code
using System ; class GFG { static readonly int N = 3 ; static readonly int M = 4 ; static int FindColumn ( int [ , ] mat ) { int row = 0 , col = M - 1 ; int flag = 0 ; while ( row < N && col >= 0 ) { if ( mat [ row , col ] == 1 ) { col -- ; flag = 1 ; } else { row ++ ; } } col ++ ; if ( flag != 0 ) return col + 1 ; else return - 1 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 0 , 0 , 1 , 1 } } ; Console . Write ( FindColumn ( mat ) ) ; } }
Find two numbers made up of a given digit such that their difference is divisible by N | C # implementation of the above approach ; Function to implement the above approach ; To store remainder - length of the number as key - value pairs ; Iterate till N + 1 length ; Search remainder in the map ; If remainder is not already present insert the length for the corresponding remainder ; Keep increasing M ; To keep M in range of integer ; Length of one number is the current Length ; Length of the other number is the length paired with current remainder in map ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findNumbers ( int N , int M ) { int m = M ; Dictionary < int , int > remLen = new Dictionary < int , int > ( ) ; int len , remainder = 0 ; for ( len = 1 ; len <= N + 1 ; ++ len ) { remainder = M % N ; if ( ! remLen . ContainsKey ( remainder ) ) { remLen . Add ( remainder , len ) ; } else { break ; } M = M * 10 + m ; M = M % N ; } int LenA = len ; int LenB = remLen [ remainder ] ; for ( int i = 0 ; i < LenB ; ++ i ) Console . Write ( m ) ; Console . Write ( " ▁ " ) ; for ( int i = 0 ; i < LenA ; ++ i ) Console . Write ( m ) ; } public static void Main ( String [ ] args ) { int N = 8 , M = 2 ; findNumbers ( N , M ) ; } }
Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements | C # implementation to count the number of ways to select equal sized subarrays such that they have atleast K common elements ; 2D prefix sum for submatrix sum query for matrix ; Function to find the prefix sum of the matrix from i and j ; Function to count the number of ways to select equal sized subarrays such that they have atleast K common elements ; Combining the two arrays ; Calculating the 2D prefix sum ; Iterating through all the elements of matrix and considering them to be the bottom right ; Applying binary search over side length ; If sum of this submatrix >= k then new search space will be [ low , mid ] ; Else new search space will be [ mid + 1 , high ] ; Adding the total submatrices ; Driver Code
using System ; class GFG { static int [ , ] prefix_2D = new int [ 2005 , 2005 ] ; static int subMatrixSum ( int i , int j , int len ) { return prefix_2D [ i , j ] - prefix_2D [ i , j - len ] - prefix_2D [ i - len , j ] + prefix_2D [ i - len , j - len ] ; } static int numberOfWays ( int [ ] a , int [ ] b , int n , int m , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( a [ i - 1 ] == b [ j - 1 ] ) prefix_2D [ i , j ] = 1 ; else prefix_2D [ i , j ] = 0 ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { prefix_2D [ i , j ] += prefix_2D [ i , j - 1 ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { prefix_2D [ i , j ] += prefix_2D [ i - 1 , j ] ; } } int answer = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { int low = 1 ; int high = Math . Min ( i , j ) ; while ( low < high ) { int mid = ( low + high ) >> 1 ; if ( subMatrixSum ( i , j , mid ) >= k ) { high = mid ; } else { low = mid + 1 ; } } if ( subMatrixSum ( i , j , low ) >= k ) { answer += ( Math . Min ( i , j ) - low + 1 ) ; } } } return answer ; } public static void Main ( String [ ] args ) { int N = 2 , M = 3 ; int [ ] A = { 1 , 2 } ; int [ ] B = { 1 , 2 , 3 } ; int K = 1 ; Console . Write ( numberOfWays ( A , B , N , M , K ) ) ; } }
Find lexicographically smallest string in at most one swaps | C # implementation of the above approach ; Function to return the lexicographically smallest String that can be formed by swapping at most one character . The characters might not necessarily be adjacent . ; Store last occurrence of every character ; Set - 1 as default for every character . ; char index to fill in the last occurrence array ; If this is true then this character is being visited for the first time from the last Thus last occurrence of this character is stored in this index ; char to replace ; Find the last occurrence of this character . ; Swap this with the last occurrence ; Driver code
using System ; class GFG { static String findSmallest ( char [ ] s ) { int len = s . Length ; int [ ] loccur = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) loccur [ i ] = - 1 ; for ( int i = len - 1 ; i >= 0 ; -- i ) { int chI = s [ i ] - ' a ' ; if ( loccur [ chI ] == - 1 ) { loccur [ chI ] = i ; } } char [ ] sorted_s = s ; Array . Sort ( sorted_s ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { int chI = sorted_s [ i ] - ' a ' ; int last_occ = loccur [ chI ] ; char temp = s [ last_occ ] ; s [ last_occ ] = s [ i ] ; s [ i ] = temp ; break ; } } return String . Join ( " " , s ) ; } public static void Main ( String [ ] args ) { String s = " geeks " ; Console . Write ( findSmallest ( s . ToCharArray ( ) ) ) ; } }
Maximum size of square such that all submatrices of that size have sum less than K | C # implementation to find the maximum size square submatrix such that their sum is less than K ; Size of matrix ; Function to preprocess the matrix for computing the sum of every possible matrix of the given size ; Loop to copy the first row of the matrix into the aux matrix ; Computing the sum column - wise ; Computing row wise sum ; Function to find the sum of a submatrix with the given indices ; Overall sum from the top to right corner of matrix ; Removing the sum from the top corer of the matrix ; Remove the overlapping sum ; Add the sum of top corner which is subtracted twice ; Function to check whether square sub matrices of size mid satisfy the condition or not ; Iterating throught all possible submatrices of given size ; Function to find the maximum square size possible with the such that every submatrix have sum less than the given sum ; Search space ; Binary search for size ; Check if the mid satisfies the given condition ; Driver Code
using System ; class GFG { static readonly int N = 4 ; static readonly int M = 5 ; static void preProcess ( int [ , ] mat , int [ , ] aux ) { for ( int i = 0 ; i < M ; i ++ ) aux [ 0 , i ] = mat [ 0 , i ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) aux [ i , j ] = mat [ i , j ] + aux [ i - 1 , j ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 1 ; j < M ; j ++ ) aux [ i , j ] += aux [ i , j - 1 ] ; } static int sumQuery ( int [ , ] aux , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi , rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 , rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi , tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 , tlj - 1 ] ; return res ; } static bool check ( int mid , int [ , ] aux , int K ) { bool satisfies = true ; for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < M ; y ++ ) { if ( x + mid - 1 <= N - 1 && y + mid - 1 <= M - 1 ) { if ( sumQuery ( aux , x , y , x + mid - 1 , y + mid - 1 ) > K ) satisfies = false ; } } } return ( satisfies == true ) ; } static int maximumSquareSize ( int [ , ] mat , int K ) { int [ , ] aux = new int [ N , M ] ; preProcess ( mat , aux ) ; int low = 1 , high = Math . Min ( N , M ) ; int mid ; while ( high - low > 1 ) { mid = ( low + high ) / 2 ; if ( check ( mid , aux , K ) ) { low = mid ; } else high = mid ; } if ( check ( high , aux , K ) ) return high ; return low ; } public static void Main ( String [ ] args ) { int K = 30 ; int [ , ] mat = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; Console . Write ( maximumSquareSize ( mat , K ) ) ; } }
Count of numbers from range [ L , R ] that end with any of the given digits | C # implementation of the approach ; Function to return the count of the required numbers ; Last digit of the current number ; If the last digit is equal to any of the given digits ; Driver code
using System ; class GFG { static int countNums ( int l , int r ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { int lastDigit = ( i % 10 ) ; if ( ( lastDigit % 10 ) == 2 || ( lastDigit % 10 ) == 3 || ( lastDigit % 10 ) == 9 ) { cnt ++ ; } } return cnt ; } public static void Main ( ) { int l = 11 , r = 33 ; Console . Write ( countNums ( l , r ) ) ; } }
Minimum K such that sum of array elements after division by K does not exceed S | C # implementation of the approach ; Function to return the minimum value of k that satisfies the given condition ; Find the maximum element ; Lowest answer can be 1 and the highest answer can be ( maximum + 1 ) ; Binary search ; Get the mid element ; Calculate the sum after dividing the array by new K which is mid ; Search in the second half ; First half ; Driver code
using System ; class GFG { static int findMinimumK ( int [ ] a , int n , int s ) { int maximum = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { maximum = Math . Max ( maximum , a [ i ] ) ; } int low = 1 , high = maximum + 1 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) ( a [ i ] / mid ) ; } if ( sum > s ) low = mid + 1 ; else { ans = Math . Min ( ans , mid ) ; high = mid - 1 ; } } return ans ; } public static void Main ( ) { int [ ] a = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = a . Length ; int s = 27 ; Console . WriteLine ( findMinimumK ( a , n , s ) ) ; } }
Calculate the Sum of GCD over all subarrays | C # program to find Sum of GCD over all subarrays ; int a [ 100001 ] ; ; Build Sparse Table ; Building the Sparse Table for GCD [ L , R ] Queries ; Utility Function to calculate GCD in range [ L , R ] ; Calculating where the answer is stored in our Sparse Table ; Utility Function to find next - farther position where gcd is same ; BinarySearch for Next Position for EndPointer ; Utility function to calculate sum of gcd ; Initializing all the values ; Finding the next position for endPointer ; Adding the suitable sum to our answer ; Changing prevEndPointer ; Recalculating tempGCD ; Driver code
using System ; class GFG { static int [ , ] SparseTable = new int [ 100001 , 51 ] ; static void buildSparseTable ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { SparseTable [ i , 0 ] = a [ i ] ; } for ( int j = 1 ; j <= 19 ; j ++ ) { for ( int i = 0 ; i <= n - ( 1 << j ) ; i ++ ) { SparseTable [ i , j ] = __gcd ( SparseTable [ i , j - 1 ] , SparseTable [ i + ( 1 << ( j - 1 ) ) , j - 1 ] ) ; } } } static int queryForGCD ( int L , int R ) { int returnValue ; int j = ( int ) ( Math . Log ( R - L + 1 ) ) ; returnValue = __gcd ( SparseTable [ L , j ] , SparseTable [ R - ( 1 << j ) + 1 , j ] ) ; return returnValue ; } static int nextPosition ( int tempGCD , int startPointer , int prevEndPointer , int n ) { int high = n - 1 ; int low = prevEndPointer ; int mid = prevEndPointer ; int nextPos = prevEndPointer ; while ( high >= low ) { mid = ( ( high + low ) >> 1 ) ; if ( queryForGCD ( startPointer , mid ) == tempGCD ) { nextPos = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return nextPos + 1 ; } static int calculateSum ( int [ ] a , int n ) { buildSparseTable ( a , n ) ; int endPointer , startPointer , prevEndPointer , tempGCD ; int tempAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { endPointer = i ; startPointer = i ; prevEndPointer = i ; tempGCD = a [ i ] ; while ( endPointer < n ) { endPointer = nextPosition ( tempGCD , startPointer , prevEndPointer , n ) ; tempAns += ( ( endPointer - prevEndPointer ) * tempGCD ) ; prevEndPointer = endPointer ; if ( endPointer < n ) { tempGCD = __gcd ( tempGCD , a [ endPointer ] ) ; } } } return tempAns ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int n = 6 ; int [ ] a = { 2 , 2 , 2 , 3 , 5 , 5 } ; Console . WriteLine ( calculateSum ( a , n ) ) ; } }
QuickSelect ( A Simple Iterative Implementation ) | C # program for iterative implementation of QuickSelect ; Standard Lomuto partition function ; Implementation of QuickSelect ; Partition a [ left . . right ] around a pivot and find the position of the pivot ; If pivot itself is the k - th smallest element ; If there are more than k - 1 elements on left of pivot , then k - th smallest must be on left side . ; Else k - th smallest is on right side . ; Driver Code
using System ; class GFG { static int partition ( int [ ] arr , int low , int high ) { int temp ; int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return ( i + 1 ) ; } static int kthSmallest ( int [ ] a , int left , int right , int k ) { while ( left <= right ) { int pivotIndex = partition ( a , left , right ) ; if ( pivotIndex == k - 1 ) return a [ pivotIndex ] ; else if ( pivotIndex > k - 1 ) right = pivotIndex - 1 ; else left = pivotIndex + 1 ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 4 , 5 , 8 , 11 , 6 , 26 } ; int n = arr . Length ; int k = 5 ; Console . WriteLine ( " K - th ▁ smallest ▁ element ▁ is ▁ " + kthSmallest ( arr , 0 , n - 1 , k ) ) ; } }
Pair with largest sum which is less than K in the array | C # program to find pair with largest sum which is less than K in the array ; Function to find pair with largest sum which is less than K in the array ; To store the break point ; Sort the given array ; Find the break point ; No need to look beyond i 'th index ; Find the required pair ; Print the required answer ; Driver code ; Function call
using System ; class GFG { static void Max_Sum ( int [ ] arr , int n , int k ) { int p = n ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= k ) { p = i ; break ; } } int maxsum = 0 , a = 0 , b = 0 ; for ( int i = 0 ; i < p ; i ++ ) { for ( int j = i + 1 ; j < p ; j ++ ) { if ( arr [ i ] + arr [ j ] < k && arr [ i ] + arr [ j ] > maxsum ) { maxsum = arr [ i ] + arr [ j ] ; a = arr [ i ] ; b = arr [ j ] ; } } } Console . WriteLine ( a + " ▁ " + b ) ; } public static void Main ( ) { int [ ] arr = { 5 , 20 , 110 , 100 , 10 } ; int k = 85 ; int n = arr . Length ; Max_Sum ( arr , n , k ) ; } }
Maximum length palindromic substring such that it starts and ends with given char | C # implementation of the approach ; Function that returns true if str [ i ... j ] is a palindrome ; Function to return the length of the longest palindromic sub - string such that it starts and ends with the character ch ; If current character is a valid starting index ; Instead of finding the ending index from the beginning , find the index from the end This is because if the current sub - string is a palindrome then there is no need to check the sub - strings of smaller length and we can skip to the next iteration of the outer loop ; If current character is a valid ending index ; If str [ i ... j ] is a palindrome then update the length of the maximum palindrome so far ; Driver code
using System ; class GFG { static bool isPalindrome ( string str , int i , int j ) { while ( i < j ) { if ( str [ i ] != str [ j ] ) { return false ; } i ++ ; j -- ; } return true ; } static int maxLenPalindrome ( string str , int n , char ch ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ch ) { for ( int j = n - 1 ; j >= i ; j -- ) { if ( str [ j ] == ch ) { if ( isPalindrome ( str , i , j ) ) { maxLen = Math . Max ( maxLen , j - i + 1 ) ; break ; } } } } } return maxLen ; } public static void Main ( ) { string str = " lapqooqpqpl " ; int n = str . Length ; char ch = ' p ' ; Console . WriteLine ( maxLenPalindrome ( str , n , ch ) ) ; } }
Longest sub | C # implementation of the approach ; Function to find the starting and the ending index of the sub - array with equal number of alphabets and numeric digits ; If its an alphabet ; Else its a number ; Pick a starting point as i ; Consider all sub - arrays starting from i ; If this is a 0 sum sub - array then compare it with maximum size sub - array calculated so far ; If no valid sub - array found ; Driver code
using System ; class GFG { static bool isalpha ( int input_char ) { if ( ( input_char >= 65 && input_char <= 90 ) || ( input_char >= 97 && input_char <= 122 ) ) return true ; return false ; } static void findSubArray ( int [ ] arr , int n ) { int sum = 0 ; int maxsize = - 1 , startindex = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isalpha ( arr [ i ] ) ) { arr [ i ] = 0 ; } else { arr [ i ] = 1 ; } } for ( int i = 0 ; i < n - 1 ; i ++ ) { sum = ( arr [ i ] == 0 ) ? - 1 : 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] == 0 ) sum += - 1 ; else sum += 1 ; if ( sum == 0 && maxsize < j - i + 1 ) { maxsize = j - i + 1 ; startindex = i ; } } } if ( maxsize == - 1 ) Console . WriteLine ( maxsize ) ; else Console . WriteLine ( startindex + " ▁ " + ( startindex + maxsize - 1 ) ) ; } public static void Main ( ) { int [ ] arr = { ' A ' , ' B ' , ' X ' , 4 , 6 , ' X ' , ' a ' } ; int size = arr . Length ; findSubArray ( arr , size ) ; } }