From b6324b8b81c121297ab90f7d928f971f9c2924de Mon Sep 17 00:00:00 2001 From: Spider-Testing <88180069+Spider-Testing@users.noreply.github.com> Date: Fri, 22 Oct 2021 20:52:10 +0530 Subject: [PATCH 1/3] Create Cpp_code --- Cpp_code | 1 + 1 file changed, 1 insertion(+) create mode 100644 Cpp_code diff --git a/Cpp_code b/Cpp_code new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/Cpp_code @@ -0,0 +1 @@ + From 8aa87da1b6f331b1ddf3128b32e223af0f7c8ef0 Mon Sep 17 00:00:00 2001 From: Spider-Testing <88180069+Spider-Testing@users.noreply.github.com> Date: Fri, 22 Oct 2021 20:53:48 +0530 Subject: [PATCH 2/3] Delete Cpp_code --- Cpp_code | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Cpp_code diff --git a/Cpp_code b/Cpp_code deleted file mode 100644 index 8b13789179..0000000000 --- a/Cpp_code +++ /dev/null @@ -1 +0,0 @@ - From a0df4e8fa17dd908ff181bc8e9bda0b5201d5ddf Mon Sep 17 00:00:00 2001 From: Spider-Testing <88180069+Spider-Testing@users.noreply.github.com> Date: Fri, 22 Oct 2021 20:55:56 +0530 Subject: [PATCH 3/3] Create Pair_with_difference_k.cpp --- .../Cpp_code/Pair_with_difference_k.cpp | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Cpp_code/Pair_with_difference_k.cpp diff --git a/Program's_Contributed_By_Contributors/Cpp_code/Pair_with_difference_k.cpp b/Program's_Contributed_By_Contributors/Cpp_code/Pair_with_difference_k.cpp new file mode 100644 index 0000000000..f7b981153f --- /dev/null +++ b/Program's_Contributed_By_Contributors/Cpp_code/Pair_with_difference_k.cpp @@ -0,0 +1,79 @@ +Question:- + +Pairs with difference K + +You are given with an array of integers and an integer K. You have to find and print the count of all such pairs which have difference K. + +Note: Take absolute difference between the elements of the array. + +Input Format: +The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol n. +The following line contains n space separated integers, that denote the value of the elements of the array. +The following line contains an integer, that denotes the value of K. + +Output format : +The first and only line of output contains count of all such pairs which have an absolute difference of K. + +Constraints : +0 <= n <= 10^4 + +Time Limit: 1 sec + +Sample Input 1 : +4 +5 1 2 4 +3 +Sample Output 1 : +2 + +Sample Input 2 : +4 +4 4 4 4 +0 +Sample Output 2 : +6 + +Code:- + +#include +using namespace std; + + +int getPairsWithDifferenceK(int *arr, int n, int k) +{ + unordered_map m; + int ans=0,val=0; + for(int i=0;i> n; + + int *input = new int[n]; + + for (int i = 0; i < n; i++) + { + cin >> input[i]; + } + + int k; + cin >> k; + + cout << getPairsWithDifferenceK(input, n, k); + + delete[] input; +}