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.
No need to sort. You can get it done in O(n).
create a new Array size of 2^32-1
For each element in the original array, add K to it. Use the sum as the index in the second array and set the value to true.
Take a second pass of the original array, use the value of each element as the index in the second array. If there's a match in the second array, increment a counter.
There you go.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Pairs
You are viewing a single comment's thread. Return to all comments →
No need to sort. You can get it done in O(n). create a new Array size of 2^32-1 For each element in the original array, add K to it. Use the sum as the index in the second array and set the value to true. Take a second pass of the original array, use the value of each element as the index in the second array. If there's a match in the second array, increment a counter. There you go.