Find the count of unvisited indices in an infinite array | C # implementation of the above approach ; Function to return the count of unvisited indices starting from the index 0 ; Largest index that cannot be visited ; Push the index to the queue ; To store the required count ; Current index that cannot be visited ; Increment the count for the current index ; ( curr - m ) and ( curr - n ) are also unreachable if they are valid indices ; Return the required count ; Driver code
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 ) ) ; } }