Queries to count sum of rows and columns of a Matrix present in given ranges
Last Updated :
01 Jul, 2021
Given a matrix A[][] of size N * M and a 2D array queries[][] consisting of Q queries of the form {L, R}, the task is to count the number of row-sums and column-sums which are an integer from the range [L, R].
Examples:
Input: N = 2, M = 2, A[][] = {{1, 4}, {2, 5}}, Q = 2, queries[][] = {{3, 7}, {3, 9}}
Output: 3 4
Explanation:
Sum of the first row = 1 + 4 = 5.
Sum of the second row = 2 + 5 = 7.
Sum of the first column= 1 + 2 = 3.
Sum of the second column = 4 + 5 = 9.
Query 1: L = 3, R = 7:
Column sum present in the range = 3.
Row sums present in the range = {5, 7}.
Therefore, count is 3.
Query 2: L = 3, R = 9:
Column sum present in the range = {3, 9}.
Row sums present in the range = {5, 7}.
Therefore, count is 4.
Input: N = 3, M = 2, A[][] = {{13, 3}, {9, 4}, {6, 10}}, Q = 2, queries[][] = {{10, 20}, {25, 35}}
Output: 4 1
Efficient approach: The idea is to traverse the matrix row-wise and column-wise and pre-calculate the sum of each row and column respectively. Traverse the array queries[][] and count the number of row-sums or column-sums present in the given ranges using Binary Search. Follow the steps below to solve the problem:
- Initialize two auxiliary arrays, say row_sum[] & col_sum[], to store the sum of rows and columns.
- Initialize another array, say sum_list[], to store all the row-sums and column-sums elements combined.
- Sort the array sum_list[] in ascending order.
- Traverse the array queries[] and for each query:
- Perform Binary Search to find the index, say i, of the leftmost sum, i.e. L.
- Again, perform Binary Search to find the index j of the rightmost sum, i.e R
- Print j - i + 1 as the result for the query.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to search for the
// leftmost index of given number
int left_search(vector<int> A, int num)
{
// Initialize low, high and ans
int low = 0, high = A.size() - 1;
int ans = 0;
while (low <= high)
{
// Stores mid
int mid = low + (high - low) / 2;
// If A[mid] >= num
if (A[mid] >= num)
{
ans = mid;
high = mid - 1;
}
else
{
low = mid + 1;
}
}
return ans;
}
// Function to search for the
// rightmost index of given number
int right_search(vector<int> A, int num)
{
// Initialise low, high and ans
int low = 0, high = A.size() - 1;
int ans = high;
while (low <= high)
{
// Stores mid
int mid = low + (high - low) / 2;
// If A[mid] <= num
if (A[mid] <= num)
{
// Update ans
ans = mid;
// Update mid
low = mid + 1;
}
else
{
// Update high
high = mid - 1;
}
}
return ans;
}
// Function to preprocess the matrix to execute the
// queries
void totalCount(vector<vector<int>> A, int N, int M, vector<vector<int>> queries, int Q)
{
// Stores the sum of each row
vector<int> row_sum(N);
// Stores the sum of each col
vector<int> col_sum(N);
// Traverse the matrix and calculate
// sum of each row and column
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
row_sum[i] += A[i][j];
col_sum[j] += A[i][j];
}
}
vector<int> sum_list;
// Insert all row sums in sum_list
for (int i = 0; i < N; i++)
sum_list.push_back(row_sum[i]);
// Insert all column sums in sum_list
for (int i = 0; i < M; i++)
sum_list.push_back(col_sum[i]);
// Sort the array in ascending order
sort(sum_list.begin(), sum_list.end());
// Traverse the array queries[][]
for (int i = 0; i < Q; i++)
{
int L = queries[i][0];
int R = queries[i][1];
// Search the leftmost index of L
int l = left_search(sum_list, L);
// Search the rightmost index of R
int r = right_search(sum_list, R);
cout << r - l + 1 << " ";
}
}
// Driver Code
int main()
{
// Given dimensions of matrix
int N = 3, M = 2;
// Given matrix
vector<vector<int>> A = {{13, 3},
{9, 4},
{6, 10}};
// Given number of queries
int Q = 2;
// Given queries
vector<vector<int>> queries = {{10, 20}, {25, 35}};
// Function call to count the
// number row-sums and column-sums
// present in the given ranges
totalCount(A, N, M, queries, Q);
}
// This code is contributed by nirajgusain5
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to preprocess the matrix to execute the
// queries
static void totalCount(int[][] A, int N,
int M, int[][] queries, int Q)
{
// Stores the sum of each row
int row_sum[] = new int[N];
// Stores the sum of each col
int col_sum[] = new int[M];
// Traverse the matrix and calculate
// sum of each row and column
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
row_sum[i] += A[i][j];
col_sum[j] += A[i][j];
}
}
ArrayList<Integer> sum_list
= new ArrayList<>();
// Insert all row sums in sum_list
for (int i = 0; i < N; i++)
sum_list.add(row_sum[i]);
// Insert all column sums in sum_list
for (int i = 0; i < M; i++)
sum_list.add(col_sum[i]);
// Sort the array in ascending order
Collections.sort(sum_list);
// Traverse the array queries[][]
for (int i = 0; i < Q; i++) {
int L = queries[i][0];
int R = queries[i][1];
// Search the leftmost index of L
int l = left_search(sum_list, L);
// Search the rightmost index of R
int r = right_search(sum_list, R);
System.out.print(r - l + 1 + " ");
}
}
// Function to search for the
// leftmost index of given number
static int left_search(
ArrayList<Integer> A,
int num)
{
// Initialize low, high and ans
int low = 0, high = A.size() - 1;
int ans = 0;
while (low <= high) {
// Stores mid
int mid = low + (high - low) / 2;
// If A[mid] >= num
if (A.get(mid) >= num) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
return ans;
}
// Function to search for the
// rightmost index of given number
static int right_search(
ArrayList<Integer> A,
int num)
{
// Initialise low, high and ans
int low = 0, high = A.size() - 1;
int ans = high;
while (low <= high) {
// Stores mid
int mid = low + (high - low) / 2;
// If A[mid] <= num
if (A.get(mid) <= num) {
// Update ans
ans = mid;
// Update mid
low = mid + 1;
}
else {
// Update high
high = mid - 1;
}
}
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given dimensions of matrix
int N = 3, M = 2;
// Given matrix
int A[][] = { { 13, 3 },
{ 9, 4 },
{ 6, 10 } };
// Given number of queries
int Q = 2;
// Given queries
int queries[][] = { { 10, 20 }, { 25, 35 } };
// Function call to count the
// number row-sums and column-sums
// present in the given ranges
totalCount(A, N, M, queries, Q);
}
}
Python3
# Python3 program for the above approach
from collections import deque
from bisect import bisect_left, bisect_right
# Function to preprocess the matrix to execute the
# queries
def totalCount(A, N, M, queries, Q):
# Stores the sum of each row
row_sum = [0]*N
# Stores the sum of each col
col_sum = [0]*M
# Traverse the matrix and calculate
# sum of each row and column
for i in range(N):
for j in range(M):
row_sum[i] += A[i][j]
col_sum[j] += A[i][j]
sum_list = []
# Insert all row sums in sum_list
for i in range(N):
sum_list.append(row_sum[i])
# Insert all column sums in sum_list
for i in range(M):
sum_list.append(col_sum[i])
# Sort the array in ascending order
sum_list = sorted(sum_list)
# Traverse the array queries[][]
for i in range(Q):
L = queries[i][0]
R = queries[i][1]
# Search the leftmost index of L
l = left_search(sum_list, L)
# Search the rightmost index of R
r = right_search(sum_list, R)
print(r - l + 1, end = " ")
# Function to search for the
# leftmost index of given number
def left_search(A, num):
# Initialize low, high and ans
low, high = 0, len(A) - 1
ans = 0
while (low <= high):
# Stores mid
mid = low + (high - low) // 2
# If A[mid] >= num
if (A[mid] >= num):
ans = mid
high = mid - 1
else:
low = mid + 1
return ans
# Function to search for the
# rightmost index of given number
def right_search(A, num):
# Initialise low, high and ans
low, high = 0, len(A) - 1
ans = high
while (low <= high):
# Stores mid
mid = low + (high - low) // 2
# If A[mid] <= num
if (A[mid] <= num):
# Update ans
ans = mid
# Update mid
low = mid + 1
else:
# Update high
high = mid - 1
return ans
# Driver Code
if __name__ == '__main__':
# Given dimensions of matrix
N, M = 3, 2
# Given matrix
A = [ [ 13, 3 ],
[ 9, 4 ],
[ 6, 10 ] ]
# Given number of queries
Q = 2
# Given queries
queries= [ [ 10, 20 ], [ 25, 35 ] ]
# Function call to count the
# number row-sums and column-sums
# present in the given ranges
totalCount(A, N, M, queries, Q)
# This code is contributed by mohit kumar 29.
C#
using System;
using System.Collections.Generic;
class GFG
{
// Function to preprocess the matrix to execute the
// queries
static void totalCount(int[,] A, int N, int M,
int[,] queries, int Q)
{
// Stores the sum of each row
int []row_sum = new int[N];
// Stores the sum of each col
int []col_sum = new int[M];
// Traverse the matrix and calculate
// sum of each row and column
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
row_sum[i] += A[i,j];
col_sum[j] += A[i,j];
}
}
List<int> sum_list = new List<int>();
// Insert all row sums in sum_list
for (int i = 0; i < N; i++)
sum_list.Add(row_sum[i]);
// Insert all column sums in sum_list
for (int i = 0; i < M; i++)
sum_list.Add(col_sum[i]);
// Sort the array in ascending order
sum_list.Sort();
// Traverse the array queries[][]
for (int i = 0; i < Q; i++) {
int L = queries[i,0];
int R = queries[i,1];
// Search the leftmost index of L
int l = left_search(sum_list, L);
// Search the rightmost index of R
int r = right_search(sum_list, R);
Console.Write(r - l + 1 + " ");
}
}
// Function to search for the
// leftmost index of given number
static int left_search(List<int> A, int num)
{
// Initialize low, high and ans
int low = 0, high = A.Count- 1;
int ans = 0;
while (low <= high)
{
// Stores mid
int mid = low + (high - low) / 2;
// If A[mid] >= num
if (A[mid] >= num) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
return ans;
}
// Function to search for the
// rightmost index of given number
static int right_search( List<int> A,int num)
{
// Initialise low, high and ans
int low = 0, high = A.Count- 1;
int ans = high;
while (low <= high) {
// Stores mid
int mid = low + (high - low) / 2;
// If A[mid] <= num
if (A[mid] <= num) {
// Update ans
ans = mid;
// Update mid
low = mid + 1;
}
else {
// Update high
high = mid - 1;
}
}
return ans;
}
//driver code
static void Main()
{
int N = 3, M = 2;
// Given matrix
int [,]A = new int[,]{ { 13, 3 },{ 9, 4 },{ 6, 10 } };
// Given number of queries
int Q = 2;
// Given queries
int [,]queries = new int[,]{ { 10, 20 }, { 25, 35 } };
// Function call to count the
// number row-sums and column-sums
// present in the given ranges
totalCount(A, N, M, queries, Q);
}
}
//This code is contributed by SoumikMondal
JavaScript
<script>
// Javascript program for the above approach
// Function to preprocess the matrix to execute the
// queries
function totalCount(A,N,M,queries,Q)
{
// Stores the sum of each row
let row_sum = new Array(N);
// Stores the sum of each col
let col_sum = new Array(M);
for(let i=0;i<N;i++)
row_sum[i]=0;
for(let j=0;j<M;j++)
col_sum[j]=0;
// Traverse the matrix and calculate
// sum of each row and column
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
row_sum[i] += A[i][j];
col_sum[j] += A[i][j];
}
}
let sum_list=[];
// Insert all row sums in sum_list
for (let i = 0; i < N; i++)
sum_list.push(row_sum[i]);
// Insert all column sums in sum_list
for (let i = 0; i < M; i++)
sum_list.push(col_sum[i]);
// Sort the array in ascending order
(sum_list).sort(function(a,b){return a-b;});
// Traverse the array queries[][]
for (let i = 0; i < Q; i++) {
let L = queries[i][0];
let R = queries[i][1];
// Search the leftmost index of L
let l = left_search(sum_list, L);
// Search the rightmost index of R
let r = right_search(sum_list, R);
document.write(r - l + 1 + " ");
}
}
// Function to search for the
// leftmost index of given number
function left_search(A,num)
{
// Initialize low, high and ans
let low = 0, high = A.length - 1;
let ans = 0;
while (low <= high) {
// Stores mid
let mid = low + Math.floor((high - low) / 2);
// If A[mid] >= num
if (A[mid] >= num) {
ans = mid;
high = mid - 1;
}
else {
low = mid + 1;
}
}
return ans;
}
// Function to search for the
// rightmost index of given number
function right_search(A,num)
{
// Initialise low, high and ans
let low = 0, high = A.length - 1;
let ans = high;
while (low <= high) {
// Stores mid
let mid = low + Math.floor((high - low) / 2);
// If A[mid] <= num
if (A[mid] <= num) {
// Update ans
ans = mid;
// Update mid
low = mid + 1;
}
else {
// Update high
high = mid - 1;
}
}
return ans;
}
// Given dimensions of matrix
let N = 3, M = 2;
// Given matrix
let A=[[ 13, 3 ],[ 9, 4 ],[ 6, 10 ] ];
// Given number of queries
let Q = 2;
let queries=[[ 10, 20 ], [ 25, 35 ]];
// Function call to count the
// number row-sums and column-sums
// present in the given ranges
totalCount(A, N, M, queries, Q);
// This code is contributed by unknown2108
</script>
Time Complexity: O(Q * log(N * M))
Auxiliary Space: O(N * M)
Similar Reads
Count the number of rows and columns in given Matrix having all primes Given a 2D matrix arr[] of size N*M, the task is to find the number of rows and columns having all primes. Examples: Input: arr[]= { { 2, 5, 7 }, { 3, 10, 4 }, { 11, 13, 17 } };Output: 3Explanation: 2 Rows: {2, 5, 7}, {11, 13, 17}1 Column: {2, 3, 11} Input: arr[]={ { 1, 4 }, { 4, 6 } }Output: 0 Appr
7 min read
Program to find the Sum of each Row and each Column of a Matrix Given a matrix mat of size m à n, the task is to compute the sum of each row and each column of the matrix.Examples:Input: mat = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ] Output: Sum of row 0 = 10 Sum of row 1 = 26 Sum of row 2 = 42 Sum of row 3 = 58 Sum of column 0 = 28 Sum
7 min read
Print index of columns sorted by count of zeroes in the Given Matrix Given a matrix with N rows and M columns. The task is to print the index of columns of the given matrix based on the increasing number of zeroes in each column. For Example, If the 1st column contains 2 zero's, the 2nd column contains 1 zero, and 3rd column does not contain any zeroes. Then the outp
8 min read
Count of numbers in given range L to R which are present in a Matrix Given a matrix(mat[][]), which is sorted row and column-wise in increasing order. Two integers are given, L and R, our task is to count number of elements of the matrix within the range [L, R].Examples: Input: L = 3, R = 11, matrix = {{1, 6, 9} {2, 7, 11} {3, 8, 12}} Output: 6 Explanation: The eleme
8 min read
Count of empty cells in given square Matrix after updating the rows and columns for Q queries Given a binary matrix of size NxN which is initially filled with 0's and Q queries such that: Each query is of type (r, c) where r and c denotes the row number and column number respectively.Change all the 0's of rth row and cth column to 1. The task is to find the count of 0's after performing each
9 min read
Count zeros in a row wise and column wise sorted matrix Given a n x n binary matrix (elements in matrix can be either 1 or 0) where each row and column of the matrix is sorted in ascending order, count number of 0s present in it.Examples: Input: [0, 0, 0, 0, 1][0, 0, 0, 1, 1][0, 1, 1, 1, 1][1, 1, 1, 1, 1][1, 1, 1, 1, 1]Output: 8Input: [0, 0][0, 0]Output:
6 min read
Find row and column pair in given Matrix with equal row and column sum Given a matrix Mat of size N x M, the task is to find all the pairs of rows and columns where the sum of elements in the row is equal to the sum of elements in the columns. Examples: Input: M = {{1, 2, 2}, {1, 5, 6}, {3, 8, 9}}Output: {{1, 1}}Explanation: The sum of elements of rows and columns of m
8 min read
Number of rows and columns in a Matrix that contain repeated values Given a N x N square matrix arr[][] which contains only integers between 1 and N, the task is to compute the number of rows and the number of columns in the matrix that contain repeated values. Examples: Input: N = 4, arr[][] = {{1, 2, 3, 4}, {2, 1, 4, 3}, {3, 4, 1, 2}, {4, 3, 2, 1}} Output: 0 0 Exp
6 min read
Construct a square matrix such that the sum of each row and column is odd Given an integer N. Then your task is to output a square matrix of length N using the numbers from the range [1, N2] such that the sum of each row and column is odd. Note: If there are multiple solution, then print any of them. Examples: Input: N = 2 Output: {{1, 2}, {4, 3}} Explanation: Let us calc
11 min read
Find a Square Matrix such that sum of elements in every row and column is K Given two integers N and K, the task is to find an N x N square matrix such that sum of every row and column should be equal to K. Note that there can be multiple such matrices possible. Print any one of them.Examples: Input: N = 3, K = 15 Output: 2 7 6 9 5 1 4 3 8Input: N = 3, K = 7 Output: 7 0 0 0
4 min read