classSolution{publicintfindPages(int[]arr,intk){if(k>arr.length)return-1;// code hereintmin=arr[0];intmax=0;for(inti:arr){max+=i;min=Math.max(min,i);// min should be such that it is the max vaule of the arr //othewise if arr[i] > min then that book can not be assigned to a single //student and we can not partially assign 1 book to 2 students}intresult=-1;while(min<=max){intmid=(min+max)/2;if(isPossible(mid,arr,k)){result=mid;max=mid-1;}elsemin=mid+1;}returnresult;}publicbooleanisPossible(intminPage,int[]arr,intk){intnoOfStudents=1;//one student will satisfy the condition intcurrentPage=0;for(intpages:arr){//per student max pages(currentPage+pages) assigned should be less than or equal to minPage else we will to assign// that pages(i.e arr[i] to the next student) if(pages+currentPage>minPage){currentPage=0;//reset the currentPage for the new studentnoOfStudents++;}currentPage+=pages;// currentPage will initialize with the current ith page i.e pages here }returnnoOfStudents<=k;}}