CompareToMax Function

A compareToMax function allows an object to be compared against the maximum value in a collection based on some comparison logic.

It takes the object and collection as input, finds the max element, compares the object to max and returns the result.

This encapsulates the logic to fetch max and compare in one reusable function.

It is useful when repeated comparisons against the maximum are needed, like checking thresholds.

Solution

Here is an example to check if an integer is less than maximum in array:

Java

1
2
3
4
5
6
7
8
9
boolean compareToMax(int num, int[] arr) {
  int max = Integer.MIN_VALUE;
  
  for (int n : arr) {
    max = Math.max(n, max); 
  }

  return num < max;
}

C++

1
2
3
4
5
6
7
8
9
bool compareToMax(int num, vector<int> vec) {
  int max = INT_MIN;

  for (int n : vec) {
    max = std::max(n, max);
  }

  return num < max; 
}

Python

1
2
3
4
5
6
7
8
def compare_to_max(num, arr):

  max_num = -float("inf")

  for n in arr:
    max_num = max(n, max_num)

  return num < max_num

We find max value first before comparing based on logic needed.

This simplifies repeated comparisons against max.

Description: CompareToMax Function

The CompareToMax Function is a specific utility function that compares each element in a collection to the maximum element in that collection. The outcome of the comparison can serve various purposes like identifying the distance of each element from the maximum or understanding how each element stacks up against the maximum.

Solution

Here are code samples in Java, C++, and Python demonstrating the CompareToMax Function.

Java

In Java, you can use the Collections.max() function to find the maximum element, and then iterate through the array to compare each element to the maximum.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import java.util.Arrays;

public class Main {
    public static void compareToMax(int[] arr) {
        int max = Arrays.stream(arr).max().getAsInt();
        for (int num : arr) {
            System.out.println("Difference between " + num + " and max " + max + " is: " + (max - num));
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 5, 3};
        compareToMax(arr);
    }
}

C++

In C++, you can use the std::max_element function to find the maximum element, and then loop through the vector to compare each element to this maximum.

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

void compareToMax(const std::vector<int>& vec) {
    int max = *std::max_element(vec.begin(), vec.end());
    for (const auto& num : vec) {
        std::cout << "Difference between " << num << " and max " << max << " is: " << max - num << std::endl;
    }
}

int main() {
    std::vector<int> vec = {1, 5, 3};
    compareToMax(vec);
    return 0;
}

Python

In Python, you can use the max() function to find the maximum element and then use a for loop to compare each element to this maximum.

1
2
3
4
5
6
7
def compare_to_max(arr):
    max_val = max(arr)
    for num in arr:
        print(f"Difference between {num} and max {max_val} is: {max_val - num}")

arr = [1, 5, 3]
compare_to_max(arr)

Key Takeaways

  • The CompareToMax function compares each element in the collection to the maximum element.
  • This operation is helpful in understanding the distribution of elements in relation to the maximum value.
  • The time complexity of this function is O(n), as it requires a single loop through the collection after finding the maximum element.
  • Code examples are provided in Java, C++, and Python, and they employ built-in functions to find the maximum element for comparison.