Given an array of size n. The problem is to find the sum of numbers that are even and are at even index.
Examples:
Input : arr[] = {5, 6, 12, 1, 18, 8}
Output : 30
Explanation: Here, n = 6
Now here are index and numbers as: index->arr[index]
0->5, 1->6, 2->12, 3->1, 4->18, 5->8
so, number which are even and are at even indices
are: 2->12, 4->18
sum = 12+18 = 30
Input : arr[] = {3, 20, 17, 9, 2, 10,
18, 13, 6, 18}
Output : 26
Explanation: Here, n = 10
Now here are index and numbers as: index->arr[index]
0->3, 1->20, 2->17, 3->9, 4->2, 5->10,
6->18, 7->13, 8->6, 9->18
So, number which are even and are at even indices are:
4->2, 6->18, 8->6
sum = 2+18+6 = 26
Brute Force Approach:
The idea behind this approach is to store the even numbers at even indices in a hash table and then calculate the sum of these numbers. We can use an unordered map to store the even numbers along with their index.
Here are the steps for this approach:
- Create an empty unordered map to store the even numbers at even indices and their corresponding indices.
- Iterate over the array and check if the index is even and the number at that index is even. If both conditions are true, then insert the number into the unordered map along with its index.
- Iterate over the unordered map and calculate the sum of the even numbers.
- Return the sum.
C++
#include <bits/stdc++.h>
using namespace std;
int sum_even_and_even_index( int arr[], int n) {
unordered_map< int , int > mp;
int sum = 0;
for ( int i = 0; i < n; i += 2) {
if (arr[i] % 2 == 0) {
mp[i] = arr[i];
}
}
for ( auto x : mp) {
sum += x.second;
}
return sum;
}
int main() {
int arr[] = {5, 6, 12, 1, 18, 8};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Sum of even numbers at even indices is "
<< sum_even_and_even_index(arr, n);
return 0;
}
|
Java
import java.util.HashMap;
public class Main {
public static int sum_even_and_even_index( int arr[], int n) {
HashMap<Integer, Integer> mp = new HashMap<>();
int sum = 0 ;
for ( int i = 0 ; i < n; i += 2 ) {
if (arr[i] % 2 == 0 ) {
mp.put(i, arr[i]);
}
}
for ( int x : mp.values()) {
sum += x;
}
return sum;
}
public static void main(String[] args) {
int arr[] = { 5 , 6 , 12 , 1 , 18 , 8 };
int n = arr.length;
System.out.println( "Sum of even numbers at even indices is " + sum_even_and_even_index(arr, n));
}
}
|
Python3
def sum_even_and_even_index(arr):
mp = {}
sum = 0
for i in range ( 0 , len (arr), 2 ):
if arr[i] % 2 = = 0 :
mp[i] = arr[i]
for key, value in mp.items():
sum + = value
return sum
arr = [ 5 , 6 , 12 , 1 , 18 , 8 ]
print ( "Sum of even numbers at even indices is" , sum_even_and_even_index(arr))
|
C#
using System;
using System.Collections.Generic;
public class Program {
public static int sum_even_and_even_index( int [] arr,
int n)
{
Dictionary< int , int > mp = new Dictionary<
int , int >();
int sum = 0;
for ( int i = 0; i < n; i += 2) {
if (arr[i] % 2 == 0) {
mp.Add(
i,
arr[i]);
}
}
foreach ( int x in mp.Values)
{
sum += x;
}
return sum;
}
public static void Main( string [] args)
{
int [] arr = { 5, 6, 12, 1, 18, 8 };
int n = arr.Length;
Console.WriteLine(
"Sum of even numbers at even indices is "
+ sum_even_and_even_index(arr, n));
}
}
|
Javascript
function sum_even_and_even_index(arr) {
let mp = new Map();
let sum = 0;
for (let i = 0; i < arr.length; i += 2) {
if (arr[i] % 2 === 0) {
mp.set(i, arr[i]);
}
}
for (let [key, value] of mp) {
sum += value;
}
return sum;
}
let arr = [5, 6, 12, 1, 18, 8];
console.log( "Sum of even numbers at even indices is " + sum_even_and_even_index(arr));
|
OutputSum of even numbers at even indices is 30
Time Complexity: O(N)
Auxiliary Space: O(N)
Another Approach:
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sum_even_and_even_index(
int arr[], int n) {
int i = 0, sum = 0;
for (i = 0; i < n; i+=2) {
if (arr[i] % 2 == 0) {
sum += arr[i];
}
}
return sum;
}
int main() {
int arr[] = {5, 6, 12, 1, 18, 8};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Sum of even numbers at even indices is "
<< sum_even_and_even_index(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sum_even_and_even_index(
int arr[], int n)
{
int i = 0 , sum = 0 ;
for (i = 0 ; i < n; i+= 2 ) {
if (arr[i] % 2 == 0 ) {
sum += arr[i];
}
}
return sum;
}
public static void main (String[] args) {
int arr[] = { 5 , 6 , 12 , 1 , 18 , 8 };
int n = arr.length;
System.out.println( "Sum of even numbers"
+ " at even indices is " +
+ sum_even_and_even_index(arr, n));
}
}
|
Python3
def sum_even_and_even_index(arr,n):
i = 0
sum = 0
for i in range ( 0 ,n, 2 ):
if (arr[i] % 2 = = 0 ) :
sum + = arr[i]
return sum
arr = [ 5 , 6 , 12 , 1 , 18 , 8 ]
n = len (arr)
print ( "Sum of even numbers at" ,
"even indices is" ,
sum_even_and_even_index(arr, n))
|
C#
using System;
class GFG {
static int sum_even_and_even_index(
int []arr, int n)
{
int i = 0, sum = 0;
for (i = 0; i < n; i+=2) {
if (arr[i] % 2 == 0) {
sum += arr[i];
}
}
return sum;
}
public static void Main () {
int []arr = {5, 6, 12, 1, 18, 8};
int n = arr.Length;
Console.WriteLine( "Sum of even numbers"
+ " at even indices is " +
+ sum_even_and_even_index(arr, n));
}
}
|
PHP
<?php
function sum_even_and_even_index( $arr , $n )
{
$i = 0; $sum = 0;
for ( $i = 0; $i < $n ; $i = $i +2) {
if ( $arr [ $i ] % 2 == 0) {
$sum += $arr [ $i ];
}
}
return $sum ;
}
{
$arr = array (5, 6, 12, 1, 18, 8);
$n = sizeof( $arr ) / sizeof( $arr [0]);
echo "Sum of even numbers at " .
"even indices is " ,
sum_even_and_even_index( $arr , $n );
return 0;
}
?>
|
Javascript
<script>
function sum_even_and_even_index(
arr, n) {
let i = 0, sum = 0;
for (i = 0; i < n; i += 2)
{
if (arr[i] % 2 == 0)
{
sum += arr[i];
}
}
return sum;
}
let arr = [5, 6, 12, 1, 18, 8];
let n = arr.length;
document.write( "Sum of even numbers at even indices is "
+ sum_even_and_even_index(arr, n));
</script>
|
OutputSum of even numbers at even indices is 30
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach: Using List comprehension in python and Bitwise & operator
This method uses a list comprehension to create a new list of the even numbers at even indices in the array, and then we use the sum() function to calculate the sum of the numbers.
C++
#include <iostream>
using namespace std;
int main() {
int arr[] = {5, 6, 12, 1, 18, 8};
int n = sizeof (arr) / sizeof (arr[0]);
int even_sum = 0;
for ( int i = 0; i < n; i += 2) {
if (arr[i] % 2 == 0)
even_sum += arr[i];
}
cout << "Sum of even numbers at"
<< " even indices is " << even_sum;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int arr[] = { 5 , 6 , 12 , 1 , 18 , 8 };
int n = arr.length;
int even_sum = 0 ;
for ( int i = 0 ; i < n; i += 2 ) {
if (arr[i] % 2 == 0 )
even_sum += arr[i];
}
System.out.println( "Sum of even numbers at"
+ " even indices is " + even_sum);
}
}
|
Python3
arr = [ 5 , 6 , 12 , 1 , 18 , 8 ]
n = len (arr)
even_sum = sum ([arr[i] for i in range ( 0 ,n, 2 ) if arr[i] & 1 ! = 1 ])
print ( "Sum of even numbers at" ,
"even indices is" ,
even_sum)
|
C#
using System;
public class Program {
public static void Main()
{
int [] arr = { 5, 6, 12, 1, 18, 8 };
int n = arr.Length;
int even_sum = 0;
for ( int i = 0; i < n; i += 2) {
if (arr[i] % 2 == 0)
even_sum += arr[i];
}
Console.WriteLine( "Sum of even numbers at "
+ "even indices is " + even_sum);
}
}
|
Javascript
let arr = [5,6,12,1,18,8];
let n = arr.length;
let even_sum = 0;
for (let i = 0; i<n; i+=2){
if (arr[i] % 2 == 0)
even_sum += arr[i];
}
console.log( "Sum of even numbers at even indices is " + even_sum);
|
OutputSum of even numbers at even indices is 30
Time Complexity: O(n)
Auxiliary Space: O(1)