forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (37 loc) · 1.4 KB
/
Solution.java
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
//Problem: https://www.hackerrank.com/challenges/kaprekar-numbers
//Java 8
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int p = input.nextInt();
int q = input.nextInt();
boolean flag = false;//Denotes if a Kaprekar number has been found
if(p == 1)//Handle the one case where a 1 digit square is a Kaprekar
{
System.out.print(i+" ");
flag = true;
}
for(long i = p; i <= q; i++)
{
long squareLong = i * i;
String squareStr = String.valueOf(squareLong);
if(squareStr.length() > 1)
{
long left = Long.parseLong(squareStr.substring(0,(int)Math.ceil(squareStr.length()/2)));
long right = Long.parseLong(squareStr.substring((int)Math.ceil(squareStr.length()/2), squareStr.length()));
if(left+right == i)
{
System.out.print(i+" ");
flag = true;
}
}
}
if(!flag)
{
System.out.println("INVALID RANGE");
}
}
}