Please help me with this…
I know this program works good in ide installed in our local PC but when I copy paste the same code in the codechef ide it is giving me number format exception.
Do I need to perform null checks? but why?
public class Codechef {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int len = Integer.parseInt(bf.readLine());
String str[] = bf.readLine().split("\\s");
int ar[] = Arrays.stream(str).mapToInt(Integer::parseInt).toArray();
Arrays.sort(ar);
int ans = ar[len - 2];
System.out.println(ans);
}
}
Since I am good with java I coded it in the same, but it is not working? Should I switch to c++?
This is the question:
Chef and Dhyey have become friends recently. Chef wants to test Dhyey’s intelligence by giving him a puzzle to solve.
The puzzle contains an integer sequence A1,A2,…,AN. The answer to the puzzle is the maximum of Ai%Aj, taken over all valid i, j.
Help Dhyey solve this puzzle.
Input
The first line of each test case contains a single integer N.
The second line contains N space-separated integers A1,A2,…,AN.
Output
For each test case, print a single line containing one integer — the answer to the puzzle.
Constraints
2≤N≤105
1≤Ai≤109 for each valid i
Subtasks
Subtask #1 (30 points): 2≤N≤1,000
Subtask #2 (70 points): 2≤N≤105
Example Input 1
5
1 2 3 4 5
Example Output 1
4
Example Input 2
6
5 5 5 2 3 8
Example Output 2
5
Sure, this is not real java enviroment, this is html/javascript web client for sending your code (text file) to server where is no human to types keyboard to input.
So you can test if input is empty, or catch number format exception and inform user about correct input format
also can you give link to this prob so that we can try it
Edit:- got it, its prob from this long.
btw i usually split the string with " " since they are space separated
Hi,
Even if we write split("\s") it will do the same there is nothing wrong with that,
So what I realized is it is just showing you the error while compiling the code but if you submit the code, it will remove those Re error& exceptions.
And scanner will always throws the error at compilation time but it will consider the class on submitting it