diff options
[DAA/E1] Added work from first exam from yesterday
1 files changed, 31 insertions, 0 deletions
| -rw-r--r-- | Year 2/Design and analysis of algorithms/Practicum/Exam1/2-Shots.cpp | 31 |
diff --git a/Year 2/Design and analysis of algorithms/Practicum/Exam1/2-Shots.cpp b/Year 2/Design and analysis of algorithms/Practicum/Exam1/2-Shots.cpp new file mode 100644 index 0000000..24dadf9 --- /dev/null +++ b/Year 2/Design and analysis of algorithms/Practicum/Exam1/2-Shots.cpp @@ -0,0 +1,31 @@ +#include <cstdio> +#include <cmath> +#include <vector> +#include <algorithm> +using namespace std; + +int main() { + int N = 0; + scanf("%d", &N); + vector<long> m; + long temp = 0; + for (int i = 0; i < N; i++) { + scanf("%ld", &temp); + m.push_back(temp); + } + + int l = 0, r = N - 1; + long lalc = 0, ralc = 0; + long tot = 0; + while (l <= r) { + if (lalc < ralc) + lalc += m[l++]; + else + ralc += m[r--]; + if (lalc == ralc) { + tot = (l - 1 - r) + N; + } + } + + printf("%ld", tot); +} |
