Monotonically Increasing Function

A monotonically increasing function is one where the function’s value increases as the input increases. That is, f(x+1) >= f(x) for all x in the domain.

Some examples of monotonically increasing functions:

  • Linear: f(x) = x
  • Quadratic: f(x) = x^2
  • Exponential: f(x) = e^x

Java example:

1
2
3
4
5
6
// Polynomial 
double func(double x) {
  return 2*x*x + 3*x + 1; 
}

// Increases as x increases

C++ example:

1
2
3
4
5
6
// Exponential  
double func(double x) {
  return exp(0.5*x);
}

// Always increasing

Python example:

1
2
3
4
5
6
7
# Logarithm
import math

def func(x):
  return math.log10(x)

# Monotonically increasing

Key properties:

  • Output increases as input increases
  • No decreasing portions over domain
  • Useful for modeling growth, returns, scaling

Monotonically increasing functions are common in physics, finance, and statistics for quantities that grow over time.

The term “monotonic” comes from the word “monotone,” which implies a single, unchanging tone. In mathematics, a monotonic function is one that either never increases or never decreases as you move along the domain. The idea is that the function behaves in a single, unchanging manner—either always going up or always going down (or staying the same, which is considered consistent with the “unchanging” behavior).

Both “monotonically increasing” and “monotonically decreasing” functions adhere to this concept of having a single, unchanging direction. That’s why the term “monotonic” is used as a descriptor for both. It signals that the function has a consistent behavior over its domain, whether that’s always increasing, always decreasing, or staying constant.

A visual representation of a monotonic function will show a curve or line that moves in a single, unchanging direction when viewed from left to right along the x-axis. Here are some key visuals:

  1. Monotonically Increasing Function: The curve or line will start at a lower point and move upwards as you go from left to right. It may stay flat for some segments but will never go down.

  2. Monotonically Decreasing Function: The curve or line will start at a higher point and move downwards as you go from left to right. Similar to the increasing function, it may stay flat for some segments but will never go up.

In both cases, the function might flatten out (stay constant) for certain segments but will never reverse direction. If you were to draw a tangent line at any point along the curve, the angle of the tangent line would either be positive (for increasing), zero (for constant), or negative (for decreasing), and that would remain consistent across the entire function.

A function ( f(x) ) is said to be monotonically increasing if, for every ( x_1 < x_2 ), ( f(x_1) \leq f(x_2) ). In simple terms, as the input value ( x ) increases, the output value ( f(x) ) also increases or stays the same; it never decreases.

A monotonically increasing function can contain duplicates. The formal definition states ( f(x_1) \leq f(x_2) ) for ( x_1 < x_2 ), which allows for the possibility of ( f(x_1) = f(x_2) ). Therefore, in the context of an array, it means that adjacent or non-adjacent elements can be equal and the array can still be considered monotonically increasing.

In the code examples, the use of the <= operator in the conditions would allow arrays with duplicates to be correctly identified as monotonically increasing.

Here is the code to check whether an array is monotonically increasing.

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Main {
    public static boolean isMonotonicallyIncreasing(int[] array) {
        for (int i = 1; i < array.length; i++) {
            if (array[i] < array[i - 1]) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        System.out.println(isMonotonicallyIncreasing(array));
    }
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>

bool isMonotonicallyIncreasing(std::vector<int>& array) {
    for (int i = 1; i < array.size(); ++i) {
        if (array[i] < array[i - 1]) {
            return false;
        }
    }
    return true;
}

int main() {
    std::vector<int> array = {1, 2, 3, 4, 5};
    std::cout << isMonotonicallyIncreasing(array) << std::endl;
    return 0;
}

Python

1
2
3
4
5
6
7
8
def is_monotonically_increasing(array):
    for i in range(1, len(array)):
        if array[i] < array[i - 1]:
            return False
    return True

array = [1, 2, 3, 4, 5]
print(is_monotonically_increasing(array))

In each code snippet, we loop through the array starting from the second element. We then compare each element to its predecessor. If we find an element that is smaller than its predecessor, we return false immediately; otherwise, we return true.