medium level Problem...not for Python!!

A simple example to understand how the succinctness of Python helps to reduce the lines of code for any problem, although, it can be debatable as to is this useful or not cause this also reduces the ease with which one can understand a code, because for a person not aware about the Python syntax, it would be a real nightmare to understand such codes.

This was a medium level problem, and as I remember this was a part of the problems that are solved using the bit manipulation, but in Python when we have the libraries, so need to use tedious approaches, but yeah the original way or the logic must be clear before going for such kind of implementations. I know how to implement in that way so this makes more sense.

This problem was the POTD for 14th August 2023.

I'll be implementing this in cpp as well as in JavaScript too.

Important Thing to note here is the Time Complexity, cause that is often ignored when using such one liners in Python, ofcourse this code passes and we can know that this gave O(n) TC but how?

firstly used the Counters Constructor to create a counter dictionary- O(n)

then create a dictionary from that- O(n)

then get the elems(items) of that dict- O(n)

make a list for those items- O(n)

then filter the unnecessary stuff from that list- O(n)

lastly sort the list- we know sorting requires O(n logn) at best but here we are sure that at the end there will be just 2 numbers(so constant O(1)) so sorting them is really not a troublesome task, so that is acceptable.

Means: O(n)+O(n)+O(n)+O(n)+O(1), so ==> O(n)

Night!

The Problem Statement: 

Given an array A containing 2*N+2 positive numbers, out of which 2*N numbers exist in pairs whereas the other two number occur exactly once and are distinct. Find the other two numbers. Return in increasing order.

Example 1:

Input: 
N = 2
arr[] = {1, 2, 3, 2, 1, 4}
Output:
3 4 
Explanation:
3 and 4 occur exactly once.

Example 2:

Input:
N = 1
arr[] = {2, 1, 3, 2}
Output:
1 3
Explanation:
1 3 occur exactly once.

Your Task:
You do not need to read or print anything. Your task is to complete the function singleNumber() which takes the array as input parameter and returns a list of two numbers which occur exactly once in the array. The list must be in ascending order.

Expected Time Complexity: O(N)
Expected Space Complexity: O(1)

Constraints:
1 <= length of array <= 10

1 <= Elements in array <= 5 * 106

from collections import Counter as CT
class Solution:
    def singleNumber(self, nums):
        '''
        A normal approach
        '''
        '''
        ans=list()
        freqs=dict(CT(nums))
        for val,freq in freqs.items():
            ans.append(val) if(freq==1) else None
        ans.sort()
        return ans
        '''
       
        '''
        A single liner to do the works of the above ways
        '''
        return sorted(list(filter(lambda x:x!=-1,[val if(freq==1) else -1 for val,freq in dict(CT(nums)).items()])))
   

Comments

Popular Posts