codeparrot/xlcost-text-to-code huggingface dataset:
Maximum sum subarray having sum less than or equal to given sum using Set | Java program to find maximum sum subarray less than K ; Function to maximum required sum < K ; Hash to lookup for value ( cum_sum - K ) ; Getting cummulative sum from [ 0 to i ] ; Lookup for upperbound of ( cSum - K ) in hash ; Check if upper_bound of ( cSum - K ) exists then update max sum ; Insert cummulative value in hash ; Return maximum sum lesser than K ; x is the target value or key ; Driver code ; Initialise the array ; Initialise the value of K ; Size of array
import java . util . * ; import java . io . * ; class GFG { static int maxSubarraySum ( int arr [ ] , int N , int K ) { Set < Integer > cum_set = new HashSet < > ( ) ; cum_set . add ( 0 ) ; int max_sum = Integer . MIN_VALUE , cSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cSum += arr [ i ] ; ArrayList < Integer > al = new ArrayList < > ( ) ; Iterator < Integer > it = cum_set . iterator ( ) ; int end = 0 ; while ( it . hasNext ( ) ) { end = it . next ( ) ; al . add ( end ) ; } Collections . sort ( al ) ; int sit = lower_bound ( al , cSum - K ) ; if ( sit != end ) max_sum = Math . max ( max_sum , cSum - sit ) ; cum_set . add ( cSum ) ; } return max_sum ; } static int lower_bound ( ArrayList < Integer > al , int x ) { int l = - 1 , r = al . size ( ) ; while ( l + 1 < r ) { int m = ( l + r ) >>> 1 ; if ( al . get ( m ) >= x ) r = m ; else l = m ; } return r ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , - 2 , 6 , 3 , - 5 } ; int K = 15 ; int N = arr . length ; System . out . println ( maxSubarraySum ( arr , N , K ) ) ; } }
Print all triplets in sorted array that form AP | Java program to print all triplets in given array that form Arithmetic Progression ; Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void printAllAPTriplets ( int [ ] arr , int n ) { ArrayList < Integer > s = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int diff = arr [ j ] - arr [ i ] ; boolean exists = s . contains ( arr [ i ] - diff ) ; if ( exists ) System . out . println ( arr [ i ] - diff + " ▁ " + arr [ i ] + " ▁ " + arr [ j ] ) ; } s . add ( arr [ i ] ) ; } } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = arr . length ; printAllAPTriplets ( arr , n ) ; } }
Print all triplets in sorted array that form AP | Java implementation to print all the triplets in given array that form Arithmetic Progression ; Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code
import java . io . * ; class GFG { static void findAllTriplets ( int arr [ ] , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { for ( int j = i - 1 , k = i + 1 ; j >= 0 && k < n ; ) { if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) { System . out . println ( arr [ j ] + " ▁ " + arr [ i ] + " ▁ " + arr [ k ] ) ; k ++ ; j -- ; } else if ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) k ++ ; else j -- ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = arr . length ; findAllTriplets ( arr , n ) ; } }
Check if there is a root to leaf path with given sequence | Java program to see if there is a root to leaf path with given sequence . ; A binary tree node has data , pointer to left child and a pointer to right child ; Util function ; If root is NULL or reached end of the array ; If current node is leaf ; If current node is equal to arr [ index ] this means that till this level path has been matched and remaining path can be either in left subtree or right subtree . ; Function to check given sequence of root to leaf path exist in tree or not . index : current element in sequence of root to leaf path ; Driver code ; arr [ ] : sequence of root to leaf path
import java . io . * ; class Node { int data ; Node left ; Node right ; Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } class GFG { static boolean existPathUtil ( Node root , int arr [ ] , int n , int index ) { if ( root == null index == n ) return false ; if ( root . left == null && root . right == null ) { if ( ( root . data == arr [ index ] ) && ( index == n - 1 ) ) return true ; return false ; } return ( ( index < n ) && ( root . data == arr [ index ] ) && ( existPathUtil ( root . left , arr , n , index + 1 ) || existPathUtil ( root . right , arr , n , index + 1 ) ) ) ; } static boolean existPath ( Node root , int arr [ ] , int n , int index ) { if ( root == null ) return ( n == 0 ) ; return existPathUtil ( root , arr , n , 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 8 , 6 , 7 } ; int n = arr . length ; Node root = new Node ( 5 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 8 ) ; root . left . left = new Node ( 2 ) ; root . left . right = new Node ( 4 ) ; root . left . left . left = new Node ( 1 ) ; root . right . left = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; if ( existPath ( root , arr , n , 0 ) ) System . out . println ( " Path ▁ Exists " ) ; else System . out . println ( " Path ▁ does ▁ not ▁ Exist " ) ; } }
All unique triplets that sum up to a given value | Java program to find all unique triplets without using any extra space . ; Function to all find unique triplets without using extra space ; Sort the input array ; For handling the cases when no such triplets exits . ; Iterate over the array from start to n - 2. ; Index of the first element in remaining range . ; Index of the last element ; Setting our new target ; Checking if current element is same as previous ; Checking if current element is same as previous ; If we found the triplets then print it and set the flag ; If target is greater then increment the start index ; If target is smaller than decrement the end index ; If no such triplets found ; Driver code ; Function call
import java . util . * ; public class Main { public static void findTriplets ( int a [ ] , int n , int sum ) { int i ; Arrays . sort ( a ) ; boolean flag = false ; for ( i = 0 ; i < n - 2 ; i ++ ) { if ( i == 0 a [ i ] > a [ i - 1 ] ) { int start = i + 1 ; int end = n - 1 ; int target = sum - a [ i ] ; while ( start < end ) { if ( start > i + 1 && a [ start ] == a [ start - 1 ] ) { start ++ ; continue ; } if ( end < n - 1 && a [ end ] == a [ end + 1 ] ) { end -- ; continue ; } if ( target == a [ start ] + a [ end ] ) { System . out . print ( " [ " + a [ i ] + " , " + a [ start ] + " , " + a [ end ] + " ] ▁ " ) ; flag = true ; start ++ ; end -- ; } else if ( target > ( a [ start ] + a [ end ] ) ) { start ++ ; } else { end -- ; } } } } if ( flag == false ) { System . out . print ( " No ▁ Such ▁ Triplets ▁ Exist " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 12 , 3 , 6 , 1 , 6 , 9 } ; int n = a . length ; int sum = 24 ; findTriplets ( a , n , sum ) ; } }
Find the position of the given row in a 2 | Java implementation of the approach ; Function to find a row in the given matrix using linear search ; Assume that the current row matched with the given array ; If any element of the current row doesn 't  match with the corresponding element  of the given array ; Set matched to false and break ; ; If matched then return the row number ; No row matched with the given array ; Driver code
import java . io . * ; class GFG { static int m = 6 , n = 4 ; static int linearCheck ( int ar [ ] [ ] , int arr [ ] ) { for ( int i = 0 ; i < m ; i ++ ) { boolean matched = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ar [ i ] [ j ] != arr [ j ] ) { matched = false ; break ; } } if ( matched ) return i + 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int row [ ] = { 10 , 9 , 22 , 23 } ; System . out . println ( linearCheck ( mat , row ) ) ; } }
Count number of triplets with product equal to given number | Java program to count triplets with given product m ; Method to count such triplets ; Consider all triplets and count if their product is equal to m ; Driver method
class GFG { static int countTriplets ( int arr [ ] , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) for ( int j = i + 1 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] * arr [ j ] * arr [ k ] == m ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 2 , 3 , 8 } ; int m = 24 ; System . out . println ( countTriplets ( arr , arr . length , m ) ) ; } }
Count number of triplets with product equal to given number | Java program to count triplets with given product m ; Method to count such triplets ; Store all the elements in a set ; Consider all pairs and check for a third number so their product is equal to m ; Check if current pair divides m or not If yes , then search for ( m / arr [ i ] * arr [ j ] ) ; Check if the third number is present in the map and it is not equal to any other two elements and also check if this triplet is not counted already using their indexes ; Return number of triplets ; Driver method
import java . util . HashMap ; class GFG { static int countTriplets ( int arr [ ] , int n , int m ) { HashMap < Integer , Integer > occ = new HashMap < Integer , Integer > ( n ) ; for ( int i = 0 ; i < n ; i ++ ) occ . put ( arr [ i ] , i ) ; int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ i ] * arr [ j ] <= m ) && ( arr [ i ] * arr [ j ] != 0 ) && ( m % ( arr [ i ] * arr [ j ] ) == 0 ) ) { int check = m / ( arr [ i ] * arr [ j ] ) ; occ . containsKey ( check ) ; if ( check != arr [ i ] && check != arr [ j ] && occ . containsKey ( check ) && occ . get ( check ) > i && occ . get ( check ) > j ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 2 , 3 , 8 } ; int m = 24 ; System . out . println ( countTriplets ( arr , arr . length , m ) ) ; } }
Count of index pairs with equal elements in an array | Java program to count of pairs with equal elements in an array . ; Return the number of pairs with equal values . ; for each index i and j ; finding the index with same value but different index . ; driver code
class GFG { static int countPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
Queries to answer the number of ones and zero to the left of given index | Java implementation of the approach ; pair class ; Function to pre - calculate the left [ ] array ; Iterate in the binary array ; Initialize the number of 1 and 0 ; Increase the count ; Driver code ; Queries ; Solve queries
class GFG { static class pair { int first , second ; pair ( int a , int b ) { first = a ; second = b ; } } static void preCalculate ( int binary [ ] , int n , pair left [ ] ) { int count1 = 0 , count0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] . first = count1 ; left [ i ] . second = count0 ; if ( binary [ i ] != 0 ) count1 ++ ; else count0 ++ ; } } public static void main ( String args [ ] ) { int binary [ ] = { 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = binary . length ; pair left [ ] = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) left [ i ] = new pair ( 0 , 0 ) ; preCalculate ( binary , n , left ) ; int queries [ ] = { 0 , 1 , 2 , 4 } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( left [ queries [ i ] ] . first + " ▁ ones ▁ " + left [ queries [ i ] ] . second + " zerosNEW_LINE"); } }
Count of index pairs with equal elements in an array | Java program to count of index pairs with equal elements in an array . ; A method to return number of pairs with equal values ; Finding frequency of each number . ; Calculating count of pairs with equal values ; Driver code
import java . util . * ; class GFG { public static int countPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; else hm . put ( arr [ i ] , 1 ) ; } int ans = 0 ; for ( Map . Entry < Integer , Integer > it : hm . entrySet ( ) ) { int count = it . getValue ( ) ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 2 , 3 , 1 } ; System . out . println ( countPairs ( arr , arr . length ) ) ; } }
Partition the array into three equal sum segments |  ; First segment 's end index ; Third segment 's start index ; This function returns true if the array can be divided into three equal sum segments ; Prefix Sum Array ; Suffix Sum Array ; Stores the total sum of the array ; We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here . ; Driver code
public class Main { public static int pos1 = - 1 ; public static int pos2 = - 1 ; public static boolean equiSumUtil ( int [ ] arr ) { int n = arr . length ; int [ ] pre = new int [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; pre [ i ] = sum ; } int [ ] suf = new int [ n ] ; sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; suf [ i ] = sum ; } int total_sum = sum ; int i = 0 , j = n - 1 ; while ( i < j - 1 ) { if ( pre [ i ] == total_sum / 3 ) { pos1 = i ; } if ( suf [ j ] == total_sum / 3 ) { pos2 = j ; } if ( pos1 != - 1 && pos2 != - 1 ) { if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum / 3 ) { return true ; } else { return false ; } } if ( pre [ i ] < suf [ j ] ) { i ++ ; } else { j -- ; } } return false ; } public static void equiSum ( int [ ] arr ) { boolean ans = equiSumUtil ( arr ) ; if ( ans ) { System . out . print ( " First ▁ Segment ▁ : ▁ " ) ; for ( int i = 0 ; i <= pos1 ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " Second ▁ Segment ▁ : ▁ " ) ; for ( int i = pos1 + 1 ; i < pos2 ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " Third ▁ Segment ▁ : ▁ " ) ; for ( int i = pos2 ; i < arr . length ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } else { System . out . println ( " Array ▁ cannot ▁ be ▁ " + " divided ▁ into ▁ three ▁ equal ▁ sum ▁ segments " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 } ; equiSum ( arr ) ; } }
Print cousins of a given node in Binary Tree | Java program to print cousins of a node ; A Binary Tree Node ; A utility function to create a new Binary Tree Node ; It returns level of the node if it is present in tree , otherwise returns 0. ; base cases ; If node is present in left subtree ; If node is not present in left subtree ; Print nodes at a given level such that sibling of node is not printed if it exists ; Base cases ; If current node is parent of a node with given level ; Recur for left and right subtrees ; This function prints cousins of a given node ; Get level of given node ; Print nodes of given level . ; Driver Program to test above functions
class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . left = null ; temp . right = null ; return temp ; } static int getLevel ( Node root , Node node , int level ) { if ( root == null ) return 0 ; if ( root == node ) return level ; int downlevel = getLevel ( root . left , node , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; return getLevel ( root . right , node , level + 1 ) ; } static void printGivenLevel ( Node root , Node node , int level ) { if ( root == null level < 2 ) return ; if ( level == 2 ) { if ( root . left == node root . right == node ) return ; if ( root . left != null ) System . out . print ( root . left . data + " ▁ " ) ; if ( root . right != null ) System . out . print ( root . right . data + " ▁ " ) ; } else if ( level > 2 ) { printGivenLevel ( root . left , node , level - 1 ) ; printGivenLevel ( root . right , node , level - 1 ) ; } } static void printCousins ( Node root , Node node ) { int level = getLevel ( root , node , 1 ) ; printGivenLevel ( root , node , level ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 15 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . left . right = newNode ( 8 ) ; printCousins ( root , root . left . right ) ; } }
Leftmost and rightmost indices of the maximum and the minimum element of an array | Java implementation of above idea ; Function to return the index of the rightmost minimum element from the array ; First element is the minimum in a sorted array ; While the elements are equal to the minimum update rightMin ; Final check whether there are any elements which are equal to the minimum ; Function to return the index of the leftmost maximum element from the array ; Last element is the maximum in a sorted array ; While the elements are equal to the maximum update leftMax ; Final check whether there are any elements which are equal to the maximum ; Driver code ; First element is the leftmost minimum in a sorted array ; Last element is the rightmost maximum in a sorted array
public class GFG { public static int getRightMin ( int arr [ ] , int n ) { int min = arr [ 0 ] ; int rightMin = 0 ; int i = 1 ; while ( i < n ) { if ( arr [ i ] == min ) rightMin = i ; i *= 2 ; } i = rightMin + 1 ; while ( i < n && arr [ i ] == min ) { rightMin = i ; i ++ ; } return rightMin ; } public static int getLeftMax ( int arr [ ] , int n ) { int max = arr [ n - 1 ] ; int leftMax = n - 1 ; int i = n - 2 ; while ( i > 0 ) { if ( arr [ i ] == max ) leftMax = i ; i /= 2 ; } i = leftMax - 1 ; while ( i >= 0 && arr [ i ] == max ) { leftMax = i ; i -- ; } return leftMax ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 } ; int n = arr . length ; System . out . println ( " Minimum ▁ left ▁ : ▁ " + 0 ) ; System . out . println ( " Minimum ▁ right ▁ : ▁ " + getRightMin ( arr , n ) ) ; System . out . println ( " Maximum ▁ left ▁ : ▁ " + getLeftMax ( arr , n ) ) ; System . out . println ( " Maximum ▁ right ▁ : ▁ " + ( n - 1 ) ) ; } }
Elements to be added so that all elements of a range are present in array | java program for above implementation ; Function to count numbers to be added ; Sort the array ; Check if elements are consecutive or not . If not , update count ; Drivers code
import java . io . * ; import java . util . * ; public class GFG { static int countNum ( int [ ] arr , int n ) { int count = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; } static public void main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 8 , 6 } ; int n = arr . length ; System . out . println ( countNum ( arr , n ) ) ; } }
Find the k smallest numbers after deleting given elements | Java program to find the k maximum number from the array after n deletions ; Find k minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else push it in the min heap ; Print top k elements in the min heap ; Pop the top element ; Driver code
import java . util . * ; public class GFG { static void findElementsAfterDel ( int [ ] arr , int m , int [ ] del , int n , int k ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . containsKey ( del [ i ] ) ) { mp . put ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . put ( del [ i ] , 1 ) ; } } Vector < Integer > heap = new Vector < Integer > ( ) ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) mp . remove ( arr [ i ] ) ; } else heap . add ( arr [ i ] ) ; } Collections . sort ( heap ) ; for ( int i = 0 ; i < k ; ++ i ) { System . out . print ( heap . get ( 0 ) + " ▁ " ) ; heap . remove ( 0 ) ; } } public static void main ( String [ ] args ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . length ; int [ ] del = { 12 , 56 , 5 } ; int n = del . length ; int k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ; } }
Elements to be added so that all elements of a range are present in array | Java implementation of the approach ; Function to count numbers to be added ; Make a hash of elements and store minimum and maximum element ; Traverse all elements from minimum to maximum and count if it is not in the hash ; Drivers code
import java . util . HashSet ; class GFG { static int countNum ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; int count = 0 , maxm = Integer . MIN_VALUE , minm = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { s . add ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm ) maxm = arr [ i ] ; } for ( int i = minm ; i <= maxm ; i ++ ) if ( ! s . contains ( i ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 8 , 6 } ; int n = arr . length ; System . out . println ( countNum ( arr , n ) ) ; } }
Find minimum speed to finish all Jobs | Java program to find minimum speed to finish all jobs ; Function To findmax value in Array ; Function to check if the person can do all jobs in H hours with speed K ; Function to return the minimum speed of person to complete all jobs ; If H < N it is not possible to complete all jobs as person can not move from one element to another during current hour ; Max element of array ; Use binary search to find smallest K ; Driver Code ; Print required maxLenwer
class GFG { static int findmax ( int [ ] A ) { int r = A [ 0 ] ; for ( int i = 1 ; i < A . length ; i ++ ) r = Math . max ( r , A [ i ] ) ; return r ; } static boolean isPossible ( int [ ] A , int n , int H , int K ) { int time = 0 ; for ( int i = 0 ; i < n ; ++ i ) time += ( A [ i ] - 1 ) / K + 1 ; return time <= H ; } static int minJobSpeed ( int [ ] A , int n , int H ) { if ( H < n ) return - 1 ; int max = findmax ( A ) ; int lo = 1 , hi = max ; while ( lo < hi ) { int mi = lo + ( hi - lo ) / 2 ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 6 , 7 , 11 } ; int H = 8 ; int n = A . length ; System . out . println ( minJobSpeed ( A , n , H ) ) ; } }