1. Introduction

In this tutorial, we’ll study how to check whether the digits contained in a number are all increasing or decreasing and whether they increase or decrease alternatively.

2. Increasing or Not Decreasing – That Is the Question

One common occurrence when testing algorithms that are developed according to theoretical reasoning is that we risk encountering edge cases that will significantly worsen their efficiency. A common example of this, as it pertains to our current topic, relates to the algorithms that perform sorting over an array of numbers, such as insertion or bubble sort. If, for example, we provide a sorting algorithm with an array that’s already sorted, we risk unnecessarily increasing its computational time. For this reason, it’s a good idea to have the skill to test whether a sequence of symbols is already sorted in our programming toolbox.

First, we must conceptually distinguish between a strictly increasing sequence and a non-decreasing sequence.

Let’s consider these sequences:

  • 1, 2, 3, 4, 5: each element in this sequence is greater than the one that precedes it, for this reason, the sequence is called “increasing”
  • 1, 2, 3, 4, 4: each element in this sequence is greater than or equal to the one before it; for this reason, the sequence doesn’t decrease but doesn’t strictly increase either, and thus we call it “not decreasing”

3. Strictly Increasing or Strictly Decreasing Sequences

To test whether a sequence of digits is strictly increasing or decreasing, we have to test whether the sign of the differential remains the same; that is, whether \text{sgn} (\Delta a) | \Delta a = a_{n+1} - a_n, where a_n is the n-th digit in the sequence, doesn’t change. Two cases are possible:

  • if the sequence is strictly increasing, then \text{sgn} (\Delta a) is always positive
  • if the sequence is strictly decreasing, then \text{sgn} (\Delta a) is always negative

From here onward, we assume that all digits from a number have been stored in an array of integers. On that basis, this is how we can test for the first condition in pseudocode:

function checkIncreasing(int arrayOfDigits, int arraySize):
    // INPUT
    //    arrayOfDigits = array containing the sequence of digits, of size arraySize
    // OUTPUT
    //    returns true if all digits are strictly increasing, false otherwise
    
    for i <- 0 to (arraySize - 2): 
        delta <- arrayOfDigits[i + 1] - arrayOfDigits[i] 
        if delta <= 0: 
            return false 
    return true

Similarly, we can check whether the function is strictly decreasing just by flipping the sign of the comparison operator:

function checkDecreasing(int arrayOfDigits, int arraySize):
...
        if delta >= 0 then: 
            return false 
...

The rest of the function remains unchanged.

4. Non-Decreasing and Non-Increasing Sequences

In a similar manner to how we modified the comparison operator to go from checkIncreasing to checkDecreasing, we can do the same to test for non-decreasing and non-increasing sequences:

function checkNonDecreasing(int arrayOfDigits, int arraySize):
...
        if delta < 0 then: 
            return false 
...

As in the previous cases, to check for non-increasing sequences we have to flip the direction of the comparison operator:

function checkNonIncreasing(int arrayOfDigits, int arraySize):
...
        if delta > 0 then: 
            return false 
...

5. Sequences Increasing and Decreasing Alternatively

Finally, we can consider the case in which we want the sequence to increase and decrease. To check for it, we need an additional variable, previousStepDelta, to compare the differential that was observed during the previous element in the array with the differential currentStepDelta pertaining to the current element.

First, we consider the edge cases where arrayOfDigits has only one or two elements, and in that case, we consider the sequence to be alternating:

function checkAlternating(int arrayOfDigits, int arraySize):
    if arraySize <= 2:
        return true
    else:
...

Secondly, if the array contains more than 2 elements, we compare the sign of each sequential delta.

...
    else:
        for i <- 0 to (arraySize - 2):
            previousStepDelta <- arrayOfDigits[i+1] - arrayOfDigits[i]
            currentStepDelta <- arrayOfDigits[i+2] - arrayOfDigits[i+1]
...

Lastly, if these two values were ever to have the same sign, then the sequence doesn’t alternate and we can break from the loop. Otherwise, the sequence is alternating and we can return true:

...
            if ((previousStepDelta > 0) and (currentStepDelta > 0))
                 or ((previousStepDelta < 0) and (currentStepDelta < 0)):
                return false
    return true

6. Conclusion

In this article, we studied how to check whether a sequence of numbers is increasing/decreasing or non-increasing and not decreasing.

Finally, we also analyzed how to check whether a sequence of digits is alternating between increasing and decreasing.