3 ms·
The problem statement doesn't forbid duplicates. Let's assume K=1. Seems to me that both solutions from the post would return 2 for 7,7,8 and 1 for 7,8,8. Whil
by jsnell 9y ago
The problem statement doesn't forbid duplicates. Let's assume K=1. Seems to me that both solutions from the post would return 2 for 7,7,8 and 1 for 7,8,8.
While either an answer of 1 or 2 could be justified by some definition of "pair", I can't think of a reasonable definition that would allow inconsistent return values for these two examples.
- 0xB504 9y agoYou could add a dublicate counter. Once out find a pair, let j increase until numbers[j] != numbers[j+1] and count how many duplicates you encounter. Add this number to pairs everytime you find one. Once you increase j set the duplicate counter back to one.