diff --git a/Cpp/largest_row_or_column.cpp b/Cpp/largest_row_or_column.cpp new file mode 100644 index 0000000..0bce7eb --- /dev/null +++ b/Cpp/largest_row_or_column.cpp @@ -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 +#include +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;imax) + { + 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;jmax) + { + 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"<<" "<> 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; + } +} \ No newline at end of file