×

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

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

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.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

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

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Adding one to the number represented an array of digits

You have given one array, which consists of values which represent the different digits of a number. You have to add 1 to this number and store the result in...

3 minutes read.

B Tree in Data Structure

Data management is called database management. A data model is a system that stores, manages, and optimizes computer resources. Data processing is not just about data storage. Almost every app...

9 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

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

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

What Is Graph Data Structure

A graph is generally a set of vertices and edges or border that is mainly used to join these vertices. A graph is basically pictured as a cyclic tree in...

7 minutes read.

Queue Implementation using stacks Data Structure

Queue Implementation using stacks In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances...

4 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

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

Stack Using Array

Stack – A Stack is a linear abstract data type used to store elements. It is also called last in first out or first in last out data structure because...

6 minutes read.