This is a lesson in codility for the Time Complexity algorithm. Given an array of integer, you need to find the lowest missing integer.
I managed to score 100% for it.
Here is the snapshot of the code:
Based on the length, for all value in Array A, start the search and compare the initial value; that is 1 (expectedInt). If the value exists, the expectedInt is added 1 value.
if( A.length > 0 ){
for (int x : A){
//if found a value based on expected value
if (x == expectedInt){
expectedInt++;
} else { //if found a mising value
missingInt = expectedInt;
}
}
}
if( A.length > 0 ){
for (int x : A){
//if found a value based on expected value
if (x == expectedInt){
expectedInt++;
} else { //if found a mising value
missingInt = expectedInt;
}
}
}
If the Array is empty, we will just set the missingInt to 1.
if (A.length == 0) {
missingInt = 1;
}
if (A.length == 0) {
missingInt = 1;
}
If no missing int found, then we just add additional 1 to the last value found
if (missingInt == 0){
missingInt = A[A.length — 1] + 1;
}
if (missingInt == 0){
missingInt = A[A.length — 1] + 1;
}
The complete code is shown below
My result is shown below
You can download the code from here:
Codility - PermMissingElem sample code |
Result |
You can download the code from here:
- Bitbucket - git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-permmissingelem.git
- Github - git clone https://github.com/masteramuk/codility-lessoncode.git
0 comments:
Post a Comment