Skip to content

Commit

Permalink
Added solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Vidhish-Trivedi committed Sep 12, 2022
1 parent 27c9541 commit 738dec5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 22. Pairs/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Topic : Algorithms
Subtopic : Pairs
Language : C++
Problem Statement :
Given an array of integers and a target value, determine the number of pairs of array elements that have a difference equal to the target value.
Url : https://www.hackerrank.com/challenges/pairs/problem
*/
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
* 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
*/

int pairs(int k, vector<int> arr) {
map <int, int> m;
for(int x : arr)
{
m[x]++;
}
int c = 0;
int i;
for(i = 0; i < arr.size(); i++)
{
m[arr[i]]--;
if(m[k + arr[i]] > 0)
{
c++;
}
m[arr[i]]++;
}
return(c);
}
53 changes: 53 additions & 0 deletions 22. Pairs/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'''
Topic : Algorithms
Subtopic : Pairs
Language : Python 3
Problem Statement :
Given an array of integers and a target value, determine the number of pairs of array elements that have a difference equal to the target value.
Url : https://www.hackerrank.com/challenges/pairs/problem
'''
#!/bin/python3

import os

#
# 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):
# Write your code here
m = {}
for _ in arr:
if(_ not in m.keys()):
m[_] = 1
else:
m[_] += 1
c = 0
for i in range(len(arr)):
if((k + arr[i]) in m.keys()):
if(m[k + arr[i]] > 0):
c += 1
m[arr[i]] += 1
return(c)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

first_multiple_input = input().rstrip().split()

n = int(first_multiple_input[0])

k = int(first_multiple_input[1])

arr = list(map(int, input().rstrip().split()))

result = pairs(k, arr)

fptr.write(str(result) + '\n')

fptr.close()

0 comments on commit 738dec5

Please sign in to comment.