Frogs are positioned at one end of a pond, and each wants to reach the other end. The pond has some leaves arranged in a straight line.
Each frog has a strength s, meaning it jumps exactly s leaves at a time - for example, a frog with strength 2 visits leaves 2, 4, 6, and so on while crossing the pond.
Given the strength of each frog (as an array arr[]) and the total number of leaves k, find how many leaves are not visited by any frog after all frogs have crossed the pond.
Examples:
Input: arr[] = [3, 6, 2], k = 6
Output: 2
Explanation:
Let's look for the first frog with strength 3:
It jumps to 3, then 6 and then, in the next jump it reaches the other side. Thus, marking the leaves 3 and 6 visited. Now for the next frog with strength 6:
It jumps to 6 and in the next jump it reaches the other side. Thus, marking the leaf 6 visited(but that leaf was already marked visited by frog 1).
Similarly, frog 3 will jump to leaf 2 then leaf 4 and then leaf 6. Thus, marking leaves 2, 4 and 6 visited. Total of 4 leaves are visited: 2, 3, 4 and 6. So, unvisited leaves are 1 and 5. Hence, answer is 2.Input: arr[] = [1, 3, 5], k = 6
Output: 0
Explanation: Frog with strength 1 visits leaves 1, 2, 3, 4, 5, 6 every leaf. All leaves are already covered, so none are left unvisited.
Table of Content
[Naive Approach] By Checking Every Leaf for Every Frog - O(n * k) Time and O(k) Space
The idea is to examine every leaf in the pond for each frog and check whether the frog lands on that leaf. A frog with strength s visits only those leaves whose positions are divisible by s. Therefore, for every leaf, we use the modulo operation (leaf % s == 0) to determine if the frog visits it.
- Create a boolean array visited[] of size k + 1 and initialize all values to false.
- Traverse each frog's strength in the given array.
- For every frog, iterate through all leaves from 1 to k.
- If the current leaf number is divisible by the frog's strength (leaf % strength == 0), mark that leaf as visited.
- After processing all frogs, traverse the visited[] array and count the leaves that are still unvisited.
#include <iostream>
#include <vector>
using namespace std;
// Function to count the number of unvisited leaves.
int unvisitedLeaves(vector<int> &arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
vector<bool> visited(k + 1, false);
// Process every frog one by one.
for (int strength : arr)
{
// Check every leaf in the pond.
for (int leaf = 1; leaf <= k; leaf++)
{
// If the leaf number is divisible by the frog's
// strength, then the frog visits this leaf.
if (leaf % strength == 0)
{
visited[leaf] = true;
}
}
}
// Count the leaves that were never visited.
int unvisitedCount = 0;
for (int leaf = 1; leaf <= k; leaf++)
{
if (!visited[leaf])
{
unvisitedCount++;
}
}
return unvisitedCount;
}
// Driver code
int main()
{
vector<int> arr = {3, 6, 2};
int k = 6;
cout << unvisitedLeaves(arr, k) << endl;
return 0;
}
import java.util.*;
class GFG {
// Function to count the number of unvisited leaves.
static int unvisitedLeaves(int[] arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
boolean[] visited = new boolean[k + 1];
// Process every frog one by one.
for (int strength : arr) {
// Check every leaf in the pond.
for (int leaf = 1; leaf <= k; leaf++) {
// If the leaf number is divisible by the
// frog's strength, then the frog visits
// this leaf.
if (leaf % strength == 0) {
visited[leaf] = true;
}
}
}
// Count the leaves that were never visited.
int unvisitedCount = 0;
for (int leaf = 1; leaf <= k; leaf++) {
if (!visited[leaf]) {
unvisitedCount++;
}
}
return unvisitedCount;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 3, 6, 2 };
int k = 6;
System.out.println(unvisitedLeaves(arr, k));
}
}
# Function to count the number of unvisited leaves.
def unvisitedLeaves(arr, k):
# visited[i] stores whether the ith leaf has been
# visited by any frog or not.
visited = [False] * (k + 1)
# Process every frog one by one.
for strength in arr:
# Check every leaf in the pond.
for leaf in range(1, k + 1):
# If the leaf number is divisible by the frog's
# strength, then the frog visits this leaf.
if leaf % strength == 0:
visited[leaf] = True
# Count the leaves that were never visited.
unvisitedCount = 0
for leaf in range(1, k + 1):
if not visited[leaf]:
unvisitedCount += 1
return unvisitedCount
# Driver code
if __name__ == "__main__":
arr = [3, 6, 2]
k = 6
print(unvisitedLeaves(arr, k))
using System;
class GFG {
// Function to count the number of unvisited leaves.
static int unvisitedLeaves(int[] arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
bool[] visited = new bool[k + 1];
// Process every frog one by one.
foreach(int strength in arr)
{
// Check every leaf in the pond.
for (int leaf = 1; leaf <= k; leaf++) {
// If the leaf number is divisible by the
// frog's strength, then the frog visits
// this leaf.
if (leaf % strength == 0) {
visited[leaf] = true;
}
}
}
// Count the leaves that were never visited.
int unvisitedCount = 0;
for (int leaf = 1; leaf <= k; leaf++) {
if (!visited[leaf]) {
unvisitedCount++;
}
}
return unvisitedCount;
}
// Driver code
static void Main()
{
int[] arr = { 3, 6, 2 };
int k = 6;
Console.WriteLine(unvisitedLeaves(arr, k));
}
}
// Function to count the number of unvisited leaves.
function unvisitedLeaves(arr, k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
let visited = new Array(k + 1).fill(false);
// Process every frog one by one.
for (let strength of arr) {
// Check every leaf in the pond.
for (let leaf = 1; leaf <= k; leaf++) {
// If the leaf number is divisible by the frog's
// strength, then the frog visits this leaf.
if (leaf % strength === 0) {
visited[leaf] = true;
}
}
}
// Count the leaves that were never visited.
let unvisitedCount = 0;
for (let leaf = 1; leaf <= k; leaf++) {
if (!visited[leaf]) {
unvisitedCount++;
}
}
return unvisitedCount;
}
// Driver code
let arr = [ 3, 6, 2 ];
let k = 6;
console.log(unvisitedLeaves(arr, k));
Output
2
[Expected Approach] By Processing Each Unique Frog Strength Once - O(n + k * log(k)) Time and O(k) Space
Instead of checking every leaf, directly simulate the jumps of each frog. A frog with strength s visits only the leaves s, 2s, 3s, ..., so we mark only these leaves as visited. Since frogs with the same strength visit the same set of leaves, we process each unique strength only once to avoid redundant work. Finally, the leaves that remain unvisited are counted.
- Create a visited[] array of size k + 1 to mark visited leaves.
- Create a processed[] array of size k + 1 to track the frog strengths that have already been processed.
- Traverse each frog's strength in the given array.
- If the strength is greater than k or has already been processed, skip it.
- Otherwise, mark the strength as processed and mark all its multiples (s, 2s, 3s, ...) as visited.
- Traverse the visited[] array and count the leaves that remain unvisited.
- Return the count.
#include <iostream>
#include <vector>
using namespace std;
// Function to count the number of unvisited leaves.
int unvisitedLeaves(vector<int> &arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
vector<bool> visited(k + 1, false);
// processed[i] stores whether a frog with
// strength i has already been processed.
vector<bool> processed(k + 1, false);
// Process every frog one by one.
for (int strength : arr)
{
// Skip if the frog cannot visit any leaf or
// another frog with the same strength is
// already processed.
if (strength > k || processed[strength])
{
continue;
}
// Mark this strength as processed.
processed[strength] = true;
// Visit all leaves that are multiples
// of the frog's strength.
for (int leaf = strength; leaf <= k; leaf += strength)
{
visited[leaf] = true;
}
}
// Count the leaves that were never visited.
int unvisitedCount = 0;
for (int leaf = 1; leaf <= k; leaf++)
{
if (!visited[leaf])
{
unvisitedCount++;
}
}
return unvisitedCount;
}
// Driver code
int main()
{
vector<int> arr = {3, 6, 2};
int k = 6;
cout << unvisitedLeaves(arr, k) << endl;
return 0;
}
import java.util.*;
class GFG {
// Function to count the number of unvisited leaves.
static int unvisitedLeaves(int[] arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
boolean[] visited = new boolean[k + 1];
// processed[i] stores whether a frog with
// strength i has already been processed.
boolean[] processed = new boolean[k + 1];
// Process every frog one by one.
for (int strength : arr) {
// Skip if the frog cannot visit any leaf or
// another frog with the same strength is
// already processed.
if (strength > k || processed[strength]) {
continue;
}
// Mark this strength as processed.
processed[strength] = true;
// Visit all leaves that are multiples
// of the frog's strength.
for (int leaf = strength; leaf <= k;
leaf += strength) {
visited[leaf] = true;
}
}
// Count the leaves that were never visited.
int unvisitedCount = 0;
for (int leaf = 1; leaf <= k; leaf++) {
if (!visited[leaf]) {
unvisitedCount++;
}
}
return unvisitedCount;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 3, 6, 2 };
int k = 6;
System.out.println(unvisitedLeaves(arr, k));
}
}
# Function to count the number of unvisited leaves.
def unvisitedLeaves(arr, k):
n = len(arr)
# Marking visited status for each leaf (1-indexed).
visited = [0] * (k + 1)
for i in range(n):
# Skip if the frog's strength exceeds the total number
# of leaves, or if this strength has already been
# processed (avoids redundant work for duplicate strengths).
if arr[i] <= k and visited[arr[i]] == 0:
# Mark all leaves reachable by this frog's strength.
for j in range(arr[i], k + 1, arr[i]):
visited[j] = 1
# Count the leaves that were never visited.
unvisitedCount = k
for status in visited:
if status:
unvisitedCount -= 1
return unvisitedCount
# Driver code
if __name__ == "__main__":
arr = [3, 6, 2]
k = 6
print(unvisitedLeaves(arr, k))
using System;
class GFG {
// Function to count the number of unvisited leaves.
static int unvisitedLeaves(int[] arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
bool[] visited = new bool[k + 1];
// processed[i] stores whether a frog with
// strength i has already been processed.
bool[] processed = new bool[k + 1];
// Process every frog one by one.
foreach(int strength in arr)
{
// Skip if the frog cannot visit any leaf or
// another frog with the same strength is
// already processed.
if (strength > k || processed[strength]) {
continue;
}
// Mark this strength as processed.
processed[strength] = true;
// Visit all leaves that are multiples
// of the frog's strength.
for (int leaf = strength; leaf <= k;
leaf += strength) {
visited[leaf] = true;
}
}
// Count the leaves that were never visited.
int unvisitedCount = 0;
for (int leaf = 1; leaf <= k; leaf++) {
if (!visited[leaf]) {
unvisitedCount++;
}
}
return unvisitedCount;
}
// Driver code
static void Main()
{
int[] arr = { 3, 6, 2 };
int k = 6;
Console.WriteLine(unvisitedLeaves(arr, k));
}
}
// Function to count the number of unvisited leaves.
function unvisitedLeaves(arr, k) {
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
let visited = new Array(k + 1).fill(false);
// processed[i] stores whether a frog with
// strength i has already been processed.
let processed = new Array(k + 1).fill(false);
// Process every frog one by one.
for (let strength of arr) {
// Skip if the frog cannot visit any leaf or
// another frog with the same strength is
// already processed.
if (strength > k || processed[strength]) {
continue;
}
// Mark this strength as processed.
processed[strength] = true;
// Visit all leaves that are multiples
// of the frog's strength.
for (let leaf = strength; leaf <= k; leaf += strength) {
visited[leaf] = true;
}
}
// Count the leaves that were never visited.
let unvisitedCount = 0;
for (let leaf = 1; leaf <= k; leaf++) {
if (!visited[leaf]) {
unvisitedCount++;
}
}
return unvisitedCount;
}
// Driver code
let arr = [3, 6, 2];
let k = 6;
console.log(unvisitedLeaves(arr, k));
Output
2
[Alternate Approach] By Sorting Frog Strengths - O(n * log(n) + k * log(k)) Time and O(k) Space
The idea is to sort the frog strengths so that duplicate strengths appear together. Then, process each unique strength only once by marking all its multiples as visited. While marking, keep reducing the count of unvisited leaves whenever a leaf is visited for the first time.
- Sort the frog strengths in non-decreasing order.
- Create a visited[] array and initialize the unvisited leaf count as k.
- Traverse each frog's strength.
- Skip the frog if its strength is 1, greater than k, or has already been processed.
- Mark all multiples of the current strength as visited, and decrement the unvisited count whenever a leaf is visited for the first time.
- Return the count of unvisited leaves.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// Function to count the number of unvisited leaves.
int unvisitedLeaves(vector<int> &arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
vector<bool> visited(k + 1, false);
// Initially, assume that all leaves are unvisited.
int unvisitedCount = k;
// Sort frog strengths to group duplicate strengths together.
sort(arr.begin(), arr.end());
// Process every frog one by one.
for (int strength : arr)
{
// If a frog has strength 1, it visits every leaf.
if (strength == 1)
return 0;
// Skip if the frog cannot visit any leaf.
if (strength > k)
continue;
// Skip duplicate strengths as they visit
// the same set of leaves.
if (visited[strength])
continue;
// Visit all leaves that are multiples
// of the frog's strength.
for (int leaf = strength; leaf <= k; leaf += strength)
{
// If the leaf is visited for the first time,
// decrement the count of unvisited leaves.
if (!visited[leaf])
unvisitedCount--;
visited[leaf] = true;
}
}
return unvisitedCount;
}
// Driver code
int main()
{
vector<int> arr = {3, 6, 2};
int k = 6;
cout << unvisitedLeaves(arr, k) << endl;
return 0;
}
import java.util.*;
class GFG {
// Function to count the number of unvisited leaves.
static int unvisitedLeaves(int[] arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
boolean[] visited = new boolean[k + 1];
// Initially, assume that all leaves are unvisited.
int unvisitedCount = k;
// Sort frog strengths to group duplicate strengths
// together.
Arrays.sort(arr);
// Process every frog one by one.
for (int strength : arr) {
// If a frog has strength 1, it visits every
// leaf.
if (strength == 1)
return 0;
// Skip if the frog cannot visit any leaf.
if (strength > k)
continue;
// Skip duplicate strengths as they visit
// the same set of leaves.
if (visited[strength])
continue;
// Visit all leaves that are multiples
// of the frog's strength.
for (int leaf = strength; leaf <= k;
leaf += strength) {
// If the leaf is visited for the first
// time, decrement the count of unvisited
// leaves.
if (!visited[leaf])
unvisitedCount--;
visited[leaf] = true;
}
}
return unvisitedCount;
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 3, 6, 2 };
int k = 6;
System.out.println(unvisitedLeaves(arr, k));
}
}
# Function to count the number of unvisited leaves.
def unvisitedLeaves(arr, k):
# Marking visited status for each leaf (1-indexed).
visited = [0] * (k + 1)
# Initially, assume that all leaves are unvisited.
unvisitedCount = k
# Sort frog strengths to group duplicate strengths together.
arr.sort()
# Process every frog one by one.
for strength in arr:
# If a frog has strength 1, it visits every leaf.
if strength == 1:
return 0
# Skip if the frog cannot visit any leaf.
if strength > k:
continue
# Skip duplicate strengths as they visit
# the same set of leaves.
if visited[strength]:
continue
# Visit all leaves that are multiples
# of the frog's strength.
for leaf in range(strength, k + 1, strength):
# If the leaf is visited for the first time,
# decrement the count of unvisited leaves.
if not visited[leaf]:
unvisitedCount -= 1
visited[leaf] = 1
return unvisitedCount
# Driver code
if __name__ == "__main__":
arr = [3, 6, 2]
k = 6
print(unvisitedLeaves(arr, k))
using System;
class GFG {
// Function to count the number of unvisited leaves.
static int unvisitedLeaves(int[] arr, int k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
bool[] visited = new bool[k + 1];
// Initially, assume that all leaves are unvisited.
int unvisitedCount = k;
// Sort frog strengths to group duplicate strengths
// together.
Array.Sort(arr);
// Process every frog one by one.
foreach(int strength in arr)
{
// If a frog has strength 1, it visits every
// leaf.
if (strength == 1)
return 0;
// Skip if the frog cannot visit any leaf.
if (strength > k)
continue;
// Skip duplicate strengths as they visit
// the same set of leaves.
if (visited[strength])
continue;
// Visit all leaves that are multiples
// of the frog's strength.
for (int leaf = strength; leaf <= k;
leaf += strength) {
// If the leaf is visited for the first
// time, decrement the count of unvisited
// leaves.
if (!visited[leaf])
unvisitedCount--;
visited[leaf] = true;
}
}
return unvisitedCount;
}
// Driver code
static void Main()
{
int[] arr = { 3, 6, 2 };
int k = 6;
Console.WriteLine(unvisitedLeaves(arr, k));
}
}
// Function to count the number of unvisited leaves.
function unvisitedLeaves(arr, k)
{
// visited[i] stores whether the ith leaf has been
// visited by any frog or not.
let visited = new Array(k + 1).fill(false);
// Initially, assume that all leaves are unvisited.
let unvisitedCount = k;
// Sort frog strengths to group duplicate strengths
// together.
arr.sort((a, b) => a - b);
// Process every frog one by one.
for (let strength of arr) {
// If a frog has strength 1, it visits every leaf.
if (strength === 1)
return 0;
// Skip if the frog cannot visit any leaf.
if (strength > k)
continue;
// Skip duplicate strengths as they visit
// the same set of leaves.
if (visited[strength])
continue;
// Visit all leaves that are multiples
// of the frog's strength.
for (let leaf = strength; leaf <= k;
leaf += strength) {
// If the leaf is visited for the first time,
// decrement the count of unvisited leaves.
if (!visited[leaf])
unvisitedCount--;
visited[leaf] = true;
}
}
return unvisitedCount;
}
// Driver code
let arr = [ 3, 6, 2 ];
let k = 6;
console.log(unvisitedLeaves(arr, k));
Output
2



