[LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
2015-04-07 00:34
721 查看
A great answer from here :http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/
Following is an interesting O(nk) solution:
We can solve the above problem in O(nk) time using O(k-1) extra space. Note that there can never be more than k-1 elements in output (Why?). There are mainly three steps in this algorithm.
1) Create a temporary array of size (k-1) to store elements and their counts (The output elements are going to be among these k-1 elements). Following is structure of temporary array elements.
This step takes O(k) time.
2) Traverse through the input array and update temp[] (add/remove an element or increase/decrease count) for every traversed element. The array temp[] stores potential (k-1) candidates at every step. This step takes O(nk) time.
3) Iterate through final (k-1) potential candidates (stored in temp[]). or every element, check if it actually has count more than n/k. This step takes O(nk) time.
The main step is step 2, how to maintain (k-1) potential candidates at every point? The steps used in step 2 are like famous game: Tetris. We treat each number as a piece in Tetris, which falls down in our temporary array temp[]. Our task is to try to keep the same number stacked on the same column (count in temporary array is incremented).
Consider k = 4, n = 9
Given array: 3 1 2 2 2 1 4 3 3
i = 0
temp[] has one element, 3 with count 1
i = 1
temp[] has two elements, 3 and 1 with counts 1 and 1 respectively
i = 2
temp[] has three elements, 3, 1 and 2 with counts as 1, 1 and 1 respectively.
i = 3
temp[] has three elements, 3, 1 and 2 with counts as 1, 1 and 2 respectively.
i = 4
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 3 respectively.
i = 5
temp[] has three elements, 3, 1 and 2 with counts as 1, 2 and 3 respectively.
Now the question arises, what to do when temp[] is full and we see a new element – we remove the bottom row from stacks of elements, i.e., we decrease count of every element by 1 in temp[]. We ignore the current element.
i = 6
temp[] has two elements, 1 and 2 with counts as 1 and 2 respectively.
i = 7
temp[] has three elements, 3, 1 and 2 with counts as 1, 1 and 2 respectively.
i = 8
temp[] has three elements, 3, 1 and 2 with counts as 2, 1 and 2 respectively.
Finally, we have at most k-1 numbers in temp[]. The elements in temp are {3, 1, 2}. Note that the counts in temp[] are useless now, the counts were needed only in step 2. Now we need to check whether the actual counts of elements in temp[] are more than n/k (9/4) or not. The elements 3 and 2 have counts more than 9/4. So we print 3 and 2. (We check the count by going through the array again for each number)
Note that the algorithm doesn’t miss any output element. There can be two possibilities, many occurrences are together or spread across the array. If occurrences are together, then count will be high and won’t become 0. If occurrences are spread, then the element would come again in temp[]. Following is C++ implementation of above algorithm.
Following is an interesting O(nk) solution:
We can solve the above problem in O(nk) time using O(k-1) extra space. Note that there can never be more than k-1 elements in output (Why?). There are mainly three steps in this algorithm.
1) Create a temporary array of size (k-1) to store elements and their counts (The output elements are going to be among these k-1 elements). Following is structure of temporary array elements.
struct eleCount { int element; int count; }; struct eleCount temp[];
This step takes O(k) time.
2) Traverse through the input array and update temp[] (add/remove an element or increase/decrease count) for every traversed element. The array temp[] stores potential (k-1) candidates at every step. This step takes O(nk) time.
3) Iterate through final (k-1) potential candidates (stored in temp[]). or every element, check if it actually has count more than n/k. This step takes O(nk) time.
The main step is step 2, how to maintain (k-1) potential candidates at every point? The steps used in step 2 are like famous game: Tetris. We treat each number as a piece in Tetris, which falls down in our temporary array temp[]. Our task is to try to keep the same number stacked on the same column (count in temporary array is incremented).
Consider k = 4, n = 9
Given array: 3 1 2 2 2 1 4 3 3
i = 0
3 _ _
temp[] has one element, 3 with count 1
i = 1
3 1 _
temp[] has two elements, 3 and 1 with counts 1 and 1 respectively
i = 2
3 1 2
temp[] has three elements, 3, 1 and 2 with counts as 1, 1 and 1 respectively.
i = 3
- - 2 3 1 2
temp[] has three elements, 3, 1 and 2 with counts as 1, 1 and 2 respectively.
i = 4
- - 2 - - 2 3 1 2
temp[] has three elements, 3, 1 and 2 with
counts as 1, 1 and 3 respectively.
i = 5
- - 2 - 1 2 3 1 2
temp[] has three elements, 3, 1 and 2 with counts as 1, 2 and 3 respectively.
Now the question arises, what to do when temp[] is full and we see a new element – we remove the bottom row from stacks of elements, i.e., we decrease count of every element by 1 in temp[]. We ignore the current element.
i = 6
- - 2 - 1 2
temp[] has two elements, 1 and 2 with counts as 1 and 2 respectively.
i = 7
- 2 3 1 2
temp[] has three elements, 3, 1 and 2 with counts as 1, 1 and 2 respectively.
i = 8
3 - 2 3 1 2
temp[] has three elements, 3, 1 and 2 with counts as 2, 1 and 2 respectively.
Finally, we have at most k-1 numbers in temp[]. The elements in temp are {3, 1, 2}. Note that the counts in temp[] are useless now, the counts were needed only in step 2. Now we need to check whether the actual counts of elements in temp[] are more than n/k (9/4) or not. The elements 3 and 2 have counts more than 9/4. So we print 3 and 2. (We check the count by going through the array again for each number)
Note that the algorithm doesn’t miss any output element. There can be two possibilities, many occurrences are together or spread across the array. If occurrences are together, then count will be high and won’t become 0. If occurrences are spread, then the element would come again in temp[]. Following is C++ implementation of above algorithm.
相关文章推荐
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
- CareerCup Finds all the elements that appear more than n/3 times in the list
- Given an array of size n, find the majority element. The majority element is the element that appear
- Given an array of size n, find all the possible sub set of the array of size k
- Find all max of elements of an array
- Find all max of elements of an array
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- Given an array of integers, every element appears twice except for one. Find that single one.
- find all pairs of elements in a balanced BST that sum to a certain number
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
- pointer to ref of an array, and to the func that return a ref of an array
- ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.al
- Python-numpy逻辑报错:The truth value of an array with more than one element is ambiguous
- Given an array of integers, every element appears twice except for one. Find that single one.
- Find M Integers from an Array of Size N equally
- Ruby: How to find all indices of elements that match a given condition?
- Given an array of positive and negative integers find the first subarray with zero sum
- How can I combine elements of an array that have a common element?
- b083 [置顶] 面试题02:Delete occurrences of an element if it occurs more than n times