package algorithms.implementation;

import java.util.*;

/*
 * Problem Statement
 * 
 * A modified Kaprekar number is a positive whole number n with d digits, such
 * that when we split its square into two pieces - a right hand piece r with d
 * digits and a left hand piece l that contains the remaining d or d-1 digits,
 * the sum of the pieces is equal to the original number (i.e. l + r = n).
 * 
 * Note: r may have leading zeros.
 * 
 * Here's an explanation from Wikipedia about the ORIGINAL Kaprekar Number (spot
 * the difference!): In mathematics, a Kaprekar number for a given base is a
 * non-negative integer, the representation of whose square in that base can be
 * split into two parts that add up to the original number again. For instance,
 * 45 is a Kaprekar number, because 45^2 = 2025 and 20+25 = 45.
 * 
 * The Task
 * You are given the two positive integers p and q, where p is lower than q.
 * Write a program to determine how many Kaprekar numbers are there in the range
 * between p and q (both inclusive) and display them all.
 * 
 * Input Format: 
 * There will be two lines of input: p, lowest value q, highest value
 * 
 * Constraints:
 * 0 < p < q < 100000
 * 
 * Output Format: 
 * Output each Kaprekar number in the given range, space-separated on a single
 * line. If no Kaprekar numbers exist in the given range, print INVALID RANGE.
 * 
 * Sample Input: 
 * 1
 * 100
 * 
 * Sample Output: 
 * 1 9 45 55 99
 */
public class ModKaprekarNumbers {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int lowValue = sc.nextInt();
		int highValue = sc.nextInt();

		ArrayList<Integer> kapNums = new ArrayList<>();

		for (int i = lowValue; i <= highValue; i++) {
			if (isModKaprekar(i)) {
				kapNums.add(i);

			}
		}

		if (kapNums.size() == 0) {
			System.out.println("INVALID RANGE");
		} else {
			for (int num : kapNums) {
				System.out.print(num + " ");
			}
		}
		sc.close();
	}

	private static boolean isModKaprekar(int n) throws NumberFormatException {

		if (n == 1) {
			return true;
		} else if (n == 2 || n == 3) {
			return false;
		}
		
		long numSquare;
		
		if (n < 65536) {
			numSquare = n * n;
		} else {
			numSquare = (long) Math.pow(n, 2);
		}

		String number = numSquare + "";

		String left = number.substring(0, number.length() / 2);
		String right = number.substring(number.length() / 2);

		int leftNum = Integer.parseInt(left);
		int	rightNum = Integer.parseInt(right);


		return (leftNum + rightNum == n);
	}
}
Add ModKaprekarNumbers