Open In App

Sum of columns of a 2-D Matrix where first element is odd

Improve
Improve
Like Article
Like
Save
Share
Report

Given a matrix mat[][], the task is to print the sum of the columns where the first element is odd.

Examples:  

Input: mat[][] = { 
{8, 2, 3, 5}, 
{9, 8, 7, 6}, 
{1, 2, 5, 5} } 
Output: 31 
Only the third and the fourth columns start with an odd element. 
And, sum = 3 + 7 + 5 + 5 + 6 + 5 = 31

Input: mat[][] = { 
{10, 80, 20, 12, 40}, 
{10, 21, 23, 45, 29}, 
{19, 18, 17, 16, 15}, 
{10, 11, 12, 13, 14} } 
Output: -1 

Approach: Initialize sum = 0 and start traversing the first row. If any element in the first row is odd, then add all the elements of that column to the sum. Print the sum at the end.

Below is the implementation of the above approach: 

C++




// CPP implementation of the approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to return the sum of those columns
// of a given matrix that start with an odd element
int sumColumns(int arr[][4], int r, int c)
{
     
    // Initialize sum to 0
    int sum = 0;
     
    // Traverse through all the elements of the first row
    for (int j = 0; j < c; j++)
    {
        // If the element is odd
        if (arr[0][j] % 2 != 0)
        {
            // Add all the elements of that column
            for (int i = 0; i < r; i++)
                sum += arr[i][j];
        }
             
             
    }
 
    // Return the sum
    return sum;
 
}
     
// Driver code
int main()
{
int arr[3][4] = {{8, 2, 3, 5},{9, 8, 7, 6}, {1, 2, 5, 5}};
int r = sizeof(arr)/sizeof(arr[0]);
int c = sizeof(arr[0])/sizeof(int);
cout<<(sumColumns(arr, 3, 4));
}
 
// This code is contributed by
// Surendra_Gangwar


Java




// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to return the sum of those columns
// of a given matrix that start with an odd element
static int sumColumns(int arr[][], int r, int c)
{
     
    // Initialize sum to 0
    int sum = 0;
     
    // Traverse through all the
    // elements of the first row
    for (int j = 0; j < c; j++)
    {
        // If the element is odd
        if (arr[0][j] % 2 != 0)
        {
            // Add all the elements of that column
            for (int i = 0; i < r; i++)
                sum += arr[i][j];
        }
    }
 
    // Return the sum
    return sum;
}
     
// Driver code
public static void main (String[] args)
{
    int arr[][] = {{8, 2, 3, 5},{9, 8, 7, 6}, {1, 2, 5, 5}};
    System.out.println(sumColumns(arr, 3, 4));
}
}
 
// This code is contributed by
// shs..


Python3




# Python3 implementation of the approach
 
# Function to return the sum of those columns
# of a given matrix that start with an odd element
def sumColumns(arr, r, c):
     
    # Initialize sum to 0
    sum = 0
     
    # Traverse through all the elements of the first row
    for j in range(c):
         
        # If the element is odd
        if (arr[0][j] % 2 != 0):
             
            # Add all the elements of that column
            for i in range(r):
                sum += arr[i][j]
 
    # Return the sum
    return sum
 
# Driver code
arr = [ [8, 2, 3, 5], [9, 8, 7, 6], [1, 2, 5, 5] ]
r = len(arr)
c = len(arr[0])
print(sumColumns(arr, r, c))


C#




// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the sum of those columns
    // of a given matrix that start with an odd element
    static int sumColumns(int [,]arr, int r, int c)
    {
         
        // Initialize sum to 0
        int sum = 0;
         
        // Traverse through all the
        // elements of the first row
        for (int j = 0; j < c; j++)
        {
            // If the element is odd
            if (arr[0, j] % 2 != 0)
            {
                // Add all the elements of that column
                for (int i = 0; i < r; i++)
                    sum += arr[i, j];
            }
        }
     
        // Return the sum
        return sum;
    }
         
    // Driver code
    public static void Main()
    {
        int [,]arr= {{8, 2, 3, 5}, {9, 8, 7, 6}, {1, 2, 5, 5}};
        Console.WriteLine(sumColumns(arr, 3, 4));
    }
}
 
// This code is contributed by aishwarya.27


PHP




<?php
// PHP implementation of the approach
 
// Function to return the sum of those
// columns of a given matrix that start
// with an odd element
function sumColumns($arr, $r, $c)
{
     
    // Initialize sum to 0
    $sum = 0;
     
    // Traverse through all the elements
    // of the first row
    for ($j = 0; $j < $c; $j++)
    {
        // If the element is odd
        if ($arr[0][$j] % 2 != 0)
        {
            // Add all the elements of that column
            for ($i = 0; $i < $r; $i++)
                $sum += $arr[$i][$j];
        }
    }
 
    // Return the sum
    return $sum;
}
     
// Driver code
$arr = array(array(8, 2, 3, 5),
             array(9, 8, 7, 6),
             array(1, 2, 5, 5));
             
$r = sizeof($arr);
$c = sizeof($arr[0]);
 
echo (sumColumns($arr, $r, $c));
 
// This code is contributed by Ryuga
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the sum of those columns
// of a given matrix that start with an odd element
function sumColumns(arr, r, c)
{
     
    // Initialize sum to 0
    var sum = 0;
     
    // Traverse through all the elements of the first row
    for (var j = 0; j < c; j++)
    {
        // If the element is odd
        if (arr[0][j] % 2 != 0)
        {
            // Add all the elements of that column
            for (var i = 0; i < r; i++)
                sum += arr[i][j];
        }
             
             
    }
 
    // Return the sum
    return sum;
 
}
     
// Driver code
 
var arr = [[8, 2, 3, 5],[9, 8, 7, 6], [1, 2, 5, 5]];
var r = arr.length;
var c = arr[0].length;
document.write(sumColumns(arr, 3, 4));
 
</script>


Output

31


Last Updated : 14 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads