📜  在矩阵的任何列中查找具有最大差异的对

📅  最后修改于: 2021-04-26 19:37:10             🧑  作者: Mango

给定正整数的N * N矩阵。任务是找到矩阵的任何列中的任何一对元素之间的最大差异。

例子

Input : mat[N][N] = {
        { 1, 2, 3, 4, 5 },
        { 5, 3, 5, 4, 0 },
        { 5, 6, 7, 8, 9 },
        { 0, 6, 3, 4, 12 },
        { 9, 7, 12, 4, 3 },
    };
Output : Max difference : 12
5th column has the pair with max difference
(0, 12)

Input : mat[N][N] = {
        { 1, 2, 3 },
        { 5, 3, 5 },
        { 9, 6, 7 }
    };
Output : Max difference : 8

这样做的目的是观察在任何列中具有最大差异的那对是该列的最大和最小元素之间的差异。现在,明智地遍历矩阵列。找到每列的max和min元素之间的差,然后返回最大差。

下面是上述方法的实现:

C++
// C++ program to find column with
// max difference of any pair of elements
#include 
using namespace std;
  
#define N 5 // No of rows and column
  
// Function to find the column with max difference
int colMaxDiff(int mat[N][N])
{
    int max_diff = INT_MIN;
  
    // Traverse matrix column wise
    for (int i = 0; i < N; i++) {
  
        // Insert elements of column to vector
        int max_val = mat[0][i], min_val = mat[0][i];
        for (int j = 1; j < N; j++) {
            max_val = max(max_val, mat[j][i]);
            min_val = min(min_val, mat[j][i]);
        }
  
        // calculating difference between maximum and
        // minimum
        max_diff = max(max_diff, max_val - min_val);
    }
  
    return max_diff;
}
  
// driver code
int main()
{
    int mat[N][N] = {
        { 1, 2, 3, 4, 5 },
        { 5, 3, 5, 4, 0 },
        { 5, 6, 7, 8, 9 },
        { 0, 6, 3, 4, 12 },
        { 9, 7, 12, 4, 3 },
    };
  
    cout << "Max difference : " << colMaxDiff(mat) << endl;
  
    return 0;
}


Java
// Java program to find column with 
// max difference of any pair of elements 
  
public class GOC1 {
  
    static int N = 5; // No of rows and column 
  
    // Function to find the column with max difference
    static int colMaxDiff(int mat[][]) {
        int max_diff = Integer.MIN_VALUE;
  
        // Traverse matrix column wise
        for (int i = 0; i < N; i++) {
  
            // Insert elements of column to vector
            int max_val = mat[0][i], min_val = mat[0][i];
            for (int j = 1; j < N; j++) {
                max_val = Math.max(max_val, mat[j][i]);
                min_val = Math.min(min_val, mat[j][i]);
            }
  
            // calculating difference between maximum and
            // minimum
            max_diff = Math.max(max_diff, max_val - min_val);
        }
  
        return max_diff;
    }
  
    // Driver Code
    public static void main(String args[]) {
        int mat[][] = { 
                { 1, 2, 3, 4, 5 }, 
                { 5, 3, 5, 4, 0 }, 
                { 5, 6, 7, 8, 9 }, 
                { 0, 6, 3, 4, 12 }, 
                { 9, 7, 12, 4, 3 }, 
            }; 
            
        System.out.println("Max difference : "+colMaxDiff(mat));
    }
}


Python3
# Python3 program to find column with
# max difference of any pair of elements
  
N = 5 # No of rows and column
  
# Function to find the column 
# with max difference
def colMaxDiff(mat):
  
    max_diff = 0
  
    # Traverse matrix column wise
    for i in range (N):
  
        # Insert elements of column 
        # to vector
        max_val = mat[0][i]
        min_val = mat[0][i]
        for j in range (1, N) :
            max_val = max(max_val, mat[j][i])
            min_val = min(min_val, mat[j][i])
  
        # calculating difference between 
        # maximum and minimum
        max_diff = max(max_diff, max_val - 
                                 min_val)
  
    return max_diff
  
# Driver Code
if __name__ == "__main__":
    mat = [[ 1, 2, 3, 4, 5 ],
           [5, 3, 5, 4, 0 ],
           [ 5, 6, 7, 8, 9 ],
           [ 0, 6, 3, 4, 12 ],
           [ 9, 7, 12, 4, 3 ]]
  
    print ("Max difference :", colMaxDiff(mat))
  
# This code is contributed by ita_c


C#
// C# program to find column  
// with max difference of 
// any pair of elements 
using System;
  
class GFG 
{
  
static int N = 5; // No of rows and column 
  
// Function to find the column
// with max difference
static int colMaxDiff(int [,]mat) 
{
    int max_diff = int.MinValue;
  
    // Traverse matrix column wise
    for (int i = 0; i < N; i++)
    {
  
        // Insert elements of column
        // to vector
        int max_val = mat[0, i], 
            min_val = mat[0, i];
        for (int j = 1; j < N; j++) 
        {
            max_val = Math.Max(max_val, 
                               mat[j, i]);
            min_val = Math.Min(min_val, 
                               mat[j, i]);
        }
  
        // calculating difference between
        // maximum and minimum
        max_diff = Math.Max(max_diff, 
                            max_val - min_val);
    }
  
    return max_diff;
}
  
// Driver Code
public static void Main() 
{
    int [,]mat = { { 1, 2, 3, 4, 5 }, 
                   { 5, 3, 5, 4, 0 }, 
                   { 5, 6, 7, 8, 9 }, 
                   { 0, 6, 3, 4, 12 }, 
                   { 9, 7, 12, 4, 3 }}; 
          
    Console.WriteLine("Max difference : " + 
                          colMaxDiff(mat));
}
}
  
// This code is contributed
// by Subhadeep


PHP


输出:
Max difference : 12

时间复杂度: O(n ^ 2)