prakash951's blog

By prakash951, history, 5 hours ago, In English
Write a program to find the number of pairs of elements in an array, that satisfy the below condition.

min(∣a−b∣,∣a+b∣)≤min(∣a∣,∣b∣) 
max(∣a−b∣,∣a+b∣)≥max⁡(∣a∣,∣b∣)

min(∣a−b∣,∣a+b∣)≤min(∣a∣,∣b∣)≤max⁡(∣a∣,∣b∣)≤max(∣a−b∣,∣a+b∣)

Example:

[-9, 6, -5, 3]

-9 -5
-9 6
-5 3
-5 6
3 6

constraints:

1<=N<=200000

-1000000000<=a[i]<=1000000000

Solved it with O(NxN)

Required Complexity: O(NlogN)

If anybody has better solution please share.

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it