Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Matrix
  • Practice Matrix
  • MCQs on Matrix
  • Tutorial on Matrix
  • Matrix Traversal
  • Sorting in Matrix
  • Matrix Rotation
  • Transpose of Matrix
  • Inverse of Matrix
  • Determinant of Matrix
  • Matrix Application
  • Adjoint & Inverse Matrix
  • Sparse Matrix
  • Matrix Exponentiation
Open In App
Next Article:
Sum of all maximum frequency elements in Matrix
Next article icon

Number of elements greater than modified mean in matrix

Last Updated : 20 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Normally mean of the matrix is the average of all elements present in the matrix. Consider a modified mean as the floor of the mean of the row-wise minimum and column-wise maximum. The row-wise minimum is the minimum element from each row of the given matrix and the column-wise maximum is the maximum element from each column. Given a matrix of order n*m, find the number of elements greater than the new mean obtained.

mean = floor ( (sum(row-wise Min) + sum (col-wise Max )) /                                    (row_no. + col_no.) )

Examples :  

Input : mat[][] = {1, 5, 6,                    2, 3, 0,                    5, 2, 8} Output : 4  Input : mat[][] = {1, 5,                    5, 2} Output : 2

Find the sum of all row-wise minimums and the sum of all column-wise maximums. Take the mean of this sum by dividing the sum value with (n+m) i.e., the total number of rows and columns. Now, as we have the mean of the row-wise minimum and column-wise maximum, iterate over the matrix to find the number of elements greater than the calculated mean

Below is the implementation of the above approach:

C++




// CPP program to count number of
// elements greater than mean
#include <bits/stdc++.h>
using namespace std;
 
// define constant for row and column
#define n 4
#define m 5
 
// function to count elements
// greater than mean
int countElements(int mat[][m])
{
    // For each row find minimum
    // element and add to rowSum
    int rowSum = 0;
    for (int i = 0; i < n; i++) {
        int min = mat[i][0];
        for (int j = 1; j < m; j++)
            if (mat[i][j] < min)
                min = mat[i][j];
        rowSum += min;
    }
     
    // For each column find maximum
    // element and add to colSum
    int colSum = 0;
    for (int i = 0; i < m; i++) {
        int max = mat[0][i];
        for (int j = 1; j < n; j++)
            if (max < mat[j][i])
                max = mat[j][i];
        colSum += max;
    }
 
    // Calculate mean of row-wise
    // minimum and column wise maximum
    int mean = (rowSum + colSum) / (m + n);
     
    // For whole matrix count
    // elements greater than mean
    int count = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            if (mean < mat[i][j])
                count++;
    return count;
}
 
// driver function
int main()
{
    int mat[n][m] = { 5, 4, 2, 8, 7,
                      1, 5, 8, 3, 4,
                      8, 5, 4, 6, 0,
                      2, 5, 8, 9, 4 };
    cout << countElements(mat);
    return 0;
}
 
 

Java




// Java program to count
// number of elements
// greater than mean
class GFG {
     
    static int n = 4, m = 5;
     
    // function to count
    // elements greater
    // than mean
    static int countElements(int mat[][])
    {
        // For each row find
        // minimum element
        // and add to rowSum
        int rowSum = 0;
        for (int i = 0; i < n; i++)
        {
            int min = mat[i][0];
         
            for (int j = 1; j < m; j++)
                if (mat[i][j] < min)
                    min = mat[i][j];
             
            rowSum += min;
        }
         
        // For each column
        // find maximum
        // element and add
        // to colSum
        int colSum = 0;
        for (int i = 0; i < m; i++) {
            int max = mat[0][i];
         
            for (int j = 1; j < n; j++)
                if (max < mat[j][i])
                    max = mat[j][i];
         
            colSum += max;
        }
     
        // Calculate mean of
        // row-wise minimum
        // and column wise
        // maximum
        int mean = (rowSum + colSum) / (m + n);
         
        // For whole matrix
        // count elements
        // greater than mean
        int count = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
         
                if (mean < mat[i][j])
                    count++;
         
        return count;
    }
     
    // driver function
    public static void main(String[] args)
    {
        int mat[][] = {{ 5, 4, 2, 8, 7},
                        {1, 5, 8, 3, 4},
                        {8, 5, 4, 6, 0},
                        {2, 5, 8, 9, 4}};
        System.out.println(countElements(mat));
    }
}
 
// This article is contribute by Prerna Saini.
 
 

Python3




# Python3 program to count
# number of elements
# greater than mean
n = 4;
m = 5;
 
# Function to count
# elements greater
# than mean
def countElements(mat):
   
    # For each row find
    # minimum element
    # and add to rowSum
    rowSum = 0;
     
    for i in range(n):
        min = mat[i][0];
        for j in range(1, m):
            if (mat[i][j] < min):
                min = mat[i][j];
 
        rowSum += min;
 
    # For each column
    # find maximum
    # element and add
    # to colSum
    colSum = 0;
     
    for i in range(m):
        max = mat[0][i];
        for j in range(1, n):
            if (max < mat[j][i]):
                max = mat[j][i];
 
        colSum += max;
 
    # Calculate mean of
    # row-wise minimum
    # and column wise
    # maximum
    mean = ((rowSum + colSum) /
           (m + n));
 
    # For whole matrix
    # count elements
    # greater than mean
    count = 0;
    for i in range(n):
        for j in range(m):
            if (mean < mat[i][j]):
                count += 1;
 
    return count;
 
# Driver code
if __name__ == '__main__':
   
    mat = [[5, 4, 2, 8, 7],
           [1, 5, 8, 3, 4],
           [8, 5, 4, 6, 0],
           [2, 5, 8, 9, 4]];
    print(countElements(mat));
 
# This code is contributed by 29AjayKumar
 
 

C#




// C# program to count number of
// elements greater than mean
using System;
 
class GFG {
     
    static int n = 4, m = 5;
     
    // function to count elements
    // greater than mean
    static int countElements(int [,]mat)
    {
        // For each row find minimum
        // element and add to rowSum
        int rowSum = 0;
        for (int i = 0; i < n; i++)
        {
            int min = mat[i,0];
         
            for (int j = 1; j < m; j++)
                if (mat[i,j] < min)
                    min = mat[i,j];
             
            rowSum += min;
        }
         
        // For each column find maximum
        // element and add to colSum
        int colSum = 0;
        for (int i = 0; i < m; i++) {
            int max = mat[0,i];
         
            for (int j = 1; j < n; j++)
                if (max < mat[j,i])
                    max = mat[j,i];
         
            colSum += max;
        }
     
        // Calculate mean of row-wise minimum
        // and column wise maximum
        int mean = (rowSum + colSum) / (m + n);
         
        // For whole matrix count
        // elements greater than mean
        int count = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
         
                if (mean < mat[i,j])
                    count++;
         
        return count;
    }
     
    // Driver function
    public static void Main()
    {
        int [,]mat = {{5, 4, 2, 8, 7},
                      {1, 5, 8, 3, 4},
                      {8, 5, 4, 6, 0},
                      {2, 5, 8, 9, 4}};
    Console.Write(countElements(mat));
    }
}
 
//
 
 

PHP




<?php
// PHP program to count number of
// elements greater than mean
 
// define constant for
// row and column
$n = 4;
$m = 5;
 
// function to count elements
// greater than mean
function countElements($mat)
{
     
    // For each row find minimum
    // element and add to rowSum
    $rowSum = 0;
    global $n, $m;
    for ($i = 0; $i < $n; $i++)
    {
        $min = $mat[$i][0];
        for ($j = 1; $j < $m; $j++)
            if ($mat[$i][$j] < $min)
                $min = $mat[$i][$j];
        $rowSum += $min;
    }
     
    // For each column find maximum
    // element and add to colSum
    $colSum = 0;
    for ($i = 0; $i < $m; $i++)
    {
        $max = $mat[0][$i];
        for ($j = 1; $j < $n; $j++)
            if ($max < $mat[$j][$i])
                $max = $mat[$j][$i];
        $colSum += $max;
    }
 
    // Calculate mean of row-wise
    // minimum and column wise maximum
    $mean = ($rowSum + $colSum) / ($m + $n);
     
    // For whole matrix count
    // elements greater than mean
    $count = 0;
    for ($i = 0; $i < $n; $i++)
        for ($j = 0; $j < $m; $j++)
            if ($mean < $mat[$i][$j])
                $count++;
    return $count;
}
 
// Driver Code
$mat = array(array(5, 4, 2, 8, 7),
             array(1, 5, 8, 3, 4),
             array(8, 5, 4, 6, 0),
             array(2, 5, 8, 9, 4));
echo countElements($mat);
// This code is contribute by vt_m.
?>
 
 

Javascript




<script>
 
// Javascript program to count number of
// elements greater than mean
 
// define constant for row and column
var n = 4
var m = 5
 
// function to count elements
// greater than mean
function countElements(mat)
{
    // For each row find minimum
    // element and add to rowSum
    var rowSum = 0;
    for (var i = 0; i < n; i++) {
        var min = mat[i][0];
        for (var j = 1; j < m; j++)
            if (mat[i][j] < min)
                min = mat[i][j];
        rowSum += min;
    }
     
    // For each column find maximum
    // element and add to colSum
    var colSum = 0;
    for (var i = 0; i < m; i++) {
        var max = mat[0][i];
        for (var j = 1; j < n; j++)
            if (max < mat[j][i])
                max = mat[j][i];
        colSum += max;
    }
 
    // Calculate mean of row-wise
    // minimum and column wise maximum
    var mean = (rowSum + colSum) / (m + n);
     
    // For whole matrix count
    // elements greater than mean
    var count = 0;
    for (var i = 0; i < n; i++)
        for (var j = 0; j < m; j++)
            if (mean < mat[i][j])
                count++;
    return count;
}
 
// driver function
var mat = [ [5, 4, 2, 8, 7],
                  [1, 5, 8, 3, 4],
                  [8, 5, 4, 6, 0],
                  [2, 5, 8, 9, 4 ]];
document.write( countElements(mat));
 
</script>
 
 
Output
11

Time Complexity: O(n*m), where n is the number of rows and m is the number of columns.
Auxiliary Space: O(1)



Next Article
Sum of all maximum frequency elements in Matrix

N

nitin mittal
Improve
Article Tags :
  • DSA
  • Matrix
  • statistical-algorithms
Practice Tags :
  • Matrix

Similar Reads

  • Sum of all minimum frequency elements in Matrix
    Given a NxM matrix of integers containing duplicate elements. The task is to find the sum of all minimum occurring elements in the given matrix. That is the sum of all such elements whose frequency is even in the matrix. Examples: Input : mat[] = {{1, 1, 2}, {2, 3, 3}, {4, 5, 3}} Output : 9 The min
    6 min read
  • Sum of all maximum frequency elements in Matrix
    Given a NxM matrix of integers containing duplicate elements. The task is to find the sum of all maximum occurring elements in the given matrix. That is the sum of all such elements whose frequency is even in the matrix. Examples: Input : mat[] = {{1, 1, 1}, {2, 3, 3}, {4, 5, 3}} Output : 12 The max
    6 min read
  • Maximum sum of elements from each row in the matrix
    Given a matrix, find the maximum sum we can have by selecting just one element from every row. Condition is element selected from nth row must be strictly greater than element from (n-1)th row, else no element must be taken from row. Print the sum if possible else print -1. Examples : Input : 1 2 31
    7 min read
  • Sum of all odd frequency elements in a Matrix
    Given a NxM matrix of integers containing duplicate elements. The task is to find the sum of all odd occurring elements in the given matrix. That is the sum of all such elements whose frequency is odd in the matrix. Examples: Input : mat[] = {{1, 1, 2}, {2, 3, 3}, {4, 5, 3}} Output : 18 The odd occu
    5 min read
  • Count elements smaller than or equal to x in a sorted matrix
    Given a n x n strictly sorted matrix and a value x. The problem is to count the elements smaller than or equal to x in the given matrix. Here strictly sorted matrix means that matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row ‘i’, where 1 <= i
    14 min read
  • Total number of decreasing paths in a matrix
    Given a matrix of size N X N of integers. The task is to find the number of decreasing path in the matrix. You are allowed to start from any cell and from the cell (i, j), you are allowed to move to (i + 1, j), (i - 1, j), (i, j + 1) and (i, j - 1) cell. Examples: Input : m[][] = { { 1, 2 }, { 1, 3
    10 min read
  • Sum of all even frequency elements in Matrix
    Given a NxM matrix of integers containing duplicate elements. The task is to find the sum of all even occurring elements in the given matrix. That is the sum of all such elements whose frequency is even in the matrix. Examples: Input : mat[] = {{1, 1, 2}, {2, 3, 3}, {4, 5, 3}} Output : 18 The even o
    5 min read
  • Mean and Median of a matrix
    Given a sorted matrix of size n*n. Calculate the mean and median of the matrix . Examples: Input : 1 2 3 4 5 6 7 8 9Output :Mean: 5 Median: 5Input : 1 1 1 2 2 2 4 4 4Output :Mean: 2 Median: 2Mean of matrix is = (sum of all elements of matrix)/ (total elements of matrix)Note that this definition does
    12 min read
  • Find number of Positional Elements
    Given a matrix of integers, task is to find out number of positional elements. A positional element is one which is either minimum or maximum in a row or in a column. Examples: Input : a = {{1, 3, 4}, {5, 2, 9}, {8, 7, 6}} Output : 7 There are total 7 elements min elements are 1, 2, 6 and 4. And max
    8 min read
  • Find row with maximum and minimum number of zeroes in given Matrix
    Given a 2D matrix containing only zeroes and ones, where each row is sorted. The task is to find the row with the maximum number of 0s and the row with minimum number of 0s.Example: Input: mat[][] = { {0, 1, 1, 1}, {0, 0, 1, 1}, {1, 1, 1, 1}, {0, 0, 0, 0}} Output: Row with min zeroes: 3 Row with max
    11 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences