Binary Search Book Allocation Problem

please help i am unable to figure out what is wrong in my code as it is not submitting on any platform and failing some test cases but working fine for some given cases.

//Book Allocation Problem

bool isPossible(int *arr, int n, int students, int currmin)

{

int st=1, sum=0;

for(int i=0; i<n; i++)
{

// if(arr[i]>currmin)

//return false;

if((sum+arr[i]) > currmin)
{
    st++;
    sum=arr[i];
    if(st>students)
    return false;
}
else
sum+=arr[i];

}

return true;

}

int findPages(int *arr, int n, int m)

{int sum=0;

if(n<m)

return -1;

sort(arr, arr+n);

for(int i=0; i<n; i++)

sum+=arr[i];

int start = arr[n-1];
int end = sum;
int ans=-1;
while(start<=end)
{int mid = start + (end-start)/2;
    if( isPossible(arr, n, m, mid) )
    {
        ans = mid;
        end = mid-1;
    }
    else
    start = mid+1;
}
return ans;

}

Hey @Rajat02
Please share your code in Coding blocks ide (ide.codingblocks.com)
save and share

ohh ok i will also mention the problem link there :+1:

  • Go to the above link
  • Paste ur code
  • Presss ctrl+ s and click save
  • paste the url here
1 Like

@Kartikkhariwal1


this is the code

Hey @Rajat02
read coments https://ide.codingblocks.com/s/405316

1 Like

@Kartikkhariwal1 Really Thanks alot!!! i was stuck in this question for 1 whole day and not getting whats wrong. Now i got to know that due to sort the ordering was changed only that was the error .
thanks again :relaxed:

1 Like

please send the link for feedback form!!

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.

1 Like

Done!!! :relaxed: :relaxed:

1 Like