DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

🧠 Solving LeetCode Until I Become Top 1% β€” Day `74`

πŸ”Ή Problem: 3516 Find Closest Person

Difficulty: #Easy
Tags: #Math, #Comparison


πŸ“ Problem Summary

First of all, maybe the EASIEST PROBLEM IN LEETCODE HISTORY.

You are given three integers x, y, and z. The task is to determine which of x or y is closer to z.

  • If both are equally close β†’ return 0.
  • If x is closer β†’ return 1.
  • If y is closer β†’ return 2.

🧠 My Thought Process

  • Brute Force Idea:
    I think you can solve this eeeasily with simple math.

  • Optimized Strategy:
    The brute force is optimal here β€” no loops, no data structures, just math.

    • Compute |x - z| and |y - z|.
    • Compare the two distances.
    • Decide the answer based on which difference is smaller.
  • Algorithm Used:

    Pure mathematical comparison using absolute difference.


βš™οΈ Code Implementation (Python)

class Solution:
    def findClosest(self, x: int, y: int, z: int) -> int:
        x_abs = abs(x - z)
        y_abs = abs(y - z)

        if x_abs == y_abs:
            return 0
        elif x_abs < y_abs:
            return 1
        else:
            return 2
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(1) β†’ Only a few arithmetic operations.
  • Space: O(1) β†’ No extra memory used.

🧩 Key Takeaways

  • βœ… Learned how simple absolute difference comparisons solve closest-number problems.
  • πŸ’‘ The trick is recognizing that absolute difference directly measures closeness.
  • πŸ’­ This technique can be reused in problems like β€œfind closest element to a target in an array” (but with loops/binary search).

πŸ” Reflection (Self-Check)

  • [x] Could I solve this without help? (Yes, straightforward math)
  • [x] Did I write code from scratch? (Yes)
  • [x] Did I understand why it works? (Yes, absolute difference logic)
  • [x] Will I be able to recall this in a week? (Yes, very simple pattern)

πŸš€ Progress Tracker

Metric Value
Day 74
Total Problems Solved 437
Confidence Today πŸ˜ƒ
Leetcode Rating 1530

Top comments (0)