Java: Consecutive Numbers in an array
76. Check if numbers in array can form a consecutive list
Write a Java program to determine whether numbers in an array can be rearranged so that each number appears exactly once in a consecutive list of numbers. Return true otherwise false.
Sample Data:
{1, 2 ,5, 0, 4, 3, 6} -> true
{1, 2 ,5, 0, 3, 6, 7} ->false
Pictorial Presentation:
Sample Solution-1:
Java Code:
Sample Output:
Original array: [1, 2, 5, 0, 4, 3, 6] Check consecutive numbers in the said array!true
Flowchart:
Sample Solution-2:
Java Code:
Sample Output:
Original array: [1, 2, 5, 0, 3, 6, 7] Check consecutive numbers in the said array!false
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to check if an array can be rearranged into an arithmetic sequence.
- Write a Java program to determine if an array contains all numbers in a given range.
- Write a Java program to find the missing numbers in a sequence from a given array.
- Write a Java program to check if an array can be rearranged to form a sequence with a constant difference.
Go to:
PREV : Calculate the largest gap between sorted elements in array.
NEXT : Check if array alternates between positive and negative.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.