Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array nums consisting of positive integers.
You have to take each integer in the array, reverse its digits, and add it to the end of the array. You should apply this operation to the original integers in nums.
Return the number of distinct integers in the final array.
Example 1:
Input: nums = [1,13,10,12,31] Output: 6 Explanation: After including the reverse of each number, the resulting array is [1,13,10,12,31,1,31,1,21,13]. The reversed integers that were added to the end of the array are underlined. Note that for the integer 10, after reversing it, it becomes 01 which is just 1. The number of distinct integers in this array is 6 (The numbers 1, 10, 12, 13, 21, and 31).
Example 2:
Input: nums = [2,2,2] Output: 1 Explanation: After including the reverse of each number, the resulting array is [2,2,2,2,2,2]. The number of distinct integers in this array is 1 (The number 2).
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 106Problem summary: You are given an array nums consisting of positive integers. You have to take each integer in the array, reverse its digits, and add it to the end of the array. You should apply this operation to the original integers in nums. Return the number of distinct integers in the final array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[1,13,10,12,31]
[2,2,2]
reverse-integer)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2442: Count Number of Distinct Integers After Reverse Operations
class Solution {
public int countDistinctIntegers(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int x : nums) {
s.add(x);
}
for (int x : nums) {
int y = 0;
while (x > 0) {
y = y * 10 + x % 10;
x /= 10;
}
s.add(y);
}
return s.size();
}
}
// Accepted solution for LeetCode #2442: Count Number of Distinct Integers After Reverse Operations
func countDistinctIntegers(nums []int) int {
s := map[int]struct{}{}
for _, x := range nums {
s[x] = struct{}{}
}
for _, x := range nums {
y := 0
for x > 0 {
y = y*10 + x%10
x /= 10
}
s[y] = struct{}{}
}
return len(s)
}
# Accepted solution for LeetCode #2442: Count Number of Distinct Integers After Reverse Operations
class Solution:
def countDistinctIntegers(self, nums: List[int]) -> int:
s = set(nums)
for x in nums:
y = int(str(x)[::-1])
s.add(y)
return len(s)
// Accepted solution for LeetCode #2442: Count Number of Distinct Integers After Reverse Operations
use std::collections::HashSet;
impl Solution {
pub fn count_distinct_integers(nums: Vec<i32>) -> i32 {
let mut set = HashSet::new();
for i in 0..nums.len() {
let mut num = nums[i];
set.insert(num);
set.insert({
let mut item = 0;
while num > 0 {
item = item * 10 + (num % 10);
num /= 10;
}
item
});
}
set.len() as i32
}
}
// Accepted solution for LeetCode #2442: Count Number of Distinct Integers After Reverse Operations
function countDistinctIntegers(nums: number[]): number {
const n = nums.length;
for (let i = 0; i < n; i++) {
nums.push(Number([...(nums[i] + '')].reverse().join('')));
}
return new Set(nums).size;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.