Answers
Detailed solutions and code implementations for Competitive Programming II.
1. 230B - T-primes
#include <bits/stdc++.h>
using namespace std;
bool is_prime(long long x)
{
if (x < 2)
return false;
for (long long i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}
void solve()
{
long long n;
cin >> n;
long long root = sqrt(n);
if (root * root == n && is_prime(root))
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}2. 2218A - The 67th Integer Problem
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int x;
cin >> x;
if (x < 67)
cout << x + 1 << '\n';
else
cout << x << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}3. 2218E - The 67th XOR Problem
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
int ans = 0;
vector<int> arr(n);
for (int &x : arr)
cin >> x;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
ans = max(ans, arr[i] ^ arr[j]);
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}4. Minimum Changes to Make an Alternating Binary String
Count mismatches against 010101... and 101010.... Print the minimum.
#include <bits/stdc++.h>
using namespace std;
void solve()
{
string s;
cin >> s;
int c1 = 0, c2 = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] != (i % 2 ? '1' : '0'))
c1++;
if (s[i] != (i % 2 ? '0' : '1'))
c2++;
}
cout << min(c1, c2) << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}5. Maximum Array Sum After Removing at Most One Element
Calculate total sum. Remove the minimum element only if it is negative.
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
long long sum = 0;
long long mn = LLONG_MAX;
while (n--)
{
long long x;
cin >> x;
sum += x;
mn = min(mn, x);
}
if (mn < 0)
sum -= mn;
cout << sum << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}6. Count Segments of Consecutive Equal Characters
Start with one segment. Every character different from the previous character starts a new segment.
#include <bits/stdc++.h>
using namespace std;
void solve()
{
string s;
cin >> s;
int cnt = 1;
for (int i = 1; i < s.size(); i++)
if (s[i] != s[i - 1])
cnt++;
cout << cnt << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}7. Count Zero-Sum Subarrays
Store the frequency of each prefix sum. If the current prefix sum appeared before, every previous occurrence forms one zero-sum subarray.
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
unordered_map<long long, long long> mp;
mp[0] = 1;
long long pref = 0, ans = 0;
while (n--)
{
long long x;
cin >> x;
pref += x;
ans += mp[pref];
mp[pref]++;
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}8. TODO List Using an Array
#include <bits/stdc++.h>
using namespace std;
vector<string> arr;
void show_task()
{
for (auto s : arr)
cout << s << " ";
cout << '\n';
}
void solve()
{
int n;
cin >> n;
string s;
if (n == 1)
{
cin >> s;
arr.push_back(s);
}
else if (n == 2)
{
cin >> s;
auto it = find(arr.begin(), arr.end(), s);
arr.erase(it);
}
else if (n == 3)
{
if (arr.empty())
cout << "no task" << '\n';
else
show_task();
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--)
solve();
return 0;
}9. 977A - Wrong Subtraction
#include <bits/stdc++.h>
using namespace std;
void solve()
{
long long n;
int k;
cin >> n >> k;
while (k--)
{
if (n % 10 == 0)
n /= 10;
else
n--;
}
cout << n << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}10. 734A - Anton and Danik
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
string s;
cin >> n >> s;
int count_a = 0, count_d = 0;
for (char c : s)
{
if (c == 'A')
count_a++;
else if (c == 'D')
count_d++;
}
if (count_a > count_d)
cout << "Anton" << '\n';
else if (count_d > count_a)
cout << "Danik" << '\n';
else
cout << "Friendship" << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}11. 158B - Taxi
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for (int &x : a)
cin >> x;
sort(a.begin(), a.end());
int l = 0, r = n - 1, cnt = 0;
while (l <= r)
{
int s = 4 - a[r];
r--;
while (l <= r && a[l] <= s)
{
s -= a[l];
l++;
}
cnt++;
}
cout << cnt;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}