Minimum days to make Array elements with value at least K sum at least X | Java program for the above approach ; Function to find the minimum number of days such that the sum of array elements >= K is at least X ; Initialize the boundaries of search space ; Perform the binary search ; Find the value of mid ; Traverse the array , arr [ ] ; Find the value of arr [ i ] after mid number of days ; Check if temp is not less than K ; Update the value of sum ; Check if the value of sum is greater than X ; Update value of high ; Update the value of low ; Print the minimum number of days ; Driver Code
import java . io . * ; class GFG { static void findMinDays ( int arr [ ] , int R [ ] , int N , int X , int K ) { int low = 0 , high = X ; int minDays = - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int temp = arr [ i ] + R [ i ] * mid ; if ( temp >= K ) { sum += temp ; } } if ( sum >= X ) { minDays = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } System . out . println ( minDays ) ; } public static void main ( String [ ] args ) { int X = 100 , K = 45 ; int arr [ ] = { 2 , 5 , 2 , 6 } ; int R [ ] = { 10 , 13 , 15 , 12 } ; int N = arr . length ; findMinDays ( arr , R , N , X , K ) ; } }