Golang Program to Find Max No. of 1 in a Sorted Row of the Matrix


In programming languages, we can create 2−Dimensional Matrices and store elements in them. The 2−Dimensional metric is a data structure that has rows and columns. In this article, we are going to see the two different logic to find the maximum no. of ones in a sorted row of the matrix.

For example, we have below the matrix

{0, 1, 1, 1}, no. of 1s = 3

{0, 0, 1, 1}, no. of 1s = 2

{1, 1, 1, 1}, no. of 1s = 4

{0, 0, 0, 0}, no. of 1s = 0

In the third row, we have 4 ones that are greater than the other rows.

Method 1

In this method, we are going to use the nested for loops in which we will iterate over the array and count the no. of ones, in the end, return the maximum count. The time complexity of this approach is O(N*M) where N is the size row and M is the size of the column of the matrix.

Algorithm

Step 1: Import the required packages.

Step 2: First the program will start from the main function.

  • Inside the main function first, we are declaring the 2−Dimensional matrix of size 4X4

  • Later we are calling the max1in2DMatrix() function which is returning the maximum no. of ones in a row.

  • In the end, we are printing the value return in the last step.

Step 3: In max1in2DMatrix() function

  • we are declaring a maxCount variable in which we are storing zero as a value.

  • Then we are running nested for loop.

  • The outer loop is to iterate over all the rows.

  • In the inner loop, we are counting the ones in that row.

  • In the end, we are returning the number.

Example

This is the code to find the maximum 1 in the 2D matrix by running a nested loop over the matrix and on each iteration find the no. of 1 in each rows and updating the variable that holds the count of maximum 1.

package main

import "fmt"

// function to find the maximum number of 1 in a row
// having 2D array and int variables as arguments and
// int return type
func max1in2DMatrix(matrix [4][4]int, n int, m int) int {
    // declaring and initializing a variable to store the max count
    var maxCount int
    maxCount = 0

    // running nested for loop to iterate over the matrix and
    // count no. of ones in each row
    for i := 0; i < n; i++ {
        count := 0
        for j := 0; j < m; j++ {
            if matrix[i][j] == 1 {
                count++
            }
        }
        if maxCount < count {
            maxCount = count
        }
    }
    // returning the max count
    return maxCount
}

func main() {
    // creating a 2-dimensional array with 0 and 1 as value
    matrix := [4][4]int{
        {0, 1, 1, 1},
        {0, 0, 1, 1},
        {1, 1, 1, 1},
        {0, 0, 0, 0},
    }

    fmt.Println("Golang program to find the maximum number of 1 in a row using a for loop.")

    // calling a function with matrix name and size as parameters
    maxCount := max1in2DMatrix(matrix, 4, 4)

    fmt.Println("The maximum number of 1 in the array is", maxCount)
}

Output

Golang program to find the maximum number of 1 in a row using a for loop.
The maximum number of 1 in the array is 4

Method 2

In this method, we are going to find the max number of ones in a row using the binary search algorithm. The binary search algorithm is a searching algorithm to find an element in a sorted array with O(logN) time complexity. Using a binary search algorithm the time complexity for this method will be O(NlogM) where N is the size of the row and M is the size of the column.

Algorithm

Step 1: Import the required packages

Step 2: First the program will start from the main function.

  • Inside the main function first we are declaring the 2-Dimensional matrix of size 4X4

  • Later we are calling the max1in2DMatrix() function which is returning the maximum no. of ones in a row.

  • In the end we are printing the value return in the last step.

Step 3: In max1in2DMatrix() function

  • we are declaring a maxCount variable in which we are storing zero as value.

  • Next we are running a for loop on every row.

  • Inside the for loop we are calling the binarySearch() function in which we are finding the point where the first one occurred.

  • In the end, we are returning the maxCount of one in a row of the matrix.

Example

This is the code to find the maximum 1 in the 2D matrix by sorting each row and applying binary search to find the index from where 1 is occurring and subtracting that from the size of the array will tell us the total number of 1 in the row.

package main

import "fmt"

// recursive function to find the pivot point where the first one occurs
// using the binary search and having array and integers as arguments
func binarySearch(array [4]int, l int, r int) int {
    if l <= r {
        // find the midpoint in the current array
        m := l + (r-l)/2
        // condition to see if first one is starting from current index
        if (m == 0 || array[m-1] == 0) && array[m] == 1 {
            return m
        } else if array[m] == 0 {
            return binarySearch(array, m+1, r)
        } else if array[m] == 1 {
            return binarySearch(array, l, m-1)
        }
    }
    return -1
}

// function to find the maximum number of 1 in a row
// having 2D array and int variables as arguments and
// int return type
func max1in2DMatrix(matrix [4][4]int, n int, m int) int {
    // declaring and initializing variable to store max count
    var maxCount int
    maxCount = 0

    // running for loop to iterate over each row of matrix and
    // call binary search function to find the point where 1 is starting
    for i := 0; i < n; i++ {
        // calling the binary search function to find the pivot point
        count := binarySearch(matrix[i], 0, 3)
        if count != -1 && maxCount < 4-count {
            maxCount = 4 - count
        }
    }
    // returning the max count
    return maxCount
}

func main() {
    // creating a 2 dimensional array with 0 and 1 as value
    matrix := [4][4]int{
        {0, 1, 1, 1},
        {0, 0, 1, 1},
        {1, 1, 1, 1},
        {0, 0, 0, 0},
    }

    fmt.Println("Golang program to find the maximum number of 1 in a row using the binary search algorithm.")

    // calling a function with matrix name and size as parameters
    maxCount := max1in2DMatrix(matrix, 4, 4)

    fmt.Println("The maximum number of 1 in the array is", maxCount)
}

Output

Golang program to find the maximum number of 1 in a row using the binary search algorithm.
The maximum number of 1 in the array is 4

Conclusion

These are the two ways to find the maximum no. of one in a row of the matrix. If we compare which approach is efficient in terms of time then whenever we use an appropriate algorithm then the time reduces that is why the second one is more efficient as we are using a binary search algorithm. To learn more about Golang you can explore these tutorials.

Updated on: 10-Jul-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements