Pregunta de entrevista

Entrevista para Senior Software Engineer

-Buenos Aires

MuleSoft

Given an array of size n with range of numbers from 1 to n+1. The array doesn’t contain any duplicate, one number is missing, find the missing number.

Respuesta

Respuestas de entrevistas

2 respuestas

0

^ description says there is no duplicate. So the answer here is wrong. If the numbers are ordered, then you can just make a binary search and check the (index + 1) matches value for that index until you find the missing one (i.e the first one that does not matches within the array). That's likely the fastest way O(logN).

None en

0

Traverse, use absolute value on each element as index, make the value at index as negative to mark it. If already marked negative, then is a repeating element. For missing ones, traverse again and check for positive values instead. #include #include void printTwoElements(int arr[], int size) { int i; printf("\n element repeating: "); for(i = 0; i 0) { arr[abs(arr[i])-1] = -arr[abs(arr[i])-1]; } else { printf(" %d ", abs(arr[i])); } } printf("\n missing one: "); for(i=0; i0) { printf("%d",i+1); } } } /* test method */ int main() { int arr[] = {7, 3, 4, 5, 5, 6, 2}; int n = sizeof(arr)/sizeof(arr[0]); printTwoElements(arr, n); return 0; }

Anónimo en

Agregar respuestas o comentarios

Para comentar esto, Inicia sesión o regístrate.