🔥Day 1 Recap – CodeChef 1400+ Challenge Begins

Pranav ZambarePranav Zambare
4 min read

Today I officially started my grind toward 1400+ on CodeChef. I’ve kept the first day light but insightful — focused mostly on two-pointer logic, frequency maps, and a neat parity-based math problem.

Here’s what I worked on:


🔹 Problem 1: LeetCode 977 - Squares of a Sorted Array

Problem Link →

The problem is simple: Given a non-decreasing (sorted) array of integers, return a new array of the squares of each number, also sorted.

Now, the brute force way is just:

  • Square every number → O(n)

  • Sort the result → O(n log n)

But since the input is already sorted, I figured I can optimize further using the two-pointer technique.

Here's the idea:

  • The highest square will always come from the number with the highest absolute value, which could be at either end of the array (negative or positive).

  • So I set up two pointers, one at the beginning (low) and one at the end (high), and filled the result array from the back.

That way, I build the answer in O(n) time without needing to sort again.

It's one of those classic “use the input structure” moments.


🔹 Problem 2: LeetCode 167 - Two Sum II (Input Array Is Sorted)

Problem Link →

This one’s a direct application of two pointers as well.

The input is sorted, and we need to return the 1-based indices of two numbers that sum up to a given target.

Approach:

  • I started from both ends: low = 0, high = n - 1.

  • If the sum is exactly the target, we’re done — return the indices (after adjusting for 1-based indexing).

  • If the sum is too small, I shift the left pointer one step right (increase the value).

  • If the sum is too big, I move the right pointer one step left (decrease the value).

This keeps it clean, efficient, and runs in O(n) time with O(1) space.

It’s elegant because:

  • Left side = always minimum

  • Right side = always maximum

  • You shrink the search space linearly.


🔹 Problem 3: Codeforces 977B - Two-Gram

Problem Link →

This was a fun one.

You're given a string of length n, and you need to find the two-letter substring (gram) that occurs the most frequently.

Here’s how I approached it:

  • I looped from i = 0 to n - 2 and took every substring of length 2 using s.substr(i, 2).

  • I stored each substring in a map with its frequency.

  • Then, in another loop, I just tracked the most frequent substring.

The nice thing here is that:

  • It’s a single-pass frequency mapping problem.

  • The logic is simple but powerful — many string problems boil down to “how many times does something repeat?”

This was a reminder that string maps are super handy tools, especially in Codeforces-style problems.


🔹 Problem 4: Codeforces 486A - Calculating Function

Problem Link →

This one is more math-y and pattern-based.

The function we’re asked to compute follows a pattern:

  • If n is even → return n / 2

  • If n is odd → return -ceil(n / 2)

You can try a few examples and you'll notice this alternating sign pattern:

  • f(1) = -1

  • f(2) = 1

  • f(3) = -2

  • f(4) = 2

So I just wrote a conditional:

  • If n is odd: result = -1 Ă— (n / 2 + 1)

  • If n is even: result = n / 2

What I liked here is how simple observations can completely avoid looping.

Key takeaway:

  • Don't simulate blindly. Sometimes, there’s a pattern hiding in plain sight.

đź§© Summary of Day 1

What I loved about today’s set is how much you can do with simple ideas when you use them well:

I also realized how important it is to pause, think, and not rush to implement. Just observing constraints and input patterns makes a big difference.


🕹️ ACT 1 Begins…

0
Subscribe to my newsletter

Read articles from Pranav Zambare directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pranav Zambare
Pranav Zambare