Largest Row or Column
- Get link
- X
- Other Apps
Largest Row or Column
Send Feedback
For a given two-dimensional integer array/list of size (N x M), you need to find out which row or column has the largest sum(sum of all the elements in a row/column) amongst all the rows and columns.
Note :
If there are more than one rows/columns with maximum sum, consider the row/column that comes first. And if ith row and jth column has the same largest sum, consider the ith row as answer.
Input Format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
First line of each test case or query contains two integer values, 'N' and 'M', separated by a single space. They represent the 'rows' and 'columns' respectively, for the two-dimensional array/list.
Second line onwards, the next 'N' lines or rows represent the ith row values.
Each of the ith row constitutes 'M' column values separated by a single space.
Output Format :
For each test case, If row sum is maximum, then print: "row" <row_index> <row_sum>
OR
If column sum is maximum, then print: "column" <col_index> <col_sum>
It will be printed in a single line separated by a single space between each piece of information.
Output for every test case will be printed in a seperate line.
Consider :
If there doesn't exist a sum at all then print "row 0 -2147483648", where -2147483648 or -2^31 is the smallest value for the range of Integer.
Constraints :
1 <= t <= 10^2
1 <= N <= 10^3
1 <= M <= 10^3
Time Limit: 1sec
/*You can use minimum value of integer as -2147483647 and maximum value of integer as 2147483647*//*#include<cmath>void findLargest(int **input, int nRows, int mCols){ //Write your code here // int sumr=0; // int sumc=0; for(int i=0;i<nRows;i++){ int sumr[i]=0; for(int j=0;j<mCols;j++){ sumr[i]=sumr+input[i][j]; } maxm(sumr[i]); } for(int j=0;j<mCols;j++){ int sumc[j]=0; for(int i=0;i<nRows;i++){ sumc[j]+=input[j][i]; } maxm(sum[j]); } }
*/#include<climits>
void findLargest(int **input, int nRows, int mCols){ int i,j; if(nRows==0 || mCols==0){ cout<<"row 0 -2147483648"; return; } int r,c; // Keep track of row and column index int rlargest=INT_MIN; int clargest =INT_MIN; for( i =0; i<nRows; i++){ int sum=0; for( j =0; j<mCols; j++){ sum= sum+ input[i][j]; } if(rlargest< sum){ rlargest = sum; r = i; // r will be equal to that row index where sum is largest } } for( j =0; j<mCols; j++){ int sum =0; for( i =0; i<nRows; i++){ sum = sum + input[i][j]; } if(clargest < sum ){ clargest = sum; c = j; // c will be equal to that column index where sum is largest } } if(clargest>rlargest){ cout<<"column "<<c<<" "<<clargest; } else{ cout<<"row "<<r<<" "<<rlargest; }}
#include <iostream>using namespace std;
/*You can use minimum value of integer as -2147483647 and maximum value of integer as 2147483647*//*#include<cmath>void findLargest(int **input, int nRows, int mCols){ //Write your code here // int sumr=0; // int sumc=0; for(int i=0;i<nRows;i++){ int sumr[i]=0; for(int j=0;j<mCols;j++){ sumr[i]=sumr+input[i][j]; } maxm(sumr[i]); } for(int j=0;j<mCols;j++){ int sumc[j]=0; for(int i=0;i<nRows;i++){ sumc[j]+=input[j][i]; } maxm(sum[j]); } }
*/#include<climits>
void findLargest(int **input, int nRows, int mCols){ int i,j; if(nRows==0 || mCols==0){ cout<<"row 0 -2147483648"; return; } int r,c; // Keep track of row and column index int rlargest=INT_MIN; int clargest =INT_MIN; for( i =0; i<nRows; i++){ int sum=0; for( j =0; j<mCols; j++){ sum= sum+ input[i][j]; } if(rlargest< sum){ rlargest = sum; r = i; // r will be equal to that row index where sum is largest } } for( j =0; j<mCols; j++){ int sum =0; for( i =0; i<nRows; i++){ sum = sum + input[i][j]; } if(clargest < sum ){ clargest = sum; c = j; // c will be equal to that column index where sum is largest } } if(clargest>rlargest){ cout<<"column "<<c<<" "<<clargest; } else{ cout<<"row "<<r<<" "<<rlargest; }}
int main(){ int t; cin >> t; while (t--) { int row, col; cin >> row >> col;
int **input = new int *[row]; for (int i = 0; i < row; i++) { input[i] = new int[col]; for (int j = 0; j < col; j++) { cin >> input[i][j]; } }
findLargest(input, row, col); cout << endl; }}
- Get link
- X
- Other Apps
Comments
Post a Comment