-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Vidhish-Trivedi/vt_b1
Added solution.
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |