Specific pair in the Matrix

Definition & Explanation

Example:
Input:
Output:

Program Logic

Desc

C

Method 1 :

#include <stdio.h>
#include <limits.h>

int findSpecificPair(int matrix[3][3]) {
    int rows = 3, cols = 3;
    int max_element = INT_MIN, min_element = INT_MAX;

    // Find the minimum and maximum elements in the matrix
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            min_element = (matrix[i][j] < min_element) ? matrix[i][j] : min_element;
            max_element = (matrix[i][j] > max_element) ? matrix[i][j] : max_element;
        }
    }

    int max_diff = INT_MIN;
    // Find the maximum absolute difference between any two elements in the matrix
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int diff = abs(matrix[i][j] - max_element);
            max_diff = (diff > max_diff) ? diff : max_diff;
        }
    }

    return max_diff;
}

int main() {
    int matrix[3][3] = {
        {1, 2,

Output :

C++

Method 1 :

#include <iostream>
#include <vector>
#include <climits>

using namespace std;

int findSpecificPair(vector<vector<int>>& matrix) {
    int rows = matrix.size(), cols = matrix[0].size();
    int max_element = INT_MIN, min_element = INT_MAX;

    // Find the minimum and maximum elements in the matrix
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            min_element = min(matrix[i][j], min_element);
            max_element = max(matrix[i][j], max_element);
        }
    }

    int max_diff = INT_MIN;
    // Find the maximum absolute difference between any two elements in the matrix
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            int diff = abs(matrix[i][j] - max_element);
            max_diff = max(diff, max_diff);
        }
    }

    return max_diff;
}

int main() {
    vector<vector<int>> matrix = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    int max_diff = findSpecificPair(matrix);
    cout << "Maximum absolute difference: " << max_diff << endl; // Output: 8 (|9 - 1| =

Output :

JAVA

Method 1 :

public class Main {
    public static int findSpecificPair(int[][] matrix) {
        int rows = matrix.length, cols = matrix[0].length;
        int max_element = Integer.MIN_VALUE, min_element = Integer.MAX_VALUE;

        // Find the minimum and maximum elements in the matrix
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                min_element = Math.min(matrix[i][j], min_element);
                max_element = Math.max(matrix[i][j], max_element);
            }
        }

        int max_diff = Integer.MIN_VALUE;
        // Find the maximum absolute difference between any two elements in the matrix
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                int diff = Math.abs(matrix[i][j] - max_element);
                max_diff = Math.max(diff, max_diff);
            }
        }

        return max_diff;
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        int max_diff = findSpecificPair(matrix);
        System.out.println("Maximum absolute difference: " + max_diff); // Output: 8 (|9 - 1| = 8)
    }
}

Output :

Python

Method 1 :

def find_specific_pair(matrix):
    if not matrix or not matrix[0]:
        return None

    rows, cols = len(matrix), len(matrix[0])
    max_element = matrix[0][0]
    min_element = matrix[0][0]

    # Find the minimum and maximum elements in the matrix
    for row in matrix:
        for elem in row:
            min_element = min(min_element, elem)
            max_element = max(max_element, elem)

    max_diff = -1
    for i in range(rows):
        for j in range(cols):
            diff = abs(matrix[i][j] - max_element)
            max_diff = max(max_diff, diff)

    return max_diff

# Example usage:
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
max_diff = find_specific_pair(matrix)
print("Maximum absolute difference:", max_diff)  # Output: 8 (|9 - 1| = 8)

Output :

Maximum absolute difference: 8