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

CodeForces: Xenia and Ringroad(339B) Solution in Java for Beginners

  Xenia and Ringroad(339B) Problem Link: Since this is a beginner's problem, I suggest you try it out yourself first. Java programm...