-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayEquilibriumIndex.java
99 lines (83 loc) · 3.37 KB
/
ArrayEquilibriumIndex.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Array Equilibrium Index
// Send Feedback
// For a given array/list(ARR) of size 'N,' find and return the 'Equilibrium Index' of the array/list.
// Equilibrium Index of an array/list is an index 'i' such that the sum of elements at indices [0 to (i - 1)] is equal to the sum of elements at indices [(i + 1) to (N-1)]. One thing to note here is, the item at the index 'i' is not included in either part.
// If more than one equilibrium indices are present, then the index appearing first in left to right fashion should be returned. Negative one(-1) if no such index is present.
// Example:
// Let's consider an array/list Arr = [2, 3, 10, -10, 4, 2, 9] of size, N = 7.
// There exist three equilibrium indices, one at 2, another at 3, and another at 5.
// At index 2, the sum of all the elements to the left, [2 + 3] is 5, and the elements to its right, [-10 + 4 + 2 + 9] is also 5. Hence index 2 is an equilibrium index according to the condition we want to achieve. Mind it that we haven't included the item at index 2, which is 10, to either of the parts.
// Similarly, we can see at index 3 and 5, the elements to its left sum up to 15 and 9 respectively and to the right, sum up to 15 and 9 respectively either.
// Hence the answer would be 2.
// Input Format :
// The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
// The first line of each test case or query contains an integer 'N' representing the size of the first array/list.
// The second line contains 'N' single space separated integers representing the elements of the array/list
// Output Format :
// For each test case, print the 'Equilibrium Index'.
// Output for every test case will be printed in a separate line.
// Constraints :
// 1 <= t <= 10^2
// 0 <= N <= 10^6
// Time Limit: 1 sec
// Sample Input 1 :
// 1
// 5
// 1 4 9 3 2
// Sample Output 1 :
// 2
// Sample Input 2 :
// 2
// 3
// 1 4 6
// 3
// 1 -1 4
// Sample Output 2 :
// -1
// 2
import java.util.Scanner;
public class ArrayEquilibriumIndex {
public static void takeInput(int arr[], int n, Scanner s) {
System.out.println("Enter Values Of The Array : ");
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter Number Of Test Cases : ");
int t = s.nextInt();
for (int i = 1; i <= t; i++) {
System.out.print("Enter Length of " + i + "th Array : ");
int n = s.nextInt();
int arr[] = new int[n];
takeInput(arr, n, s);
int ans = arrayEquilibriumIndex(arr);
System.out.println("Output : " + ans);
}
s.close();
}
public static int arrayEquilibriumIndex(int[] arr) {
for (int i = 0; i < arr.length; i++) {
boolean isTrue = check(arr, i);
if (isTrue) {
return i;
}
}
return -1;
}
public static boolean check(int[] arr, int index) {
int leftSum = 0;
int rightSum = 0;
for (int i = 0; i < index; i++) {
leftSum += arr[i];
}
for (int i = index + 1; i < arr.length; i++) {
rightSum += arr[i];
}
if (leftSum == rightSum) {
return true;
}
return false;
}
}