Sum of even numbers at even position
The problem aims to find the sum of elements that are present at even positions in an array, considering 0-indexing. Additionally, we need to include only those elements in the sum that are even numbers.
Problem Statement
Given an array of integers, the task is to find the sum of elements at even positions (index 0, 2, 4, ...) if the elements at those positions are even numbers.
Example
Let's take an example to understand the problem and its solution better:
Input
Array A = [3, 1, 2, 1, 6]
Explanation
The array A has the following elements at even positions (0-indexed):
- Index 0: 3 (Not an even number)
- Index 2: 2 (Even number)
- Index 4: 6 (Even number)
Output
The sum of elements at even positions (even numbers) = 2 + 6 = 8
Pseudocode
function evenAtEvenLocation(arr: array of integers)
sum = 0
for i = 0 to length(arr) - 1 do
if i is even and arr[i] is even then
sum += arr[i]
end if
end for
return sum
end function
Algorithm Explanation
- Create a function
evenAtEvenLocation
that takes an array of integers as input. - Initialize a variable
sum
to store the sum of even elements at even positions. - Iterate over each element in the array using a loop from index 0 to length(arr) - 1.
- Check if the current index
i
is even and if the element at that indexarr[i]
is also even. - If both conditions are satisfied, add the element
arr[i]
to thesum
. - After the loop ends, return the value of
sum
.
Code Solution
Here given code implementation process.
// C Program
// Sum of even numbers at even position
#include <stdio.h>
// Display array elements
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf(" %d", arr[i]);
}
printf("\n");
}
void evenAtEvenLocation(int arr[], int n)
{
int sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (int i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
printArr(arr, n);
printf(" Result : %d \n", sum);
}
int main()
{
// Sorted Arrays
int a[] = {
3 , 1 , 2 , 1 , 6
};
int b[] = {
6 , 1 , 1 , -3 , -2 , 0 , 1 , 2
};
// Get the size
int l1 = sizeof(a) / sizeof(a[0]);
int l2 = sizeof(b) / sizeof(b[0]);
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
evenAtEvenLocation(b, l2);
return 0;
}
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
/*
Java Program
Sum of even numbers at even position
*/
public class EvenSum
{
// Display array elements
public void printArr(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
public void evenAtEvenLocation(int[] arr, int n)
{
int sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (int i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
printArr(arr, n);
System.out.print(" Result : " + sum + " \n");
}
public static void main(String[] args)
{
EvenSum task = new EvenSum();
// Sorted Arrays
int[] a = {
3 , 1 , 2 , 1 , 6
};
int[] b = {
6 , 1 , 1 , -3 , -2 , 0 , 1 , 2
};
// Get the size
int l1 = a.length;
int l2 = b.length;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
task.evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
task.evenAtEvenLocation(b, l2);
}
}
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Sum of even numbers at even position
*/
class EvenSum
{
public:
// Display array elements
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
void evenAtEvenLocation(int arr[], int n)
{
int sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (int i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
this->printArr(arr, n);
cout << " Result : " << sum << " \n";
}
};
int main()
{
EvenSum *task = new EvenSum();
// Sorted Arrays
int a[] = {
3 , 1 , 2 , 1 , 6
};
int b[] = {
6 , 1 , 1 , -3 , -2 , 0 , 1 , 2
};
// Get the size
int l1 = sizeof(a) / sizeof(a[0]);
int l2 = sizeof(b) / sizeof(b[0]);
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
task->evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
task->evenAtEvenLocation(b, l2);
return 0;
}
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
// Include namespace system
using System;
/*
Csharp Program
Sum of even numbers at even position
*/
public class EvenSum
{
// Display array elements
public void printArr(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
public void evenAtEvenLocation(int[] arr, int n)
{
int sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (int i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
this.printArr(arr, n);
Console.Write(" Result : " + sum + " \n");
}
public static void Main(String[] args)
{
EvenSum task = new EvenSum();
// Sorted Arrays
int[] a = {
3 , 1 , 2 , 1 , 6
};
int[] b = {
6 , 1 , 1 , -3 , -2 , 0 , 1 , 2
};
// Get the size
int l1 = a.Length;
int l2 = b.Length;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
task.evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
task.evenAtEvenLocation(b, l2);
}
}
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
package main
import "fmt"
/*
Go Program
Sum of even numbers at even position
*/
// Display array elements
func printArr(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
fmt.Print("\n")
}
func evenAtEvenLocation(arr[] int, n int) {
var sum int = 0
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for i := 0 ; i < n ; i++ {
if (i % 2) == 0 && (arr[i] % 2) == 0 {
sum += arr[i]
}
}
printArr(arr, n)
fmt.Print(" Result : ", sum, " \n")
}
func main() {
// Sorted Arrays
var a = [] int { 3 , 1 , 2 , 1 , 6 }
var b = [] int { 6 , 1 , 1 , -3 , -2 , 0 , 1 , 2 }
// Get the size
var l1 int = len(a)
var l2 int = len(b)
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
evenAtEvenLocation(a, l1)
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
evenAtEvenLocation(b, l2)
}
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
<?php
/*
Php Program
Sum of even numbers at even position
*/
class EvenSum
{
// Display array elements
public function printArr($arr, $n)
{
for ($i = 0; $i < $n; ++$i)
{
echo(" ".$arr[$i]);
}
echo("\n");
}
public function evenAtEvenLocation($arr, $n)
{
$sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for ($i = 0; $i < $n; ++$i)
{
if (($i % 2) == 0 && ($arr[$i] % 2) == 0)
{
$sum += $arr[$i];
}
}
$this->printArr($arr, $n);
echo(" Result : ".$sum.
" \n");
}
}
function main()
{
$task = new EvenSum();
// Sorted Arrays
$a = array(3, 1, 2, 1, 6);
$b = array(6, 1, 1, -3, -2, 0, 1, 2);
// Get the size
$l1 = count($a);
$l2 = count($b);
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
$task->evenAtEvenLocation($a, $l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
$task->evenAtEvenLocation($b, $l2);
}
main();
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
/*
Node JS Program
Sum of even numbers at even position
*/
class EvenSum
{
// Display array elements
printArr(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
evenAtEvenLocation(arr, n)
{
var sum = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
for (var i = 0; i < n; ++i)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
}
this.printArr(arr, n);
process.stdout.write(" Result : " + sum + " \n");
}
}
function main()
{
var task = new EvenSum();
// Sorted Arrays
var a = [3, 1, 2, 1, 6];
var b = [6, 1, 1, -3, -2, 0, 1, 2];
// Get the size
var l1 = a.length;
var l2 = b.length;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
task.evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
task.evenAtEvenLocation(b, l2);
}
main();
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
# Python 3 Program
# Sum of even numbers at even position
class EvenSum :
# Display list elements
def printArr(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1
print(end = "\n")
def evenAtEvenLocation(self, arr, n) :
sum = 0
i = 0
# This loop are visiting the list element from 0 to n-1.
# And sum of elements which are exist in Even position.
while (i < n) :
if ((i % 2) == 0 and(arr[i] % 2) == 0) :
sum += arr[i]
i += 1
self.printArr(arr, n)
print(" Result : ", sum ," ")
def main() :
task = EvenSum()
# Sorted Arrays
a = [3, 1, 2, 1, 6]
b = [6, 1, 1, -3, -2, 0, 1, 2]
# Get the size
l1 = len(a)
l2 = len(b)
# Test A
# arr = [0, 1, 0,1]
# --------------------------
# index Value
# 0 [3] Index even but value not even
# ➀ [1]
# ➁ [2] Index even and value is even
# ➂ [1]
# ➃ [6] Index even and value is even [6]
# ---------------
# Total : [2] + [6] = 8
task.evenAtEvenLocation(a, l1)
# Test B
# arr = [2, 1, 1, -2, 1, 0, 1, 2]
# --------------------------
# Index Value
# 0 [6] Index even and value is even
# ➀ [1]
# ➁ [1] Index even but value not even
# ➂ [-3]
# ➃ [-2] Index even and value is even
# ➄ [0]
# ➅ [1] Index even but value not even
# ➆ [2]
# ---------------
# Total : [6] + [-2]
task.evenAtEvenLocation(b, l2)
if __name__ == "__main__": main()
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
# Ruby Program
# Sum of even numbers at even position
class EvenSum
# Display array elements
def printArr(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end
print("\n")
end
def evenAtEvenLocation(arr, n)
sum = 0
i = 0
# This loop are visiting the array element from 0 to n-1.
# And sum of elements which are exist in Even position.
while (i < n)
if ((i % 2) == 0 && (arr[i] % 2) == 0)
sum += arr[i]
end
i += 1
end
self.printArr(arr, n)
print(" Result : ", sum ," \n")
end
end
def main()
task = EvenSum.new()
# Sorted Arrays
a = [3, 1, 2, 1, 6]
b = [6, 1, 1, -3, -2, 0, 1, 2]
# Get the size
l1 = a.length
l2 = b.length
# Test A
# arr = [0, 1, 0,1]
# --------------------------
# index Value
# 0 [3] Index even but value not even
# ➀ [1]
# ➁ [2] Index even and value is even
# ➂ [1]
# ➃ [6] Index even and value is even [6]
# ---------------
# Total : [2] + [6] = 8
task.evenAtEvenLocation(a, l1)
# Test B
# arr = [2, 1, 1, -2, 1, 0, 1, 2]
# --------------------------
# Index Value
# 0 [6] Index even and value is even
# ➀ [1]
# ➁ [1] Index even but value not even
# ➂ [-3]
# ➃ [-2] Index even and value is even
# ➄ [0]
# ➅ [1] Index even but value not even
# ➆ [2]
# ---------------
# Total : [6] + [-2]
task.evenAtEvenLocation(b, l2)
end
main()
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
/*
Scala Program
Sum of even numbers at even position
*/
class EvenSum()
{
// Display array elements
def printArr(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
def evenAtEvenLocation(arr: Array[Int], n: Int): Unit = {
var sum: Int = 0;
var i: Int = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
while (i < n)
{
if ((i % 2) == 0 && (arr(i) % 2) == 0)
{
sum += arr(i);
}
i += 1;
}
printArr(arr, n);
print(" Result : " + sum + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: EvenSum = new EvenSum();
// Sorted Arrays
var a: Array[Int] = Array(3, 1, 2, 1, 6);
var b: Array[Int] = Array(6, 1, 1, -3, -2, 0, 1, 2);
// Get the size
var l1: Int = a.length;
var l2: Int = b.length;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
task.evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
task.evenAtEvenLocation(b, l2);
}
}
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
import Foundation;
/*
Swift 4 Program
Sum of even numbers at even position
*/
class EvenSum
{
// Display array elements
func printArr(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func evenAtEvenLocation(_ arr: [Int], _ n: Int)
{
var sum: Int = 0;
var i: Int = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
while (i < n)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
i += 1;
}
self.printArr(arr, n);
print(" Result : ", sum ," ");
}
}
func main()
{
let task: EvenSum = EvenSum();
// Sorted Arrays
let a: [Int] = [3, 1, 2, 1, 6];
let b: [Int] = [6, 1, 1, -3, -2, 0, 1, 2];
// Get the size
let l1: Int = a.count;
let l2: Int = b.count;
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
task.evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
task.evenAtEvenLocation(b, l2);
}
main();
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
/*
Kotlin Program
Sum of even numbers at even position
*/
class EvenSum
{
// Display array elements
fun printArr(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
fun evenAtEvenLocation(arr: Array < Int > , n: Int): Unit
{
var sum: Int = 0;
var i: Int = 0;
// This loop are visiting the array element from 0 to n-1.
// And sum of elements which are exist in Even position.
while (i < n)
{
if ((i % 2) == 0 && (arr[i] % 2) == 0)
{
sum += arr[i];
}
i += 1;
}
this.printArr(arr, n);
print(" Result : " + sum + " \n");
}
}
fun main(args: Array < String > ): Unit
{
val task: EvenSum = EvenSum();
// Sorted Arrays
val a: Array < Int > = arrayOf(3, 1, 2, 1, 6);
val b: Array < Int > = arrayOf(6, 1, 1, -3, -2, 0, 1, 2);
// Get the size
val l1: Int = a.count();
val l2: Int = b.count();
// Test A
// arr = [0, 1, 0,1]
// --------------------------
// index Value
// 0 [3] Index even but value not even
// ➀ [1]
// ➁ [2] Index even and value is even
// ➂ [1]
// ➃ [6] Index even and value is even [6]
// ---------------
// Total : [2] + [6] = 8
task.evenAtEvenLocation(a, l1);
// Test B
// arr = [2, 1, 1, -2, 1, 0, 1, 2]
// --------------------------
// Index Value
// 0 [6] Index even and value is even
// ➀ [1]
// ➁ [1] Index even but value not even
// ➂ [-3]
// ➃ [-2] Index even and value is even
// ➄ [0]
// ➅ [1] Index even but value not even
// ➆ [2]
// ---------------
// Total : [6] + [-2]
task.evenAtEvenLocation(b, l2);
}
Output
3 1 2 1 6
Result : 8
6 1 1 -3 -2 0 1 2
Result : 4
Time Complexity
The time complexity of this algorithm is O(n), where n is the number of elements in the input array. This is because the algorithm iterates through each element in the array once.
Output Explanation
Using the provided example array A and the algorithm, we get the sum of even elements at even positions as 8, which matches the output given in the code.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment