#include <bits/stdc++.h>
using namespace std;
void BookAllocation(vector<int> &v, int n) {
int Sum = 0, sum = 0, k = 1;
vector MX(k);
for (int i = 0; i < n - 1; i++) {
sum += v[i];
for (int j = i + 1; j < n; i++) {
Sum += v[j];
}
k = max(Sum, sum);
MX.push_back(k);
k = 0;
Sum = 0;
}
int size = MX.size();
int Ans;
for (int i = 0; i < size; i++) {
Ans = min(MX[i], Ans);
}
cout << Ans;
}
int main() {
int n = 4;
vector v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
BookAllocation(v, n); // i'm just doing some book allocation question from leet code
return 0;
}
Asked
Active
Viewed 68 times
-1

cigien
- 57,834
- 11
- 73
- 112

Rohan Gupta
- 7
- 1
-
Well after the edits were made for you, we can see there's more than one loop in that code. _@rohan_ can you please [edit] and clarify? It seems you have been caught up by typos made in your code, and such questions are considered off-topic here. – πάντα ῥεῖ Jan 22 '22 at 14:55
-
1See [Why should I not #include
?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Jan 22 '22 at 16:24
2 Answers
1
Your program will never end due to this loop:
for (int j = i + 1; j < n; i++) {
Sum += v[j];
}
Neither j
or n
are updated if true the first time. Thus endless loop.
Maybe you wanted:
for (int j = i + 1; j < n; ++j) {
Sum += v[j];
}

Bo R
- 2,334
- 1
- 9
- 17
-
You appear to have formatted the OP's code yourself. If so, it would be really helpful if you edited the question to format the code there as well. You are not required to do so, but it would improve the overall quality of the question considerably. – cigien Jan 22 '22 at 14:45
-
I hope you flagged this question for closure, after realizing it's a typo? – πάντα ῥεῖ Jan 22 '22 at 14:45
-
It's not just a typo. The program will not compile without telling type for the container `vector??>`. It's unlikely that the compiled code version was pasted here. – Bo R Jan 22 '22 at 14:54
-
1Actually, if you look at the [source](https://stackoverflow.com/revisions/1ff6bf0d-9aff-4c05-b3d1-a6a59d5ef89a/view-source) of revision 1, OP does have `
` specified in the function signature. It just didn't show up because of the formatting. As such, this question appears to be a typo, so it is preferable (as already mentioned by another user) to vote to close the question as a typo, rather than answer it. – cigien Jan 22 '22 at 14:58 -
-1
please try this
#include <iostream>
#include <vector>
void BookAllocation(std::vector<int> &v, int n) {
int Sum = 0, sum = 0, k = 1;
std:: vector<int> MX(k);
for (int i = 0; i < n - 1; i++) {
sum += v.at(i);
for (int j = i + 1; j < n; j++) {//notce j++ as @BO R mentioned
Sum += v.at(j);
}
k = max(Sum, sum);
MX.push_back(k);
k = 0;
Sum = 0;
}
int size = (int) MX.size(); // needs explicit cast because by default it returns uint
int Ans;
for (int i = 0; i < size; i++) {
Ans = min(MX.at(i), Ans);
}
std::cout << Ans;
}
int main() {
int n = 4;
std::vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v.at(i);
}
BookAllocation(v, n); // i'm just doing some book allocation question from leet code
return 0;
}

Abdul Rauf
- 67
- 6
-
2This won't compile at any state of the art c++ compiler. Please don't advise defitely wrong things like `#include
#include – πάντα ῥεῖ Jan 22 '22 at 16:04using namespace std;` -
1. https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h 2. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice 3. https://stackoverflow.com/questions/6729428/cannot-open-include-file-vector-h-no-such-file-or-directory/20540960 – πάντα ῥεῖ Jan 22 '22 at 16:12
-
thanks man I am new to stack overflow and i just copy pasted his code in hurry fixing issues i found and since I didn't test it I happen to have this vector.h extremely sorry may i edit it now ? – Abdul Rauf Jan 22 '22 at 16:59
-
Yes, please go ahead and improved your answer. You don't need permission to do that :) – cigien Jan 23 '22 at 23:38