Find the sum of the first half and second half elements of an array
Last Updated :
27 Mar, 2023
Given an array arr of size N. The task is to find the sum of the first half (N/2) elements and the second half elements (N – N/2) of an array.
Examples:
Input : arr[] = {20, 30, 60, 10, 25, 15, 40}
Output : 110, 90
Sum of first N/2 elements 20 + 30 + 60 is 110
Input : arr[] = {50, 35, 20, 15}
Output : 85, 35
Approach:
- Initialize SumFirst and SumSecond as 0.
- Traverse the given array.
- Now add elements in SumFirst if the current index is less than N/2 otherwise add in SumSecond.
Below is the implementation of the above approach:
C
#include <stdio.h>
void sum_of_elements( int arr[], int n)
{
int sum_of_first = 0, sum_of_second = 0;
for ( int i = 0; i < n; i++) {
if (i < n / 2) {
sum_of_first += arr[i];
}
else {
sum_of_second += arr[i];
}
}
printf ( "Sum of first half elements is %d\n" ,
sum_of_first);
printf ( "Sum of second half elements is %d\n" ,
sum_of_second);
}
int main()
{
int arr[] = { 20, 30, 60, 10, 25, 15, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
sum_of_elements(arr, n);
return 0;
}
|
C#
using System;
class GFG {
public static void sum_of_elements( int [] arr, int n)
{
int sumfirst = 0, sumsecond = 0;
for ( int i = 0; i < n; i++) {
if (i < n / 2) {
sumfirst += arr[i];
}
else {
sumsecond += arr[i];
}
}
Console.WriteLine( "Sum of first half elements is "
+ sumfirst);
Console.WriteLine( "Sum of second half elements is "
+ sumsecond);
}
static public void Main()
{
int [] arr = { 20, 30, 60, 10, 25, 15, 40 };
int n = arr.Length;
sum_of_elements(arr, n);
}
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void sum_of_elements( int arr[], int n) {
int sumfirst = 0, sumsecond = 0;
for ( int i = 0; i < n; i++) {
if (i < n / 2)
sumfirst += arr[i];
else
sumsecond += arr[i];
}
cout << "Sum of first half elements is " << sumfirst
<< endl;
cout << "Sum of second half elements is " << sumsecond
<< endl;
}
int main()
{
int arr[] = { 20, 30, 60, 10, 25, 15, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
sum_of_elements(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void sum_of_elements( int [] arr, int n)
{
int sumfirst = 0 , sumsecond = 0 ;
for ( int i = 0 ; i < n; i++) {
if (i < n / 2 ) {
sumfirst += arr[i];
}
else {
sumsecond += arr[i];
}
}
System.out.println( "Sum of first half elements is "
+ sumfirst);
System.out.println( "Sum of second half elements is "
+ sumsecond);
}
public static void main(String[] args)
{
int [] arr = { 20 , 30 , 60 , 10 , 25 , 15 , 40 };
int n = arr.length;
sum_of_elements(arr, n);
}
}
|
Python3
def sum_of_elements(arr, n):
sumfirst = 0
sumsecond = 0
for i in range (n):
if (i < n / / 2 ):
sumfirst + = arr[i]
else :
sumsecond + = arr[i]
print ( "Sum of first half elements is" ,
sumfirst, end = "\n" )
print ( "Sum of second half elements is" ,
sumsecond, end = "\n" )
arr = [ 20 , 30 , 60 , 10 , 25 , 15 , 40 ]
n = len (arr)
sum_of_elements(arr, n)
|
Javascript
<script>
function sum_of_elements(arr , n)
{
var sumfirst = 0, sumsecond = 0;
for (i = 0; i < n; i++) {
if (i < parseInt(n / 2))
{
sumfirst += arr[i];
}
else {
sumsecond += arr[i];
}
}
document.write(
"Sum of first half elements is " + sumfirst+ "<br/>"
);
document.write(
"Sum of second half elements is " + sumsecond+ "<br/>"
);
}
var arr = [ 20, 30, 60, 10, 25, 15, 40 ];
var n = arr.length;
sum_of_elements(arr, n);
</script>
|
Output:
Sum of first half elements is 110
Sum of second half elements is 90
Time complexity: O(N), as we are using a loop to traverse the array.
Auxiliary Space: O(1), as we are not using any extra space.
Approach 2: Traversing half of the array length.
The idea is to traverse half the length of the array and calculate the first sum and second sum simultaneously by
- Initializing firstSum=0 and LastSum=0 and also traversing the array starting from the 0th index to N/2.
- Adding values of arr[i] to the firstSum.
- Adding values of arr[i+n/2] to the secondSum.
- An edge case occurs when the length of the array is odd. In this case, the first sum has the sum of the first N/2 elements while secondSum has the sum of the remaining elements excluding the last element (since the loop runs for n/2 times the last element is excluded) Hence we take care of this exclusively.
Below is the code for the same.
C#
using System;
public class GFG {
public static void Main()
{
int [] arr = { 20, 30, 60, 10, 25, 15, 40 };
int n = arr.Length;
sum(arr, n);
}
public static void sum( int [] arr, int n)
{
int firstSum = 0, secondSum = 0;
for ( int i = 0; i < n / 2; i++) {
firstSum += arr[i];
secondSum += arr[i + n / 2];
}
if (n % 2 != 0)
secondSum += arr[n - 1];
Console.WriteLine( "Sum of first half elements is "
+ firstSum);
Console.WriteLine( "Sum of second half elements is "
+ secondSum);
}
}
|
C++
#include <bits/stdc++.h>
using namespace std;
void sum( int arr[], int n)
{
int firstSum = 0, secondSum = 0;
for ( int i = 0; i < n / 2; i++) {
firstSum += arr[i];
secondSum += arr[i + n / 2];
}
if (n % 2 != 0)
secondSum += arr[n - 1];
cout << "Sum of first half elements is " << firstSum
<< endl;
cout << "Sum of second half elements is " << secondSum
<< endl;
}
int main()
{
int arr[] = { 20, 30, 60, 10, 25, 15, 40 };
int n = sizeof (arr) / sizeof (arr[0]);
sum(arr, n);
return 0;
}
|
Java
import java.util.*;
public class Main {
static void sum( int arr[], int n)
{
int firstSum = 0 , secondSum = 0 ;
for ( int i = 0 ; i < n / 2 ; i++) {
firstSum += arr[i];
secondSum += arr[i + n / 2 ];
}
if (n % 2 != 0 )
secondSum += arr[n - 1 ];
System.out.println( "Sum of first half elements is "
+ firstSum);
System.out.println( "Sum of second half elements is "
+ secondSum);
}
public static void main(String[] args)
{
int arr[] = { 20 , 30 , 60 , 10 , 25 , 15 , 40 };
int n = arr.length;
sum(arr, n);
}
}
|
Python
def sum (arr, n):
first_sum, second_sum = 0 , 0
for i in range (n / / 2 ):
first_sum + = arr[i]
second_sum + = arr[i + n / / 2 ]
if n % 2 ! = 0 :
second_sum + = arr[n - 1 ]
print ( 'Sum of first half elements is' , first_sum)
print ( "Sum of second half elements is" , second_sum)
arr = [ 20 , 30 , 60 , 10 , 25 , 15 , 40 ]
n = len (arr)
sum (arr, n)
|
Javascript
function sum(arr, n) {
let firstSum = 0, secondSum = 0;
for (let i = 0; i < Math.floor(n / 2); i++) {
firstSum += arr[i];
secondSum += arr[i + Math.floor(n / 2)];
}
if (n % 2 !== 0)
secondSum += arr[n - 1];
console.log(`Sum of first half elements is ${firstSum}`);
console.log(`Sum of second half elements is ${secondSum}`);
}
const arr = [20, 30, 60, 10, 25, 15, 40];
const n = arr.length;
sum(arr, n);
|
Output:
Sum of first half elements is 110
Sum of second half elements is 90
Time complexity: O(N/2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...