what's wrong with my solution? * Finds the index of an element in an array where the sum…
- User
what's wrong with my solution? * Finds the index of an element in an array where the sum of elements to the left equals the sum of elements to the right.
- If such an index does not return -1.
- In this task, the use of methods of the Array and String classes is not allowed.
- @param {number[]} arr - The array to check.
- @return {number} The index of the balance point, or -1 if none exists.
- @example:
- [1, 2, 5, 3, 0] => 2 => 1 + 2 === 3 + 0 then balance element is 5 and its index = 2
- [2, 3, 9, 5] => 2 => 2 + 3 === 5 then balance element is 9 and its index = 2
- [1, 2, 3, 4, 5] => -1 => no balance element */ function getBalanceIndex(arr) { let sum1 = 0; let sum2 = 0; for (let i = 0; i < arr.length; i += 1) { for (let j = 0; j < i; j += 1) { sum1 += arr[j]; } for (let f = i + 1; f < arr.length; f += 1) { sum2 += arr[f]; } if (sum1 === sum2) { return i; } } return -1; }
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.