My experience on my daily works... helping others ease each other

Saturday, January 11, 2020

Codility - PermMissingElem (Find the missing element in a given permutation)

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 the Array is empty, we will just set the missingInt to 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;
       }

The complete code is shown below
Codility - PermMissingElem sample code
My result is shown below
Result


You can download the code from here:

  1. Bitbucket - git clone https://masteramuk@bitbucket.org/fullstacksdev/codility-permmissingelem.git
  2. Github - git clone https://github.com/masteramuk/codility-lessoncode.git



Share:

0 comments:

About Me

Somewhere, Selangor, Malaysia
An IT by profession, a beginner in photography

Blog Archive

Blogger templates