+ 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).
5 Antworten
+ 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);
+ 3
https://code.sololearn.com/c26uqFr1bm3w/?ref=app
In one line
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
- 1
Kartikey Nice but n^2/2 execution time