Skip to content

Commit 84610e6

Browse files
committed
Added the Allocate Minimum No. of Pages.cpp
1 parent 6e01d61 commit 84610e6

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
//Problem Statement Link https://practice.geeksforgeeks.org/problems/allocate-minimum-number-of-pages0937/1
2+
3+
//Example Test Case
4+
//Input
5+
// n -> Size of Array
6+
// arr[] -> Array
7+
// m -> No. of Students
8+
/*
9+
10+
n = 4
11+
arr[] = {12,34,67,90}
12+
m = 2
13+
14+
Here possible allocations can be:
15+
16+
1. {12}, {34, 67, 90} i.e one student reads 12 pages and another
17+
reads 191 (34+67+91) pages, so here maximum is 191
18+
19+
2. {12,34}, {67,90} i.e one student reads 46 (12+34) pages and another
20+
reads 158 (67+91) pages, so here maximum is 158
21+
22+
3. {12,34, 67} i.e one student reads 113 (12+34+67) pages and another
23+
reads 91 pages, so here maximum is 113
24+
25+
Out of three cases minimum maximum is 113 so that would be printed in the output
26+
27+
*/
28+
29+
//The program for this problem goes as follows
30+
31+
//Header file to include all header files of C++
32+
#include <bits/stdc++.h>
33+
using namespace std;
34+
35+
class Solution
36+
{
37+
public:
38+
//Function to find minimum number of pages.
39+
int findPages(int arr[], int n, int m)
40+
{
41+
// we store the maximum of array in min i.e a student can read atleast
42+
// one book (with max pages) from the given array of books
43+
int min = findMax(arr, n);
44+
45+
//we store suma of all books in max variable
46+
int max = findSum(arr, n);
47+
48+
// ans is minimum of maximum pages that can be assigned to a student
49+
int ans = -1;
50+
51+
//Applying Binary Search
52+
while (min <= max)
53+
{
54+
int mid = (min + max) / 2;
55+
56+
//calling isFeaible function to check whether we can have mid as the
57+
//minimum maximum no. of pages possible
58+
59+
//If yes, assign mid to ans and check further with smaller values.
60+
if (isFeasible(arr, n, m, mid))
61+
{
62+
ans = mid;
63+
max = mid - 1;
64+
}
65+
66+
//If no, increment the min and check further with larger values
67+
else
68+
{
69+
min = mid + 1;
70+
}
71+
}
72+
73+
//return the final ans
74+
return ans;
75+
}
76+
77+
// A utility function to find sum of array elements
78+
static int findSum(int arr[], int n)
79+
{
80+
int sum = 0;
81+
for (int i = 0; i < n; i++)
82+
{
83+
sum += arr[i];
84+
}
85+
return sum;
86+
}
87+
88+
// A utility function to find max of all array elements
89+
static int findMax(int arr[], int n)
90+
{
91+
int max = arr[0];
92+
for (int i = 1; i < n; i++)
93+
{
94+
if (max < arr[i])
95+
max = arr[i];
96+
}
97+
return max;
98+
}
99+
100+
// A function to check wheter it is feasible to allocate 'mid' no of pages
101+
// to m students
102+
static bool isFeasible(int arr[], int n, int m, int mid)
103+
{
104+
int student = 1;
105+
int sum = 0;
106+
107+
//Iterating over the array elements
108+
for (int i = 0; i < n; i++)
109+
{
110+
//if the current sum is less than mid, add it into the sum variable
111+
if (sum + arr[i] <= mid)
112+
{
113+
sum += arr[i];
114+
}
115+
116+
//else increment the number of students and reassign sum with array element
117+
else
118+
{
119+
student++;
120+
sum = arr[i];
121+
}
122+
}
123+
124+
// finally return whether no of students are less than or equal to m
125+
return (student <= m);
126+
}
127+
};
128+
129+
// Main Function
130+
int main()
131+
{
132+
133+
// t -> No of Test Cases
134+
// n -> Size of Array
135+
// A -> Array
136+
// m -> No of Students
137+
int t;
138+
cin >> t;
139+
while (t--)
140+
{
141+
int n;
142+
cin >> n;
143+
int A[n];
144+
145+
//Input Array Elements
146+
for (int i = 0; i < n; i++)
147+
{
148+
cin >> A[i];
149+
}
150+
int m;
151+
cin >> m;
152+
153+
//making object of class
154+
Solution ob;
155+
156+
//calling findPages Method and displaying the final ans
157+
cout << ob.findPages(A, n, m) << endl;
158+
}
159+
return 0;
160+
}

0 commit comments

Comments
 (0)