Java Program to Check if an Array is Decreasing
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is the Java Program to Check if an Array is Strictly Decreasing.
Given an array of integers check whether it is strictly decreasing or not.
A strictly decreasing array is an array whose each element is smaller than it’s preceding element.
Example:
Array = [5, 4, 3, 2, 1]
Output: Array is strictly decreasing.
Iterate through the array and check whether the current array element, is smaller than its preceding element if all the elements are smaller than its preceding element return true, else return false.
Here is the source code of the Java Program to Check if an Array is Strictly Decreasing. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Check if an Array is Strictly Decreasing
// Function to check array is strictly decreasing
{
}
}
}
// Function to read input and decreasing output
}
{
}
}
{
}
}
}
}
}
}
1. In function checkStrictlyDecreasing(), the loop for(i=0; i<array.length; i++) is used to iterate through the array.
2. The condition if(array[i] <= array[i+1]) checks if the current element is smaller than its following element.
3. If the condition is true, flag is set to false, indicating that the array is not strictly decreasing.
4. Finally, the flag variable is returned.
Time Complexity: O(n) where n is the number of elements in the array.