Java Program to Check if an Array is Decreasing

This is the Java Program to Check if an Array is Strictly Decreasing.

Problem Description

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.

Problem Solution

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.

Program/Source Code
advertisement
advertisement

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.

  1.  
  2. //Java Program to Check if an Array is Strictly Decreasing
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class StrictlyDecreasing {
  8.     // Function to check array is strictly decreasing 
  9.     static boolean checkStrictlyDecreasing(int[] array){
  10.     boolean result=true;
  11.     int i;
  12.     for(i=0;i<array.length-1;i++){
  13.         if(array[i]<=array[i+1])
  14.         {
  15.             result=false;
  16.             break;
  17.         }
  18.       }
  19.     return result;
  20.     }
  21.     // Function to read input and decreasing output
  22.     public static void main(String[] args) {
  23.         BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
  24.         int size;
  25.         System.out.println("Enter the size of the array");
  26.         try{
  27.             size=Integer.parseInt(br.readLine());
  28.         }
  29.         catch(Exception e)
  30.         {
  31.             System.out.println("Invalid Input");
  32.             return;
  33.         }
  34.         int[] array=new int[size];
  35.         System.out.println("Enter array elements");
  36.         int i;
  37.         for(i=0;i<array.length;i++){
  38.             try{
  39.                 array[i]=Integer.parseInt(br.readLine());
  40.             }
  41.             catch(Exception e)
  42.             {
  43.                 System.out.println("An error occurred");
  44.             }
  45.         }
  46.         boolean result=checkStrictlyDecreasing(array);
  47.         if(result){
  48.             System.out.println("Array is strictly decreasing");
  49.         }
  50.         else{
  51.             System.out.println("Array is not strictly decreasing");
  52.         }
  53.     }
  54. }
Program Explanation

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.

Runtime Test Cases
 
Case 1 (Positive Test Case):
 
Enter the size of the array
5
Enter array elements
5
4
3
2
1
Array is strictly decreasing
 
Case 2 (Negative Test Case):
 
Enter the size of the array
8
Enter array elements
6
5
5
4
3
2
1
1
Array is not strictly decreasing

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.