Skip to content

Commit

Permalink
Updates search pairs challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
esl4m committed Jun 3, 2022
1 parent 0eb0c90 commit d09fc0c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions InterviewPreparationKit/search/pairs.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
#!/bin/python3
# Determine the number of pairs of array elements that have a difference equal to a target value.

import math
import os
import random
import re
import sys

# Complete the 'pairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER k
# 2. INTEGER_ARRAY arr

def pairs(k, arr):
# arr = [1, 5, 3, 4, 2]
res = 0
for value in arr:
if (value + k) in arr:
res += 1
return res
# res = 0
# for value in arr:
# if (value + k) in arr:
# res += 1
# return res
set1 = set(arr) # set : without duplicates
set2 = [value + k for value in arr]
return len(set1.intersection(set2))

arr_input = [1, 5, 3, 4, 2]
print(pairs(2, arr_input))
Expand Down

0 comments on commit d09fc0c

Please sign in to comment.