Task:
With an Array of numbers name A, give the first integer above 0.
example: A = {1,2,4,5}, the answer will be 3
example: A = {-1, 0, -2, -4, 1}, the answer will be 2
The number of A could be as high as 1,000,000.
An Answer:
class FindFirstNumber {
public int findNumber(int[] A) {
for (int N = 1; N <= 1000001; N++) {
int isFound = 0; for (int countA = 0; countA < A.length; countA++) {
// check
if (N == A[countA]) {
// found number in array, keep going isFound = 1;
} else {
// keep loop through A until done
}
System.out.println("N: " + N + ", A: " + A[countA]);
}
if (isFound != 1) {
// number not found return N;
}
}
// not found
return 1;
}
}
previous page
|