Prerequisite – Array Basics
Given an array, write a program to find the sum of values of even and odd index positions separately.
Examples:
Input : arr[] = {1, 2, 3, 4, 5, 6}
Output :Even index positions sum 9
Odd index positions sum 12
Explanation: Here, n = 6 so there will be 3 even
index positions and 3 odd index positions in an array
Even = 1 + 3 + 5 = 9
Odd = 2 + 4 + 6 = 12
Input : arr[] = {10, 20, 30, 40, 50, 60, 70}
Output : Even index positions sum 160
Odd index positions sum 120
Explanation: Here, n = 7 so there will be 3 odd
index positions and 4 even index positions in an array
Even = 10 + 30 + 50 + 70 = 160
Odd = 20 + 40 + 60 = 120
Implementation:
C++
#include <iostream>
using namespace std;
void EvenOddSum( int arr[], int n)
{
int even = 0;
int odd = 0;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
cout << "Even index positions sum " << even;
cout << "\nOdd index positions sum " << odd;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
EvenOddSum(arr, n);
return 0;
}
|
Java
import java.io.*;
class EvenOddSum {
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 4 , 5 , 6 };
int even = 0 , odd = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (i % 2 == 0 )
even += arr[i];
else
odd += arr[i];
}
System.out.println( "Even index positions sum: " + even);
System.out.println( "Odd index positions sum: " + odd);
}
}
|
Python3
def EvenOddSum(a, n):
even = 0
odd = 0
for i in range (n):
if i % 2 = = 0 :
even + = a[i]
else :
odd + = a[i]
print ( "Even index positions sum " , even)
print ( "nOdd index positions sum " , odd)
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len (arr)
EvenOddSum(arr, n)
|
C#
using System;
public class GFG {
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int even = 0, odd = 0;
for ( int i = 0; i < arr.Length; i++)
{
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
Console.WriteLine( "Even index positions"
+ " sum: " + even);
Console.WriteLine( "Odd index positions "
+ "sum: " + odd);
}
}
|
PHP
<?php
function EvenOddSum( $arr , $n )
{
$even = 0;
$odd = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $i % 2 == 0)
$even += $arr [ $i ];
else
$odd += $arr [ $i ];
}
echo ( "Even index positions sum " . $even );
echo ( "\nOdd index positions sum " . $odd );
}
$arr = array ( 1, 2, 3, 4, 5, 6 );
$n = sizeof( $arr );
EvenOddSum( $arr , $n );
?>
|
Javascript
<script>
function EvenOddSum(arr, n)
{
let even = 0;
let odd = 0;
for (let i = 0; i < n; i++)
{
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
document.write( "Even index positions sum " + even);
document.write( "<br>" + "Odd index positions sum " + odd);
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let n = arr.length;
EvenOddSum(arr, n);
</script>
|
Output
Even index positions sum 9
Odd index positions sum 12
Time Complexity: O(length(arr))
Auxiliary Space: 0(1)
Method 2: Using bitwise & operator
C++
#include <iostream>
using namespace std;
void EvenOddSum( int arr[], int n)
{
int even = 0;
int odd = 0;
for ( int i = 0; i < n; i++) {
if (i & 1 != 0)
odd += arr[i];
else
even += arr[i];
}
cout << "Even index positions sum " << even;
cout << "\nOdd index positions sum " << odd;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
EvenOddSum(arr, n);
return 0;
}
|
Java
public class Main {
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 };
int n = arr.length;
int even = 0 ;
int odd = 0 ;
for ( int i = 0 ; i < n; i++)
{
if ((i & 1 ) != 0 ) {
odd += arr[i];
}
else {
even += arr[i];
}
}
System.out.println( "Even index positions sum: "
+ even);
System.out.println( "Odd index positions sum: "
+ odd);
}
}
|
Python3
def EvenOddSum(a, n):
even = 0
odd = 0
for i in range (n):
if i & 1 :
odd + = a[i]
else :
even + = a[i]
print ( "Even index positions sum " , even)
print ( "Odd index positions sum " , odd)
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len (arr)
EvenOddSum(arr, n)
|
C#
using System;
namespace TestApplication
{
class Program
{
static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
int even = 0;
int odd = 0;
for ( int i = 0; i < n; i++)
{
if ((i & 1) != 0)
{
odd += arr[i];
}
else
{
even += arr[i];
}
}
Console.WriteLine( "Even index positions sum: " + even);
Console.WriteLine( "Odd index positions sum: " + odd);
}
}
}
|
Javascript
function EvenOddSum(arr, n)
{
let even = 0;
let odd = 0;
for (let i = 0; i < n; i++)
{
if (i & 1 != 0)
odd += arr[i];
else
even += arr[i];
}
console.log( "Even index positions sum " + even);
console.log( "\nOdd index positions sum " + odd);
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let n = arr.length;
EvenOddSum(arr, n);
|
Output
Even index positions sum 9
Odd index positions sum 12
Time Complexity: O(length(arr))
Auxiliary Space: 0(1)
Calculate sum of all even indices using slicing and repeat the same with odd indices and print sum.
Below is the implementation:
C++
#include <iostream>
using namespace std;
int EvenOddSum( int arr[] , int n)
{
int even = 0;
int odd = 0;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
cout<< "Even index positions sum " <<even<< "\n" ;
cout<< "Odd index positions sum " <<odd;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof (arr)/ sizeof (arr[0]);
EvenOddSum(arr, n);
}
|
Java
import java.io.*;
class GFG {
static void EvenOddSum( int [] arr, int n)
{
int even = 0 ;
int odd = 0 ;
for ( int i = 0 ; i < n; i++) {
if (i % 2 == 0 )
even += arr[i];
else
odd += arr[i];
}
System.out.println( "Even index positions sum "
+ even);
System.out.print( "Odd index positions sum " + odd);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 , 6 };
int n = arr.length;
EvenOddSum(arr, n);
}
}
|
Python3
def EvenOddSum(a, n):
even_sum = sum (a[:: 2 ])
odd_sum = sum (a[ 1 :: 2 ])
print ( "Even index positions sum" , even_sum)
print ( "Odd index positions sum" , odd_sum)
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len (arr)
EvenOddSum(arr, n)
|
C#
using System;
public class GFG {
static void EvenOddSum( int [] arr, int n)
{
int even = 0;
int odd = 0;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
Console.WriteLine( "Even index positions sum "
+ even);
Console.WriteLine( "Odd index positions sum " + odd);
}
static public void Main()
{
int [] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.Length;
EvenOddSum(arr, n);
}
}
|
Javascript
<script>
function EvenOddSum(arr, n)
{
let even = 0;
let odd = 0;
for (let i = 0; i < n; i++)
{
if (i % 2 == 0)
even += arr[i];
else
odd += arr[i];
}
document.write( "Even index positions sum " + even);
document.write( "<br>" + "Odd index positions sum " + odd);
}
let arr = [ 1, 2, 3, 4, 5, 6 ];
let n = arr.length;
EvenOddSum(arr, n);
</script>
|
Output
Even index positions sum 9
Odd index positions sum 12
Time Complexity: O(n) where n is no of elements in given array
Auxiliary Space: 0(1)
Method 2: Using bitwise | operator
C++14
#include<bits/stdc++.h>
using namespace std;
void EvenOddSum(vector< int >a, int n)
{
int even = 0;
int odd = 0;
for ( int i=0; i<n; i++)
{
if ((i|1)==i)
odd+=a[i];
else
even+=a[i];
}
cout<< "Even index positions sum " << even<<endl;
cout<< "Odd index positions sum " << odd<<endl;
}
int main()
{
vector< int >arr = {1, 2, 3, 4, 5, 6};
int n = arr.size();
EvenOddSum(arr, n);
}
|
Python3
def EvenOddSum(a, n):
even = 0
odd = 0
for i in range (n):
if i| 1 = = i:
odd + = a[i]
else :
even + = a[i]
print ( "Even index positions sum " , even)
print ( "Odd index positions sum " , odd)
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
n = len (arr)
EvenOddSum(arr, n)
|
Javascript
function EvenOddSum(a, n) {
let even = 0;
let odd = 0;
for (let i = 0; i < n; i++) {
if ((i|1) === i)
odd += a[i];
else
even += a[i];
}
console.log( "Even index positions sum " , even);
console.log( "Odd index positions sum " , odd);
}
const arr = [1, 2, 3, 4, 5, 6];
const n = arr.length;
EvenOddSum(arr, n);
|
Java
import java.util.*;
class Main {
static void EvenOddSum(List<Integer> a, int n)
{
int even = 0 ;
int odd = 0 ;
for ( int i = 0 ; i < n; i++) {
if ((i | 1 ) == i) {
odd += a.get(i);
}
else {
even += a.get(i);
}
}
System.out.println( "Even index positions sum "
+ even);
System.out.println( "Odd index positions sum "
+ odd);
}
public static void main(String[] args)
{
List<Integer> arr = new ArrayList<>(
Arrays.asList( 1 , 2 , 3 , 4 , 5 , 6 ));
int n = arr.size();
EvenOddSum(arr, n);
}
}
|
C#
using System;
using System.Collections.Generic;
class MainClass {
static void EvenOddSum(List< int > a, int n)
{
int even = 0;
int odd = 0;
for ( int i = 0; i < n; i++) {
if ((i | 1) == i)
odd += a[i];
else
even += a[i];
}
Console.WriteLine( "Even index positions sum "
+ even);
Console.WriteLine( "Odd index positions sum " + odd);
}
public static void Main( string [] args)
{
List< int > arr = new List< int >{ 1, 2, 3, 4, 5, 6 };
int n = arr.Count;
EvenOddSum(arr, n);
}
}
|
Output
Even index positions sum 9
Odd index positions sum 12
Time Complexity: O(length(arr))
Auxiliary Space: 0(1)
This article is contributed by Rishabh Jain. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!