Thursday, June 15, 2017

Hackerrank Solution: Sock Merchant in Java for Beginners

Problem Link 

Solution Link

Algorithm : Brute Force

It is a programming problem for beginners. The problem is about array manipulation. I have used a nested  for  loop to compare every possible pair. If they have the same value they are assigned -1 and the count is incremented. Here of course you need to check whether any element is -1 before comparing for equality, otherwise the count will be incremented, if both are -1, which is not within the range of values mentioned in the constraints of the problem statement.

Runtime : O(n^2) (due to the second order nested for loop)
Required Space : O(n) (Using one dimension array)

NOTE: I will try to post a more efficient solution with less run-time in near future.

No comments:

Post a Comment

Beginner's Guide: Some Useful Machine Learning Websites(Part 1)

I have been away for quite some time now. The reason is I was quite busy with my academic thesis and general academic pressure. Now I do fe...