Problem Description: I found this problem in my Algorithms Assignment . It wants me to find the frequencies of all the elements of an array in O(n) time and O(1) space.
Array Can be anything like Ar[]={1,6,3,78,4,6,1}
After Thinking a little bit i came up with this approach:
- Iterating the array and finding the max element.( O(n) time)
- Creating a new array of the size= max element ( O(1) space)
- Iterating over the original array and storing the frequencies at the indices of the new array (O(n) time).
I have a doubt regarding step 2. After finding the max element(say m) in Step 1 i am making a new array of size m. does thing array occupies O(1) space ? if not please explain