Sum of Odd Odd, Even Even Problem Statement You are given a positive integer n .The second line will contain n positive integers. Write a function that will take an integer array as an argument and return the sum in the following way. >> If the index is odd and at the same time the value is odd then you can add the index and values >> If the index is even and at the same time the value is even then you can add the index and values Return the total sum from the function. If there is no such value which is mentioned in the above then return 0 from the function. See the sample input ,output and explanation for more clarification. Constraints- 1<=n<=100 Values of array will be given between 1-1000 Sample Input 1: Sample Output 1: 6 22 12 19 3 1 6 10 Sample Input 2: Sample Output 2: 8 12 6 1 3 4 2 2 3 20 Explanation - In sample input -1 3rd index means index = 3 and the value is 3 , index+value=3+3=6 6the index means index = 6 and the value is 10 , index+value=6+10=16 Total = 22

 Sum of Odd Odd, Even Even 

Problem Statement

You are given a positive integer n .The second line will contain n positive integers.

Write a function that will take an integer array as an argument and return the sum in the following way.

>> If the index is odd and at the same time the value is odd then you can add the index and values

>> If the index is even and at the same time the value is even then you can add the index and values



Return the total sum from the function. If there is no such value which is mentioned in the above then return 0 from the function.

See the sample input ,output and explanation for more clarification. 


Constraints- 

1<=n<=100

Values of array will be given between 1-1000


Sample Input 1: Sample Output 1:

6           22

12 19 3 1 6 10

Sample Input 2: Sample Output 2:  

8

12 6 1 3 4 2 2 3                 20                                                            



Explanation -

In sample input -1  

3rd index means index = 3 and the value is 3 , index+value=3+3=6

6the index means index = 6 and the value is 10 , index+value=6+10=16

Total = 22




answer:




Here is a function that takes an integer array and its length as arguments and returns the sum of the indices and values of the array elements that satisfy the conditions mentioned in the problem statement:

int sum_odd_odd_even_even(int *arr, int n) { int sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && arr[i] % 2 == 0) { sum += i + arr[i]; } else if (i % 2 == 1 && arr[i] % 2 == 1) { sum += i + arr[i]; } } return sum; }

This function starts a loop from 0 to n, and for each element in the array, it checks if the index is even and the value is even, or if the index is odd and the value is odd. If either of these conditions is true, it adds the index and the value of the element to the sum.

For example, if you call sum_odd_odd_even_even([12, 19, 3, 1, 6, 10], 6), the function will return 22, as the 3rd element (index=2, value=3) and the 6th element (index=5, value=10) satisfy the conditions.

If you call sum_odd_odd_even_even([12, 6, 1, 3, 4, 2, 2, 3], 8), the function will return 20, as the 2nd element (index=1, value=6) and the 4th element (index=3, value=3) satisfy the conditions.

Note that the indices are 0-based, so the first element has index 0, the second element has index 1, and so on.


Post a Comment

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();