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 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++
// 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 |
Javascript
<script> // JavaScript implementation of the approach // Function to return the count // of unvisited indices starting // from the index 0 function countUnvisited(n, m) { // Largest index that // cannot be visited let X = (m * n) - m - n; // Push the index to the queue let queue = []; queue.push(X); // To store the required count let count = 0; while (queue.length > 0) { // Current index that cannot be visited let curr = queue[0]; 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 let n = 2, m = 5; document.write(countUnvisited(n, m)); // This code is contributed by shinjanpatra </script> |
Output:
2
Time Complexity: O(n * m)
Auxiliary Space: O(1)
Please Login to comment...