×

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

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

What is a Threaded Binary Tree?

When we consider those binary trees that are interlinked with each other, we do come across the fact that the fields present in there do consist of NULL values that...

3 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.

Difference between Stack and Queue

In this article, we will learn about the major differences between Stack and Queue data structures. What is a stack? Stack – A stack is an abstract data structure defined as the...

3 minutes read.

What is a Sparse Matrix in Data Structure?

Definition A matrix in which a few non-zero elements are present is called a Sparse matrix. In a Sparse matrix, almost all the matrices are filled with zero (0). A matrix...

5 minutes read.

Merge Conflicts and ways to handle them

Merge Conflicts Whenever dealing with the Git merge operations, conflicts will be the frequently occurred. When more than two developers work on the same file on different systems using Git, they...

4 minutes read.

Minimum Spanning Tree

Before getting to know about the minimum spanning tree, we should first discuss about what is a spanning tree. A spanning tree is basically a sub or minimized graph that...

7 minutes read.

Binary Tree Inorder Traversal

The binary tree is a type of tree in which each and every node has atleast two children except the leaf nodes. We have various operations in the binary tree,...

4 minutes read.

Insertion Sort vs Selection Sort

In this article, we will discuss insertion sort, Selection sort and the basic differences between these two sorting techniques in detail: What is Insertion Sort? Insertion Sort – The insertion sort is...

5 minutes read.

Implementation of stack

Implementation of stack: The stack can be implemented in two ways: using array and using a linked list. The pop and push operations in the array are simpler than the...

3 minutes read.

Asymptotic Notation

Asymptotic notation is expressions that are used to represent the complexity of algorithms. The complexity of the algorithm is analyzed from two perspectives:  Time complexitySpace complexity Time complexity The time complexity of an algorithm is the...

3 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Spanning Tree

Spanning Tree: The spanning tree is a subset of the graph. It is a non-cyclic graph. If any node in the spanning tree is truncated, the entire graph fails. There are...

10 minutes read.

Circular Linked List

Circular Linked List A circular linked list where all nodes are connected to their next node and last node is connected to the starting node or we can say all nodes...

5 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Stack vs Queue: Data Structure

 Difference Between Stack and Queue What is Stack? The LIFO principle applies on insertion and deletion operations of the stack which means last inserted element to the stack will remove first....

3 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Binary tree insertion

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. Here the name of the tree itself portrays the mechanism...

4 minutes read.

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.