
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find pair with maximum difference in any column of a Matrix in C++
Suppose we have one matrix or order NxN. We have to find a pair of elements which forms maximum difference from any column of the matrix. So if the matrix is like −
1 | 2 | 3 |
5 | 3 | 5 |
9 | 6 | 7 |
So output will be 8. As the pair is (1, 9) from column 0.
The idea is simple, we have to simply find the difference between max and min elements of each column. Then return max difference.
Example
#include<iostream> #define N 5 using namespace std; int maxVal(int x, int y){ return (x > y) ? x : y; } int minVal(int x, int y){ return (x > y) ? y : x; } int colMaxDiff(int mat[N][N]) { int diff = INT_MIN; for (int i = 0; i < N; i++) { int max_val = mat[0][i], min_val = mat[0][i]; for (int j = 1; j < N; j++) { max_val = maxVal(max_val, mat[j][i]); min_val = minVal(min_val, mat[j][i]); } diff = maxVal(diff, max_val - min_val); } return diff; } 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; }
Output
Max difference : 12
- Related Articles
- Find pair of rows in a binary matrix that has maximum bit difference in C++
- Find the Pair with a Maximum Sum in a Matrix using C++
- Find column with maximum sum in a Matrix using C++.
- Find Maximum difference pair in Python
- Find maximum element of each column in a matrix in C++
- Count ways of choosing a pair with maximum difference in C++
- Find a pair with maximum product in array of Integers in C++
- Find row with maximum sum in a Matrix in C++
- How to find the maximum value for each column of a matrix in R?
- Find the Pair with Given Sum in a Matrix using C++
- Find a specific pair in Matrix in C++
- Find a pair with the given difference in C++
- Find pair with maximum GCD in an array in C++
- Maximum trace possible for any sub-matrix of the given matrix in C++
- Find any pair with given GCD and LCM in C++

Advertisements