Introduction batch
Introduction To The Team
Saturday, 31 October 2026, 3:00 pm – 5:00 pm IST
5006 - CC3
CodePlus Senior Members Team
This is the first class where you will be introduced to the CodePlus team and competitive programming. We will also address your doubts regarding careers in competitive programming and competitions like ICPC.
Class notes
Welcome to CodePlus! The slides from today are above. If you're starting from scratch, don't worry: nothing below is needed for the next class. These are just two small ideas that show why competitive programming is so much fun.
Nim: a game you can solve with one operator
Nim is a two-player game. There are a few piles of stones. On your turn, you pick one pile and remove any number of stones from it (at least one). Whoever takes the last stone wins.
Try it with a friend on piles of 3, 4 and 5. It feels like a game of guessing, but it isn't. If both players play perfectly, the winner is decided before the first move.
The trick is XOR (^ in C++). XOR all the pile sizes together. This total is called the nim-sum.
| Pile | Binary |
|---|---|
| 3 | 011 |
| 4 | 100 |
| 5 | 101 |
| XOR of all three | 010 = 2 |
- If the nim-sum is not 0, the first player can always win.
- If the nim-sum is 0, the second player can always win.
Why? Two facts do all the work:
- From a position with nim-sum 0, every move makes it non-zero.
- From a position with a non-zero nim-sum, there is always a move that makes it 0.
So the player who is handed a non-zero position can keep giving their opponent a nim-sum of 0. The final position, with no stones left, also has a nim-sum of 0, and it's the opponent who's stuck with it.
In our example the nim-sum is 2, so the first player wins. The winning move is to turn the pile of 3 into a pile of 1, because 1 ^ 4 ^ 5 = 0. Whatever your opponent does next, you can bring the nim-sum back to 0.
The whole game fits in a few lines of code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int nimSum = 0;
for (int i = 0; i < n; i++) {
int pile;
cin >> pile;
nimSum ^= pile;
}
cout << (nimSum != 0 ? "First player wins" : "Second player wins") << "\n";
}
Want to try it? Solve Nim Game I on CSES. To go deeper, read Sprague-Grundy theory on cp-algorithms, but that's for much later.
Binary search: halve the problem
Think of a number from 1 to 100. Guessing 1, 2, 3… could take 100 tries. A better plan is to guess 50 and ask "higher or lower?". Each answer throws away half of the numbers that are left.
| Numbers to search | Guesses one by one (worst case) | Guesses with halving (worst case) |
|---|---|---|
| 100 | 100 | 7 |
| 1,000,000 | 1,000,000 | 20 |
| 1,000,000,000 | 1,000,000,000 | 30 |
That's the power of O(log n): a thousand times more data only adds about 10 more steps.
The one requirement is that the data must be sorted, or at least have a clear yes/no boundary. Here's the classic version, which finds target in a sorted array:
int search(vector<int>& a, int target) {
int lo = 0, hi = (int)a.size() - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2; // safer than (lo + hi) / 2
if (a[mid] == target) return mid;
if (a[mid] < target) lo = mid + 1; // answer is on the right
else hi = mid - 1; // answer is on the left
}
return -1; // not found
}
It looks simple, but binary search is famous for off-by-one bugs: < or <=? mid or mid + 1? We'll sort that out properly in the next class, Binary Search Foundations, and see how the same idea solves problems that have nothing to do with arrays.
Read ahead if you like: Binary search on cp-algorithms.
Before the next class
- Get C++ running on your laptop. The Resources page has setup guides.
- Make an account on Codeforces if you don't have one.
- Stuck on anything? Ask any of us.
See you in class!
Feedback