Core Java

Java Implementation of Frog River One

This article explains the Frog River One problem and explores different approaches to solve it, comparing their efficiency and practicality using set-based and bitmap-based solutions. Let us delve into understanding the Java Frog River One problem and explore different approaches to solve it efficiently.

1. Introduction

Frog River One problem a popular algorithmic challenge used to test concepts like arrays, sets, and bitmap techniques in programming. In this problem, a frog is trying to cross a river where positions are labeled from 1 to X, and leaves fall on these positions over time. The frog can only cross when all positions from 1 to X are covered by at least one leaf, making it a classic example to understand efficient data tracking and time-based problem solving.

2. Problem Statement

You are given an integer X and an array A where A[i] represents the position where a leaf falls at time i. Your task is to find the earliest time when the frog can jump to the other side of the river. If it’s never possible, return -1.

2.1 Set-Based Approach

One simple approach is to use a Set to track all the positions where leaves have fallen. Once the set contains all positions from 1 to X, the frog can cross.

import java.util.HashSet;
import java.util.Set;

public class FrogRiverOneSet {
  public static int earliestTime(int X, int[] A) {
    Set < Integer > positions = new HashSet < >();
    for (int i = 0; i < A.length; i++) {
      if (A[i] <= X) {
        positions.add(A[i]);
      }
      if (positions.size() == X) {
        return i;
      }
    }
    return - 1;
  }

  public static void main(String[] args) {
    int X = 5;
    int[] A = {
      1,
      3,
      1,
      4,
      2,
      3,
      5,
      4
    };
    System.out.println("Earliest time for frog to cross: " + earliestTime(X, A));
  }
}

2.1.1 Code Explanation

This Java program solves the “Frog River One” problem using a HashSet. It defines a method earliestTime that takes an integer X and an array A representing falling leaves over time. The method iterates through the array, adding each leaf position to a set if it is less than or equal to X. Since a Set only stores unique values, it keeps track of all positions covered by leaves. When the size of the set reaches X, it means all positions from 1 to X are covered, so the method returns the current index as the earliest time the frog can cross. If the loop finishes without covering all positions, it returns -1. The main method demonstrates this by initializing X to 5 and an example array A, then prints the result of earliestTime.

2.1.2 Code Output

Earliest time for frog to cross: 6

This is because the method earliestTime iterates through the array A = {1, 3, 1, 4, 2, 3, 5, 4}, adding each leaf position to a HashSet to track unique positions from 1 to X = 5. Once all positions 1 through 5 are present in the set, which happens at index 6 (the 7th second), the method returns 6 as the earliest time the frog can cross the river. If the frog could not cross, it would return -1, but in this case, all positions are covered in time 6, allowing safe passage.

2.2 Bitmap-Based Solution

A more memory-efficient approach is using a bitmap (boolean array) to track positions. This avoids the overhead of a Set and still ensures O(N) complexity.

public class FrogRiverOneBitmap {
  public static int earliestTime(int X, int[] A) {
    boolean[] positions = new boolean[X + 1];
    int covered = 0;

    for (int i = 0; i < A.length; i++) {
      if (!positions[A[i]]) {
        positions[A[i]] = true;
        covered++;
      }
      if (covered == X) {
        return i;
      }
    }
    return - 1;
  }

  public static void main(String[] args) {
    int X = 5;
    int[] A = {
      1,
      3,
      1,
      4,
      2,
      3,
      5,
      4
    };
    System.out.println("Earliest time for frog to cross: " + earliestTime(X, A));
  }
}

2.2.1 Code Explanation

This Java code defines a class FrogRiverOneBitmap with a method earliestTime that determines the earliest moment a frog can cross a river. Given a target position X and an array A representing leaves falling at positions over time, it uses a boolean array positions to track which positions have leaves. It iterates through A, marking positions as covered and counting them, and once all positions from 1 to X are covered, it returns the index (time) when this occurs. If the frog can’t cross, it returns -1. The main method provides a sample input with X = 5 and A = {1, 3, 1, 4, 2, 3, 5, 4}, and prints the earliest time the frog can cross.

2.2.2 Code Output

Earliest time for frog to cross: 6

The frog wants to cross a river to position X = 5, and leaves fall at positions given by the array A = {1, 3, 1, 4, 2, 3, 5, 4}, where each index represents a time step. The program tracks which positions have leaves using a Boolean array. As it iterates through A, at time 0 a leaf falls at 1 (positions covered: 1), at time 1 a leaf falls at 3 (positions covered: 2), time 2 a leaf falls at 1 (already covered), time 3 a leaf falls at 4 (positions covered: 3), time 4 a leaf falls at 2 (positions covered: 4), time 5 a leaf falls at 3 (already covered), and at time 6 a leaf falls at 5 (positions covered: 5). At this point, all positions from 1 to 5 have leaves, so the frog can cross the river, which is why the program outputs 6.

3. Conclusion

The Frog River One problem is a classic example to understand different approaches for solving array coverage problems. While the set-based approach is simple and intuitive, the bitmap-based approach is more memory-efficient and faster for large datasets. Both approaches have O(N) time complexity, making them suitable for real-time scenarios.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button