LeetCode Entry

3516. Find Closest Person

04.09.2025 easy 2025 kotlin rust

Compare two distances

3516. Find Closest Person easy blog post substack youtube

1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1102

Problem TLDR

Compare two distances #easy

Intuition

Distance is abs(z - x or y)

Approach

  • use when

Complexity

  • Time complexity: \(O(1)\)

  • Space complexity: \(O(1)\)

Code


// 14ms
    fun findClosest(x: Int, y: Int, z: Int) =
        listOf(1,0,2)[abs(z - x).compareTo(abs(z - y))+1]


// 0ms
    pub fn find_closest(x: i32, y: i32, z: i32) -> i32 {
        [1,0,2][1+1.min((z-x).abs()-(z-y).abs()).max(-1) as usize]
    }


// 0ms
    int findClosest(int x, int y, int z) {
        return (abs(z-x)>abs(z-y))*2+(abs(z-x)<abs(z-y));
    }


// 0ms
    findClosest=lambda _,x,y,z:(abs(z-x)>abs(z-y))<<1|(abs(z-x)<abs(z-y))