-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAbsolute Difference of 1.cpp
59 lines (51 loc) · 1.12 KB
/
Absolute Difference of 1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Problem Statement : https://practice.geeksforgeeks.org/problems/absolute-difference-1/0/?track=SP-Arrays%20and%20Searching
Asked in : Jabong
Code By:
Abhinav,
CSE, NIT P
(Masters_Abh on codechef, codeforces, hackerrank and Spoj)
*/
#include <bits/stdc++.h>
using namespace std;
#define lint long long int
bool adj_abs_one(lint n){ // Function to check if adjancent characters of a number have an abs difference of 1
lint m=n%10;
n/=10;
while(n){
lint x=n%10;
if(abs(x-m)!=1){
return false;
}
m=x;
n/=10;
}
return true;
}
int main() {
lint t;
cin>>t;
while(t--){
lint n,k;
cin>>n>>k;
vector<lint> v;
for(lint i=0;i<n;i++){
lint x;
cin>>x;
if(x<k){
v.push_back(x); // storing only the numbers that are less than k
}
}
lint flag=0;
for(lint i=0;i<v.size();i++){
if(adj_abs_one(v[i])){
cout<<v[i]<<" ";
flag=1;
}
}
if(!flag){
cout<<-1; // When no expected numbers found
}
cout<<endl;
}
}