×

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array

You have been given an array of numbers. You have to find out the element which divides the array in such a manner that the sum of the two parts will be equal.

Let’s take an example to understand it well:

Input- [ 2 6 3 4 1 4 5 3 3 ]

Output- 1

Explanation- In this example the number 1 divides the whole array in two parts which are (2, 6, 3, 4) and (4, 5, 3, 3). The sum of both parts is equal to 15. So the answer will be 1.

Algorithm

Step 1: Start

Step 2: The size of array is taken from the user

Step 3: Array is created

Step 4: A function is called.

Step 5: We use one main loop and two sub loops.

Step 6: For each element we will calculate the sum of two parts.

Step 7: If sums are equal then the element will be returned.

Step 8: The returned value will be printed.

Step 9: Stop.

Explanation of Algorithm

The approach is very simple. We traverse all the elements of the array. For each element we calculate the sum of left part and right part of the array. If we find an element for which left and right sum are same then the element will be printed.

Code-

//C++ program to find the element of an array which makes sum of the left array is equal to sum of the right array

Program in C++

#include <bits/stdc++.h>
#include <iostream>
using namespace std;


int findElement(int arr[], int n)
{
	for (int i = 1; i < n; i++) {
		int leftSum = 0;
		for (int j = i - 1; j >= 0; j--) {
			leftSum += arr[j];
		}


		int rightSum = 0;
		for (int k = i + 1; k < n; k++) {
			rightSum += arr[k];
		}


		if (leftSum == rightSum) {
			return arr[i];
		}
	}


	return -1;
}


int main()
{
	// Case 1
	int arr1[] = { 1, 4, 3, 3, 2, 5 ,6};
	int n1 = sizeof(arr1) / sizeof(arr1[0]);
	cout << findElement(arr1, n1) << "\n";


	// Case 2
	int arr2[] = { 8, 1, 4, 3, 3, 2, 5, 6, 4, 4};
	int n2 = sizeof(arr2) / sizeof(arr2[0]);
	cout << findElement(arr2, n2);
	return 0;
}

# Python to find the element of an array which makes sum of the left array is equal to sum of the right array

Program in Python

def findElement(arr, n):
	for i in range(1, n):
		leftSum = sum(arr[0:i])
		rightSum = sum(arr[i+1:])
		if(leftSum == rightSum):
			return arr[i]
	return -1


if __name__ == "__main__":


	# Case 1
	arr = [1, 4, 3, 3, 2, 5 ,6]
	n = len(arr)
	print(findElement(arr, n))


	# Case 2
	arr = [8, 1, 4, 3, 3, 2, 5, 6, 4, 4]
	n = len(arr)
	print(findElement(arr, n))

//java program to find the element of an array which makes sum of the left array is equal to sum of the right array

Program in Java

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
class bn {
	static int findElement(int arr[], int n)
	{
		List<Integer> list
			= Arrays.stream(arr).boxed().collect(
				Collectors.toList());
		for (int i = 1; i <= n; i++) {
			int leftSum = list.subList(0, i)
							.stream()
							.mapToInt(x -> x)
							.sum();
			int rightSum = list.subList(i + 1, n)
							.stream()
							.mapToInt(x -> x)
							.sum();


			if (leftSum == rightSum)
				return list.get(i);
		}
		return -1;
	}
	public static void main(String[] args)
	{
		// Case 1
		int arr1[] = { 1, 4, 3, 3, 2, 5 ,6};
		int n1 = arr1.length;
		System.out.println(findElement(arr1, n1));


		// Case 2
		int arr2[] = { 8, 1, 4, 3, 3, 2, 5, 6, 4, 4};
		int n2 = arr2.length;
		System.out.println(findElement(arr2, n2));
	}
}


<script>

// JavaScript program to find the element of an array which makes sum of the left array is equal to sum of the right array

Program in JavaScript

function findElement(arr , n)
	{
		for(i = 1; i < n; i++){
		let leftSum = 0;
		for(j = i-1; j >= 0; j--){
			leftSum += arr[j];
		}
		
		let rightSum = 0;
		for(k = i+1; k < n; k++){
			rightSum += arr[k];
		}


		if(leftSum === rightSum){
			return arr[i];
		}
		
		}
		
		return -1;
	}
	//Case 1
	var arr = [ 1, 4, 3, 3, 2, 5 ,6];
	var n = arr.length;
	document.write(findElement(arr, n));
	
	document.write("<br><br>")
	
	//Case 2
	var arr = [ 8, 1, 4, 3, 3, 2, 5, 6, 4, 4];
	var n = arr.length;
	document.write(findElement(arr, n));


</script>

//C# program to find the element of an array which makes sum of the left array is equal to sum of the right array

Program in C#

using System;
public class bn {
	static int findElement(int[] arr, int n)
	{
		for (int i = 1; i < n; i++) {
			int leftSum = 0;
			for (int j = i - 1; j >= 0; j--) {
				leftSum += arr[j];
			}


			int rightSum = 0;
			for (int k = i + 1; k < n; k++) {
				rightSum += arr[k];
			}


			if (leftSum == rightSum) {
				return arr[i];
			}
		}
		return -1;
	}
	static public void Main()
	{
		// Case 1
		int[] arr1 = { 1, 4, 3, 3, 2, 5 ,6};
		int n1 = arr1.Length;
		Console.WriteLine(findElement(arr1, n1));


		// Case 2
		int[] arr2 = { 8, 1, 4, 3, 3, 2, 5, 6, 4, 4};
		int n2 = arr2.Length;
		Console.WriteLine(findElement(arr2, n2));
	}
}

Output: 

Case 1: [1, 4, 3, 3, 2, 5 ,6]
Ans: 2
Case 2: [8, 1, 4, 3, 3, 2, 5, 6, 4, 4]
Ans: 2

Related Topics

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in...

4 minutes read.

Linear Queue Data Structure in C

Data Structure There are many ways to store data in programming, that Queue has features that make it all the more special. We all know that data structure is a way...

9 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.

Diameter of a Binary Tree

Implementation We will now witness the implementation of the diameter of a binary tree. // Creating a recursive and challenging C program that will help us determine the diameter of a binary...

4 minutes read.

Find out the area between two concentric circles

You have given two values of the radius of two circles. You have to find out the area between these two circles. Let's take an example - For the above diagram,...

3 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Burning binary tree

Burn the Binary tree starting from the target node You have given a binary tree and a target node value. Now you have to burn the tree from target node. You...

4 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

How to Start Learning DSA

All programmer experiences a point along the way where they wish they could approach a problem in a more effective manner. They finally learn about the terminology DSA while trying...

10 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 minutes read.

Flattening a Linked List

In this article, we are going to study about the logic behind the flattening of linked list and we also going to build a code in the C++ to flatten...

3 minutes read.

Operations on 2D-Arrays

Two Dimensional Array Operations Adding Elements to Two-D Arrays We must put data in both rows and columns when inserting items in 2-D Arrays. As a result, we employ the idea of...

10 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

Huffman tree in Data Structures

The Huffman trees in the field of data structures are pretty impressive in their work. They are generally treated as the binary tree, which is linked with the least external...

6 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Create a binary search tree

Implementation In this section of the article, we will see the usage and mechanism of how we will create a given binary tree. Let's observe these in more depth and then...

7 minutes read.

Stack vs Heap Memory Allocation Data Structure

Difference Between Stack and Heap Memory Allocation Stack Memory Stack memory allocation is a way to use the system memory as a temporary storage of the data which is act like last-in-first-out...

3 minutes read.