Given an array of integers, find the first missing positive integer in linear time and constant space

后端 未结 15 2237
暖寄归人
暖寄归人 2021-02-01 07:17

In other words, find the lowest positive integer that does not exist in the array. The array can contain duplicates and negative numbers as well. This question was asked by Stri

15条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 07:46

    Here is a C implementation
    INPUT

    #include 
    #include 
    //Here we separate the positive and negative number
    int separate (int arr[], int size)
    {
        int j = 0, i , temp;
        for(i = 0; i < size; i++)
        {
        if (arr[i] <= 0)
        {
            /*Here we using bitwise operator to swap the
            numbers instead of using the temp variable*/
             arr[j] = arr[j]^arr[i];
             arr[i] = arr[j]^arr[i];
             arr[j] = arr[j]^arr[i];
             j++;
        }
        }
        printf("First We Separate the negetive and positive number \n");
        for( i = 0 ; i  0)
        arr[ abs(arr[i]) - 1] = -arr[ abs(arr[i]) - 1];
    }
    for(i = 0; i < size; i++)
        if (arr[i] > 0)
        {
        return i+1;
        }
    return size+1;
    }
    int findMissing(int arr[], int size)
    {
    int j = separate (arr, size);
    return findMissingPositive(arr+j, size-j);
    }
    int main()
    {
    int size ;
    printf("Enter the Value of Size of Array : ");
    scanf("%d",&size);
    int arr[size];
    printf("Enter the values :\n");
    for( int i = 0 ; i < size ; i++)
    {
        printf("Array[%d] = ",i);
        scanf("%d",&arr[i]);
    }
    int missing = findMissing(arr,size);
    printf("The smallest positive missing number is %d ", missing);
    return 0;
    }
    


    OUTPUT

    Enter the Value of Size of Array : 8
    Enter the values :
    Array[0] = 1
    Array[1] = -1
    Array[2] = -5
    Array[3] = -3
    Array[4] = 3
    Array[5] = 4
    Array[6] = 2
    Array[7] = 8
    First We Separate the negetive and positive number
    Array[0] = -1
    Array[1] = -5
    Array[2] = -3
    Array[3] = 1
    Array[4] = 3
    Array[5] = 4
    Array[6] = 2
    Array[7] = 8
    Remove the negative numbers from array
    Array[0] = 1
    Array[1] = 3
    Array[2] = 4
    Array[3] = 2
    Array[4] = 8
    The smallest positive missing number is 5
    Process returned 0 (0x0)   execution time : 27.914 s
    Press any key to continue.
    

     /*
            How work :
            [if(abs(arr[i]) - 1 < size && arr[ abs(arr[i]) - 1] > 0)
            arr[ abs(arr[i]) - 1] = -arr[ abs(arr[i]) - 1];]
            before: arr = { 7, 3, 4, 5, 5, 3, 2}
        i == 0: arr[0] = 7
                arr[7-1] is 2 > 0 ~> negate
                arr = { 7, 3, 4, 5, 5, 3, -2}
        i == 1: arr[1] = 3
                arr[3-1] is 4 > 0 ~> negate
                arr = { 7, 3, -4, 5, 5, 3, -2}
        i == 2: arr[2] is -4 ~> abs for indexing
                arr[4-1] is 5 > 0 ~> negate
                arr = { 7, 3, -4,-5, 5, 3, -2}
        i == 3: arr[3] is -5 ~> abs for indexing
                arr[5-1] is 5 > 0 ~> negate
                arr = { 7, 3, -4, -5, -5, 3, -2}
        i == 4: arr[4] is -5 ~> abs for indexing
                arr[5-1] is -5 < 0 ~> print abs(-5) as duplicate
        i == 5: arr[5] is 3
                arr[3-1] is -4 < 0 ~> print abs(3) as duplicate
        i == 6: arr[6] is -2 ~> abs for indexing
                arr[2-1] is 3 > 0 ~> negate
                arr = { 7, -3, -4, -5, -5, 3, -2}
    
                indices of positive entries: 0, 5 ~> 1 and 6 not in original array
                indices of negative entries: 1, 2, 3, 4, 6 ~> 2, 3, 4, 5, 7 in original array
    */
    

提交回复
热议问题