Count Number of Distinct Integers After Reverse Operations - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,5,1,3]
Output: 6
💡 Note: Original: [2,5,1,3]. After adding reverses: [2,5,1,3,2,5,1,3]. Distinct values: {1,2,3,5} = 4 unique numbers. Wait, let me recalculate: reverse(2)=2, reverse(5)=5, reverse(1)=1, reverse(3)=3. So we have [2,5,1,3,2,5,1,3] which gives us {1,2,3,5} = 4 distinct.
Example 2 — Some Different Reverses
$ Input: nums = [13,32,13]
Output: 4
💡 Note: Original: [13,32,13]. Reverses: reverse(13)=31, reverse(32)=23, reverse(13)=31. Final array: [13,32,13,31,23,31]. Distinct values: {13,32,31,23} = 4 unique numbers.
Example 3 — All Same Digits
$ Input: nums = [1,13,10,12,31]
Output: 6
💡 Note: Original: [1,13,10,12,31]. Reverses: reverse(1)=1, reverse(13)=31, reverse(10)=1, reverse(12)=21, reverse(31)=13. Final: [1,13,10,12,31,1,31,1,21,13]. Distinct: {1,13,10,12,31,21} = 6 unique numbers.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 106

Visualization

Tap to expand
Count Distinct Integers After Reverse OperationsStep 1: Original Array133213Step 2: Add Digit Reverses133213312331originaloriginaloriginalrev(13)rev(32)rev(13)Step 3: Count Distinct Values13323123Unique values in final arrayResult: 4 distinct integers
Understanding the Visualization
1
Input Array
Original array of positive integers
2
Add Reverses
Append digit-reversed version of each number
3
Count Distinct
Count unique values in final array
Key Takeaway
🎯 Key Insight: Use a hash set to automatically eliminate duplicates while processing original numbers and their digit-reversed versions
Asked in
Amazon 15 Google 10 Microsoft 8
12.5K Views
Medium Frequency
~15 min Avg. Time
285 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen