PROBLEM LINK :
Setter: Chirag Thakur
Tester: Harsh Raj
Editorialist: Harsh Raj
DIFFICULTY:
Cakewalk
PREREQUISITES:
Basic Implementatio
EXPLAINATION:
For each turn, keep on drawing cards and store the points.
When there is no card left, print the diffrence of output.
TIME COMPLEXITY:
O(N)
SOLUTIONS:
Settler's Solution
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
void test_case() {
int n;
cin >> n;
vector<int> C(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> C[i];
}
int k = 0, p = 0;
int l = 1, r = n, turn = 1;
while (l <= r) {
if (turn) {
p += C[l++];
} else {
k += C[r--];
}
turn ^= 1;
}
cout << k - p << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
test_case();
return 0;
}
Tester's Solution
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
ll i,j,n,turn=0,ketu=0,petu=0;
cin>>n;
vector<int> c(n);
for(i=0;i<n;i++)
cin>>c[i];
i=0,j=n-1;
while(i<=j){//while there are elements remaining
if(turn%2==0){
petu+=c[i];
i++;
}
else{
ketu+=c[j];
j--;
}
turn+=1;
}
cout<<(ketu-petu)<<endl;
return 0;
}