Java: Check if an array of integers contains three increasing adjacent numbers
Three Increasing Adjacent Numbers
Write a Java program to check if an array of integers contains three increasing adjacent numbers.
Pictorial Presentation:
Sample Solution:
Java Code:
import java.util.*;
public class Exercise107 {
public static void main(String[] args) {
// Initialize an integer array with some values
int[] array_nums = {11, 12, 13, 14, 45, 20};
// Print the original array
System.out.println("Original Array: " + Arrays.toString(array_nums));
// Initialize a result variable to 1
int result = 1;
// Iterate through the array to check for consecutive integers
for (int i = 0; i <= array_nums.length - 3; i++) {
// Check if the current number, the next, and the one after that are consecutive
if (array_nums[i] + 1 == array_nums[i + 1] && array_nums[i + 1] + 1 == array_nums[i + 2]) {
// If they are consecutive, set the result to 0
result = 0;
}
}
// Check the result and print either true or false
if (result == 1) {
System.out.printf(String.valueOf(false));
} else {
System.out.printf(String.valueOf(true));
}
// Print a newline character for formatting
System.out.printf("\n");
}
}
Sample Output:
Original Array: [11, 12, 13, 14, 45, 20] true
Flowchart:
Java Code Editor:
Previous: Write a Java program to create a new array that is left shifted from a given array of integers.
Next: Write a Java program to add all the digits of a given positive integer until the result has a single digit.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics