codeparrot/xlcost-text-to-code huggingface dataset:
Count of N | Java Program to implement the above approach ; Function to calculate the total count of N - digit numbers such that the sum of digits at even positions and odd positions are divisible by A and B respectively ; For single digit numbers ; Largest possible number ; Count of possible odd digits ; Count of possible even digits ; Calculate total count of sequences of length even_count with sum divisible by A where first digit can be zero ; Calculate total count of sequences of length odd_count with sum divisible by B where cannot be zero ; Return their product as answer ; Driver Code
class GFG { public static long count ( int N , int A , int B ) { if ( N == 1 ) { return 9 / B + 1 ; } int max_sum = 9 * N ; int odd_count = N / 2 + N % 2 ; int even_count = N - odd_count ; long dp [ ] [ ] = new long [ even_count ] [ max_sum + 1 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) dp [ 0 ] [ i % A ] ++ ; for ( int i = 1 ; i < even_count ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { for ( int k = 0 ; k <= max_sum ; k ++ ) { if ( dp [ i - 1 ] [ k ] > 0 ) dp [ i ] [ ( j + k ) % A ] += dp [ i - 1 ] [ k ] ; } } } long dp1 [ ] [ ] = new long [ odd_count ] [ max_sum + 1 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) dp1 [ 0 ] [ i % B ] ++ ; for ( int i = 1 ; i < odd_count ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { for ( int k = 0 ; k <= max_sum ; k ++ ) { if ( dp1 [ i - 1 ] [ k ] > 0 ) dp1 [ i ] [ ( j + k ) % B ] += dp1 [ i - 1 ] [ k ] ; } } } return dp [ even_count - 1 ] [ 0 ] * dp1 [ odd_count - 1 ] [ 0 ] ; } public static void main ( String [ ] args ) { int N = 2 , A = 2 , B = 5 ; System . out . println ( count ( N , A , B ) ) ; } }
Maximum possible difference of two subsets of an array | java find maximum difference of subset sum ; function for maximum subset diff ; if frequency of any element is two make both equal to zero ; driver program
import java . io . * ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { boolean isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = arr [ j ] = 0 ; break ; } } if ( isSingleOccurance ) { if ( arr [ i ] > 0 ) SubsetSum_1 += arr [ i ] ; else SubsetSum_2 += arr [ i ] ; } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
Count of all possible Paths in a Tree such that Node X does not appear before Node Y | Java Program to implement the above approach ; Maximum number of nodes ; Vector to store the tree ; Function to perform DFS Traversal ; Mark the node as visited ; Initialize the subtree size of each node as 1 ; If the node is same as A ; Mark check_subtree [ node ] as true ; Otherwise ; Iterate over the adjacent nodes ; If the adjacent node is not visited ; Update the size of the subtree of current node ; Check if the subtree of current node contains node A ; Return size of subtree of node ; Function to add edges to the tree ; Function to calculate the number of possible paths ; Stores the size of subtree of each node ; Stores which nodes are visited ; Stores if the subtree of a node contains node A ; DFS Call ; Stores the difference between total number of nodes and subtree size of an immediate child of Y lies between the path from A to B ; Iterate over the adjacent nodes B ; If the node is in the path from A to B ; Calculate the difference ; Return the final answer ; Driver Code ; Insert Edges
import java . util . * ; class GFG { static int NN = ( int ) 3e5 ; static Vector < Integer > [ ] G = new Vector [ NN + 1 ] ; static int dfs ( int node , int A , int [ ] subtree_size , int [ ] visited , int [ ] check_subtree ) { visited [ node ] = 1 ; subtree_size [ node ] = 1 ; if ( node == A ) { check_subtree [ node ] = 1 ; } else check_subtree [ node ] = 0 ; for ( int v : G [ node ] ) { if ( visited [ v ] == 0 ) { subtree_size [ node ] += dfs ( v , A , subtree_size , visited , check_subtree ) ; check_subtree [ node ] = check_subtree [ node ] | check_subtree [ v ] ; } } return subtree_size [ node ] ; } static void addedge ( int node1 , int node2 ) { G [ node1 ] . add ( node2 ) ; G [ node2 ] . add ( node1 ) ; } static int numberOfPairs ( int N , int B , int A ) { int [ ] subtree_size = new int [ N + 1 ] ; int [ ] visited = new int [ N + 1 ] ; int [ ] check_subtree = new int [ N + 1 ] ; dfs ( B , A , subtree_size , visited , check_subtree ) ; int difference = 0 ; for ( int v : G [ B ] ) { if ( check_subtree [ v ] > 0 ) { difference = N - subtree_size [ v ] ; break ; } } return ( N * ( N - 1 ) ) - difference * ( subtree_size [ A ] ) ; } public static void main ( String [ ] args ) { int N = 9 ; int X = 5 , Y = 3 ; for ( int i = 0 ; i < G . length ; i ++ ) G [ i ] = new Vector < Integer > ( ) ; addedge ( 0 , 2 ) ; addedge ( 1 , 2 ) ; addedge ( 2 , 3 ) ; addedge ( 3 , 4 ) ; addedge ( 4 , 6 ) ; addedge ( 4 , 5 ) ; addedge ( 5 , 7 ) ; addedge ( 5 , 8 ) ; System . out . print ( numberOfPairs ( N , Y , X ) ) ; } }
Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them | Java program to implement the above approach ; Function to find required number M ; Initialising m ; Finding the index of the most significant bit of N ; Calculating required number ; Driver Code
class GFG { static int equalXORandOR ( int n ) { int m = 0 ; int MSB = ( int ) Math . log ( n ) ; for ( int i = 0 ; i <= MSB ; i ++ ) { if ( ( n & ( 1 << i ) ) <= 0 ) { m += ( 1 << i ) ; } } return m ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . print ( equalXORandOR ( n ) ) ; } }
Maximum possible difference of two subsets of an array | java find maximum difference of subset sum ; function for maximum subset diff ; sort the array ; calculate the result ; check for last element ; return result ; driver program
import java . io . * ; import java . util . * ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int result = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += Math . abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . abs ( arr [ n - 1 ] ) ; return result ; } static public void main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
Count of Palindromic Strings possible by swapping of a pair of Characters | Java Program to implement the above approach ; Function to return the count of possible palindromic Strings ; Stores the frequencies of each character ; Stores the length of the String ; Increase the number of swaps , the current character make with its previous occurrences ; Increase frequency ; Driver Code
import java . util . * ; class GFG { static long findNewString ( String s ) { long ans = 0 ; int [ ] freq = new int [ 26 ] ; int n = s . length ( ) ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; ++ i ) { ans += freq [ s . charAt ( i ) - ' a ' ] ; freq [ s . charAt ( i ) - ' a ' ] ++ ; } return ans ; } public static void main ( String [ ] args ) { String s = " aaabaaa " ; System . out . print ( findNewString ( s ) ) ; } }
Maximum possible difference of two subsets of an array | Java find maximum difference of subset sum ; Function for maximum subset diff ; Construct hash for positive elements ; Calculate subset sum for positive elements ; Construct hash for negative elements ; Calculate subset sum for negative elements ; Driver code
import java . util . * ; class GFG { public static int maxDiff ( int arr [ ] , int n ) { HashMap < Integer , Integer > hashPositive = new HashMap < > ( ) ; HashMap < Integer , Integer > hashNegative = new HashMap < > ( ) ; int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 ) { if ( hashPositive . containsKey ( arr [ i ] ) ) { hashPositive . replace ( arr [ i ] , hashPositive . get ( arr [ i ] ) + 1 ) ; } else { hashPositive . put ( arr [ i ] , 1 ) ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 && hashPositive . containsKey ( arr [ i ] ) ) { if ( hashPositive . get ( arr [ i ] ) == 1 ) { SubsetSum_1 += arr [ i ] ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 ) { if ( hashNegative . containsKey ( Math . abs ( arr [ i ] ) ) ) { hashNegative . replace ( Math . abs ( arr [ i ] ) , hashNegative . get ( Math . abs ( arr [ i ] ) ) + 1 ) ; } else { hashNegative . put ( Math . abs ( arr [ i ] ) , 1 ) ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 && hashNegative . containsKey ( Math . abs ( arr [ i ] ) ) ) { if ( hashNegative . get ( Math . abs ( arr [ i ] ) ) == 1 ) { SubsetSum_2 += arr [ i ] ; } } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . print ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
Change the given string according to the given conditions | Java program for the above approach ; Function to proofread the spells ; Loop to iterate over the characters of the string ; Push the current character c in the stack ; Check for Rule 1 ; Check for Rule 2 ; To store the resultant string ; Loop to iterate over the characters of stack ; Return the resultant string ; Driver Code ; Given string str ; Function call
import java . util . * ; class GFG { public static String proofreadSpell ( String str ) { Vector < Character > result = new Vector < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { result . add ( str . charAt ( i ) ) ; int n = result . size ( ) ; if ( n >= 3 ) { if ( result . get ( n - 1 ) == result . get ( n - 2 ) && result . get ( n - 1 ) == result . get ( n - 3 ) ) { result . remove ( result . size ( ) - 1 ) ; } } n = result . size ( ) ; if ( n >= 4 ) { if ( result . get ( n - 1 ) == result . get ( n - 2 ) && result . get ( n - 3 ) == result . get ( n - 4 ) ) { result . remove ( result . size ( ) - 1 ) ; } } } String resultStr = " " ; for ( Character c : result ) { resultStr += c ; } return resultStr ; } public static void main ( String [ ] args ) { String str = " hello " ; System . out . println ( proofreadSpell ( str ) ) ; } }
Smallest subarray with k distinct numbers | Java program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Consider every element as starting point . ; Find the smallest window starting with arr [ i ] and containing exactly k distinct elements . ; There are less than k distinct elements now , so no need to continue . ; If there was no window with k distinct elements ( k is greater than total distinct elements ) ; Driver code
import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { Set < Integer > s = new HashSet < Integer > ( ) ; int j ; for ( j = i ; j < n ; j ++ ) { s . add ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }
Smallest subarray with k distinct numbers | Java program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Initially left and right side is - 1 and - 1 , number of distinct elements are zero and range is n . ; Initialize right side ; Increment right side . ; If number of distinct elements less than k . ; If distinct elements are equal to k and length is less than previous length . ; If number of distinct elements less than k , then break . ; If distinct elements equals to k then try to increment left side . ; Increment left side . ; It is same as explained in above loop . ; Driver code
import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; int j = - 1 ; HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( j < n && hm . size ( ) < k ) hm . put ( arr [ j ] , hm . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; if ( hm . size ( ) == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . size ( ) < k ) break ; while ( hm . size ( ) == k ) { if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; i ++ ; if ( hm . size ( ) == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }
Sum of f ( a [ i ] , a [ j ] ) over all pairs in an array of n integers | Java program to calculate the sum of f ( a [ i ] , aj ] ) ; Function to calculate the sum ; Map to keep a count of occurrences ; Traverse in the list from start to end number of times a [ i ] can be in a pair and to get the difference we subtract pre_sum ; If the ( a [ i ] - 1 ) is present then subtract that value as f ( a [ i ] , a [ i ] - 1 ) = 0 ; If the ( a [ i ] + 1 ) is present then add that value as f ( a [ i ] , a [ i ] - 1 ) = 0 here we add as a [ i ] - ( a [ i ] - 1 ) < 0 which would have been added as negative sum , so we add to remove this pair from the sum value ; keeping a counter for every element ; Driver code
import java . util . * ; public class GfG { public static int sum ( int a [ ] , int n ) { Map < Integer , Integer > cnt = new HashMap < Integer , Integer > ( ) ; int ans = 0 , pre_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( i * a [ i ] ) - pre_sum ; pre_sum += a [ i ] ; if ( cnt . containsKey ( a [ i ] - 1 ) ) ans -= cnt . get ( a [ i ] - 1 ) ; if ( cnt . containsKey ( a [ i ] + 1 ) ) ans += cnt . get ( a [ i ] + 1 ) ; if ( cnt . containsKey ( a [ i ] ) ) { cnt . put ( a [ i ] , cnt . get ( a [ i ] ) + 1 ) ; } else { cnt . put ( a [ i ] , 1 ) ; } } return ans ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 , 1 , 3 } ; int n = a . length ; System . out . println ( sum ( a , n ) ) ; } }
Minimum distance between the maximum and minimum element of a given Array | Java Program to implement the above approach ; Function to find the minimum distance between the minimum and the maximum element ; Stores the minimum and maximum array element ; Stores the most recently traversed indices of the minimum and the maximum element ; Stores the minimum distance between the minimum and the maximium ; Find the maximum and the minimum element from the given array ; Find the minimum distance ; Check if current element is equal to minimum ; Check if current element is equal to maximum ; If both the minimum and the maximum element has occurred at least once ; Driver Code
import java . util . * ; class GFG { public static int minDistance ( int a [ ] , int n ) { int max = - 1 , min = Integer . MAX_VALUE ; int min_index = - 1 , max_index = - 1 ; int min_dist = n + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; if ( a [ i ] < min ) min = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == min ) min_index = i ; if ( a [ i ] == max ) max_index = i ; if ( min_index != - 1 && max_index != - 1 ) min_dist = Math . min ( min_dist , Math . abs ( min_index - max_index ) ) ; } return min_dist ; } public static void main ( String [ ] args ) { int n = 12 ; int a [ ] = { 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 8 , 2 } ; System . out . println ( minDistance ( a , n ) ) ; } }
Kth diagonal from the top left of a given matrix | Java implementation of the above approach ; Function returns required diagonal ; Initialize values to print upper diagonals ; Initialize values to print lower diagonals ; Traverse the diagonal ; Print its contents ; Driver Code
import java . util . * ; class GFG { static void printDiagonal ( int K , int N , int [ ] [ ] M ) { int startrow , startcol ; if ( K - 1 < N ) { startrow = K - 1 ; startcol = 0 ; } else { startrow = N - 1 ; startcol = K - N ; } for ( ; startrow >= 0 && startcol < N ; startrow -- , startcol ++ ) { System . out . print ( M [ startrow ] [ startcol ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 3 , K = 4 ; int [ ] [ ] M = { { 4 , 7 , 8 } , { 9 , 2 , 3 } , { 0 , 4 , 1 } } ; printDiagonal ( K , N , M ) ; } }
Longest substring that starts with X and ends with Y | Java program for the above approach ; Function returns length of longest substring starting with X and ending with Y ; Length of string ; Find the length of the string starting with X from the beginning ; Find the length of the string ending with Y from the end ; Longest substring ; Print the length ; Driver code ; Given string str ; Starting and Ending characters ; Function Call
class GFG { public static void longestSubstring ( String str , char X , char Y ) { int N = str . length ( ) ; int start = 0 ; int end = N - 1 ; int xPos = 0 ; int yPos = 0 ; while ( true ) { if ( str . charAt ( start ) == X ) { xPos = start ; break ; } start ++ ; } while ( true ) { if ( str . charAt ( end ) == Y ) { yPos = end ; break ; } end -- ; } int length = ( yPos - xPos ) + 1 ; System . out . print ( length ) ; } public static void main ( String [ ] args ) { String str = " HASFJGHOGAKZZFEGA " ; char X = ' A ' , Y = ' Z ' ; longestSubstring ( str , X , Y ) ; } }
Find parent of given node in a Binary Tree with given postorder traversal | Java implementation to find the parent of the given node K in a binary tree whose post - order traversal is N natural numbers ; Function to find the parent of the given node ; Condition to check whether the given node is a root node . if it is then return - 1 because root node has no parent ; Loop till we found the given node ; Finding the middle node of the tree because at every level tree parent is divided into two halves ; if the node is found return the parent always the child nodes of every node is node 2 or ( node - 1 ) ; if the node to be found is greater than the mid search for left subtree else search in right subtree ; Driver Code
import java . util . * ; class GFG { static int findParent ( int height , int node ) { int start = 1 ; int end = ( int ) Math . pow ( 2 , height ) - 1 ; if ( end == node ) return - 1 ; while ( node >= 1 ) { end = end - 1 ; int mid = start + ( end - start ) / 2 ; if ( mid == node end == node ) { return ( end + 1 ) ; } else if ( node < mid ) { end = mid ; } else { start = mid ; } } return - 1 ; } public static void main ( String [ ] args ) { int height = 4 ; int node = 6 ; int k = findParent ( height , node ) ; System . out . print ( k ) ; } }
Replace every element with the smallest of the others | Java code for the above approach . ; There should be atleast two elements ; If current element is smaller than firstSmallest then update both firstSmallest and secondSmallest ; If arr [ i ] is in between firstSmallest and secondSmallest then update secondSmallest ; Replace every element by smallest of all other elements ; Print the modified array . ; Driver code
import java . util . * ; class GFG { static void ReplaceElements ( int [ ] arr , int n ) { if ( n < 2 ) { System . out . println ( " Invalid ▁ Input " ) ; return ; } int firstSmallest = Integer . MAX_VALUE ; int secondSmallest = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < firstSmallest ) { secondSmallest = firstSmallest ; firstSmallest = arr [ i ] ; } else if ( arr [ i ] < secondSmallest && arr [ i ] != firstSmallest ) secondSmallest = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != firstSmallest ) arr [ i ] = firstSmallest ; else arr [ i ] = secondSmallest ; } for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 3 } ; int n = arr . length ; ReplaceElements ( arr , n ) ; } }
Count subarrays with equal number of 1 ' s ▁ and ▁ 0' s | Java implementation to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; function to count subarrays with equal number of 1 ' s ▁ and ▁ 0' s ; ' um ' implemented as hash table to store frequency of values obtained through cumulative sum ; Traverse original array and compute cumulative sum and increase count by 1 for this sum in ' um ' . Adds ' - 1' when arr [ i ] == 0 ; traverse the hash table ' um ' ; If there are more than one prefix subarrays with a particular sum ; add the subarrays starting from 1 st element and have equal number of 1 ' s ▁ and ▁ 0' s ; required count of subarrays ; Driver program to test above
import java . util . * ; class GFG { static int countSubarrWithEqualZeroAndOne ( int arr [ ] , int n ) { Map < Integer , Integer > um = new HashMap < > ( ) ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ] ; um . put ( curr_sum , um . get ( curr_sum ) == null ? 1 : um . get ( curr_sum ) + 1 ) ; } int count = 0 ; for ( Map . Entry < Integer , Integer > itr : um . entrySet ( ) ) { if ( itr . getValue ( ) > 1 ) count += ( ( itr . getValue ( ) * ( itr . getValue ( ) - 1 ) ) / 2 ) ; } if ( um . containsKey ( 0 ) ) count += um . get ( 0 ) ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ = ▁ " + countSubarrWithEqualZeroAndOne ( arr , n ) ) ; } }
Longest subarray having count of 1 s one more than count of 0 s | Java implementation to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; function to find the length of longest subarray having count of 1 ' s ▁ one ▁ more ▁ than ▁ count ▁ of ▁ 0' s ; unordered_map ' um ' implemented as hash table ; traverse the given array ; consider '0' as ' - 1' ; when subarray starts form index '0' ; make an entry for ' sum ' if it is not present in ' um ' ; check if ' sum - 1' is present in ' um ' or not ; update maxLength ; required maximum length ; Driver Code
import java . util . * ; class GFG { static int lenOfLongSubarr ( int arr [ ] , int n ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( ! um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - 1 ) ) { if ( maxLen < ( i - um . get ( sum - 1 ) ) ) maxLen = i - um . get ( sum - 1 ) ; } } return maxLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 , 0 , 1 } ; int n = arr . length ; System . out . println ( " Length ▁ = ▁ " + lenOfLongSubarr ( arr , n ) ) ; } }