Thursday, July 4, 2024
HomeLanguagesJavascriptJavascript Program to Find maximum element of each row in a matrix

Javascript Program to Find maximum element of each row in a matrix

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 : 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 : 

Javascript




<script>
// javascript program to find maximum
// element of each row in a matrix
// Function to get max element
function maxelement(no_of_rows, arr)
{
    var i = 0;
     
    // Initialize max to 0 at beginning
    // of finding max element of each row
    var max = 0;
    var result = Array.from({length: no_of_rows}, (_, i) => 0);
    while (i < no_of_rows)
    {
        for (var j = 0; j < arr[i].length; j++)
        {
            if (arr[i][j] > max)
            {
                max = arr[i][j];
            }
        }
        result[i] = max;
        max = 0;
        i++;
 
    }
    printArray(result);
 
}
 
// Print array element
function printArray(result)
{
    for (var i = 0; i < result.length; i++)
    {
        document.write(result[i]+"<br>");
    }
 
}
 
// Driver code
    var arr = [[3, 4, 1, 8],
    [ 1, 4, 9, 11],
    [ 76, 34, 21, 1],
   [ 2, 1, 4, 5] ];
    
// Calling the function 
maxelement(4, arr); 
 
// This code is contributed by 29AjayKumar
</script>


Output :

8
11
76
5

Time Complexity: O(N*M), where N is the number of rows and M is the number of columns in the given matrix.

Space Complexity: O(N), where N is the number of rows in the given matrix.

Please refer complete article on Find maximum element of each row in a matrix for more details!
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments