Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9f5dce

Browse files
Merge pull request #10 from Vidhish-Trivedi/vt_b1
Added solution.
2 parents 27c9541 + 738dec5 commit f9f5dce

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

β€Ž22. Pairs/solution.cppβ€Ž

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Topic : Algorithms
3+
Subtopic : Pairs
4+
Language : C++
5+
Problem Statement :
6+
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.
7+
Url : https://www.hackerrank.com/challenges/pairs/problem
8+
*/
9+
#include <bits/stdc++.h>
10+
11+
using namespace std;
12+
13+
string ltrim(const string &);
14+
string rtrim(const string &);
15+
vector<string> split(const string &);
16+
17+
/*
18+
* Complete the 'pairs' function below.
19+
*
20+
* The function is expected to return an INTEGER.
21+
* The function accepts following parameters:
22+
* 1. INTEGER k
23+
* 2. INTEGER_ARRAY arr
24+
*/
25+
26+
int pairs(int k, vector<int> arr) {
27+
map <int, int> m;
28+
for(int x : arr)
29+
{
30+
m[x]++;
31+
}
32+
int c = 0;
33+
int i;
34+
for(i = 0; i < arr.size(); i++)
35+
{
36+
m[arr[i]]--;
37+
if(m[k + arr[i]] > 0)
38+
{
39+
c++;
40+
}
41+
m[arr[i]]++;
42+
}
43+
return(c);
44+
}

β€Ž22. Pairs/solution.pyβ€Ž

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
Topic : Algorithms
3+
Subtopic : Pairs
4+
Language : Python 3
5+
Problem Statement :
6+
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.
7+
Url : https://www.hackerrank.com/challenges/pairs/problem
8+
'''
9+
#!/bin/python3
10+
11+
import os
12+
13+
#
14+
# Complete the 'pairs' function below.
15+
#
16+
# The function is expected to return an INTEGER.
17+
# The function accepts following parameters:
18+
# 1. INTEGER k
19+
# 2. INTEGER_ARRAY arr
20+
#
21+
22+
def pairs(k, arr):
23+
# Write your code here
24+
m = {}
25+
for _ in arr:
26+
if(_ not in m.keys()):
27+
m[_] = 1
28+
else:
29+
m[_] += 1
30+
c = 0
31+
for i in range(len(arr)):
32+
if((k + arr[i]) in m.keys()):
33+
if(m[k + arr[i]] > 0):
34+
c += 1
35+
m[arr[i]] += 1
36+
return(c)
37+
38+
if __name__ == '__main__':
39+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
40+
41+
first_multiple_input = input().rstrip().split()
42+
43+
n = int(first_multiple_input[0])
44+
45+
k = int(first_multiple_input[1])
46+
47+
arr = list(map(int, input().rstrip().split()))
48+
49+
result = pairs(k, arr)
50+
51+
fptr.write(str(result) + '\n')
52+
53+
fptr.close()

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /