📌  相关文章
📜  国际空间研究组织 | ISRO CS 2008 |问题2(1)

📅  最后修改于: 2023-12-03 15:37:14.092000             🧑  作者: Mango

ISRO CS 2008 - Question 2

This is a programming problem from the ISRO CS 2008 exam. The problem statement is as follows:

Given an array of integers, write a program to find the sum of all even and odd numbers separately.

Approach

To solve this problem, we simply loop through the array and keep track of the sum of even and odd numbers separately. We can do this by using a variable for each sum and adding the current number to the appropriate variable.

Here's the implementation of the approach in Python:

def sum_odd_even(arr):
    odd_sum = 0
    even_sum = 0
    
    for num in arr:
        if num % 2 == 0:
            even_sum += num
        else:
            odd_sum += num
    
    return odd_sum, even_sum

This function takes an array of integers as input and returns a tuple of two integers - the sum of odd numbers and the sum of even numbers.

Complexity Analysis

The time complexity of this implementation is O(n), where n is the length of the input array. This is because we are iterating through each element of the array once.

The space complexity of this implementation is O(1), because we are only using two variables to store the sum of odd and even numbers.

Conclusion

In this article, we have discussed how to solve the ISRO CS 2008 - Question 2 programming problem. We have shown an implementation of the solution using Python, along with a complexity analysis.