Find the count of unvisited indices in an infinite array
Last Updated :
30 Jun, 2022
Given an array of infinite length and two integers M and N which are co-primes, the task is to find the number of positions that cannot be visited starting from the first position when in a single move from arr[i], either arr[i + M] or arr[i + N] can be reached. Note that the result is always finite.
Examples:
Input: M = 2, N = 5
Output: 2
From index 0, the indices that can be visited are
0 + 2 = 2
0 + 2 + 2 = 4
0 + 5 = 5
0 + 2 + 2 + 2 = 6
0 + 2 + 5 = 7
0 + 2 + 2 + 2 + 2 = 8
0 + 2 + 2 + 5 = 9
0 + 5 + 5 = 10
…
1 and 3 are the only indices that cannot be visited.
Input: M = 5, N = 6
Output: 15
Approach:
- Find the largest index that can’t be obtained using any combination of M & N using Frobenius number say X = (M * N) – M – N .
- Since, X is the largest index than cannot be visited so every index greater than it doesn’t need to be checked.
- Now, for the indices smaller than X, if X is unvisited then Y = X – M and Z = X – N are also unreachable and same goes Y – M and Z – N and so on.. until the indices are greater than 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countUnvisited( int n, int m)
{
int X = (m * n) - m - n;
queue< int > queue;
queue.push(X);
int count = 0;
while (queue.size() > 0)
{
int curr = queue.front();
queue.pop();
count++;
if (curr - m > 0)
queue.push(curr - m);
if (curr - n > 0)
queue.push(curr - n);
}
return count;
}
int main()
{
int n = 2, m = 5;
cout << countUnvisited(n, m);
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
class GFG {
public static int countUnvisited( int n, int m)
{
int X = (m * n) - m - n;
Queue<Integer> queue = new LinkedList<>();
queue.add(X);
int count = 0 ;
while (!queue.isEmpty()) {
int curr = queue.poll();
count++;
if (curr - m > 0 )
queue.add(curr - m);
if (curr - n > 0 )
queue.add(curr - n);
}
return count;
}
public static void main(String args[])
{
int n = 2 , m = 5 ;
System.out.print(countUnvisited(n, m));
}
}
|
Python 3
def countUnvisited(n, m):
i = 0
X = (m * n) - m - n
queue = []
queue.append(X)
count = 0
while ( len (queue) > 0 ):
curr = queue[ 0 ]
queue.remove(queue[ 0 ])
count + = 1
if (curr - m > 0 ):
queue.append(curr - m)
if (curr - n > 0 ):
queue.append(curr - n)
return count
if __name__ = = '__main__' :
n = 2
m = 5
print (countUnvisited(n, m))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static int countUnvisited( int n, int m)
{
int X = (m * n) - m - n;
Queue< int > queue = new Queue< int >();
queue.Enqueue(X);
int count = 0;
while (queue.Count != 0)
{
int curr = queue.Dequeue();
count++;
if (curr - m > 0)
queue.Enqueue(curr - m);
if (curr - n > 0)
queue.Enqueue(curr - n);
}
return count;
}
public static void Main(String []args)
{
int n = 2, m = 5;
Console.WriteLine(countUnvisited(n, m));
}
}
|
Javascript
<script>
function countUnvisited(n, m)
{
let X = (m * n) - m - n;
let queue = [];
queue.push(X);
let count = 0;
while (queue.length > 0)
{
let curr = queue[0];
queue.pop();
count++;
if (curr - m > 0)
queue.push(curr - m);
if (curr - n > 0)
queue.push(curr - n);
}
return count;
}
let n = 2, m = 5;
document.write(countUnvisited(n, m));
</script>
|
Time Complexity: O(n * m)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...