Single Number II - Complete Solution Guide
Single Number II is LeetCode problem 137, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given an integer array nums where every element appears three times except for one, which appears exactly once . Find the single element and return it . You must implement a solution with a linear runtime complexity and use only constant extra space. Example 1: Input: nums = [2,2,3,2] Output: 3 Example 2: Input: nums = [0,1,0,1,0,1,99] Output: 99 Constraints: 1 <= nums.length <= 3 * 10 4 -2 31 <= nums[i] <= 2 31 - 1 Each element in nums appears exactly three times except for one element which ap
Detailed Explanation
The problem states that we are given an array of integers, `nums`, where every number appears exactly three times, except for one number which appears only once. The goal is to find and return that single number that occurs only once. The challenge is to achieve this with a linear time complexity (O(n)) and constant extra space (O(1)).
Solution Approach
The solution uses bit manipulation to keep track of the number of times each bit has appeared. The `ones` and `twos` variables are used to simulate a base-3 counter. For each number in the array, we update `ones` and `twos` based on the current number and their previous values. The final value of `ones` will hold the single number, because it represents the bits that have appeared only once (mod 3).
Step-by-Step Algorithm
- Step 1: Initialize two integer variables, `ones` and `twos`, to 0. These will store the bits that have appeared once and twice, respectively.
- Step 2: Iterate through each number `num` in the input array `nums`.
- Step 3: Update `ones` as follows: `ones = (ones ^ num) & ~twos`. This XORs `ones` with the current number `num`. If a bit appears for the first time, it sets the corresponding bit in `ones`. The `& ~twos` part ensures that if a bit already exists in `twos` (meaning it has appeared twice), it's removed from `ones` to avoid counting it as a single occurrence.
- Step 4: Update `twos` as follows: `twos = (twos ^ num) & ~ones`. This XORs `twos` with the current number `num`. If a bit appears for the second time, it sets the corresponding bit in `twos`. The `& ~ones` part ensures that if a bit already exists in `ones` (meaning it has appeared once), it's removed from `twos` to avoid double-counting.
- Step 5: After iterating through all numbers, the value of `ones` will contain the single number that appears only once. Return `ones`.
Key Insights
- Insight 1: Bit manipulation provides an efficient way to track the count of each bit without using extra space for a hash map or similar data structure.
- Insight 2: The key idea is to simulate a ternary (base-3) counter for each bit position. Since each number appears three times, the bits for those numbers will effectively cancel out when counted modulo 3, leaving only the bits of the single number.
- Insight 3: Using two variables, `ones` and `twos`, we can keep track of the count of each bit modulo 3. `ones` represents the bits that have appeared once, and `twos` represents the bits that have appeared twice.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Bit Manipulation.
Companies
Asked at: Accenture, Yahoo.