Java Program to Check if an Array is Increasing

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

Problem Description

Given an array of integers check whether it is strictly increasing or not.
A strictly increasing array is an array whose each element is greater than it’s preceding element.

Example:
Array = [1, 2, 3, 4, 5]

Output: Array is strictly increasing.

Problem Solution

Iterate through the array and check whether the current array element is greater than its preceding element. If all the elements are greater 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 Increasing. 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 Increasing
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class StrictlyIncreasing {
  8.     // Function to check array is strictly increasing or not.
  9.     static boolean checkStrictlyIncreasing(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.  
  22.     // Function to read the user input
  23.     public static void main(String[] args) {
  24.         BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
  25.         int size;
  26.         System.out.println("Enter the size of the array");
  27.         try{
  28.             size=Integer.parseInt(br.readLine());
  29.         }
  30.         catch(Exception e)
  31.         {
  32.             System.out.println("Invalid Input");
  33.             return;
  34.         }
  35.         int[] array=new int[size];
  36.         System.out.println("Enter array elements");
  37.         int i;
  38.         for(i=0;i<array.length;i++){
  39.             try{
  40.                 array[i]=Integer.parseInt(br.readLine());
  41.             }
  42.             catch(Exception e)
  43.             {
  44.                 System.out.println("An error occurred");
  45.                 return;
  46.             }
  47.         }
  48.         boolean result=checkStrictlyIncreasing(array);
  49.         if(result){
  50.             System.out.println("Array is strictly increasing");
  51.         }
  52.         else{
  53.             System.out.println("Array is not strictly increasing");
  54.         }
  55.     }
  56. }
Program Explanation

1. In function checkStrictlyIncreasing(), the loop for(i=0; i<array.length-1; i++) checks whether each array element is greater than the element following it, through the condition if(array[i] >= array[i+1]).
2. Any element that fulfils this condition, implies that the array is not strictly increasing, and it sets the result to false and breaks from the loop.
3. Finally, the boolean variable result is returned as the answer.

Note: Join free Sanfoundry classes at Telegram or Youtube

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
1
2
3
4
5
Array is strictly increasing
 
Case 2 (Negative Test Case):
 
Enter the size of the array
8
Enter array elements
1
2
2
3
4
5
6
6
Array is not strictly increasing
 
Case 3 (Positive Test Case - another example):
 
Enter the size of the array
7
Enter array elements
12
24
45
56
678
890
980
Array is strictly increasing

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.