Programming Geek
Rated 4.7/5 based on 1446 reviews  ### CodeVita 2013 : Solution of Jumble With Numbers

Problem
In NASA, two researchers, Mathew and John, started their work on a new planet, but while practicing research they faced a mathematical difficulty. In order to save the time they divided their work.
So scientist Mathew worked on a piece and invented a number computed with the following formula:
A Mathew number is computed as follows using the formula:
H(n) = n(2n-1)
And scientist John invented another number which is built by the following formula which is called John number.
T(n) = n(n+1)/2
Now Mathew and John are jumbled while combining their work. Now help them combine their research work by finding out number in a given range that satisfies both properties. Using the above formula, the first few Mathew-John numbers are:
1 6 15 28 …

Input Format:

Input consists of 3 integers T1,T2,M separated by space . T1 and T2 are upper and lower limits of the range. The range is inclusive of both T1 and T2. Find Mth number in range [T1,T2] which is actually a Mathew-John number.

 Line 1 T1 T2 M,where T1 is upper limit of the range, T2 is lower limit of the range and M ,where Mth element of the series is required

Constraints:

0 < T1 < T2 < 1000000

Output Format:

Print Mth number from formed sequence between T1 and T2(inclusive).

 Line 1 For Valid Input,print Print Mth number from formed sequence between T1 and T2 Or No number is present at this index For Invalid Input,print Invalid Input

Sample Input and Output

SNo.InputOutput
1
90 150 2
120
2
20 80 6
No number is present at this index
3
-5 3 a
Invalid Input

Solution :
```import java.util.Scanner;

/**
*
* @author VIK
*/
public class JumbleWithNumbers {

public static void main(String[] args) {
int lower = 0;
int upper = 0;
int n = 0;
Scanner sc = new Scanner(System.in);
String[] s=sc.nextLine().trim().split(" ");
try {
boolean flag = false;
try {
lower = Integer.parseInt(s.trim());

} catch (Exception e) {
flag = true;
}
try {
upper = Integer.parseInt(s.trim());

} catch (Exception e) {
flag = true;
}
try {
n = Integer.parseInt(s.trim());
} catch (Exception e) {
flag = true;
}
if (flag) {
throw new Exception();
}
if (lower > upper) {
throw new Exception();
}

int i = 1;
flag = false;
int count = 0;
while (true) {
int nu = i * (2 * i - 1);

if (nu < lower) {
i++;
continue;
}
if (getMathew(lower,upper,nu)) {
count++;
if (count == n) {
System.out.println(nu);
break;
}

}

if (nu > upper) {
flag = true;
break;
}

i++;
}
if (flag) {
System.out.println("No number is present at this index");
}
} catch (Exception e) {
System.out.println("Invalid Input");
}
}

static boolean getMathew(int lower,int upper,int num) {

int i = 1;
while (true) {
int nu = i * (i + 1);
nu /= 2;
if (nu < lower) {
i++;
continue;
}

if (nu == num) {
return true;
}
if (nu > upper) {

break;
}

i++;

}
return false;
}
}

```