Even and Odd Indexes
Given an array of integers, print two integer values:
First, sum of all numbers which are even as well as the index is also even.
Second, sum of all numbers which are odd as well as the index is also odd.
Print the two integers space separated. (0 indexed array)
Input:
Given an integer denoting the size of array.
Next line will have a line containing ‘t’ space separated integers.
Constraints:
1<=t<=10^5
1 <= A[i] <= 10^6
Output:
Two space separated integers denoting even and odd sums respectively.
Sample Input:
5
2 3 5 1 4
Sample Output:
6 4
Program for Even and Odd Indexes in C++
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int even,odd;
int* arr = new int [n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n;i++){
if((arr[i]%2 == 0) && i%2==0 ){
even = even + arr[i];
}
if((arr[i]%2 == 1) && i%2==1 ){
odd += arr[i];
}
}
cout<<even<<" "<<odd;
return 0;
}
Comments
Post a Comment