Find the count of unvisited indices in an infinite array
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 unrechable 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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count // of unvisited indices starting // from the index 0 int countUnvisited(int n, int m) { // Largest index that // cannot be visited int X = (m * n) - m - n; // Push the index to the queue queue<int> queue; queue.push(X); // To store the required count int count = 0; while (queue.size() > 0) { // Current index that cannot be visited int curr = queue.front(); queue.pop(); // Increment the count for // the current index count++; // (curr - m) and (curr - n) are also // unreachable if they are valid indices if (curr - m > 0) queue.push(curr - m); if (curr - n > 0) queue.push(curr - n); } // Return the required count return count; } // Driver code int main() { int n = 2, m = 5; cout << countUnvisited(n, m); return 0; } // This code is contributed by Sanjit_Prasad |
Java
// Java implementation of the approach import java.util.LinkedList; import java.util.Queue; class GFG { // Function to return the count // of unvisited indices starting // from the index 0 public static int countUnvisited(int n, int m) { // Largest index that // cannot be visited int X = (m * n) - m - n; // Push the index to the queue Queue<Integer> queue = new LinkedList<>(); queue.add(X); // To store the required count int count = 0; while (!queue.isEmpty()) { // Current index that cannot be visited int curr = queue.poll(); // Increment the count for // the current index count++; // (curr - m) and (curr - n) are also // unreachable if they are valid indices if (curr - m > 0) queue.add(curr - m); if (curr - n > 0) queue.add(curr - n); } // Return the required count return count; } // Driver code public static void main(String args[]) { int n = 2, m = 5; System.out.print(countUnvisited(n, m)); } } |
Python 3
# Python 3 implementation of the approach # Function to return the count # of unvisited indices starting # from the index 0 def countUnvisited(n, m): # Largest index that # cannot be visited i = 0 X = (m * n) - m - n # Push the index to the queue queue = [] queue.append(X) # To store the required count count = 0 while (len(queue) > 0): # Current index that cannot be visited curr = queue[0] queue.remove(queue[0]) # Increment the count for # the current index count += 1 # (curr - m) and (curr - n) are also # unreachable if they are valid indices if (curr - m > 0): queue.append(curr - m) if (curr - n > 0): queue.append(curr - n) # Return the required count return count # Driver code if __name__ == '__main__': n = 2 m = 5 print(countUnvisited(n, m)) # This code is contributed by Surendra_Gangwar |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Function to return the count // of unvisited indices starting // from the index 0 public static int countUnvisited(int n, int m) { // Largest index that // cannot be visited int X = (m * n) - m - n; // Push the index to the queue Queue<int> queue = new Queue<int>(); queue.Enqueue(X); // To store the required count int count = 0; while (queue.Count != 0) { // Current index that cannot be visited int curr = queue.Dequeue(); // Increment the count for // the current index count++; // (curr - m) and (curr - n) are also // unreachable if they are valid indices if (curr - m > 0) queue.Enqueue(curr - m); if (curr - n > 0) queue.Enqueue(curr - n); } // Return the required count return count; } // Driver code public static void Main(String []args) { int n = 2, m = 5; Console.WriteLine(countUnvisited(n, m)); } } // This code is contributed by PrinciRaj1992 |
2
Recommended Posts:
- Count of indices in an array that satisfy the given condition
- Count the pairs in an array such that the difference between them and their indices is equal
- Find the index of first 1 in an infinite sorted array of 0s and 1s
- Find all good indices in the given Array
- Find the sum of infinite series 1^2.x^0 + 2^2.x^1 + 3^2.x^2 + 4^2.x^3 +.......
- Find if the given number is present in the infinite sequence or not
- Magical Indices in an array
- Find the GCD of N Fibonacci Numbers with given Indices
- Find a permutation such that number of indices for which gcd(p[i], i) > 1 is exactly K
- Find minimum moves to reach target on an infinite line
- Check if the array can be sorted using swaps between given indices only
- Recursive program to find all Indices of a Number
- Leftmost and rightmost indices of the maximum and the minimum element of an array
- Find Surpasser Count of each element in array
- Find the Rotation Count in Rotated Sorted array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


