From 8db616aee49faadaa6f880ee630dbae4225e6300 Mon Sep 17 00:00:00 2001 From: Rishabh Singh Chauhan <143814425+Rsc2414@users.noreply.github.com> Date: Sun, 20 Jul 2025 22:39:30 +0530 Subject: [PATCH] Update 007. Pairs.py --- Algorithms/05. Search/007. Pairs.py | 31 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/Algorithms/05. Search/007. Pairs.py b/Algorithms/05. Search/007. Pairs.py index 187b5f4..9267033 100644 --- a/Algorithms/05. Search/007. Pairs.py +++ b/Algorithms/05. Search/007. Pairs.py @@ -1,22 +1,35 @@ # Problem: https://www.hackerrank.com/challenges/pairs/problem # Score: 50 - -# use two-pointers approach on a sorted array +# Read input: n = number of elements, value = target difference n, value = map(int, input().split()) -points = sorted(list(map(int, input().split()))) -ans = 0 -i = 0 -j = 1 +# Read and sort the array +points = sorted(map(int, input().split())) + +# Initialize two pointers +i = 0 # First pointer +j = 1 # Second pointer +ans = 0 # Count of valid pairs +# Loop through the array while j < n: - if points[j] - points[i] == value: + diff = points[j] - points[i] # Calculate difference + + if diff == value: + # Found a valid pair ans += 1 + j += 1 # Move j to check next pair + elif diff < value: + # Difference too small, move j forward j += 1 - elif points[j] - points[i] > value: + else: + # Difference too large, move i forward i += 1 - elif points[j] - points[i] < value: + + # Ensure i is always less than j + if i == j: j += 1 +# Output the answer print(ans)