Hesre again same case

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int nob=sc.nextInt();
int nos=sc.nextInt();
int []page=new int[nob];
for(int i=0;i<page.length;i++) {
page[i]=sc.nextInt();
}
System.out.println(no_of_page(page,nos));
sc.close();
}
public static int no_of_page(int []page,int nos) {
int low=0;
int high=0;
for(int i=0;i<page.length;i++) {
high=high+page[i];
}
int ans=0;
while(low<=high) {
int mid=(low+high)/2;
if(itispossible(page,nos,mid)) {
ans=mid;
high=mid-1;
}
else {
low=mid+1;
}
}
return ans;
}
public static boolean itispossible(int []page,int nos,int mid) {
int student=1;
int read_page=0;
int i=0;
while(i<page.length) {
if(page[i]+read_page<=mid) {
read_page=read_page+page[i];
i++;
}
else {
student++;
read_page=0;
}
if(student>nos) {
return false;
}
}
return true;
}
}
my output of the program is correct but all the test case is wrong.

Hello subham
Please take your ans variagle as long as the ans can also exceed int if the data is too large . You can also find this context in question constrain

by using long i am still getting error check my code properly

Shubham Please share your code with me using the coding blocks IDE so that I can run your code and find out the exact error.

i have shared the code

Hello Subham I have made some changes to your logic like using long instead of int and intializing your lo variable with the element present at arr.length -1 so that the exact mid value will come . Please give a read to below code and let me know if you still face any problem.

public static long min(long[] arr, long n, long k) {
long lo = arr[arr.length - 1];
long totalpages = 0;
long hi = 0;
for (int i = 0; i < n; i++) {
totalpages += arr[i];
}
hi = total_pages;
long ans = 0;
while (lo <= hi) {
long mid = (lo + hi) / 2;
boolean isvalid = isValid(arr, k, mid);
if (isvalid) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
public static boolean isValid(long[] arr, long k, long mid) {
long sum = 0;
int noOfstudents = 1;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
if (sum > mid) {
noOfstudents++;
sum = arr[i];
if (noOfstudents > k) {
return false;
}
}
}
return true;
}