Question
(JAVA) Let the user to input a sequence of positive integers by Scanner, and this number sequence shall be stored in an array. The length
(JAVA) Let the user to input a sequence of positive integers by Scanner, and this number sequence shall be stored in an array. The length of this array is set to be less than 1,000,000. The value of every number in this array is greater than 0 and less than or equal to 232 -1. The number sequence shall end with -1. After the number -1, a number k will also be given by the user. Your job is to find the next number in the array after k. Please print your result to the console. If the length of this array is 0, please print DNE. If k does not exist in this array, please print DNE. If k is located at the end of this array, please print DNE. If the array contains duplicating ks, please find the next number after the first k. Sample input/output: Input 20 32 6 54 12 20 -1 6 Output 54 Explanation The user has input a sequence of positive integers [20, 32, 6, 54, 12, 20], and the -1 denotes the end of this sequence. You need to store this sequence into an array, so it looks like this: Index: 0 1 2 3 4 5 Value: 20 32 6 54 12 20 The user provided the number k after -1. In this case, k is 6. The next number in the array after 6 is 54. Hence, the output is 54. Input 20 32 6 7 8 -1 2 Output DNE Explanation The array would be [20, 32, 6, 7, 8], and k is 2. Apparently, 2 does not exist in this array. Hence, the output is DNE. Input 1 2 3 3 4 4 5 -1 3 Output 3 Explanation The array would be [1, 2, 3, 3, 4, 4, 5], and k is 3. We have multiple 3s in this array. The output should be the number after the first 3. Hence, the output is 3.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started