Given a Prufer sequence of N integers, the task is to check if the given sequence is a valid Prufer sequence or not.
Examples:
Input: arr[] = {4, 1, 3, 4}
Output: Valid
The tree is:
2----4----3----1----5
|
6
Input: arr[] = {4, 1, 7, 4}
Output: Invalid
Approach: Since we know the Prufer sequence is of length N – 2 where N is the number of vertices. Hence we need to check if the Prufer sequence consists of elements which are in the range [1, N].
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function that returns true if// given Prufer sequence is validbool isValidSeq(int a[], int n){ int nodes = n + 2; // Iterate in the Prufer sequence for (int i = 0; i < n; i++) { // If out of range if (a[i] < 1 || a[i] > nodes) return false; } return true;}// Driver codeint main(){ int a[] = { 4, 1, 3, 4 }; int n = sizeof(a) / sizeof(a[0]); if (isValidSeq(a, n)) cout << "Valid"; else cout << "Invalid"; return 0;} |
Java
// Java implementation of the approachimport java.io.*;class GFG{// Function that returns true if// given Prufer sequence is validstatic boolean isValidSeq(int []a, int n){ int nodes = n + 2; // Iterate in the Prufer sequence for (int i = 0; i < n; i++) { // If out of range if (a[i] < 1 || a[i] > nodes) return false; } return true;}// Driver codepublic 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");}}// This code is contributed by anuj_67.. |
Python3
# Python3 implementation of the approach# Function that returns true if# given Prufer sequence is validdef isValidSeq(a, n) : nodes = n + 2; # Iterate in the Prufer sequence for i in range(n) : # If out of range if (a[i] < 1 or a[i] > nodes) : return False; return True;# Driver codeif __name__ == "__main__" : a = [ 4, 1, 3, 4 ]; n = len(a); if (isValidSeq(a, n)) : print("Valid"); else : print("Invalid"); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System; class GFG{// Function that returns true if// given Prufer sequence is validstatic Boolean isValidSeq(int []a, int n){ int nodes = n + 2; // Iterate in the Prufer sequence for (int i = 0; i < n; i++) { // If out of range if (a[i] < 1 || a[i] > nodes) return false; } return true;}// Driver codepublic static void Main (String[] args){ int []a = { 4, 1, 3, 4 }; int n = a.Length; if (isValidSeq(a, n)) Console.WriteLine( "Valid"); else Console.WriteLine( "Invalid");}}// This code has been contributed by 29AjayKumar |
Javascript
<script>// Javascript implementation of the approach// Function that returns true if// given Prufer sequence is validfunction isValidSeq( a, n){ var nodes = n + 2; // Iterate in the Prufer sequence for (var i = 0; i < n; i++) { // If out of range if (a[i] < 1 || a[i] > nodes) return false; } return true;}// Driver codevar a = [ 4, 1, 3, 4 ];var n = a.length;if (isValidSeq(a, n)) document.write( "Valid" );else document.write( "Invalid");// This code is contributed by itsok.</script> |
Valid
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA


