Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5271 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4356 |

@sanjay05 | 3762 |

@mantu_singh | 3638 |

@gfgaccount | 3601 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 871 |

@akhileshkumar562002 | 862 |

@pradeeppatidar1999 | 840 |

@kanewilliamson123 | 837 |

@pd420786 | 813 |

@infilooop | 800 |

@bunnybug320 | 796 |

@smitabose7826 | 784 |

@virgat | 768 |

Complete Leaderboard |

Given an array **a[ ] **of **N** elements, the task is to find the number of ways to choose pairs **{a[i], a[j]}** such that their absolute difference is maximum.

**Example:**

Input:N = 5 a[] = {3, 2, 1, 1, 3}Output:4Explanation:The maximum difference you can find is 2 which is from 3 - 1 = 2. Number of ways of choosing it: 1) Choosing the first and third element 2) Choosing the first and fourth element 3) Choosing the third and fifth element 4) Choosing the fourth and fifth element Hence, the answer is 4.

**Your Task:**

This is a function problem. The input is already taken care of by the driver code. You only need to complete the function **countPairs() **that take *array(a) **and*** sizeOfArray(N)** as input parameters, and returns the count of pairs which has the maximum difference. The driver code takes care of the printing.

**Expected Time Complexity:** O(n).

**Expected Auxiliary Space:** O(1).

**Constraints:**

1 ≤ N ≤ 10^{5}

1 ≤ A[i] ≤ 10^{5}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Count the pairs with maximum difference

...