Home > Software engineering >  Find out if the numbers in an array are in increasing order , decreasing order , Increasing and decr
Find out if the numbers in an array are in increasing order , decreasing order , Increasing and decr

Time:07-23

Question : Find out if the numbers in an array are in increasing order 

(or) decreasing order (or) Increasing and decreasing (or) decreasing and increasing (or) no order? Test case 1 : [1,2,3] -> "Increasing" Test case 2 : [3,2,1] -> "Decreasing" Test case 3 : [1,2,3,2,1] -> "Increasing and decreasing" Test case 4 : [3,2,1,2,3] -> "Decreasing and Increasing" Test case 5 : [1,2,3,2,1,2] (0r) [1,1,1,1,1] (or) [1] (or) [] ->"No order"

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:

One way to test whether an array is in ascending order is to sort it in ascending order, and if it stays the same, it is in ascending order. The same goes for descending.

Now, if an array stays the same when sorted in ascending and descending order, it is in both these orders. That happens when all array elements are equal and when the array is empty.

Finally, if an array changes when sorted in both ascending and descending order, it is in none of these orders. It lacks order.

With this instrumental view on the issue, one does not even have to know the array contents, just whether it changes. And there are no corner cases to consider.

My suggested test works conceptually to support thinking about the issue. It also works implemented in any programming language using standard sorting because there is an overwhelming consensus on what constitutes ascending/descending 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