Skip to content

Commit 66dde22

Browse files
Create Subtract Operations Explanation.txt
1 parent 3aa7f6d commit 66dde22

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Suppose we subtract the whole array by X in the first step
2+
3+
Each A2[i] = A[i] - x
4+
5+
Suppose we subtract the whole array by Y = A2[j] in the second step
6+
7+
Each A3[i] = A2[i] - Y = (A[i] - X) - (A[j] - X)
8+
9+
Where A[i], A2[i] and A3[i] represent the state of A[i] at time 1, 2, 3 respectively
10+
11+
X gets cancelled out twice.
12+
13+
Every operation cancels out everything that was subtracted in the previous step.
14+
15+
What we will be left with in the end is the difference of 2 elements A[i] - A[j]
16+
17+
We have to check if there is a pair who's difference is K in the original array.
18+
19+
------
20+
21+
void solve()
22+
{
23+
int no_of_elements, k;
24+
cin >> no_of_elements >> k;
25+
26+
vector <long long> A(no_of_elements + 1);
27+
for(int i = 1; i <= no_of_elements; i++)
28+
{
29+
cin >> A[i];
30+
}
31+
32+
map <int, int> present;
33+
for(int i = 1; i <= no_of_elements; i++)
34+
{
35+
present[A[i]] = true;
36+
}
37+
38+
int possible = false;
39+
for(int i = 1; i <= no_of_elements; i++)
40+
{
41+
if(present[A[i] - k] || present[A[i] + k])
42+
{
43+
possible = true;
44+
break;
45+
}
46+
}
47+
48+
cout << (possible ? "Yes" : "No") << "\n";
49+
}

0 commit comments

Comments
 (0)