Check if the given Prufer sequence is valid or not
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 valid bool 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 code int 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; } |
chevron_right
filter_none
Java
// Java implementation of the approach import java.io.*; class GFG { // Function that returns true if // given Prufer sequence is valid static 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 code 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"); } } // This code is contributed by anuj_67.. |
chevron_right
filter_none
Python3
# Python3 implementation of the approach # Function that returns true if # given Prufer sequence is valid def 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 code if __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 |
chevron_right
filter_none
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if // given Prufer sequence is valid static 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 code public 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 |
chevron_right
filter_none
Output:
Valid
Recommended Posts:
- Check if any valid sequence is divisible by M
- Print the degree of every node from the given Prufer sequence
- Print the node with the maximum degree in the prufer sequence
- Program to check if a date is valid or not
- Check if the given Binary Expressions are valid
- Check whether Quadrilateral is valid or not if angles are given
- Check whether the triangle is valid or not if angles are given
- Check if the given push and pop sequences of Stack is valid or not
- Check if the number is valid when flipped upside down
- Check whether right angled triangle is valid or not for large sides
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Check if there is a root to leaf path with given sequence
- Prufer Code to Tree Creation
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- k-th missing element in increasing sequence which is not present in a given sequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



