Write a program to find the Equilibrium index of an array.
Equilibrium Index: The sum of the left side of the index and the right side of the index is equal then the index is an equilibrium index.
Example:
Input: arr[]={3,2,4,5,6,8,3,2,1}
Output: Equilibrium index is 4 //i.e 3+2+5+6 == 8+3+2+1
Approach 1: Simple Method
C
#include <stdio.h>int main(){int arr[] = {3, 2, 4, 5, 6, 8, 3, 2, 1};int n = sizeof(arr) / sizeof(arr[0]);int flag = 0;for (int i = 0; i < n; i++){int leftSum = 0;for (int j = 0; j < i; j++){leftSum = leftSum + arr[j];}int rightSum = 0;for (int j = i + 1; j < n; j++){rightSum = rightSum + arr[j];}if (leftSum == rightSum){printf("Equilibrium index is %d", i);flag = 1;break;}}if (flag == 0){printf("No Equilibrium index\n");}return 0;}
C++
#include <bits/stdc++.h>using namespace std;int main(){int arr[] = {3, 2, 4, 5, 6, 8, 3, 2, 1};int n = sizeof(arr) / sizeof(arr[0]);int flag = 0;for (int i = 0; i < n; i++){int leftSum = 0;for (int j = 0; j < i; j++){leftSum = leftSum + arr[j];}int rightSum = 0;for (int j = i + 1; j < n; j++){rightSum = rightSum + arr[j];}if (leftSum == rightSum){cout << "Equilibrium index is " << i << "\n";flag = 1;break;}}if (flag == 0){cout << "No Equilibrium index\n";}return 0;}
Java
public class EquilibriumArrayIndex {public static void main(String[] args) {int arr[] = { 3, 2, 4, 5, 6, 8, 3, 2, 1 };int n = arr.length;int flag = 0;for (int i = 0; i < n; i++) {int leftSum = 0;for (int j = 0; j < i; j++) {leftSum = leftSum + arr[j];}int rightSum = 0;for (int j = i + 1; j < n; j++) {rightSum = rightSum + arr[j];}if (leftSum == rightSum) {System.out.printf("Equilibrium index is %d", i);flag = 1;break;}}if (flag == 0) {System.out.printf("No Equilibrium index\n");}}}
Approach 2: Efficient Method
C
#include <stdio.h>int main(){int arr[] = {3, 2, 4, 5, 6, 8, 3, 2, 1};//find the length of arrayint n = sizeof(arr) / sizeof(arr[0]);int sumRem = 0;for (int i = 0; i < n; i++){sumRem = sumRem + arr[i];}int flag = 0;int leftSum = 0;for (int i = 0; i < n; i++){//update remaining sumsumRem = sumRem - arr[i];//if leftSum is same as remaining sumif (leftSum == sumRem){printf("Equilibrium index is %d", i);flag = 1;break;}//update leftSumleftSum = leftSum + arr[i];}if (flag == 0){printf("No Equilibrium index\n");}return 0;}
C++
#include <bits/stdc++.h>using namespace std;int main(){int arr[] = {3, 2, 4, 5, 6, 8, 3, 2, 1};//find the length of arrayint n = sizeof(arr) / sizeof(arr[0]);int sumRem = 0;for (int i = 0; i < n; i++){sumRem = sumRem + arr[i];}int flag = 0;int leftSum = 0;for (int i = 0; i < n; i++){//update remaining sumsumRem = sumRem - arr[i];//if leftSum is same as remaining sumif (leftSum == sumRem){cout << "Equilibrium index is " << i << "\n";flag = 1;break;}//update leftSumleftSum = leftSum + arr[i];}if (flag == 0){cout << "No Equilibrium index\n";}return 0;}
Java
public class EquilibriumArrayIndex {public static void main(String[] args) {int arr[] = { 3, 2, 4, 5, 6, 8, 3, 2, 1 };int n = arr.length;int sumRem = 0;for (int i = 0; i < n; i++) {sumRem = sumRem + arr[i];}int flag = 0;int leftSum = 0;for (int i = 0; i < n; i++) {// update remaining sumsumRem = sumRem - arr[i];// if leftSum is same as remaining sumif (leftSum == sumRem) {System.out.printf("Equilibrium index is %d", i);flag = 1;break;}// update leftSumleftSum = leftSum + arr[i];}if (flag == 0) {System.out.printf("No Equilibrium index\n");}}}
No comments:
Post a Comment