You are on page 1of 5

import java.util.

*;

public class FirstMissingPositive {

// invalidNumbers: anything <= 0; anything > len;


static boolean isValidNumber(int n, int len) {
if (n > len || n < 1) {
return false;
}
return true;
} // isValidNumber().

public int firstMissingPositive(int[] nums) {


int len = nums.length;
int maxOutput = len + 1;
System.out.println("len = " + len);

int i = 0;
while (i < len) {
// 3 4 -1 1
// -1 4 3 1
// 0 4 3 1. i = 1
// 0 1 3 4 i = 1
// 1 0 3 4. i = 1
int currentNumber = nums[i];
System.out.println("Current Number = " + currentNumber + " i = " + i);

if (isValidNumber(currentNumber, len)) {
if (currentNumber == (i-1)) {
i++;
} // the number is already in the right location.
else {
// swap(nums[currentNumber-1], nums[i]);
nums[i] = nums[currentNumber-1];

// currentNumber goes to the appropriate location.


nums[currentNumber-1] = currentNumber;
// shouldn't be advancing i.
} // else
} else {
// TODO: Will 0 cause any problem.
nums[i] = 0;
i++;
} // else
} // while i

for (int j = 0; j < len; j++) {


if (nums[j] == j + 1) {
continue;
} else {
return j + 1;
}
} // for.

// Deal with 1 2 3 4 case.


return len + 1;
}

public static void main(String[] args) {


System.out.println("Starting program");

Scanner in = new Scanner(System.in);


int n = in.nextInt();
int[] nums = new int[n];
for(int i = 0 ; i < n ; ++i) {
nums[i] = in.nextInt();
}
System.out.println("Starting program");
int result = new FirstMissingPositive().firstMissingPositive(nums);
System.out.println(result);
}
}

// Milestone 1: Understand the problem clearly


// - Ask questions & clarify the problem statement clearly.
// - Take an example or two to confirm your understanding of the input/output & extend it to
testcases
// #1: 0
// 1

// #2: -1 -2 -3 -9
// 1

// #3: 1 2 3 4
// 5

// #4: 100 999 1000 10000


// 1

// #5: 1 5 3 4 => 1 3 4 5
// 2

// #5: 2 100 4 999


// OutputArray: - - - -
// OutputArray: - 2 - -
// OutputArray: - 2 - 4

// // Milestone 2: Finalize approach & execution plan


// // - Understand what type of problem you are solving. - Logic
// // - Brainstorm multiple ways to solve the problem and pick one
// #1: Sort this whole array. O(nlogn)
// #2: With an extra output array O(n), definitely we solve the problem
// 2 100 4 999
// OutputArray: - - - -
// OutputArray: - 2 - -
// OutputArray: - 2 - 4

// #3: Max output value <= n + 1


// InputArray: 100 999 1000 10000 1 3

// InputArray: - 999 1000 10000 1 3


// InputArray: - - - - 1 3
// InputArray: 1 - - - - 3
// InputArray: 1 - 3 - - -

// InputArray: 3 4 -1 1

// InputArray: -1 4 3 1

// InputArray: - 4 3 1
// InputArray: - 1 3 4
// InputArray: 1 - 3 4

// // - Get to a point where you can explain your approach to a 10 year old
// - Take the number, and insert it in the appropriate index
// - If there is a number in that index already (in the forward motion), swap the numbers.
// - Don't move the current pointer.
// - If the current number fits in the right index or its an useless number, then discard and
move forward.
// - Find the gap where there is no number.
// // - Take a stab at the high level logic & write it down.
// len = sizeof(inputArray);
// maxOutput = len + 1;
// invalidNumbers: anything <= 0; anything > len;

// while i -> 0 : len {


// // 3 4 -1 1
// currentNumber = inputArray[i];
// if (isValidNumber(currentNumber)) {
// if (currentNumber == (i-1)) {
// i++;
// } // the number is already in the right location.
// else {
// swap(inputArray[currentNumber-1], inputArray[i]);
// // shouldn't be advancing i.
// }
// } else {
// // TODO: Will 0 cause any problem.
// inputArray[i] = 0;
// i++;
// }
// }

// - Try to offload processing to functions & keeping your main code small.
// Milestone 3: Code by expanding your pseudocode
// - Make sure you name the variables, functions clearly.
// - Avoid constants in your code unless necessary; go for generic functions, you can use
examples for your thinking though.
// - Use libraries as much as possible
// Milestone 4: Prove to the interviewer that your code works with unit tests
// - Make sure you check boundary conditions
// Time & storage complexity
// Suggest optimizations

You might also like