Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: May 16, 2024
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.
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:
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 , where
is the
-th digit in the sequence, doesn’t change. Two cases are possible:
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.
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
...
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
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.