Given an array arr of size N. The task is to find the sum of the first half (N/2) elements and 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 and 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++
// C++ program to find the sum of the first half// elements and second half elements of an array#include <bits/stdc++.h>using namespace std;// Function to find the sum of the first half// elements and second half elements of an arrayvoid sum_of_elements(int arr[], int n){ int sumfirst = 0, sumsecond = 0; for (int i = 0; i < n; i++) { // Add elements in first half sum if (i < n / 2) sumfirst += arr[i]; // Add elements in the second half sum else sumsecond += arr[i]; } cout << "Sum of first half elements is " << sumfirst << endl; cout << "Sum of second half elements is " << sumsecond << endl;}// Driver Codeint main(){ int arr[] = { 20, 30, 60, 10, 25, 15, 40 }; int n = sizeof(arr) / sizeof(arr[0]); // Function call sum_of_elements(arr, n); return 0;} |
Java
// Java program to count pairs// whose sum divisible by 'K'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++) { // Add elements in first half sum if (i < n / 2) { sumfirst += arr[i]; } // Add elements in the second half sum else { sumsecond += arr[i]; } } System.out.println("Sum of first half elements is " + sumfirst); System.out.println("Sum of second half elements is " + sumsecond);}// Driver codepublic static void main(String[] args){ int []arr = { 20, 30, 60, 10, 25, 15, 40 }; int n = arr.length; // Function call sum_of_elements(arr, n);}}// This code is contributed by Princi Singh |
Python3
# Python3 program to find the sum of# the first half elements and# second half elements of an array# Function to find the sum of # the first half elements and# second half elements of an arraydef sum_of_elements(arr, n): sumfirst = 0; sumsecond = 0; for i in range(n): # Add elements in first half sum if (i < n // 2): sumfirst += arr[i]; # Add elements in the second half sum else: sumsecond += arr[i]; print("Sum of first half elements is", sumfirst, end = "\n"); print("Sum of second half elements is", sumsecond, end = "\n");# Driver Codearr = [20, 30, 60, 10, 25, 15, 40];n = len(arr);# Function callsum_of_elements(arr, n);# This code is contributed# by Akanksha Rai |
C#
// C# program to count pairs// whose sum divisible by 'K'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++) { // Add elements in first half sum if (i < n / 2) { sumfirst += arr[i]; } // Add elements in the second half sum else { sumsecond += arr[i]; } } Console.WriteLine("Sum of first half elements is " + sumfirst); Console.WriteLine("Sum of second half elements is " + sumsecond);}// Driver codestatic public void Main (){ int []arr = { 20, 30, 60, 10, 25, 15, 40 }; int n = arr.Length; // Function call sum_of_elements(arr, n);}}// This code is contributed by nidhiva |
Javascript
<script>// Javascript program to count pairs// whose sum divisible by 'K' function sum_of_elements(arr , n) { var sumfirst = 0, sumsecond = 0; for (i = 0; i < n; i++) { // Add elements in first half sum if (i < parseInt(n / 2)) { sumfirst += arr[i]; } // Add elements in the second half sum else { sumsecond += arr[i]; } } document.write( "Sum of first half elements is " + sumfirst+"<br/>" ); document.write( "Sum of second half elements is " + sumsecond+"<br/>" ); } // Driver code var arr = [ 20, 30, 60, 10, 25, 15, 40 ]; var n = arr.length; // Function call sum_of_elements(arr, n);// This code contributed by umadevi9616</script> |
Sum of first half elements is 110 Sum of second half elements is 90
Time complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes


