Skip to content

Commit f3e2eed

Browse files
solved on 02/10/23
1 parent 3c51a7a commit f3e2eed

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

GFG_BookAllocationProblem.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int findPages(int A[], int N, int M)
5858
int main()
5959
{
6060
int t;
61+
cout << "Enter the number of test cases: ";
6162
cin >> t;
6263
while (t--)
6364
{

GFG_PaintersPartitionProblem.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
bool isPossibleSol(int a[], int n, int k, long long mid)
5+
{
6+
long long timeSum = 0;
7+
int cnt = 1;
8+
for (int i = 0; i < n; i++)
9+
{
10+
if (a[i] > mid)
11+
{
12+
return false;
13+
}
14+
if (a[i] + timeSum > mid)
15+
{
16+
cnt++;
17+
timeSum = a[i];
18+
if (cnt > k)
19+
return false;
20+
}
21+
else
22+
{
23+
timeSum += a[i];
24+
}
25+
}
26+
return true;
27+
}
28+
long long minTime(int arr[], int n, int k)
29+
{
30+
// code here
31+
// return minimum time
32+
long long start = 0, end = 0;
33+
for (int i = 0; i < n; i++)
34+
{
35+
end += arr[i];
36+
}
37+
long long ans = -1;
38+
39+
while (start <= end)
40+
{
41+
long long mid = start + (end - start) / 2;
42+
if (isPossibleSol(arr, n, k, mid))
43+
{
44+
ans = mid;
45+
end = mid - 1;
46+
}
47+
else
48+
{
49+
start = mid + 1;
50+
}
51+
}
52+
return ans;
53+
}
54+
55+
int main()
56+
{
57+
int t;
58+
cout << "Enter the number of test cases: ";
59+
cin >> t;
60+
while (t--)
61+
{
62+
int k, n;
63+
cin >> k >> n;
64+
int a[n];
65+
for (int i = 0; i < n; i++)
66+
cin >> a[i];
67+
cout << minTime(a, n, k) << endl;
68+
}
69+
return 0;
70+
}

0 commit comments

Comments
 (0)