Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced

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



ADVERTISEMENT
ADVERTISEMENT