Home > Software design >  Find out if the numbers in an array are in increasing order (or) decreasing order (or) no order?
Find out if the numbers in an array are in increasing order (or) decreasing order (or) no order?

Time:07-17

public class javaProb{

     public static void main(String []args){
         int increasing=0,decreasing=0;
         int []a = {1,2,3,2,1};
         for(int i=0;i<a.length-1;i  ){
         if(a[i] < a[i 1]){
             increasing  ;
         }else{
             decreasing  ;
         }
     }
     if(increasing>0 && decreasing==0){
         System.out.println("Increasing");
     }else if(increasing==0 && decreasing>0){
         System.out.println("Decreasing");
     }else if(increasing>0 && decreasing>0){
         System.out.println("Increasing & Decreasing");
     }else if(??????????????????????????????){
         System.out.println("No order");
        }
    }
}

I can't find the logic behind the "No order"...Please anyone tell me the logic ....Thanks in advance :)

CodePudding user response:

I created a method that would accomplish what you are looking to accomplish.

The method below will return 1 if the array is increasing, 0 if the array is either null; in no order; or contains a single element, and -1 if the array is decreasing.

    public static int isArrayIncreasingOrDecreasing(int[] array) {
        boolean increasing = true;
        boolean decreasing = true;

        if (array == null || array.length <= 1) {
            return 0;
        }

        for (int i = 1; i < array.length; i  ) {
            if (increasing) {
                if (array[i] < array[i - 1]) {
                    increasing = false;
                }
            }
            if (decreasing) {
                if (array[i] > array[i - 1]) {
                    decreasing = false;
                }
            }
        }

        if (increasing) {
            return 1;
        } else if (decreasing) {
            return -1;
        }
        return 0;
    }

To implement this method within your main method you would do the following.

    public static void main(String[] args) {
        int result = isArrayIncreasingOrDecreasing(new int[]{1, 2, 3, 4, 5});
        if (result == -1) {
            System.out.println("The array is decreasing in order.");
        } else if (result == 0) {
            System.out.println("The array is in no order.");
        } else {
            System.out.println("The array is increasing in order.");
        }
    }

CodePudding user response:

There's an edge-case when all elements in the array are equal or the number of elements in the array less than 2. In such a case, we can't call this array unordered, it's definitely ordered (since doesn't require sorting - it's already arranged in order), but it's not possible to determine if the order is increasing or decreasing.

Describing this case as "Increasing & Decreasing" is a bit counterintuitive.

To approach this problem, we can maintain two boolean variables: one denoting whether the array is ordered or not, another denoting the type of order (ascending or descending). And while iterating over the given array we need to check whether the next pair of elements (previous and current) matches the order, if it's not the case we to set isOrdered to false and break the loop.

That's how it might be implemented.

public static void determineOrder(int[] arr) {
    
    boolean isAsc = arr[0] < arr[arr.length - 1]; // if array is sorted in ascending order the first element has to be less than the last element
    boolean isOrdered = true;
    
    for (int i = 1; i < arr.length; i  ) { // iteration starts from 1 because at each iteration step we need to compare `current` and `previous` elements
        int previous = arr[i - 1];
        int current = arr[i];
        
        if (previous > current && isAsc || previous < current && !isAsc) { // the order doesn't match
            isOrdered = false;
            break;
        }
    }
    // printing the result
    if (!isOrdered) {
        System.out.println("Unordered");
    } else if (arr[0] == arr[arr.length - 1]) {
        System.out.println("Impossible to determine the order");
    } else if (isAsc) {
        System.out.println("Array is sorted in Ascending order");
    } else {
        System.out.println("Array is sorted in Descending order");
    }
}

main()

public static void main(String[] args) {
    determineOrder(new int[]{1, 2, 3, 3, 3}); // ASC
    determineOrder(new int[]{7, 5, 3, 3, 5}); // unordered
    determineOrder(new int[]{7, 8, 9, 8, 8}); // unordered
    determineOrder(new int[]{7, 5, 3, 3, 1}); // DSC
    determineOrder(new int[]{9, 9, 9, 9, 9}); // impossible to determine the order
}

Output:

Array is sorted in Ascending order
Unordered
Unordered
Array is sorted in Descending order
Impossible to determine the order
  • Related