Monotonically Decreasing Function

A monotonically decreasing function is one whose value decreases as its input increases. That is, f(x+1) <= f(x) for all x in the domain.

Some examples:

-reciprocal: f(x) = 1/x -negative logarithm: f(x) = -ln(x)
-non-positive powers: f(x) = x^-a, a > 0

Java example:

1
2
3
4
5
6
// Exponential decay 
double func(double x) {
  return Math.exp(-2*x);
}

// Values decrease as x increases

C++ example:

1
2
3
4
5
6
// Reciprocal
double func(double x) {
  return 1.0 / x;
}

// Monotonically decreases

Python example:

1
2
3
4
5
# Negative power function
def func(x):
  return x**(-3)

# Values always decrease as x increases

Key properties:

  • Function output decreases as input increases
  • No increasing portions over domain
  • Useful for modeling decay, attenuation, consumption

Monotonically decreasing functions appear in physics, statistics, economics to model declining quantities.

A function ( f(x) ) is said to be monotonically decreasing if for every pair ( x_1, x_2 ) such that ( x_1 < x_2 ), ( f(x_1) \geq f(x_2) ). This means the function does not increase as its input increases; it either stays the same or decreases. The same concept can apply to arrays, where each subsequent element is either the same as or smaller than the previous one.

Duplicates are allowed in a monotonically decreasing function or sequence. In the formal definition, the condition is ( f(x_1) \geq f(x_2) ) for ( x_1 < x_2 ), where the ( \geq ) symbol indicates that the function value can either decrease or stay the same as ( x ) increases. Therefore, duplicates do not violate the condition for a sequence or function to be monotonically decreasing.

Code Explanation

Java

1
2
3
4
5
6
7
8
public static boolean isMonotonicDecreasing(int[] arr) {
    for(int i = 1; i < arr.length; i++) {
        if(arr[i] > arr[i-1]) {
            return false;
        }
    }
    return true;
}

In this Java code, we loop through the array starting from the second element. If we find an element greater than the previous one, the array is not monotonically decreasing.

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <iostream>
#include <vector>
using namespace std;

bool isMonotonicDecreasing(vector<int> arr) {
    for(int i = 1; i < arr.size(); ++i) {
        if(arr[i] > arr[i-1]) {
            return false;
        }
    }
    return true;
}

In the C++ code, we use a vector<int> and iterate through it, checking that every subsequent element is less than or equal to the previous one.

Python

1
2
3
4
5
def is_monotonic_decreasing(arr):
    for i in range(1, len(arr)):
        if arr[i] > arr[i-1]:
            return False
    return True

In Python, the structure is the same as in Java and C++. We loop through the list arr and if we find an element that is greater than the previous, we return False.

In each of these implementations, the use of the > operator means arrays with duplicates will still be classified as monotonically decreasing. This adheres to the formal definition.