# Find Even Odd Difference | Tech Mahindra coding questions

## Find Even Odd Difference

Given an array .Write a program to find the difference between the elements at odd index and even index.

Note : You are expected to write code in the findDifference function only which receive the first parameter as the numbers of items in the array and second parameter as the array itself. You are not required to take the input from the console.

Sample Input
input 1 : 7
input 2 : 10 20 30 40 50 60 70

Output
40

Explanation:-

Sum of element at even index of array is 10 + 30 + 50 + 70 = 160 and sum of elements at odd index of array is 20 + 40 + 60 = 120. The difference between both is 40

## Find Even Odd Difference

The objective of the code is to find the difference between the elements at odd index and even index. So first we will find the sum of all the element at even index and sum of element at odd index after that we will find the difference between both .

C++ Code:-
#include<bits/stdc++.h>
using namespace std;

int findDifference(int n,int a[])
{
int sum_at_odd=0,sum_at_even=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
sum_at_even+=a[i];
}
else
{
sum_at_odd+=a[i];
}
}
return (sum_at_even-sum_at_odd);
}

// Driver function
int main()
{
int n,start;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<findDifference(n,a);
return 0;
}

Output:-

7
10 20 30 40 50 60 70
40