+ 1

Challenge - find out middle index where sum of both ends are equals

You are given a arrays of number. Find out the array index or position where sum of numbers preceeding the index is equal to sum of number succeeding the index. For ex. arr{2, 4, 4, 5, 4, 1}. in this example middle index will be 2, as sum of preceeding numbers (2+4+4=10) is same as sum of succeeding numbers(5+4+1=10).

8th Oct 2017, 9:21 AM
Adarsh Srivastava
Adarsh Srivastava - avatar
5 odpowiedzi
+ 11
JavaScript: var a = prompt("Array:", "2 3 1 4").split(" "), b; for (b = 0; b < a.length; b++) { var c = 0, d = 0; for (var e = 0; e <= b; e++) c += a[e] - 0; for (var f = b + 1; f < a.length; f++) d += a[f] - 0; if (c === d) break; } if (b === a.length) alert("No Index Found!"); else alert("Breaking Index : " + b);
8th Oct 2017, 12:04 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
8th Oct 2017, 2:07 PM
Kartikey Sahu
Kartikey Sahu - avatar
0
that is absolutely right but i think you should give a proper input screen so that no confusion for giving an input.. but that was good
8th Oct 2017, 12:09 PM
Adarsh Srivastava
Adarsh Srivastava - avatar
- 1
Kartikey Nice but n^2/2 execution time
8th Oct 2017, 3:38 PM
VcC
VcC - avatar