forked from amankayat/HackerRank-Solution-Algorithm-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaprekar_num.cpp
More file actions
67 lines (53 loc) · 1.37 KB
/
kaprekar_num.cpp
File metadata and controls
67 lines (53 loc) · 1.37 KB
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
#include<iostream>
#include<stdio.h>
#include<sstream>
#include<vector>
#include<cmath>
using namespace std;
int count=0;
vector<int> kaprekar_numbers(int p,int q){
string str,str_c;
int count=0;
vector<int> store;
for(int i=p;i<=q;i++){
int len_str = 0,len_str2=0;
string str = "",str2 ="";
long int sq = pow(i,2);
stringstream ss;
ss<<i;
ss>>str;
len_str = str.length();
stringstream ss1;
ss1<<sq;
ss1>>str2;
len_str2=str2.length();
if(len_str2==2*len_str || len_str2==(2*len_str)-1){
string r = str2.substr(len_str2-len_str,len_str);
string l = str2.substr(0,len_str2-len_str);
long in_r=0,in_l=0;
stringstream s;
s<<r;
s>>in_r;
stringstream s1;
s1<<l;
s1>>in_l;
if(in_r + in_l == i){
store.push_back(i);
count++;
}
}
}
return store;
}
int main(){
int p,q;
cin>>p>>q;
vector<int> res;
res = kaprekar_numbers(p,q);
//display
if(res.size()==0)
cout<<"INVALID RANGE";
else
for(int i=0;i<res.size();i++)
cout<<res[i]<<" ";
}