Find a number in minimum steps | Java program to find a number in minimum steps ; To represent data of a node in tree ; Prints level of node n ; Create a queue and insert root ; Do level order traversal ; Remove a node from queue ; To astatic void infinite loop ; Check if dequeued number is same as n ; Insert children of dequeued node to queue ; Driver code
import java . util . * ; class GFG { static final int InF = 99999 ; static class number { int no ; int level ; number ( ) { } number ( int n , int l ) { this . no = n ; this . level = l ; } } ; static void findnthnumber ( int n ) { Queue < number > q = new LinkedList < > ( ) ; number r = new number ( 0 , 1 ) ; q . add ( r ) ; while ( ! q . isEmpty ( ) ) { number temp = q . peek ( ) ; q . remove ( ) ; if ( temp . no >= InF temp . no <= - InF ) break ; if ( temp . no == n ) { System . out . print ( " Found ▁ number ▁ n ▁ at ▁ level ▁ " + ( temp . level - 1 ) ) ; break ; } q . add ( new number ( temp . no + temp . level , temp . level + 1 ) ) ; q . add ( new number ( temp . no - temp . level , temp . level + 1 ) ) ; } } public static void main ( String [ ] args ) { findnthnumber ( 13 ) ; } }