-
Notifications
You must be signed in to change notification settings - Fork 22
/
absolute difference of 1
69 lines (57 loc) · 1.41 KB
/
absolute difference of 1
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
60
61
62
63
64
65
66
67
68
69
/*
Given an array A of size N. Print all the numbers less than K in the array. The numbers should be such that the difference between every
adjacent digit should be 1.
Note: Print '-1' if no number if valid.
Input:
The first line consists of an integer T i.e number of test cases. T testcases follow. Each testcase contains two lines of input. The first
line consists of two integers N and K separated by a space. The next line consists of N spaced integers.
Output:
For each testcase, print the required output.
Constraints:
1 <= T <= 100
1 <= N <= 107
1 <= K, A[i] <= 1018
Example:
Input:
2
8 54
7 98 56 43 45 23 12 8
10 1000
87 89 45 235 465 765 123 987 499 655
Output:
43 45 23 12
87 89 45 765 123 987
Explanation:
Testcase1: 43 45 23 12 all these numbers have adjacent digits diff as 1 and they are less than 54
*/
#include <bits/stdc++.h>
using namespace std;
bool digit(long long n)
{
vector<int> a;
while(n!=0)
{
a.push_back(n%10);
n/=10;
}
if(a.size()==1) return false;
for(int i=0;i<a.size()-1;i++)
{
if(abs(a[i]-a[i+1]) != 1) return false;
}
return true;
}
int main() {
int t; cin>>t; while(t--)
{
long long n,k; cin>>n>>k;
long long a[n],flag=0;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++){
if(a[i]<k && (digit(a[i])==true)) {flag=1; cout<<a[i]<<" "; }
}
if(flag==0) cout<<-1;
cout<<endl;
}
return 0;
}