codeparrot/xlcost-text-to-code huggingface dataset:
Largest subset with M as smallest missing number | Java program to implement the above approach ; Function to find and return the length of the longest subset whose smallest missing value is M ; Initialize a set ; If array element is not equal to M ; Insert into set ; Increment frequency ; Stores minimum missing number ; Iterate to find the minimum missing integer ; If minimum obtained is less than M ; Update answer ; Return answer ; Driver code
import java . util . * ; class GFG { static int findLengthOfMaxSubset ( int arr [ ] , int n , int m ) { Set < Integer > s = new HashSet < > ( ) ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int tmp = arr [ i ] ; if ( tmp != m ) { s . add ( tmp ) ; answer ++ ; } } int min = 1 ; while ( s . contains ( min ) ) { min ++ ; } if ( min != m ) { answer = - 1 ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 } ; int N = arr . length ; int M = 3 ; System . out . print ( findLengthOfMaxSubset ( arr , N , M ) ) ; } }
Count of submatrix with sum X in a given Matrix | Java program for the above approach ; Size of a column ; Function to find the count of submatrix whose sum is X ; Copying arr to dp and making it indexed 1 ; Precalculate and store the sum of all rectangles with upper left corner at ( 0 , 0 ) ; ; Calculating sum in a 2d grid ; Stores the answer ; Minimum length of square ; Maximum length of square ; Flag to set if sub - square with sum X is found ; Calculate lower right index if upper right corner is at { i , j } ; Calculate the sum of elements in the submatrix with upper left column { i , j } and lower right column at { ni , nj } ; ; If sum X is found ; If sum > X , then size of the square with sum X must be less than mid ; If sum < X , then size of the square with sum X must be greater than mid ; If found , increment count by 1 ; ; Driver Code ; Given Matrix arr [ ] [ ] ; Function Call
import java . util . * ; class GFG { static final int m = 5 ; static int countSubsquare ( int arr [ ] [ ] , int n , int X ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i + 1 ] [ j + 1 ] = arr [ i ] [ j ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { int lo = 1 ; int hi = Math . min ( n - i , m - j ) + 1 ; boolean found = false ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int ni = i + mid - 1 ; int nj = j + mid - 1 ; int sum = dp [ ni ] [ nj ] - dp [ ni ] [ j - 1 ] - dp [ i - 1 ] [ nj ] + dp [ i - 1 ] [ j - 1 ] ; if ( sum >= X ) { if ( sum == X ) { found = true ; } hi = mid - 1 ; } else { lo = mid + 1 ; } } if ( found == true ) { cnt ++ ; } } } return cnt ; } public static void main ( String [ ] args ) { int N = 4 , X = 10 ; int arr [ ] [ ] = { { 2 , 4 , 3 , 2 , 10 } , { 3 , 1 , 1 , 1 , 5 } , { 1 , 1 , 2 , 1 , 4 } , { 2 , 1 , 1 , 1 , 3 } } ; System . out . print ( countSubsquare ( arr , N , X ) + "NEW_LINE"); } }
Check if two arrays can be made equal by reversing any subarray once | Java implementation to check whether two arrays can be made equal by reversing a sub - array only once ; Function to check whether two arrays can be made equal by reversing a sub - array only once ; Integer variable for storing the required starting and ending indices in the array ; Finding the smallest index for which A [ i ] != B [ i ] i . e the starting index of the unequal sub - array ; Finding the largest index for which A [ i ] != B [ i ] i . e the ending index of the unequal sub - array ; Reversing the sub - array A [ start ] , A [ start + 1 ] . . A [ end ] ; Checking whether on reversing the sub - array A [ start ] ... A [ end ] makes the arrays equal ; If any element of the two arrays is unequal print No and return ; Print Yes if arrays are equal after reversing the sub - array ; Driver code
import java . util . * ; class GFG { static void checkArray ( int A [ ] , int B [ ] , int N ) { int start = 0 ; int end = N - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { start = i ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != B [ i ] ) { end = i ; break ; } } Collections . reverse ( Arrays . asList ( A ) ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { System . out . println ( " No " ) ; return ; } } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 3 , 2 , 4 } ; int B [ ] = { 1 , 2 , 3 , 4 } ; int N = A . length ; checkArray ( A , B , N ) ; } }
Count of substrings having all distinct characters | Java program to implement the above appraoach ; Function to count total number of valid subStrings ; Stores the count of subStrings ; Stores the frequency of characters ; Initialised both pointers to beginning of the String ; If all characters in subString from index i to j are distinct ; Increment count of j - th character ; Add all subString ending at j and starting at any index between i and j to the answer ; Increment 2 nd pointer ; If some characters are repeated or j pointer has reached to end ; Decrement count of j - th character ; Increment first pointer ; Return the final count of subStrings ; Driver Code
import java . util . * ; class GFG { static int countSub ( String str ) { int n = ( int ) str . length ( ) ; int ans = 0 ; int [ ] cnt = new int [ 26 ] ; int i = 0 , j = 0 ; while ( i < n ) { if ( j < n && ( cnt [ str . charAt ( j ) - ' a ' ] == 0 ) ) { cnt [ str . charAt ( j ) - ' a ' ] ++ ; ans += ( j - i + 1 ) ; j ++ ; } else { cnt [ str . charAt ( i ) - ' a ' ] -- ; i ++ ; } } return ans ; } public static void main ( String [ ] args ) { String str = " gffg " ; System . out . print ( countSub ( str ) ) ; } }
Find largest factor of N such that N / F is less than K | Java program for the above approach ; Function to find the largest factor of N which is less than or equal to K ; Initialise the variable to store the largest factor of N <= K ; Loop to find all factors of N ; Check if j is a factor of N or not ; Check if j <= K If yes , then store the larger value between ans and j in ans ; Check if N / j <= K If yes , then store the larger value between ans and j in ans ; Since max value is always stored in ans , the maximum value divisible by N less than or equal to K will be returned . ; Driver Code ; Given N and K ; Function call
import java . util . * ; class GFG { static int solve ( int n , int k ) { int ans = 0 ; for ( int j = 1 ; j * j <= n ; j ++ ) { if ( n % j == 0 ) { if ( j <= k ) { ans = Math . max ( ans , j ) ; } if ( n / j <= k ) { ans = Math . max ( ans , n / j ) ; } } } return ans ; } public static void main ( String [ ] args ) { int N = 8 , K = 7 ; System . out . print ( ( N / solve ( N , K ) ) ) ; } }
Maximum sum subarray of size range [ L , R ] | Java program to find Maximum sum subarray of size between L and R . ; function to find Maximum sum subarray of size between L and R ; calculating prefix sum ; maintain 0 for initial values of i upto R Once i = R , then we need to erase that 0 from our multiset as our first index of subarray cannot be 0 anymore . ; we maintain flag to counter if that initial 0 was erased from set or not . ; erase 0 from multiset once i = b ; insert pre [ i - L ] ; find minimum value in multiset . ; erase pre [ i - R ] ; Driver code
import java . util . * ; class GFG { static void max_sum_subarray ( List < Integer > arr , int L , int R ) { int n = arr . size ( ) ; int [ ] pre = new int [ n ] ; pre [ 0 ] = arr . get ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + arr . get ( i ) ; } List < Integer > s1 = new ArrayList < > ( ) ; s1 . add ( 0 ) ; int ans = Integer . MIN_VALUE ; ans = Math . max ( ans , pre [ L - 1 ] ) ; int flag = 0 ; for ( int i = L ; i < n ; i ++ ) { if ( i - R >= 0 ) { if ( flag == 0 ) { int it = s1 . indexOf ( 0 ) ; s1 . remove ( it ) ; flag = 1 ; } } if ( i - L >= 0 ) s1 . add ( pre [ i - L ] ) ; ans = Math . max ( ans , pre [ i ] - s1 . get ( 0 ) ) ; if ( i - R >= 0 ) { int it = s1 . indexOf ( pre [ i - R ] ) ; s1 . remove ( it ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int L , R ; L = 1 ; R = 3 ; List < Integer > arr = Arrays . asList ( 1 , 2 , 2 , 1 ) ; max_sum_subarray ( arr , L , R ) ; } }
Count of ways to select K consecutive empty cells from a given Matrix | Java program to find no of ways to select K consecutive empty cells from a row or column ; Function to Traverse the matrix row wise ; Initialize ans ; Traverse row wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , then increment countcons ; Check if number of empty consecutive cells is greater or equal to K , increment the ans ; Return the count ; Function to Traverse the matrix column wise ; Initialize ans ; Traverse column wise ; Initialize no of consecutive empty cells ; Check if blocked cell is encountered then reset countcons to 0 ; Check if empty cell is encountered , increment countcons ; Check if number of empty consecutive cells is greater than or equal to K , increment the ans ; Return the count ; Driver Code ; If k = 1 only traverse row wise ; Traverse both row and column wise
import java . util . * ; class GFG { static int rowWise ( char [ ] [ ] v , int n , int m , int k ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int countcons = 0 ; for ( int j = 0 ; j < m ; j ++ ) { if ( v [ i ] [ j ] == '1' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } static int colWise ( char [ ] [ ] v , int n , int m , int k ) { int ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int countcons = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ j ] [ i ] == '1' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int n = 3 , m = 3 , k = 1 ; char v [ ] [ ] = { { '0' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; if ( k == 1 ) { System . out . print ( rowWise ( v , n , m , k ) ) ; } else { System . out . print ( colWise ( v , n , m , k ) + rowWise ( v , n , m , k ) ) ; } } }
Check if the count of inversions of two given types on an Array are equal or not | Java program to implement the above approach ; Function to check if the count of inversion of two types are same or not ; If maximum value is found to be greater than a [ j ] , then that pair of indices ( i , j ) will add extra value to inversion of Type 1 ; Update max ; Driver code
import java . io . * ; class GFG { static boolean solve ( int a [ ] , int n ) { int mx = Integer . MIN_VALUE ; for ( int j = 1 ; j < n ; j ++ ) { if ( mx > a [ j ] ) return false ; mx = Math . max ( mx , a [ j - 1 ] ) ; } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 0 , 2 } ; int n = a . length ; boolean possible = solve ( a , n ) ; if ( possible ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count of prime factors of N to be added at each step to convert N to M | Java program to find the minimum steps required to convert a number N to M . ; Array to store shortest prime factor of every integer ; Function to precompute shortest prime factors ; Function to insert distinct prime factors of every integer into a set ; Store distinct prime factors ; Function to return minimum steps using BFS ; Queue of pairs to store the current number and distance from root . ; Set to store distinct prime factors ; Run BFS ; Find out the prime factors of newNum ; Iterate over every prime factor of newNum . ; If M is obtained ; Return number of operations ; If M is exceeded ; Otherwise ; Update and store the new number obtained by prime factor ; If M cannot be obtained ; Driver code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int [ ] spf = new int [ 100009 ] ; static void sieve ( ) { for ( int i = 0 ; i < 100005 ; i ++ ) spf [ i ] = - 1 ; for ( int i = 2 ; i * i <= 100005 ; i ++ ) { for ( int j = i ; j <= 100005 ; j += i ) { if ( spf [ j ] == - 1 ) { spf [ j ] = i ; } } } } static HashSet < Integer > findPrimeFactors ( HashSet < Integer > s , int n ) { while ( n > 1 ) { s . add ( spf [ n ] ) ; n /= spf [ n ] ; } return s ; } static int MinimumSteps ( int n , int m ) { Queue < pair > q = new LinkedList < > ( ) ; HashSet < Integer > s = new HashSet < Integer > ( ) ; q . add ( new pair ( n , 0 ) ) ; while ( ! q . isEmpty ( ) ) { int newNum = q . peek ( ) . first ; int distance = q . peek ( ) . second ; q . remove ( ) ; HashSet < Integer > k = findPrimeFactors ( s , newNum ) ; for ( int i : k ) { if ( newNum == m ) { return distance ; } else if ( newNum > m ) { break ; } else { q . add ( new pair ( newNum + i , distance + 1 ) ) ; } } } return - 1 ; } public static void main ( String [ ] args ) { int N = 7 , M = 16 ; sieve ( ) ; System . out . print ( MinimumSteps ( N , M ) ) ; } }
Count of elements which is product of a pair or an element square | Java Program to implement the above approach ; Stores all factors a number ; Function to calculate and store in a vector ; Function to return the count of array elements which are a product of two array elements ; Copy elements into a a duplicate array ; Sort the duplicate array ; Store the count of elements ; If the factors are not calculated already ; Traverse its factors ; If a pair of factors is found ; Driver Code
import java . util . * ; class GFG { static Vector < Integer > [ ] v = new Vector [ 100000 ] ; static void div ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v [ n ] . add ( i ) ; } } } static int prodof2elements ( int arr [ ] , int n ) { int [ ] arr2 = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; } Arrays . sort ( arr2 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ arr [ i ] ] . size ( ) == 0 ) div ( arr [ i ] ) ; for ( int j : v [ arr [ i ] ] ) { if ( Arrays . binarySearch ( arr2 , j ) >= 0 && Arrays . binarySearch ( arr2 , ( int ) arr [ i ] / j ) >= 0 ) { ans ++ ; break ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 8 , 4 , 32 , 18 } ; int N = arr . length ; for ( int i = 0 ; i < v . length ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; System . out . print ( prodof2elements ( arr , N ) ) ; } }
Largest subarray with frequency of all elements same | Java program for the above approach ; Function to find maximum subarray size ; Generating all subarray i -> starting index j -> end index ; Map 1 to hash frequency of all elements in subarray ; Map 2 to hash frequency of all frequencies of elements ; ele_count is the previous frequency of arr [ j ] ; Finding previous frequency of arr [ j ] in map 1 ; Increasing frequency of arr [ j ] by 1 ; Check if previous frequency is present in map 2 ; Delete previous frequency if hash is equal to 1 ; Decrement the hash of previous frequency ; Incrementing hash of new frequency in map 2 ; Check if map2 size is 1 and updating answer ; Return the maximum size of subarray ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static int max_subarray_size ( int N , int [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { HashMap < Integer , Integer > map1 = new HashMap < > ( ) ; HashMap < Integer , Integer > map2 = new HashMap < > ( ) ; for ( int j = i ; j < N ; j ++ ) { int ele_count ; if ( ! map1 . containsKey ( arr [ j ] ) ) { ele_count = 0 ; } else { ele_count = map1 . get ( arr [ j ] ) ; } if ( map1 . containsKey ( arr [ j ] ) ) { map1 . put ( arr [ j ] , map1 . get ( arr [ j ] ) + 1 ) ; } else { map1 . put ( arr [ j ] , 1 ) ; } if ( map2 . containsKey ( ele_count ) ) { if ( map2 . get ( ele_count ) == 1 ) { map2 . remove ( ele_count ) ; } else { map2 . put ( ele_count , map2 . get ( ele_count ) - 1 ) ; } } if ( map2 . containsKey ( ele_count + 1 ) ) { map2 . put ( ele_count + 1 , map2 . get ( ele_count + 1 ) + 1 ) ; } else { map2 . put ( ele_count + 1 , 1 ) ; } if ( map2 . size ( ) == 1 ) ans = Math . max ( ans , j - i + 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 5 , 6 , 5 , 6 } ; int N = arr . length ; System . out . println ( max_subarray_size ( N , arr ) ) ; } }
Longest substring consisting of vowels using Binary Search | Java implementation of the above approach ; Function to check if a character is vowel or not ; 0 - a 1 - b 2 - c and so on 25 - z ; Function to check if any subString of length k exists which contains only vowels ; Applying sliding window to get all subStrings of length K ; Remove the occurence of ( i - k + 1 ) th character ; Function to perform Binary Search ; Doing binary search on the lengths ; Driver Code
import java . util . * ; class GFG { static boolean vowel ( int vo ) { if ( vo == 0 vo == 4 vo == 8 vo == 14 vo == 20 ) return true ; else return false ; } static boolean check ( String s , int k ) { int [ ] cnt = new int [ 26 ] ; for ( int i = 0 ; i < k - 1 ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] ++ ; } for ( int i = k - 1 ; i < s . length ( ) ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] ++ ; int flag1 = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( vowel ( j ) == false && cnt [ j ] > 0 ) { flag1 = 1 ; break ; } } if ( flag1 == 0 ) return true ; cnt [ s . charAt ( i - k + 1 ) - ' a ' ] -- ; } return false ; } static int longestSubString ( String s ) { int l = 1 , r = s . length ( ) ; int maxi = 0 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( check ( s , mid ) ) { l = mid + 1 ; maxi = Math . max ( maxi , mid ) ; } else r = mid - 1 ; } return maxi ; } public static void main ( String [ ] args ) { String s = " sedrewaefhoiu " ; System . out . print ( longestSubString ( s ) ) ; } }
Find number of pairs ( x , y ) in an Array such that x ^ y > y ^ x | Set 2 | Java program to finds the number of pairs ( x , y ) from X [ ] and Y [ ] such that x ^ y > y ^ x ; Function to return the count of pairs ; Compute suffix sums till i = 3 ; Base Case : x = 0 ; No valid pairs ; Base Case : x = 1 ; Store the count of 0 's ; Base Case : x = 2 ; Store suffix sum upto 5 ; Base Case : x = 3 ; Store count of 2 and suffix sum upto 4 ; For all other values of x ; For all x >= 2 , every y = 0 and every y = 1 makes a valid pair ; Return the count of pairs ; Driver code
class GFG { static int countPairs ( int X [ ] , int Y [ ] , int m , int n ) { int [ ] suffix = new int [ 1005 ] ; long total_pairs = 0 ; for ( int i = 0 ; i < n ; i ++ ) suffix [ Y [ i ] ] ++ ; for ( int i = ( int ) 1e3 ; i >= 3 ; i -- ) suffix [ i ] += suffix [ i + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) { if ( X [ i ] == 0 ) continue ; else if ( X [ i ] == 1 ) { total_pairs += suffix [ 0 ] ; continue ; } else if ( X [ i ] == 2 ) total_pairs += suffix [ 5 ] ; else if ( X [ i ] == 3 ) total_pairs += suffix [ 2 ] + suffix [ 4 ] ; else total_pairs += suffix [ X [ i ] + 1 ] ; total_pairs += suffix [ 0 ] + suffix [ 1 ] ; } return ( int ) total_pairs ; } public static void main ( String [ ] args ) { int X [ ] = { 10 , 19 , 18 } ; int Y [ ] = { 11 , 15 , 9 } ; int m = X . length ; int n = Y . length ; System . out . print ( countPairs ( X , Y , m , n ) ) ; } }
Min number of moves to traverse entire Matrix through connected cells with equal values | Java program to find the minimum number of moves to traverse a given matrix ; Function to find the minimum number of moves to traverse the given matrix ; Constructing another matrix consisting of distinct values ; Updating the array B by checking the values of A that if there are same values connected through an edge or not ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Store all distinct elements in a set ; Return answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static int solve ( int A [ ] [ ] , int N , int M ) { int [ ] [ ] B = new int [ N ] [ M ] ; int c = 1 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) B [ i ] [ j ] = c ++ ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i != 0 ) { if ( A [ i - 1 ] [ j ] == A [ i ] [ j ] ) B [ i - 1 ] [ j ] = B [ i ] [ j ] ; } if ( i != N - 1 ) { if ( A [ i + 1 ] [ j ] == A [ i ] [ j ] ) B [ i + 1 ] [ j ] = B [ i ] [ j ] ; } if ( j != 0 ) { if ( A [ i ] [ j - 1 ] == A [ i ] [ j ] ) B [ i ] [ j - 1 ] = B [ i ] [ j ] ; } if ( j != M - 1 ) { if ( A [ i ] [ j + 1 ] == A [ i ] [ j ] ) B [ i ] [ j + 1 ] = B [ i ] [ j ] ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) s . add ( B [ i ] [ j ] ) ; } return s . size ( ) ; } public static void main ( String [ ] args ) { int N = 2 , M = 3 ; int A [ ] [ ] = { { 2 , 1 , 3 } , { 1 , 1 , 2 } } ; System . out . print ( solve ( A , N , M ) ) ; } }
Number of times an array can be partitioned repetitively into two subarrays with equal sum | Java program for the above approach ; Recursion Function to calculate the possible splitting ; If there are less than two elements , we cannot partition the sub - array . ; Iterate from the start to end - 1. ; Recursive call to the left and the right sub - array . ; If there is no such partition , then return 0 ; Function to find the total splitting ; Prefix array to store the prefix - sum using 1 based indexing ; Store the prefix - sum ; Function Call to count the number of splitting ; Driver Code ; Given array ; Function call
class GFG { static int splitArray ( int start , int end , int [ ] arr , int [ ] prefix_sum ) { if ( start >= end ) return 0 ; for ( int k = start ; k < end ; ++ k ) { if ( ( prefix_sum [ k ] - prefix_sum [ start - 1 ] ) == ( prefix_sum [ end ] - prefix_sum [ k ] ) ) { return 1 + splitArray ( start , k , arr , prefix_sum ) + splitArray ( k + 1 , end , arr , prefix_sum ) ; } } return 0 ; } static void solve ( int arr [ ] , int n ) { int [ ] prefix_sum = new int [ n + 1 ] ; prefix_sum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i - 1 ] ; } System . out . print ( splitArray ( 1 , n , arr , prefix_sum ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 3 , 3 , 0 , 3 , 3 } ; int N = arr . length ; solve ( arr , N ) ; } }
Minimise the maximum element of N subarrays of size K | Java implementation to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Function to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Condition to check if it is not possible to choose k sized N subarrays ; Using binary search ; calculating mid ; Loop to find the count of the K sized subarrays possible with elements less than mid ; Condition to check if the answer is in right subarray ; Driver Code ; Function Call
class GFG { static int minDays ( int [ ] arr , int n , int k ) { int l = arr . length , left = 1 , right = ( int ) 1e9 ; if ( n * k > l ) return - 1 ; while ( left < right ) { int mid = ( left + right ) / 2 , cnt = 0 , product = 0 ; for ( int j = 0 ; j < l ; ++ j ) { if ( arr [ j ] > mid ) { cnt = 0 ; } else if ( ++ cnt >= k ) { product ++ ; cnt = 0 ; } } if ( product < n ) { left = mid + 1 ; } else { right = mid ; } } return left ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 10 , 3 , 10 , 2 } ; int n = 3 , k = 1 ; System . out . print ( minDays ( arr , n , k ) + "NEW_LINE"); } }