chadi4all
BAN USER- -1of 1 vote
AnswersGiven a sorted array. Find the number of couples with the same difference. For example we can have an array with 2 couples whose difference is 3 and 4 couples whose difference is 5. The Output should be 2 & 4.
- chadi4all in United States| Report Duplicate | Flag | PURGE
Algorithm
- 0 Answers Number of couples with the same difference
Given a sorted array. Find the number of couples with the same difference. For example we can have an array with 2 couples whose difference is 3 and 4 couples whose difference is 5. The Output should be 2 & 4.
- chadi4all July 12, 2013| Flag | PURGE
My example was just an example vqeek ... If the n is the number of elements in the array then total number of couples is n*(n - 1) / 2. Among these couples there are couples with equal difference. For each couple I need this difference n the number of times it's repeated. so if a difference appears only once no need to print it.
- chadi4all July 13, 2013If I apply this code to 1 3 6 then I'll get difference 2 repeated twice, and difference 4 repeated 1 time but all the couples in the array are (1, 3) with difference 2 & (1, 6) with difference 5 & (3, 6) with difference 3. Doesn't this mean that you're code is wrong? Did I miss anything?
- chadi4all July 13, 2013
I tried the following example:
- chadi4all July 13, 2013arr = {6, 7, 14, 17, 24, 25, 33, 38, 45}
after applying you're code I get one couple with diff 10 while actually there are 2 couples with difference 10 which are (7, 17) & (14, 24).