package Codility;

public class FrogRiverOne {
	public static void main(String[] args) {
		int[] A = { 1, 3, 1, 4, 2, 3, 5, 4 };
		int X = 5;
		System.out.println(solution(A, X));
	}

	public static int solution(int[] A, int X) {

		if (X > A.length) {
			return -1;
		}

		int[] isFilled = new int[X];
		int position = 0;
		for (int i = 0; i < A.length; i++) {
			int x = A[i];
			if (x <= X) {
				if (isFilled[x - 1] == 0) {
					isFilled[x - 1] = 1;
					position += 1;
					if (position == X) {
						return i;
					}
				}
			}
		}

		return -1;
	}
}

@@ -11,16 +11,1835 @@
 dility;%0A
+/**%0AA small frog wants to get to the other side of a river. The frog is initially located on one bank of the river (position 0) and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river.%0A%0AYou are given an array A consisting of N integers representing the falling leaves. A%5BK%5D represents the position where one leaf falls at time K, measured in seconds.%0A%0AThe goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X (that is, we want to find the earliest moment when all the positions from 1 to X are covered by leaves). You may assume that the speed of the current in the river is negligibly small, i.e. the leaves do not change their positions once they fall in the river.%0A%0AFor example, you are given integer X = 5 and array A such that:%0A%0A  A%5B0%5D = 1%0A  A%5B1%5D = 3%0A  A%5B2%5D = 1%0A  A%5B3%5D = 4%0A  A%5B4%5D = 2%0A  A%5B5%5D = 3%0A  A%5B6%5D = 5%0A  A%5B7%5D = 4%0AIn second 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river.%0A%0AWrite a function:%0A%0Aclass Solution %7B public int solution(int X, int%5B%5D A); %7D%0A%0Athat, given a non-empty array A consisting of N integers and integer X, returns the earliest time when the frog can jump to the other side of the river.%0A%0AIf the frog is never able to jump to the other side of the river, the function should return %E2%88%921.%0A%0AFor example, given X = 5 and array A such that:%0A%0A  A%5B0%5D = 1%0A  A%5B1%5D = 3%0A  A%5B2%5D = 1%0A  A%5B3%5D = 4%0A  A%5B4%5D = 2%0A  A%5B5%5D = 3%0A  A%5B6%5D = 5%0A  A%5B7%5D = 4%0Athe function should return 6, as explained above.%0A%0AWrite an efficient algorithm for the following assumptions:%0A%0AN and X are integers within the range %5B1..100,000%5D;%0Aeach element of array A is an integer within the range %5B1..X%5D.%0A*/
 %0Apublic 

Update FrogRiverOne.java