Open In App

POTD Solutions | 09 Nov’ 23 | Predict the Column

Last Updated : 09 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

View all POTD Solutions

Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Matrix but will also help you build up problem-solving skills.

9th-nov

POTD Solutions 9 November 2023

We recommend you to try this problem on our GeeksforGeeks Practice portal first, and maintain your streak to earn Geeksbits and other exciting prizes, before moving towards the solution.

C++




class Solution {
    columnWithMaxZeros(arr, N) {
        // Initialize variables to keep track of the maximum count and the corresponding column index.
        let max_count = 0;
        let ans = -1;
 
        // Iterate over each column.
        for (let i = 0; i < N; i++) {
            // Initialize count for the current column.
            let count = 0;
 
            // Iterate over each row in the current column.
            for (let j = 0; j < N; j++) {
                // Counting zeros in the current column.
                if (arr[j][i] === 0) {
                    count++;
                }
            }
 
            // Update max_count and ans if the current column has more zeros.
            if (count > max_count) {
                max_count = count;
                ans = i;
            }
        }
 
        // Return the column index with the maximum number of zeros.
        return ans;
    }
}


Java




class Solution {
    int columnWithMaxZeros(int arr[][], int N)
    {
        int idx = -1, val = 0;
        for (int j = 0; j < N; j++) {
            int sum = 0;
            for (int i = 0; i < N; i++) {
                sum += 1 - arr[i][j];
            }
            if (val < sum) {
                val = sum;
                idx = j;
            }
        }
        return idx;
    }
}


Python3




class Solution:
    def columnWithMaxZeros(self, arr, N):
        max_count = 0
        ans = -1
         
        for i in range(N):
            count = 0
            for j in range(N):
                # Counting zeroes
                if arr[j][i] == 0:
                    count += 1
             
            # Updating max_count and ans as required
            if count > max_count:
                max_count = count
                ans = i
         
        return ans


C#




using System;
using System.Collections.Generic;
 
class Solution
{
    /*
     * Function to find the column with the maximum number of zeros
     * N: Number of rows and columns in the array
     */
    public int ColumnWithMaxZeros(List<List<int>> arr, int N)
    {
        // Initialize variables to store the maximum count of zeros and the corresponding column index
        int maxCount = 0, columnIndexWithMaxZeros = -1;
 
        // Iterate over each column
        for (int i = 0; i < N; i++)
        {
            // Initialize count for zeros in the current column
            int count = 0;
 
            // Iterate over each row in the current column
            for (int j = 0; j < N; j++)
            {
                // Counting zeros in the current column
                if (arr[j][i] == 0)
                {
                    count++;
                }
            }
 
            // Update maxCount and columnIndexWithMaxZeros if the current column has more zeros
            if (count > maxCount)
            {
                maxCount = count;
                columnIndexWithMaxZeros = i;
            }
        }
 
        // Return the column index with the maximum number of zeros
        return columnIndexWithMaxZeros;
    }
}


Javascript




class Solution {
    columnWithMaxZeros(arr, N) {
        // Initialize variables to keep track of the maximum count and the corresponding column index.
        let maxCount = 0;
        let ans = -1;
 
        // Iterate over each column.
        for (let i = 0; i < N; i++) {
            // Initialize count for the current column.
            let count = 0;
 
            // Iterate over each row in the current column.
            for (let j = 0; j < N; j++) {
                // Counting zeros in the current column.
                if (arr[j][i] === 0) {
                    count++;
                }
            }
 
            // Update maxCount and ans if the current column has more zeros.
            if (count > maxCount) {
                maxCount = count;
                ans = i;
            }
        }
 
        // Return the column index with the maximum number of zeros.
        return ans;
    }
}


Time Complexity: O(N X N), where N is the number of rows or columns in the matrix
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads