-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModified_Kaprekar_Numbers.cpp
More file actions
52 lines (37 loc) · 968 Bytes
/
Modified_Kaprekar_Numbers.cpp
File metadata and controls
52 lines (37 loc) · 968 Bytes
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
/**
* Title : Modified Kaprekar Numbers
* Author : Tridib Samanta
* Link : https://www.hackerrank.com/challenges/kaprekar-numbers/problem
**/
#include <bits/stdc++.h>
using namespace std;
int _stoi(string s) {
int n = 0;
for (int i = 0; i < s.length(); ++i)
n = (n * 10) + (s[i] - '0');
return n;
}
bool isModifiedKaprekar(int n) {
long n_sq = 1L * n * n;
string s = to_string(n_sq);
int d = s.length() / 2;
string left = s.substr(0, d);
string right = s.substr(d);
int num_l = _stoi(left);
int num_r = _stoi(right);
return ((num_l + num_r == n) ? true : false);
}
int main() {
int p, q;
cin >> p >> q;
bool flag = false;
for(int i = p; i <= q; ++i) {
if (isModifiedKaprekar(i)) {
cout << i << ' ';
flag = true;
}
}
if (!flag)
cout << "INVALID RANGE";
return 0;
}