LeetCode 2417: Closest Fair Integer Solution

Master LeetCode problem 2417 (Closest Fair Integer), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2417. Closest Fair Integer

Problem Explanation

Explanation:

Given a positive integer n, we need to find the closest fair integer to n. A fair number is defined as a number that has the same number of odd and even digits when converted to a string. If n is already a fair number, we return n. Otherwise, we need to find the closest fair number to n.

To approach this problem, we can start by converting the given integer n into a string representation so that we can easily count the number of odd and even digits. Once we have the counts of odd and even digits, we can increment or decrement the number to find the closest fair number.

We will iterate over the digits of n from left to right. At each digit, we will consider two possibilities:

  1. If the digit is odd, we can try decrementing it until the number becomes fair.
  2. If the digit is even, we can try incrementing it until the number becomes fair.

We will keep track of the closest fair number found so far and update it whenever we find a closer one.

Finally, we return the closest fair number.

:

Solution Code

class Solution {
    public long closestFair(int n) {
        String numStr = String.valueOf(n);
        int oddCount = 0, evenCount = 0;
        
        for (char c : numStr.toCharArray()) {
            if ((c - '0') % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        
        long closestFair = n;
        for (int i = 0; i < numStr.length(); i++) {
            char c = numStr.charAt(i);
            if ((c - '0') % 2 == 0 && evenCount < oddCount) {
                closestFair = closestFair * 10 + 1;
                evenCount++;
            } else if ((c - '0') % 2 != 0 && oddCount < evenCount) {
                closestFair = closestFair * 10;
                oddCount++;
            } else {
                closestFair = closestFair * 10 + (c - '0');
            }
        }
        
        return closestFair;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2417 (Closest Fair Integer)?

This page provides optimized solutions for LeetCode problem 2417 (Closest Fair Integer) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2417 (Closest Fair Integer)?

The time complexity for LeetCode 2417 (Closest Fair Integer) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2417 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2417 in Java, C++, or Python.

Back to LeetCode Solutions