Product of maximum in first array and minimum in second in C

Given two arrays, we need to find the product of the maximum element from the first array and the minimum element from the second array. This is a common programming problem that demonstrates array traversal and basic mathematical operations.

Syntax

int findMaxElement(int arr[], int size);
int findMinElement(int arr[], int size);
int calculateProduct(int arr1[], int arr2[], int n1, int n2);

Method 1: Using Sorting Approach

In this approach, we sort both arrays and then multiply the last element of the first array with the first element of the second array −

#include <stdio.h>

void sortArray(int arr[], int n) {
    int temp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i] > arr[j]) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

int minMaxProduct(int arr1[], int arr2[], int n1, int n2) {
    sortArray(arr1, n1);
    sortArray(arr2, n2);
    return arr1[n1 - 1] * arr2[0];
}

int main() {
    int arr1[] = {2, 3, 9, 11, 1};
    int arr2[] = {5, 4, 2, 6, 9};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    
    printf("Array 1: ");
    for (int i = 0; i < n1; i++) {
        printf("%d ", arr1[i]);
    }
    printf("\nArray 2: ");
    for (int i = 0; i < n2; i++) {
        printf("%d ", arr2[i]);
    }
    
    int result = minMaxProduct(arr1, arr2, n1, n2);
    printf("\nProduct of maximum and minimum: %d<br>", result);
    
    return 0;
}
Array 1: 2 3 9 11 1 
Array 2: 5 4 2 6 9 
Product of maximum and minimum: 22

Method 2: Using Linear Search (More Efficient)

This approach finds the maximum and minimum elements without sorting, which is more efficient −

#include <stdio.h>

int findMax(int arr[], int n) {
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}

int findMin(int arr[], int n) {
    int min = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
    }
    return min;
}

int main() {
    int arr1[] = {6, 2, 5, 4, 1};
    int arr2[] = {3, 7, 5, 9, 6};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    
    int max1 = findMax(arr1, n1);
    int min2 = findMin(arr2, n2);
    int product = max1 * min2;
    
    printf("Maximum in first array: %d<br>", max1);
    printf("Minimum in second array: %d<br>", min2);
    printf("Product: %d * %d = %d<br>", max1, min2, product);
    
    return 0;
}
Maximum in first array: 6
Minimum in second array: 3
Product: 6 * 3 = 18

Comparison

Method Time Complexity Space Complexity Pros Cons
Sorting O(n²) O(1) Simple to understand Modifies original arrays
Linear Search O(n) O(1) More efficient, preserves arrays Requires separate functions

Conclusion

The linear search approach is more efficient with O(n) time complexity compared to the sorting method's O(n²). It also preserves the original arrays, making it the preferred solution for this problem.

Updated on: 2026-03-15T12:56:13+05:30

375 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements