We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
It seems that your solution is O(n2).
We can have O(nlogn) algorithm by using efficient algorithms. The key for one element x in A, we need to perform efficient searching for a matched element in B. Both binary search and hashing can achieve efficient retrival.
For example, CodeCody uses a binary search tree to keep the elements in order for fast binary search. And I use a hash table to performance even faster search in most cases.
see https://www.hackerrank.com/challenges/beautiful-pairs/submissions/code/20992981
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Pairs
You are viewing a single comment's thread. Return to all comments →
It seems that your solution is O(n2). We can have O(nlogn) algorithm by using efficient algorithms. The key for one element x in A, we need to perform efficient searching for a matched element in B. Both binary search and hashing can achieve efficient retrival. For example, CodeCody uses a binary search tree to keep the elements in order for fast binary search. And I use a hash table to performance even faster search in most cases. see https://www.hackerrank.com/challenges/beautiful-pairs/submissions/code/20992981