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 elemets 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 array ` `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]; ` ` ` `} ` ` ` ` ` `cout << ` `"Sum of first half elements is "` `<< ` ` ` `sumfirst << endl; ` ` ` ` ` `cout << ` `"Sum of second half elements is "` `<< ` ` ` `sumsecond << endl; ` `} ` ` ` `// Driver Code ` `int` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 code ` `public` `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 ` |

*chevron_right*

*filter_none*

## 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 array ` `def` `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 Code ` `arr ` `=` `[` `20` `, ` `30` `, ` `60` `, ` `10` `, ` `25` `, ` `15` `, ` `40` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` `# Function call ` `sum_of_elements(arr, n); ` ` ` `# This code is contributed ` `# by Akanksha Rai ` |

*chevron_right*

*filter_none*

## 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 code ` `static` `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 ` |

*chevron_right*

*filter_none*

**Output:**

Sum of first half elements is 110 Sum of second half elements is 90

**Time complexity:** O(N)

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.

## Recommended Posts:

- Find the XOR of first half and second half elements of an array
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Find prime numbers in the first half and second half of an array
- Sort first half in ascending and second half in descending order | 1
- Sort first half in ascending and second half in descending order | Set 2
- Count number of rotated strings which have more number of vowels in the first half than second half
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Reorder an array such that sum of left half is not equal to sum of right half
- Find elements which are present in first array and not in second
- Find the first, second and third minimum elements in an array
- Find the maximum elements in the first and the second halves of the Array
- Find numbers which are multiples of first array and factors of second array
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count distinct elements after adding each element of First Array with Second Array
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Find just strictly greater element from first array for each element in second array
- First subarray having sum at least half the maximum sum of any subarray of size K
- Find elements larger than half of the elements in an array
- Maximize sum of remaining elements after every removal of the array half with greater sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.