# Problem

Given a positive integer N, find and return the longest distance between two consecutive 1’s in the binary representation of N.

If there aren’t two consecutive 1’s, return 0.

Example 1:

Input: 22
Output: 2
Explanation:
22 in binary is 0b10110.
In the binary representation of 22, there are three ones, and two consecutive pairs of 1's.
The first consecutive pair of 1's have distance 2.
The second consecutive pair of 1's have distance 1.
The answer is the largest of these two distances, which is 2.


Example 2:

Input: 5
Output: 2
Explanation:
5 in binary is 0b101.


Example 3:

Input: 6
Output: 1
Explanation:
6 in binary is 0b110.


Example 4:

Input: 8
Output: 0
Explanation:
8 in binary is 0b1000.
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.\


Note:

• 1 <= N <= 10^9

# Solution: Bit

Time complexity: O(logN)

Space complexity: O(1)

C++

# Problem

Given an n-ary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

For example, given a 3-ary tree: We should return its level order traversal:

[
,
[3,2,4],
[5,6]
]


Note:

1. The depth of the tree is at most 1000.
2. The total number of nodes is at most 5000.

# Solution1: Recursion

Time complexity: O(n)

Space complexity: O(n)

C++

# Problem

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

Note:

• Each element in the result should appear as many times as it shows in both arrays.
• The result can be in any order.

• What if the given array is already sorted? How would you optimize your algorithm?
• What if nums1‘s size is small compared to nums2‘s size? Which algorithm is better?
• What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

# Solution1: Hashtable

Time complexity: O(m + n)

Space complexity: O(m)

# Problem

Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.

The distance between two adjacent cells is 1.

Example 1:
Input:

0 0 0
0 1 0
0 0 0


Output:

0 0 0
0 1 0
0 0 0


Example 2:
Input:

0 0 0
0 1 0
1 1 1


Output:

0 0 0
0 1 0
1 2 1


Note:

1. The number of elements of the given matrix will not exceed 10,000.
2. There are at least one 0 in the given matrix.
3. The cells are adjacent in only four directions: up, down, left and right.

# Solution 1: DP

Two passes:

1. down, right
2. up, left

Time complexity: O(mn)

Space complexity: O(mn)

# Solution 2: BFS

Start from all 0 cells and find shortest paths to rest of the cells.

Time complexity: O(mn)

Space complexity: O(mn)

# Problem

Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

Example 1:

Input: [23, 2, 4, 6, 7],  k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.


Example 2:

Input: [23, 2, 6, 4, 7],  k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.


Note:

1. The length of the array won’t exceed 10,000.
2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.

# Special case:

nums = [0,0], k = 0, return = True

# Solution: Prefix Sum Reminder

Time complexity: O(n)

Space complexity: O(min(n, k))

# Related Problems

Mission News Theme by Compete Themes.