Tutorial for GDG CC Wing Selection 2026

Revision en11, by Kunal_Khandelwal, 2026-08-10 19:18:17

Problem A : Tarun and the Cannon Range
Author : Casual_W

Solution
Code

Problem B : Aditya's Apple Tree
Author : Casual_W

Hint 1
Hint 2
Solution
Code

Problem C : Dewansh and the MEX Game
Author : Godbot

Solution
Code

Problem D : Jai, Tarun, and the Parentheses Game
Author : Godbot

Solution
Code

Problem E : Dewansh and the Growing Digits
Author : Godbot <spoiler summary="Hint" Digits are independent to each other and order does not matter

Define dp[i][j] length of digit i if i do j operations on it Transitions : dp[i][j] = dp[i+1][j-1] when 0<=i<=8 = dp[0][j-1] + dp[1][j-1] when i==9 because when i==9 and u do one more operation then it will be 10 so ad the contribution of those

Precompute this otherwise TC will be m*t which will TLE After precomputing dp[i][j] just multiply it with the number of its occcurences in the given number n

include<bits/stdc++.h>

using namespace std;

define MOD 1000000007

define MOD1 998244353

define pb push_back

typedef long long ll; typedef unsigned long long ull; typedef long double lld; ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} const int nMax = 2 * 1e5 + 10;

ll dp[10][nMax]; void solve() { int t; cin >> t; for (int i = 0; i <= 9; i++) { dp[i][0] = 1; } for (int i = 1; i < nMax; i++) { for (int j = 0; j <= 8; j++) { dp[j][i] = dp[j + 1][i — 1]; } dp[9][i] = mod_add(dp[0][i — 1], dp[1][i — 1], MOD); } while (t--) { int n, m; cin >> n >> m; vector occ(10, 0); while (n > 0) { occ[n % 10]++; n /= 10; } ll ans = 0; for (int i = 0; i < 10; i++) { ll x = dp[i][m]; ll y = occ[i]; ans = mod_add(mod_mul(x, y, MOD), ans, MOD); } cout << ans << endl; } }

int main() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); solve(); }

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(), v.end()
#define loop(i, n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
#define vin(a) for (int i = 0; i < (a).size(); i++) cin >> a[i];
#define sz(x) ((int)(x).size())

signed main() {
    ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    int t=1;
    // cin >> t;
    while (t--) {
        int n; cin >> n;
        vi a(n);
        vin(a);
        sort(all(a),greater<int>());
        int ans=0;
        for(int i=0;i<n;i+=2) ans+=(a[i]-a[i+1]);
        cout<<ans<<endl;
        
    }
    return 0;
}

Problem G : Aditya and the Magic Powder
Author : Godbot

Is the function $$$f(M) = \text{"Can we bake } M \text{ cookies?"}$$$ monotonic?

Binary Search

spoiler

To bake $$$M$$$ cookies, the $$$i$$$-th ingredient requires a total of $$$M \cdot a_i$$$ grams.Aditya already possesses $$$b_i$$$ grams of the $$$i$$$-th ingredient. Therefore, the deficit (extra amount needed) for ingredient $$$i$$$ is:

$$$\text{Deficit}_i(M) = \max\left(0, M \cdot a_i - b_i\right)$$$$$$The total amount of magic powder required across all $$$n$$$ ingredients to bake $$$M$$$ cookies is:$$$$$$P(M) = \sum_{i=1}^{n} \max\left(0, M \cdot a_i - b_i\right)$$$$$$Aditya can bake $$$M$$$ cookies if and only if the total magic powder required does not exceed his available supply $$$k$$$:$$$$$$P(M) \le k$$$

Monotonicity:If we can bake $$$x$$$ cookies, we can also bake $$$x - 1$$$ cookies. The predicate function $$$f(x) = \text{"Can bake } x \text{ cookies?"}$$$ forms a monotonic pattern: $$$\text{TTTT}\dots\text{FFFF}\dots$$$. We simply binary search for the rightmost $$$\text{True}$$$ to get the maximum possible cookies.

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin(), v.end()
#define loop(i, n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
#define vin(a) for (int i = 0; i < (a).size(); i++) cin >> a[i];
#define pb push_back
#define sz(a) (int)a.size()
bool good(vi& a, vi& b, int mid, int k) {
    int n = sz(a);
    int dec = 0;
    loop(i, n) {
        int req = a[i] * mid;
        if (req > b[i]) {
            dec += (req &mdash; b[i]);
        }
        if (dec > k) return 0;
    }
    return 1;
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    int t=1;
    // cin >> t;
    while (t--) {
        int n,k; cin >> n >> k;
        vi a(n),b(n);
        vin(a);vin(b);
        // Binary search -> ttttfff.. <- pattern
        int l=0,r=2e9;
        int ans=0;
        while(l<=r){
            int m = l+(r-l)/2;
            if(good(a,b,m,k)) ans=max(ans,m),l=m+1;
            else r=m-1;
        }
        cout<<ans<<endl;

    }
    return 0;
}

Problem H : Tarun and the rolling circle
Author : Its_Tarun

In 1 step, we can move the center by any distance $$$x \in (0, 2r]$$$ by choosing an appropriate rotation angle $$$\theta = 2 \arcsin\left(\frac{x}{2r}\right)$$$.Therefore, if $$$d \% 2r \neq 0$$$, we take $$$\lfloor \frac{d}{2r} \rfloor$$$ steps of length $$$2r$$$ plus 1 smaller step of length $$$d \bmod 2r$$$, totaling $$$\lceil \frac{d}{2r} \rceil$$$ steps.

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define loop(i, n) for (int i = 0; i < n; i++)
#define print_vec(a) for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
#define endl '\n'
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int r, x, y, xf, yf;
    cin >> r >> x >> y >> xf >> yf;
    int dx = xf - x;
    int dy = yf - y;
    
    int d2 = dx*dx + dy*dy;
    double d = sqrtl(d2);

    int ans = ceil(d/(2.0*r));
    
    cout << ans << "\n";


    return 0;
}

Problem I : Dewansh and the Secret Numbers
Author : JAS1123

$$$\begin{aligned} a &= S - (b + c) = x_4 - x_3 \\ b &= S - (a + c) = x_4 - x_2 \\ c &= S - (a + b) = x_4 - x_1 \end{aligned}$$$
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve() {
    vector<long long> x(4);
    for (int i = 0; i < 4; ++i) {
        cin >> x[i];
    }
    sort(x.begin(), x.end());

    // S = x[3] = a + b + c
    long long a = x[3] - x[2];
    long long b = x[3] - x[1];
    long long c = x[3] - x[0];

    cout << a << " " << b << " " << c << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    solve();
    
    return 0;
}

Problem J : Jai's Permutation
Author : JAS1123

We construct the array starting with $$$P_1 = 1$$$. Each subsequent term $$$P_i$$$ ($$$1 \lt i \le N$$$) is formed by alternatingly applying step sizes (jumps) of $$$N$$$ and $$$N+2$$$:

$$$P_i = \vert{}N - P_{i-1}\vert{}$$$ if $$$i$$$ is odd,
$$$P_i = \vert{}(N + 2) - P_{i-1}\vert{}$$$ if $$$i$$$ is even.

Modulo $$$(N+1)$$$, notice that:
$$$N \equiv -1 \pmod{N+1}$$$
$$$N+2 \equiv +1 \pmod{N+1}$$$

2. Jump Sum Analysis ($$$L$$$):

For a contiguous block of length $$$k$$$, the jump sum $$$L_k$$$ alternates between terms of $$$N$$$ and $$$N+2$$$. The possible jump sums $$$L$$$ take two forms:

$$$\text{Type 1 (starts with } N \text{)}: N + (N+2) + N + (N+2) + \dots$$$
$$$\text{Type 2 (starts with } N+2 \text{)}: (N+2) + N + (N+2) + N + \dots$$$

For example, when $$$k = 3$$$:
$$$\text{Type 1: } N + (N+2) + N = 3N + 2$$$
$$$\text{Type 2: } (N+2) + N + (N+2) = 3N + 4$$$

Let L be the above value

3. Parity & Impossibility Proof:

Let's analyze $$$L$$$ and $$$N+1$$$ based on the parity of $$$N$$$:

It will be different in both the cases either n = even or odd which implies L%(N+1) can never be equals to zero

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define loop(i, n) for (int i = 0; i < n; i++)
#define print_vec(a) for (int i = 0; i < a.size(); i++) cout << a[i] << " ";
#define endl '\n'
signed main() {
    int t=1;
    // cin >> t;
    while (t--) {
        int n; cin >> n;
        int s = (n*(n+1))/2;
        if(s%(n+1)){
            vi ans(n);
            ans[0] = 1;
            for(int i=1;i<n;i++){
                if(i&1) ans[i] = abs(n-ans[i-1]);
                else ans[i] = abs(n+2-ans[i-1]);
            }
            print_vec(ans);
            cout<<endl;
        }else cout<<-1<<endl;

    }
    return 0;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en22 English Kunal_Khandelwal 2026-08-12 07:51:07 676
en21 English Kunal_Khandelwal 2026-08-10 21:08:03 0 (published)
en20 English Kunal_Khandelwal 2026-08-10 21:07:10 10
en19 English Kunal_Khandelwal 2026-08-10 21:04:25 525
en18 English Kunal_Khandelwal 2026-08-10 20:57:32 95
en17 English Kunal_Khandelwal 2026-08-10 20:51:45 2485
en16 English Kunal_Khandelwal 2026-08-10 20:16:36 1171
en15 English Kunal_Khandelwal 2026-08-10 19:31:18 191
en14 English Kunal_Khandelwal 2026-08-10 19:25:35 208
en13 English Kunal_Khandelwal 2026-08-10 19:23:31 55
en12 English Kunal_Khandelwal 2026-08-10 19:21:58 913
en11 English Kunal_Khandelwal 2026-08-10 19:18:17 8819
en10 English Kunal_Khandelwal 2026-08-10 14:09:31 5010
en9 English Kunal_Khandelwal 2026-08-10 12:38:19 5837
en8 English Kunal_Khandelwal 2026-08-09 19:26:58 20
en7 English Kunal_Khandelwal 2026-08-09 19:25:06 2602
en6 English Kunal_Khandelwal 2026-08-09 18:17:22 5383
en5 English Kunal_Khandelwal 2026-08-09 14:27:06 3030
en4 English Kunal_Khandelwal 2026-08-09 13:45:39 1295
en3 English Kunal_Khandelwal 2026-08-09 13:37:55 954
en2 English Kunal_Khandelwal 2026-08-09 13:32:03 27700 Tiny change: '\n\n~~~~~\n#' -> '\n~~~~~\n#'
en1 English Kunal_Khandelwal 2026-08-09 12:48:47 546 Initial revision (saved to drafts)