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
Companies interview:-
- Swap adjacent characters
- Double the vowel characters
- Check valid parenthesis
- Print the characters with their frequencies
- Find closest value
- Word Count
- Program of CaesarCipher
- Program to find the perfect city
- Annual Day | Tech Mahindra coding question
- Find the number of pairs in the array whose sum is equal to a given target.
Wipro :-
- Update the booking ID | Wipro previous year question paper solution
- Pages in PDF
- Find the location id
- Find the odd digits
- Find the Product ID
Infytq :-
Key Points;-
Hackerrank:-
- Python : missing characters : hackerrank solution
- Python : string transformation | Hackerrank solution
- Active Traders certification test problem | Hackerrank Solution
- Usernames changes certification test problem | Hackerrank Solution
- string Representation of objects certification test hackerrank solution
- Average Function | hackerrank certification problem solution
C-tutorial:-
- Micros in C
- Pointer in c
- Function declaration
- Types of user define function
- return type of function
- 2D array
- c program to convert specified days into years weeks and days
- Print Reverse Hollow Pyramid
- Update the booking ID | Wipro previous year question paper
- Pages in PDF | Wipro previous year question paper
- Sparse Matrix in data structure
- Find the location ID | Wipro previous year Coding question
- find the odd digits | Wipro Coding question
- Find the product id | Wipro Coding question
- Difference between static and dynamic memory allocation
- What is asymptotic Notation
0 Comments