Open In App

Count of columns with odd number of 1s

Improve
Improve
Like Article
Like
Save
Share
Report

Given an N * M 2D binary matrix, the task is to find the count of columns having odd number of 1s.
Examples: 
 

Input: mat[][] = { 
{0, 0, 1, 0}, 
{1, 0, 0, 1}, 
{1, 1, 1, 0}} 
Output:
Column 2 and 4 are the only columns 
having odd number of 1’s.
Input: mat[][] = { 
{1, 1, 0, 0, 1, 1}, 
{0, 1, 0, 1, 0, 0}, 
{1, 1, 1, 0, 1, 0}} 
Output:
 

 

Approach: Find the sum of all the columns of the matrix separately. The columns having an odd sum are the columns which have an odd number of 1s.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
#include <iostream>
using namespace std;
 
const int col = 4;
const int row = 3;
 
// Function to return the count of
// columns having odd number of 1s
int countOddColumn(int arr[row][col])
{
 
    // To store the sum of every column
    int sum[col] = { 0 };
 
    // For every column
    for (int i = 0; i < col; i++) {
 
        // Sum of all the element
        // of the current column
        for (int j = 0; j < row; j++) {
            sum[i] += arr[j][i];
        }
    }
 
    // To store the required count
    int count = 0;
 
    for (int i = 0; i < col; i++) {
 
        // If the sum of the current
        // column is odd
        if (sum[i] % 2 == 1) {
            count++;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int arr[row][col] = { { 0, 0, 1, 0 },
                          { 1, 0, 0, 1 },
                          { 1, 1, 1, 0 } };
 
    cout << countOddColumn((arr));
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
static int col = 4;
static int row = 3;
 
// Function to return the count of
// columns having odd number of 1s
static int countOddColumn(int arr[][])
{
 
    // To store the sum of every column
    int []sum = new int[col];
 
    // For every column
    for (int i = 0; i < col; i++)
    {
 
        // Sum of all the element
        // of the current column
        for (int j = 0; j < row; j++)
        {
            sum[i] += arr[j][i];
        }
    }
 
    // To store the required count
    int count = 0;
 
    for (int i = 0; i < col; i++)
    {
 
        // If the sum of the current
        // column is odd
        if (sum[i] % 2 == 1)
        {
            count++;
        }
    }
    return count;
}
 
// Driver code
public static void main(String []args)
{
    int arr[][] = {{ 0, 0, 1, 0 },
                   { 1, 0, 0, 1 },
                   { 1, 1, 1, 0 }};
 
    System.out.println(countOddColumn((arr)));
}
}
 
// This code is contributed by Rajput-Ji


Python3




# Python3 implementation of the approach
col = 4
row = 3
 
# Function to return the count of
# columns having odd number of 1s
def countOddColumn(arr):
 
    # To store the sum of every column
    sum = [0 for i in range(col)]
 
    # For every column
    for i in range(col):
 
        # Sum of all the element
        # of the current column
        for j in range(row):
            sum[i] += arr[j][i]
 
    # To store the required count
    count = 0
 
    for i in range(col):
 
        # If the sum of the current
        # column is odd
        if (sum[i] % 2 == 1):
            count += 1
 
    return count
 
# Driver code
arr = [[0, 0, 1, 0],
       [1, 0, 0, 1],
       [1, 1, 1, 0]]
 
print(countOddColumn((arr)))
 
# This code is contributed by Mohit Kumar


C#




// C# implementation of the approach
using System;
 
class GFG
{
    static int col = 4;
    static int row = 3;
     
    // Function to return the count of
    // columns having odd number of 1s
    static int countOddColumn(int [,]arr)
    {
     
        // To store the sum of every column
        int []sum = new int[col];
     
        // For every column
        for (int i = 0; i < col; i++)
        {
     
            // Sum of all the element
            // of the current column
            for (int j = 0; j < row; j++)
            {
                sum[i] += arr[j, i];
            }
        }
     
        // To store the required count
        int count = 0;
     
        for (int i = 0; i < col; i++)
        {
     
            // If the sum of the current
            // column is odd
            if (sum[i] % 2 == 1)
            {
                count++;
            }
        }
        return count;
    }
     
    // Driver code
    public static void Main()
    {
        int [,]arr = {{ 0, 0, 1, 0 },
                      { 1, 0, 0, 1 },
                      { 1, 1, 1, 0 }};
     
        Console.WriteLine(countOddColumn((arr)));
    }
}
 
// This code is contributed by kanugargng


Javascript




<script>
 
// JavaScript implementation of the approach
 
 
const col = 4;
const row = 3;
 
// Function to return the count of
// columns having odd number of 1s
function countOddColumn(arr) {
 
    // To store the sum of every column
    let sum = new Array(col).fill(0);
 
    // For every column
    for (let i = 0; i < col; i++) {
 
        // Sum of all the element
        // of the current column
        for (let j = 0; j < row; j++) {
            sum[i] += arr[j][i];
        }
    }
 
    // To store the required count
    let count = 0;
 
    for (let i = 0; i < col; i++) {
 
        // If the sum of the current
        // column is odd
        if (sum[i] % 2 == 1) {
            count++;
        }
    }
 
    return count;
}
 
// Driver code
 
let arr = [[0, 0, 1, 0],
[1, 0, 0, 1],
[1, 1, 1, 0]];
 
document.write(countOddColumn((arr)))
 
</script>


Output: 

2

 

Time Complexity: O(row * col).
Auxiliary Space: O(col). 



Last Updated : 13 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads