
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the number of pairs that have column sum greater than row sum in C++
We are given a matrix of size NXN. The goal is to find the count of all valid pairs of indexes (i,j) such that the sum elements of column j is greater than the sum of elements of row i.
We will do this by traversing the matrix and calculate sums of elements of each row and column.
Store sum of elements of each in rowsum[N] and sum of elements of each column in colsum[N].
Now make pairs of rowsum[i] and colsum[j] and check if colsum[j]>rowsum[i]. If true increment count for such pairs.
Let’s understand with examples.
Input-: matrix= { { 1,2,0,1}, { 3,3,0,2}, { 1,3,0,2}, { 3,0,0,2} };
Output − Count of valid pairs − 9
Explanation −
Rowsum[0]= 1+2+0+1=5 Colsum[0]= 1+3+1+3=8 Rowsum[1]=3+3+0+2=8 Colsum[1]= 2+3+3+0=8 Rowsum[2]= 1+3+0+2=6 Colsum[2]= 0+0+0+0=0 Rowsum[3]=3+0+0+2=5 Colsum[3]= 1+2+2+2=7 Pairs of (i,j) such that rowsum[i] < colsum[j]. (0,0), (0,1), (0,3), (2,0), (2,1), (2,3), (3,0) (3,1), (3,3)
Input−
Arr[]= { {1,1,1}, {1,1,1}, {1,1,1} } N=3
Output − Count of valid pairs − 0
Explanation −
Rowsum[0]= 1+1+1=3 Colsum[0]= 1+1+1=3 Rowsum[1]= 1+1+1=3 Colsum[1]= 1+1+1=3 Rowsum[2]= 1+1+1=3 Colsum[2]= 1+1+1=3 No pairs possible where rowsum[i]<colsum[j]
Approach used in the below program is as follows
We take an integer array Arr[] initialized with random numbers.
Take a variable n which stores the length of Arr[].
Function countPairs(int arr[], int n) takes an array, its length as input and returns the pairs which are valid and meet desired conditions.
We take two arrays rowsum[n] and colsum[n].
Traverse matrix and add arr[i][j] to rowsum[i] and colsum[j] to calculate sum of row i and column j.
Now traverse arrays colsum[] and rowsum[] using two for loops.
If any colsum[j]>rowsum[i]. Increment count.
Return the count as result.
Example
#include <bits/stdc++.h> using namespace std; int countPairs(int arr[][3], int n){ // Count of pairs int count = 0; int rowsum[n]={0}; int colsum[n]={0}; int i,j; for (i = 0; i < n; i++){ for (j = 0; j < n; j++){ rowsum[i]+=arr[i][j]; colsum[j]+=arr[i][j]; } } for(i=0;i<n;i++){ for(j=0;j<n;j++) if(colsum[j]>rowsum[i]) { count++; } } return count; } int main(){ int Arr[][3] = { {1,3,5},{2,4,6},{3,5,7} }; int side=3; cout <<endl<<"Count of number of pairs : "<< countPairs(Arr, side); return 0; }
Output
If we run the above code it will generate the following output −
Count of number of pairs : 4
- Related Questions & Answers
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Count the number of words having sum of ASCII values less than and greater than k in C++
- Count pairs with sum as a prime number and less than n in C++
- Program to find lowest sum of pairs greater than given target in Python
- Count pairs with given sum in C++
- Count numbers with difference between number and its digit sum greater than specific value in C++
- Number of pairs with maximum sum in C++
- Count number of distinct pairs whose sum exists in the given array in C++
- Matrix row sum and column sum using C program
- Largest subarray having sum greater than k in C++
- Maximum count of pairs which generate the same sum in C++
- Maximum subarray size, such that all subarrays of that size have sum less than k in C++
- Count natural numbers whose all permutation are greater than that number in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count of Numbers such that difference between the number and sum of its digits not less than L in C++