C++ Program to Find maximum element of each row in a matrix Last Updated : 01 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given a matrix, the task is to find the maximum element of each row. Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : The approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside the row and find for the maximum element. Finally, print the element. Below is the implementation : C++14 // C++ program to find maximum // element of each row in a matrix #include <bits/stdc++.h> using namespace std; const int N = 4; // Print array element void printArray(vector<int>& result, int no_of_rows) { for (int i = 0; i < no_of_rows; i++) { cout << result[i] << " "; } } // Function to get max element void maxelement(int no_of_rows, vector<vector<int> >& arr) { int i = 0; // Initialize max to 0 at beginning // of finding max element of each row int max = 0; vector<int> result(no_of_rows); while (i < no_of_rows) { for (int j = 0; j < N; j++) { if (arr[i][j] > max) { max = arr[i][j]; } } result[i] = max; max = 0; i++; } printArray(result, no_of_rows); } // Driver code int main() { vector<vector<int> > arr = { { 3, 4, 1, 8 }, { 1, 4, 9, 11 }, { 76, 34, 21, 1 }, { 2, 1, 4, 5 } }; // Calling the function maxelement(4, arr); return 0; } // This code is contributed by Rajput-Ji Output8 11 76 5 Time Complexity: O(M*N), where M and N are no of rows and column of given matrixAuxiliary Space: O(M) Please refer complete article on Find maximum element of each row in a matrix for more details! Comment More infoAdvertise with us Next Article C++ Program to Find maximum element of each row in a matrix kartik Follow Improve Article Tags : Searching Matrix Technical Scripter C++ Programs C++ DSA +2 More Practice Tags : CPPMatrixSearching Similar Reads C++ Program to Find k maximum elements of array in original order Given an array arr[] and an integer k, we need to print k maximum elements of given array. The elements should printed in the order of the input.Note : k is always less than or equal to n. Examples: Input : arr[] = {10 50 30 60 15} k = 2 Output : 50 60 The top 2 elements are printed as per their app 3 min read C++ Program to Find Largest Element in an Array In this article, we will learn to write a C++ program to find the largest element in the given array arr of size N. The element that is greater than all other elements is the largest element in the array. Recommended PracticeHelp a Thief!!!Try It! One of the most simplest and basic approaches to fin 2 min read C++ Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix 3 min read How to Find the Maximum Element in a List in C++? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to find the maximum element in a list in C++. Example: Input: myList = {30, 20, 10, 5 2 min read C++ Program For Boundary Elements of a Matrix Printing Boundary Elements of a Matrix. Given a matrix of size n x m. Print the boundary elements of the matrix. Boundary elements are those elements which are not surrounded by elements on all four directions, i.e. elements in first row, first column, last row and last column. Examples: Input: 1 4 min read C++ Program to Find median in row wise sorted matrix We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9 Output : Median is 5 If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9) Input: 1 3 4 2 5 6 7 8 9 Output: Media 4 min read C++ Program to Find the Second Largest Element in an Array In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to find the second largest element in an array in C++. Examples: Input: arr[] = {34, 5, 16, 14, 56, 7, 56} Output: 34 Explanation: The 3 min read How to Find the Maximum Element of an Array using STL in C++? Given an array of n elements, the task is to find the maximum element using STL in C++.ExamplesInput: arr[] = {11, 13, 21, 45, 8}Output: 45Explanation: 45 is the largest element of the array.Input: arr[] = {1, 9, 2, 5, 7}Output: 9Explanation: 9 is the largest element of the array.STL provides the fo 3 min read How to Find the Maximum Element of a Vector using STL in C++? Given a vector, find the maximum element of the vector using STL in C++. ExampleInput: v = {2, 4, 1, 5, 3}Output: 5Explanation: 5 is the largest element of vector.Input: v = {11, 23, 3, 5, 24}Output: 24Explanation: 24 is the largest element of the given range.STL provides the following different met 3 min read C++ Program to Maximize sum of diagonal of a matrix by rotating all rows or all columns Given a square matrix, mat[][] of dimensions N * N, the task is find the maximum sum of diagonal elements possible from the given matrix by rotating either all the rows or all the columns of the matrix by a positive integer. Examples: Input: mat[][] = { { 1, 1, 2 }, { 2, 1, 2 }, { 1, 2, 2 } }Output: 3 min read Like