2442. Count Number of Distinct Integers After Reverse Operations LeetCode Solution

In this guide, you will get 2442. Count Number of Distinct Integers After Reverse Operations LeetCode Solution with the best time and space complexity. The solution to Count Number of Distinct Integers After Reverse Operations problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Number of Distinct Integers After Reverse Operations solution in C++
  4. Count Number of Distinct Integers After Reverse Operations solution in Java
  5. Count Number of Distinct Integers After Reverse Operations solution in Python
  6. Additional Resources
2442. Count Number of Distinct Integers After Reverse Operations LeetCode Solution image

Problem Statement of Count Number of Distinct Integers After Reverse Operations

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 <= 105
1 <= nums[i] <= 106

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2442. Count Number of Distinct Integers After Reverse Operations LeetCode Solution in C++

class Solution {
 public:
  int countDistinctIntegers(vector<int>& nums) {
    unordered_set<int> numsSet{nums.begin(), nums.end()};

    for (const int num : nums)
      numsSet.insert(reversed(num));

    return numsSet.size();
  }

 private:
  int reversed(int num) {
    int ans = 0;
    while (num > 0) {
      ans = ans * 10 + num % 10;
      num /= 10;
    }
    return ans;
  }
};
/* code provided by PROGIEZ */

2442. Count Number of Distinct Integers After Reverse Operations LeetCode Solution in Java

N/A
// code provided by PROGIEZ

2442. Count Number of Distinct Integers After Reverse Operations LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.