Book allocation probl em

what’s is wrong in my code;----

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
long t = cin.nextLong(), n = cin.nextLong(),s = cin.nextLong();
long arr[] = new long[(int)n];
for(long i = 0 ;i<arr.length;i++){
arr[(int)i] = cin.nextLong();
}
while(t>0){
System.out.println(binarySearchBooks(arr,n,s));
t–;
}
cin.close();

}
static long binarySearchBooks(long books[],long n, long k){
    long totalPages = 0;
    long s = 0, e = 0;
    for(long i = 0;i<n;i++){
        totalPages += books[(int)i];
        s = Math.max(s,books[(int)i]);
    }
    e = totalPages;
    long finalAns = s;

    while(s<=e){
        long mid = (s+e)/2;
        if(isValidConfig(books,n,k,mid)){
            finalAns = mid;
            e = mid -1;
        }else{
            s = mid + 1;
        }
    }
    return finalAns;
}
static boolean isValidConfig(long books[], long n, long k, long ans){
    int students=1;
    long current_pages = 0;
    for(int i=0;i<n;i++){
        if(current_pages+books[i]>ans){
            current_pages = books[i];
            students++;
            if(students>k){
                return false;
            }else{
                current_pages += books[i];
            }
        }
    }
    return true;
}

}

@AbhishekAhlawat1102
I went through your code.
The logic was very correct However,it was incorrect at few places:

  1. You were taking only single array as input but in the problem test cases are given so there will be a different array for each test case.
    2.In the isvalid function you have to add everytime to current pages,you were only adding when the current pages exceeds the limit.
    The changed code is https://ide.codingblocks.com/s/108033
    Thankyou, please mark the doubt as resolved.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.