SirKadirov009's blog

By SirKadirov009, history, 36 hours ago, In English

The Trick That Made Me Stop Fearing "Maximize the Minimum" Problems (Binary Search on the Answer)

Difficulty: beginner to intermediate. Reading time: about 6 minutes.

Binary search depiction
Binary search halving the search range at every step. Image: "Binary Search Depiction" by Wikimedia Commons (CC BY-SA).

Have you ever opened a problem and seen this?

"Maximize the minimum distance between any two cows."

You stare at it. You think about DP, greedy, sorting, and nothing clicks. Then someone in the comments writes "just binary search on the answer" and you feel like everyone knew a secret except you.

This post is that secret, explained so you can use it today.

The big idea

Normally we binary search in an array. Here we binary search on the answer itself.

Instead of asking "What is the best answer?" (hard), ask:

"Is answer X achievable?" (often easy!)

If the answer to that yes/no question is monotonic (all "yes" up to some point, then all "no", or vice versa), you can binary search over X.

X:      1  2  3  4  5  6  7  8  9
check:  Y  Y  Y  Y  Y  N  N  N  N
                    ^
              the best answer

Example: Aggressive Cows

You have

Unable to parse markup [type=CF_MATHJAX]

$ stalls at positions

Unable to parse markup [type=CF_MATHJAX]

$ and

Unable to parse markup [type=CF_MATHJAX]

$ cows. Place the cows so the minimum distance between any two cows is as large as possible.

Step 1: Flip the question. Can we place all

Unable to parse markup [type=CF_MATHJAX]

$ cows so every pair is at least

Unable to parse markup [type=CF_MATHJAX]

$ apart?

Step 2: Check greedily. Sort the stalls. Put the first cow in the first stall, then put each next cow in the first stall at least

Unable to parse markup [type=CF_MATHJAX]

$ away from the previous one. If we place

Unable to parse markup [type=CF_MATHJAX]

$ cows,

Unable to parse markup [type=CF_MATHJAX]

$ is feasible.

Step 3: Check monotonicity. If

Unable to parse markup [type=CF_MATHJAX]

$ works, then any smaller distance also works. So the feasible values form a clean prefix, which means we can binary search.
#include <bits/stdc++.h>
using namespace std;

int n, k;
vector<long long> a;

bool can(long long d) {
    int cnt = 1;
    long long last = a[0];
    for (int i = 1; i < n; i++) {
        if (a[i] — last >= d) {
            cnt++;
            last = a[i];
        }
    }
    return cnt >= k;
}

int main() {
    cin >> n >> k;
    a.resize(n);
    for (auto &x : a) cin >> x;
    sort(a.begin(), a.end());

    long long lo = 1, hi = a.back() — a.front(), ans = 0;
    while (lo <= hi) {
        long long mid = lo + (hi — lo) / 2;
        if (can(mid)) {
            ans = mid;
            lo = mid + 1;
        } else {
            hi = mid — 1;
        }
    }
    cout << ans << "\n";
}

Complexity:

Unable to parse markup [type=CF_MATHJAX]

$, where

Unable to parse markup [type=CF_MATHJAX]

$ is the maximum coordinate range. That is fast enough for

Unable to parse markup [type=CF_MATHJAX]

$ up to

Unable to parse markup [type=CF_MATHJAX]

$ or more.

The 3-question checklist

Before using this trick, ask yourself:

  1. Is the answer a number in a bounded range? (time, distance, size, cost...)
  2. Can I write a check(x) that returns yes/no?
  3. Is check monotonic? If x works, does x-1 (or x+1) also work?

If all three are yes, you've probably found a binary search problem.

Common mistakes (I've made all of them)

  • Infinite loops. Use mid = lo + (hi — lo) / 2 and be careful whether you move lo = mid + 1 or hi = mid.
  • Overflow. Upper bounds like

    Unable to parse markup [type=CF_MATHJAX]

    $ break int. Use long long.
  • Wrong bounds. If hi is too small, you silently get the wrong answer. When in doubt, make it bigger. It only costs a few extra iterations.
  • Non-monotonic checks. If your check isn't monotonic, binary search will lie to you. Test with small cases.

Practice problems

Final thoughts

The scariest-looking optimization problems often become easy once you stop asking "what's the best answer?" and start asking "is this answer possible?" That one shift in thinking took me from stuck to solving a whole new class of problems.

Your turn: what's the coolest problem you've solved with this trick? Drop it in the comments, and if this helped, an upvote helps others find it!

Full text and comments »

  • Vote: I like it
  • -20
  • Vote: I do not like it

By SirKadirov009, history, 7 months ago, In English

Codeforces Round (Div. 2) Experience

A journey through algorithms, debugging, and fast thinking.

Competitive Programming

The Contest Atmosphere

Every Codeforces Round feels like entering a battlefield of logic and speed. Thousands of programmers from all around the world compete together, trying to solve problems as quickly and accurately as possible. The timer starts, submissions begin flying, and every second becomes important.

Programming

Why Div. 2 is Amazing

Div. 2 contests are one of the best ways to improve problem-solving skills. They teach important topics like greedy algorithms, binary search, mathematics, implementation, and dynamic programming. Even one solved problem can teach a completely new idea.

Lessons Learned

During contests, staying calm is extremely important. Sometimes the easiest problem becomes difficult because of stress. Reading the statement carefully, testing edge cases, and debugging step by step are the real keys to success.

Laptop Coding

⚡ Final Thoughts

Competitive programming is not only about ratings — it is about improving logical thinking, learning new techniques, and enjoying the challenge of coding. Every Codeforces Round is another step toward becoming a stronger programmer.

Happy Coding and Good Luck in Future Contests!

Full text and comments »

  • Vote: I like it
  • -28
  • Vote: I do not like it

By SirKadirov009, history, 9 months ago, In English

Hello Codeforces Interestments!!! Screenshot 2026 02 24 163759

I reminding you for the worldwide contest 2026 ICPC Asia Pacific Championship — Online Mirror (Unrated, Online Mirror, ICPC Rules, Teams Preferred). It is for the strongest and self_confident codders. See the region rules and competition page for being the participant of this big match.

The official contest is scheduled to start at Sunday, 8 March 2026, 09:30 AM (UTC+8). The live online mirror contest is scheduled to start only 15 minutes later, to keep both contests run almost in sync. The contest is 5 hours long and consists of several problems.

Please note that we might have to postpone the live online mirror contest in case the official contest is delayed. This is to ensure that the tasks are not available to the public until the official contest starts.

The contest will use ICPC-style scoring (same as the official contest) and will be unrated. You can participate as an individual or as a team, although as a team of three members is preferred.

See you on the top of the leaderboard!

Full text and comments »

  • Vote: I like it
  • -29
  • Vote: I do not like it