How to Print Rows and Columns with max number of ” 1’s ” in Java

In this tutorial, we will print Rows and Columns with max number of ” 1’s ” in Java.

Java Problem: First generate a random 2 D  matrix of 0’s and 1’s .then print the rows in which the number of 1’s is maximum. Then print the column index in which there is a maximum number of  1’s in Java.

Print Rows and Columns with max number of  ” 1’s ” in Java

Hint: we can use ArrayList for storing the index

Solution: Steps we took to solve this problem
1. Generate the random 2d array
2. Size of the array is increased by one number to store the count of 1’s in that particular row and column.
for an eg.  matrix of 0’s and 1’s of size n with the number of 1’s in respective row and column

1 0 1  2
1 0 1  2
1 0 0  1
3 0 2

3. Create an ArrayList, compare every element of the last row /the last column if an element of the array is found greater than row max then change row max, clear the array list and add the recent index to it.

4. Print the array list

here is code for same:-

import java.util.ArrayList;
import java.util.Scanner;

public class array {

  public static void main(String[] args) { 
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the size of matrix");
    int n = s.nextInt();

    int arr[][] = new int[n + 1][n + 1];  //declaring array with extra column snd row
    for (int i = 0; i < n; i++) {  //generation of random array of '0' & '1'
      for (int j = 0; j < n; j++)
        arr[i][j] = (int) Math.round(Math.random());
    }

    for (int i = 0; i < n; i++) {  //print the obtained random matrix
      for (int j = 0; j < n; j++)
        System.out.print(arr[i][j] + " ");
      System.out.println();
    }
  largestRow(arr,n);	
  largestCol(arr,n);
  }

  static void largestRow(int[][] arr,int n) {  // function of finding largest in row
    ArrayList<Integer> arr1 = new ArrayList<Integer>();
         
       
    for (int i = 0; i < n; i++) {
      int count = 0; 
      for (int j = 0; j < n; j++) {
        if (arr[i][j] == 1)   //counting number of 1s in row
          count++;
      } 
      arr[i][n] = count;    //storing the count in last column
    }
    int row_max = arr[0][n];

    for (int i = 0; i < n; i++) {
      if (arr[i][n] > row_max) {  //if array element found greater than row max then clear array list 
        arr1.clear();
        row_max = arr[i][n];   //update the row max
      }

      if (arr[i][n] == row_max) //if row element is equal to row max store the index in array list
        arr1.add(i);

    }
    System.out.print("rows with max 1s: ");  
    for (int i = 0; i < arr1.size(); i++)   //print array list storing row index 
      System.out.print(arr1.get(i) + " ");
    System.out.println();

  }

  static void largestCol(int [][]arr,int n) {
    ArrayList<Integer> arr1 = new ArrayList<Integer>();

    for (int i = 0; i < n; i++) {     //will count all '1s' in the coloumn
      int count = 0;
      for (int j = 0; j < n; j++) {
     
   if (arr[j][i] == 1)
          count++;
      }
      arr[n][i] = count;              //will store it in the last row
    }
    int col_max = arr[n][0];

    for (int i = 0; i < n; i++) {
      if (arr[n][i] > col_max) {    //  compare all last row elemnt for max if found
        arr1.clear();            //clear the previous data
        col_max = arr[n][i];     //update column max 
      }

      if (arr[n][i] ==col_max)
   
        arr1.add(i);           //if array elemnt equal column max store the index in array list

    }
    System.out.print("columns with max 1s: ");
    for (int i = 0; i < arr1.size(); i++)   //printing the arraylist storing column index
      System.out.print(arr1.get(i) + " ");

  }
}

Output:

Enter the size of matrix
4
0 0 1 0 
1 0 0 1 
0 0 0 0 
0 1 0 1 
rows with max 1s: 1 3 
columns with max 1s: 3

Hope you understand the code.

Learn:

Leave a Reply

Your email address will not be published. Required fields are marked *