
- 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
Minimum operations of given type to make all elements of a matrix equal in C++
Problem statement
Given an integer K and a matrix of M x N, the task is to find the minimum number of operations required to make all the elements of the matrix equal. In a single operation, K can be added to or subtracted from any element of the matrix.
Example
If input matrix is: { {2, 4}, {20, 40} } and K = 2 then total 27 operations required as follows; Matrix[0][0] = 2 + (K * 9) = 20 = 9 operations Matrix[0][1] = 4 + (k * 8) = 20 = 8 operations Matrix[1][0] = 20 + (k * 10) = 40 = 10 operations
Algorithm
1. Since we are only allowed to add or subtract K from any element, we can easily infer that mod of all the elements with K should be equal. If it’s not, then return -1 2. sort all the elements of the matrix in non-deceasing order and find the median of the sorted elements 3. The minimum number of steps would occur if we convert all the elements equal to the median
Example
#include <bits/stdc++.h> using namespace std; int getMinOperations(int n, int m, int k, vector<vector<int> >& matrix) { vector<int> arr(n * m, 0); int mod = matrix[0][0] % k; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { arr[i * m + j] = matrix[i][j]; if (matrix[i][j] % k != mod) { return -1; } } } sort(arr.begin(), arr.end()); int median = arr[(n * m) / 2]; int minOperations = 0; for (int i = 0; i < n * m; ++i) minOperations += abs(arr[i] - median) / k; if ((n * m) % 2 == 0) { int newMedian = arr[(n * m) / 2]; int newMinOperations = 0; for (int i = 0; i < n * m; ++i) newMinOperations += abs(arr[i] - newMedian) / k; minOperations = min(minOperations, newMinOperations); } return minOperations; } int main() { vector<vector<int> > matrix = { { 2, 4}, { 20, 40}, }; int n = matrix.size(); int m = matrix[0].size(); int k = 2; cout << "Minimum required operations = " << getMinOperations(n, m, k, matrix) << endl; return 0; }
When you compile and execute above program. It generates following output
Output
Minimum required operations = 27
- Related Articles
- Minimum operations required to make all the array elements equal in C++
- Minimum operations required to set all elements of binary matrix in C++
- Minimum delete operations to make all elements of array same in C++.
- Number of operations required to make all array elements Equal in Python
- Minimum number of given operations required to make two strings equal using C++.
- Minimum move to end operations to make all strings equal in C++
- Minimum number of moves to make all elements equal using C++.
- Minimum operations to make the MEX of the given set equal to x in C++
- Find the number of operations required to make all array elements Equal in C++
- Minimum number of operations on an array to make all elements 0 using C++.
- Minimum operation to make all elements equal in array in C++
- Program to find minimum operations to make array equal using Python
- Program to make all elements equal by performing given operation in Python
- Minimum number of operations required to delete all elements of the array using C++.
- Finding minimum steps to make array elements equal in JavaScript

Advertisements