Check if the given Prufer sequence is valid or not | Java implementation of the approach ; Function that returns true if given Prufer sequence is valid ; Iterate in the Prufer sequence ; If out of range ; Driver code
import java . io . * ; class GFG { static boolean isValidSeq ( int [ ] a , int n ) { int nodes = n + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 a [ i ] > nodes ) return false ; } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = a . length ; if ( isValidSeq ( a , n ) ) System . out . println ( " Valid " ) ; else System . out . print ( " Invalid " ) ; } }