forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
43 lines (33 loc) · 975 Bytes
/
solution.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
#include <bits/stdc++.h>
using namespace std;
// Implementation Part
void kaprekarNumbers(int p, int q) {
// as only few numbers are kaprekar in range [1, 100000],
// it efficent to store and then check
vector<int> knum = {1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4950, 5050, 7272, 7777, 9999, 17344, 22222, 77778, 82656, 95121, 99999};
// variable used if there is no Knum in [p, q]
int flag = 0;
// traverse knum vector and check whether any number belongs to [p, q]
// if belongs print it
for(int i = 0; i <= knum.size(); i++){
if(knum[i] >= p && knum[i] <= q){
flag = 1;
cout << knum[i] << " ";
}
}
// if no number is printed till now
// print 'INVALID RANGE'
if(!flag)
cout << "INVALID RANGE" << endl;
}
int main()
{
int p;
cin >> p;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
int q;
cin >> q;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
kaprekarNumbers(p, q);
return 0;
}