Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Cpp/largest_row_or_column.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Given a 2D array of size M*N, we need to find the index and the row or column containing the largest sum and print the same

#include <iostream>
#include<climits>
using namespace std;

void findLargest(int **input, int nRows, int mCols)
{
int max=INT_MIN; // declared the max to INT_MIN initially
int i,j;
int sumR,sumC; // sumR= Sum of the elements of the Row , sumC= Sum of the elements of the Column
int max_row_index=0; // Index of the maximum sum row
int max_col_index=0; // Index of the maximum sum column
bool isRow=true; // Assuming the sumR is maximum
bool isCol=false; // Assuming the sumC is not maximum
for(int i=0;i<nRows;i++)
{
sumR=0;
for(int j=0;j<mCols;j++)
{
sumR=sumR+input[i][j];
}
if(sumR>max)
{
max=sumR; // Initilising the max to sumR if sumR>max
max_row_index=i; // Updating the maximum row index
isRow=true; // Keeping the sumR as maximum only
}
}
for(int j=0;j<mCols;j++)
{
sumC=0;
for(int i=0;i<nRows;i++)
{
sumC=sumC+input[i][j];
}
if(sumC>max)
{
max=sumC; // Initilising the max to sumC if sumC>max
max_col_index=j; // Updating the maximum column index
isCol=true; // Changing the sumC as maximum now
isRow=false; // And hence the sumR will now become minimum
}
}

// Printing Accordingly
if(isRow==true)
{
cout<<"row"<<" "<<max_row_index<<" "<<max;
}
else if(isCol==true)
{
cout<<"column"<<" "<<max_col_index<<" "<<max;
}
}

int main()
{
int t;
cout<<"Enter the number of test cases to run : ";
cin >> t;
while (t--)
{
int row, col;
cout<<"Enter the number of rows and column : ";
cin >> row >> col;

cout<<"Enter the number of elements corresponding to the each row and column : ";

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;
}
}