Codility PermMissingElem Java solution

preview_player
Показать описание
PermMissingElem is a time complexity test in Codility where an array contains a list of integers from 1 to N+1, where N is the size of the array. There is one value missing and the object of the test is to find the missing value in the quickest time possible. This video show my Java solution to PermMissingElem.
Рекомендации по теме
Комментарии
Автор

Hi Dave, We could have used Sum of Arithmetic means as well.

class Solution {
public int solution(int[] A) {
double N = A.length + 2;
double sumOfArithematicSeries = N * (N - 1) / 2;
for (int i : A) {
sumOfArithematicSeries -= i;
}
return (int) (sumOfArithematicSeries);
}
}

furqanshaikh
Автор

Hi Dave, how are you? Hope I find you doing good. Just a quick question
(i)On the first for loop, why are we iterating from 1
(ii)On the second for loop, why are we removing the object and not the value of the array index
Thanks

Ngonie_
Автор

Hi Dave, It was very helpful thanks . if we use some other array may be {23, 21, 24, 25} how to get the missing elements . for that we need do ?

jabert
Автор

def solution(A):
size_A = len(A)
if size_A == 0:
return 1
set_A = set(A)
range_A = size_A + 1
for i in range(1, range_A):
if i not in set_A:
return i

Why does this keep giving me a runtime error for inputs like [1]? I keep getting a 50%

terrencemoore
Автор

Dave, when we know that missing element is consecutive, why just do not read it in order following sorting with Arrays?
it could be I am wrong but..

import java.util.Arrays;
public class PermMissingElem {
private int solution(int [] arr) {
int result=0;
Arrays.sort(arr);
int lastval = 0;
for(int i=0; i < arr.length; i++) {
if(i==0) { lastval=arr[i]; continue; } // just init first reading
if( arr[i]-lastval==1) continue; // continuing reading
else {
result = arr[i]+1;
break;
}
}
return result;
}

public static void main(String [] args) {
int[] intarr = new int [] {2, 3, 1, 5};
PermMissingElem obj = new PermMissingElem();

}

}

jetlavi
Автор

Thanks :) return (int)Math.ceil((Y-X) / (double)D);

getinetamare
Автор

A 70% solution I got for this on Codility (JavaScript):

function solution ( A ) {
A.sort();
var res = i = 0;

while(i<A.length){
res = (A[i]>1 && res==0) ? 0: A[i];
if(res==0) break;
if(!A.includes(res+1) && (res+1)<A[A.length-1]) break;
i++;
}

return (res+1);
}

testmail