Untitled

 avatar
bruteCoder
java
a month ago
898 B
1
Indexable
Never

class Solution {
    static int splitArray(int[] arr , int N, int K) {

        int min = 0;
        int max = 0 ;
        
        for(int el : arr){
            min = Math.max(el,min);
            max += el;
        }
        
       int mid  = 0;
       
       while(min <= max)
       {
           mid  = (min+max)/2;
           
           if(check(mid,arr,K)){
               max = mid-1;
           }else {
               min = mid +1;
           }
       }
       
       return min;
    }
    
    static boolean check(int maxSum , int[] arr, int k)
    {
        int count = 1;
        int sum = 0 ;
        for(int el : arr)
        {
            if(sum+el <= maxSum) {
                sum +=el;
            }else {
                sum = el ;
                count +=1;
            }
            
            if(count > k) return false;
        }
        
        return true;
    }
};
Leave a Comment