https://www.codewars.com/kata/5679aa472b8f57fb8c000047
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
You are going to be given an array of integers. Your job is to take that array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.
For example:
Let's say you are given the array {1,2,3,4,3,2,1}:
Your function will return the index 3, because at the 3rd position of the array, the sum of left side of the index ({1,2,3}) and the sum of the right side of the index ({3,2,1}) both equal 6.
Let's look at another one.
You are given the array {1,100,50,-51,1,1}:
Your function will return the index 1, because at the 1st position of the array, the sum of left side of the index ({1}) and the sum of the right side of the index ({50,-51,1,1}) both equal 1.
Last one:
You are given the array {20,10,-80,10,10,15,35}
At index 0 the left side is {}
The right side is {10,-80,10,10,15,35}
They both are equal to 0 when added. (Empty arrays are equal to 0 in this problem)
Index 0 is the place where the left side and right side are equal.
Note: Please remember that in most programming/scripting languages the index of an array starts at 0.
Input:
An integer array of length 0 < arr < 1000. The numbers in the array can be any integer positive or negative.
Output:
The lowest index N where the side to the left of N is equal to the side to the right of N. If you do not find an index that fits these rules, then you will return -1.
Note:
If you are given an array with multiple answers, return the lowest correct index.
설명:
정수 배열이 주어집니다. 배열을 받아 N의 왼쪽 정수들의 합과 N의 오른쪽 정수들의 합이 같은 인덱스 N을 찾아야 합니다. 이런 인덱스가 없다면 -1을 반환합니다.
예:
배열 {1,2,3,4,3,2,1}이 주어졌다고 가정합니다:
배열의 세 번째 위치에서 왼쪽 인덱스 ({1,2,3})의 합과 오른쪽 인덱스 ({3,2,1})의 합이 모두 6이기 때문에 인덱스 3을 반환합니다.
다른 경우를 보겠습니다.
배열 {1,100,50,-51,1,1}이 주어졌습니다:
배열의 첫 번째 위치에서 왼쪽 인덱스 ({1})의 합과 오른쪽 인덱스 ({50,-51,1,1})의 합이 모두 1이기 때문에 인덱스 1을 반환합니다.
마지막:
배열 {20,10,-80,10,10,15,35}가 주어집니다.
인덱스 0의 왼쪽은 {}
오른쪽은 {10,-80,10,10,15,35}입니다.
둘 다 더해지면 0이 됩니다(이 문제에서 빈 배열은 0과 같습니다).
인덱스 0은 왼쪽과 오른쪽이 같은 위치입니다.
참고: 대부분의 프로그래밍/스크립트 언어에서 배열의 인덱스의 시작은 0입니다.
입력:
배열의 길이는 0 < arr < 1000입니다. 배열의 숫자는 양의 정수 또는 음의 정수일 수 있습니다.
출력:
N의 왼쪽과 N의 오른쪽이 같은 가장 작은 인덱스 N. 이러한 규칙에 맞는 인덱스를 찾지 못하면 -1을 반환합니다.
참고:
If you are given an array with multiple answers, return the lowest correct index.
답이 여러 개인 배열이 주어진 경우 가장 작고 정확한 인덱스를 반환하세요.
풀이
처음 인덱스부터 왼쪽합과 오른쪽 합을 구해 그 둘이 같다면 그 위치의 인덱스를 반환하고 모든 인덱스를 탐색했을 때도 같은 경우를 찾을 수 없다면 -1을 반환했다.
코드
function findEvenIndex(arr)
{
for(let i = 0; i < arr.length; i++){
let rSum = 0;
let lSum = 0;
for(let j = 0; j < arr.length; j ++){
if(j < i) rSum += arr[j];
if(j > i) lSum += arr[j];
}
if(rSum === lSum) return i
}
return -1;
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Find the next perfect square! (7 kyu) / JavaScript (0) | 2022.12.16 |
---|---|
[Codewars] Does my number look big in this? (6 kyu) / JavaScript (0) | 2022.12.15 |
[Codewars] Playing with digits (6 kyu) / JavaScript (0) | 2022.12.13 |
[Codewars] Decode the Morse code (6 kyu) / JavaScript (0) | 2022.12.12 |
[Codewars] Unique In Order (6 kyu) / JavaScript (0) | 2022.12.10 |
댓글