An array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.
서로 다른 N개의 정수로 구성된 배열 A가 제공됩니다. 배열에는 [1..(N + 1))] 범위의 정수가 포함되어 있는데, 이는 정확히 하나의 요소가 누락되었음을 의미합니다.
Your goal is to find that missing element.
당신의 목표는 누락된 요소를 찾는 것입니다.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A, returns the value of the missing element.
배열 A가 주어지면 누락된 요소의 값을 반환합니다.
For example, given array A such that:
A[0] = 2
A[1] = 3
A[2] = 1
A[3] = 5
the function should return 4, as it is the missing element.
누락된 요소이므로 함수가 4를 반환해야 합니다.
Write an efficient algorithm for the following assumptions:
다음 가정에 대한 효율적인 알고리즘을 작성합니다:
N is an integer within the range [0..100,000];
the elements of A are all distinct;
each element of array A is an integer within the range [1..(N + 1)].
N은 [0..100,000] 범위 내의 정수입니다;
A의 요소는 모두 구별됩니다;
배열 A의 각 요소는 [1..(N + 1)] 범위 내의 정수입니다.
2023.08.09
class Solution {
public int solution(int[] A) {
int answer = 0;
Arrays.sort(A);
for(int i = 0; i < A.length; i++) {
if(A[i] != i+1) {
return A[i]-1;
}
}
return answer;
}
}
- WRONG ANSWER(일부)
class Solution {
public int solution(int[] A) {
Arrays.sort(A);
for(int i = 0; i < A.length; i++) {
if(A[i] != i+1) {
return A[i]-1;
}
}
return A.length + 1;
}
}
1~N 까지 모든 숫자를 포함한 경우, N+1이 제외된 것으로 처리해야함.
Codility Lesson4 - FrogRiverOne (0) | 2023.08.21 |
---|---|
Codility Lesson3 - FrogJmp (0) | 2023.08.08 |
Codility Lesson2 - OddOccurrencesInArray (0) | 2023.08.08 |
Codiliity Lesson2 - CyclicRotation (0) | 2023.08.08 |
Codility Lesson1 - BinaryGap (0) | 2023.08.07 |