FrogRiverOne

Find the earliest time when a frog can jump to the other side of a river.


Solution:

Solution to Codility's Frog River One problem which is from the Codility Lesson 4: Counting Elements and, is solved in Java 8 with 100% performance and correctness scores. The goal here is to find the earliest time when a frog can jump to the other side of a river. You can find the question of this FrogRiverOne problem in the Codility website.


package Codility.Lesson4;

import java.util.*;

public class FrogRiverOne {

	public FrogRiverOne() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int input[] = {1,3,1,1,2,3,4,7,6,5,7,8};
		int result = solution(6, input);
		System.out.println (result);
	}
	
	public static int solution(int X, int[] A) {
        // write your code in Java SE 8
        int sec =0;
        Set setA = new HashSet();

        for( int i =0; i< A.length; i++){
            if(A[i]<= X){
                setA.add(A[i]);
            }
            if (setA.size() == X){               
                return i;
            }

        }
        return -1;
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *