Given an array of n elements. The task is to return the count of the number of odd numbers in the array.
Input: n=5, array = [1,2,3,4,5]
Output: 3
Explanation: The three odd elements are (1,3,5).
Input: n=6, array = [1,2,1,1,5,1]
Output: 5
Explanation: The five odd elements are one 5 and four 1's.
Input: n=5, array = [1,3,5,7,9]
A simple way to solve this problem is by traversing the whole array and checking each element if it is odd. The count of the odd numbers found will be the answer.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to count the odd numbers in an array
int countOdd(int arr[], int n) {
// Set counter to 0
int count = 0;
// Iterate through the array
for (int i = 0; i < n; i++) {
// check for odd values and increment
if (arr[i] % 2 != 0) {
count++;
}
}
return count;
}
};
//Main method
int main() {
// Creating an instance of Solution class
Solution sol;
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
// Function call to count the odd numbers in an array
int count = sol.countOdd(arr, n);
cout << "Count of odd numbers: " << count << endl;
return 0;
}
class Solution {
// Function to count the odd numbers in an array
public int countOdd(int[] arr, int n) {
int count = 0;
// Iterate through the array
for (int i = 0; i < n; i++) {
// Check for odd values and increment
if (arr[i] % 2 != 0) {
count++;
}
}
return count;
}
//Main method
public static void main(String[] args) {
// Creating an instance of Solution class
Solution sol = new Solution();
int[] arr = {1, 2, 3, 4, 5};
int n = arr.length;
// Function call to count the odd numbers in an array
int count = sol.countOdd(arr, n);
System.out.println("Count of odd numbers: " + count);
}
}
class Solution:
# Function to count the odd numbers in an array
def countOdd(self, arr, n):
count = 0
# Iterate through the array
for num in arr:
# Check for odd values and increment
if num % 2 != 0:
count += 1
return count
# Main method
if __name__ == "__main__":
# Creating an instance of Solution class
sol = Solution()
arr = [1, 2, 3, 4, 5]
n = len(arr)
# Function call to count the odd numbers in an array
count = sol.countOdd(arr, n)
print("Count of odd numbers:", count)
class Solution {
// Function to count the odd numbers in an array
countOdd(arr, n) {
let count = 0;
// Iterate through the array
for (let i = 0; i < n; i++) {
// Check for odd values and increment
if (arr[i] % 2 !== 0) {
count++;
}
}
return count;
}
}
// Creating an instance of Solution class
const sol = new Solution();
const arr = [1, 2, 3, 4, 5];
const n = arr.length;
// Function to count the odd numbers in an array
const count = sol.countOdd(arr, n);
console.log("Count of odd numbers:", count);
Time Complexity : O(N)
Each element in the array has to be inspected once to determine if it's odd, resulting in a linear time complexity where N is the number of elements in the array.
Space Complexity : O(1)
The space used is constant, as we only use a single counter regardless of the size of the input array.