Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Design and analysis of algorithms / Practicum / Exam1 / 2-Shots.cpp
diff options
context:
space:
mode:
Commitf2ac1662504c72122bf9b77fe265095b58dab575 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-04-10 09:48:06 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-04-10 09:48:06 +0300
Files90030cef9c740ce895c61e50bd3726cec2944f4a /Year 2/Design and analysis of algorithms/Practicum/Exam1/2-Shots.cpp
uni-projects-f2ac1662504c72122bf9b77fe265095b58dab575.tar
uni-projects-f2ac1662504c72122bf9b77fe265095b58dab575.tar.gz
uni-projects-f2ac1662504c72122bf9b77fe265095b58dab575.zip
Parentfe5a64bc48dd8b440fe13883f47c8fe5938ba080 (diff)
[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.cpp31
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);
+}