Skip to content

Commit

Permalink
efficient approch to find minimum alloction of page to the student
Browse files Browse the repository at this point in the history
  • Loading branch information
Raushan-Kumar007 committed Jun 22, 2022
1 parent a419362 commit eed13fd
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion Searching/AllocatedMinimumPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class AllocatedMinimumPage {

// Naive APPROCH--------->>>>> THIS APPROCH IS VERY SLOW TIME COMPLEXITY IS EXPONENTIAL

static int minPage(int arr[],int n, int k){
/* static int minPage(int arr[],int n, int k){
if(k==1)
return sum(arr,0,n-1);
if(n==1)
Expand All @@ -21,6 +21,43 @@ static int sum(int arr[], int b, int e){
s += arr[i];
}
return s;
}*/

// Efficient approch to find aloction of minimum page using binaray search with time complexity is BigO(n*log(sum-max))

static int minPage(int arr[], int n,int k){
int sum = 0;
int max = 0;
for(int i=0;i<n;i++){
sum += arr[i];
max = Math.max(max,arr[i]);
}
int low = max;
int high = sum;
int res = 0;
while(low<=high){
int mid = (low+high)/2;
if(isfavrable(arr,n,k,mid)){
res=mid;
high=mid-1;
}else{
low = mid+1;
}
}
return res;
}
static boolean isfavrable(int arr[],int n,int k, int ans){
int req = 1;
int sum = 0;
for(int i=0;i<n;i++){
if(sum+arr[i]>ans){
req++;
sum = arr[i];
}else{
sum += arr[i];
}
}
return (req<=k);
}
public static void main(String[] args) {
int arr[]={10,20,10,30};
Expand Down

0 comments on commit eed13fd

Please sign in to comment.