Skip to content

Commit 9c85b6f

Browse files
authored
Merge branch 'main' into rotateNreverseArray
2 parents 57ccda6 + 02f24fa commit 9c85b6f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,9 @@ console.log(rotated_array);
9292

9393
// output
9494
//rotated_array=[4,5,1,2,3];
95+
96+
//equilibrium_point program file execution
97+
// Array = [1,3,5,2,2]
98+
//n=5
99+
//output = 3
95100
```

index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,44 @@ function array_rotate(arr, x) {
416416
return arr1;
417417
}
418418

419+
420+
/**
421+
* Get the equilibrium point of an array which means the array element position where the
422+
* sum of elements on left side and sum of element on right side of that element in array are same.
423+
*
424+
*
425+
* @param array a
426+
* @param number n
427+
* @returns number
428+
*/
429+
430+
function equilibrium_Point(a, n){
431+
is_array(a);
432+
let i=0;
433+
let j=n-1;
434+
let s1=0;
435+
let s2=0;
436+
while(i<j){
437+
if(s1<=s2){
438+
s1=s1+a[i];
439+
i++;
440+
}
441+
else{
442+
s2=s2+a[j];
443+
j--;
444+
}
445+
}
446+
447+
if(s1==s2){
448+
return i+1;
449+
}
450+
else{
451+
return -1;
452+
}
453+
454+
}
455+
456+
419457
export {
420458
is_array,
421459
is_num_array,
@@ -439,4 +477,5 @@ export {
439477
get_only,
440478
array_reverse_part,
441479
array_rotate,
480+
equilibrium_Point,
442481
};

0 commit comments

Comments
 (0)