Given one integer X and a possible probe sequence of N integers, the task is to detect if the given sequence of integers can be visited in the exact same order while searching for a given element X in a binary search tree. In other words, detect a valid probe sequence in a binary search tree. The output is “Yes” if the given probe sequence is possible to achieve, else “No”.
Note: Probe sequence is the sequence of nodes visited while searching for a particular element in a binary search tree.
Examples:
Input: X = 43, N = 6, arr[] = {2, 3, 50, 40, 60, 43}
Output: No
Explanation: 60 cannot occur after 40 as we have previously encountered 50 which was greater than X (43), so everything after 50 should be less than 50.Input: X = 43, N = 6, arr[] = {10, 65, 31, 48, 37, 43}
Output: Yes
Explanation: Since all elements less than 43 (10, 31, 37) are in increasing order, all elements greater than 43(65, 48) are in decreasing order and the last element is 43, therefore arr[] is a valid probe sequence
Approach: To solve the problem follow the below observations:
Observations:
Consider that we are searching for a given element X in a binary search tree, and we have encountered an element q while searching. Now according to the property of the binary search tree, we know that all the elements in the left subtree of q are less than q and all the elements in the right subtree of q are greater than q.
Case 1: X < q
- As we can see that q is greater than X that means we have to search among the elements that are less than q, in other words the left subtree of q. As we enter the left subtree of q, all the elements we encounter in the rest of the probe sequence must be less than q. So we can conclude that:
- Once we encounter an element q which is greater than X, all the other elements in the upcoming part of the probe sequence must be less than q, in other words, all the keys greater than the search key must be present in Descending order.
Case 2: X > q
- As we can see that q is less than X that means we have to search among the elements that are greater than q, in other words the right subtree of q. As we enter the right subtree of q, all the elements we encounter in the rest of the probe sequence must be greater than q. So we can conclude that:
- Once we encounter an element q which is less than X, all the other elements in the upcoming part of the probe sequence must be greater than q, in other words, all the keys less than the search key must be present in ascending order.
At last the probe sequence is valid if and only if we find our desired element X at the end of the search.
Conclusion from the above observations:
- All the keys greater than the search key must be present in Descending order.
- All the keys less than the search key must be present in Ascending order.
- The search key must be present at the last of the given sequence.
Follow the steps mentioned below to implement the above observation:
-
Initialize two variables that keeps track of the following two information
- The last element visited that was greater than the search key, initialize this to INT_MAX as we are going to check a decreasing sequence.
- The last element visited that was less than the search key, initialize this to INT_MIN as we are going to check an increasing sequence.
-
Traverse the given probe sequence from 0 to N-1.
- If the current element is less than X, check that it must be greater than the last element less than X we have already visited.
- If the above condition is satisfied, update the last smaller element visited to the current element and continue. Else it is not in increasing order, then return false.
- If the current element is greater than X, check that it must be less than the last element greater than X we have already visited.
- If the above condition is satisfied, update the last greater element visited to the current element and continue. Else it is not in decreasing order, then return false.
- The search key must be present at the last of the given sequence, put an if condition to check that.
Below is the implementation for the above approach:
// C++ code for the above approach: #include <bits/stdc++.h> using namespace std;
bool probe_sequence_is_valid( int X, int N, int * arr)
{ // Variable to keep track of last element
// visited that was greater than
// the search key
int last_greater_element = INT_MAX;
// variable to keep track of last element
// visited that was less than the search key
int last_smaller_element = INT_MIN;
bool is_valid = true ;
// Traverse the given probe sequence
for ( int i = 0; i < N; i++) {
// If the current element is less than X
if (arr[i] < X) {
// It must be greater than the last
// smaller element we visited, because
// the elements less than X must be
// in increasing order
if (arr[i] <= last_smaller_element)
return false ;
last_smaller_element = arr[i];
}
// If the current element is greater than X
else if (arr[i] > X) {
// It must be less than the last
// greater element we visited,
// because the elements greater than
// X must be in decreasing order
if (arr[i] >= last_greater_element)
return false ;
last_greater_element = arr[i];
}
}
// The search key must be present at
// the last of the given sequence
return arr[N - 1] == X;
} // Drivers code int main()
{ int X = 43;
int N = 6;
// For probe sequence 1
int arr1[N] = { 10, 65, 31, 48, 37, 43 };
bool is_valid = probe_sequence_is_valid(X, N, arr1);
string output1 = (is_valid) ? "Yes" : "No";
cout << output1 << endl;
// For probe sequence 2
int arr2[N] = { 2, 3, 50, 40, 60, 43 };
is_valid = probe_sequence_is_valid(X, N, arr2);
string output2 = (is_valid) ? "Yes" : "No";
cout << output2 << endl;
return 0;
} |
// Java code for the above approach: public class Main {
public static boolean probeSequenceIsValid( int X, int N, int [] arr) {
// Variable to keep track of the last element
// visited that was greater than
// the search key
int lastGreaterElement = Integer.MAX_VALUE;
// Variable to keep track of the last element
// visited that was less than the search key
int lastSmallerElement = Integer.MIN_VALUE;
boolean isValid = true ;
// Traverse the given probe sequence
for ( int i = 0 ; i < N; i++) {
// If the current element is less than X
if (arr[i] < X) {
// It must be greater than the last
// smaller element we visited, because
// the elements less than X must be
// in increasing order
if (arr[i] <= lastSmallerElement)
return false ;
lastSmallerElement = arr[i];
}
// If the current element is greater than X
else if (arr[i] > X) {
// It must be less than the last
// greater element we visited,
// because the elements greater than
// X must be in decreasing order
if (arr[i] >= lastGreaterElement)
return false ;
lastGreaterElement = arr[i];
}
}
// The search key must be present at
// the last of the given sequence
return arr[N - 1 ] == X;
}
public static void main(String[] args) {
int X = 43 ;
int N = 6 ;
// For probe sequence 1
int [] arr1 = { 10 , 65 , 31 , 48 , 37 , 43 };
boolean isValid1 = probeSequenceIsValid(X, N, arr1);
String output1 = (isValid1) ? "Yes" : "No" ;
System.out.println(output1);
// For probe sequence 2
int [] arr2 = { 2 , 3 , 50 , 40 , 60 , 43 };
boolean isValid2 = probeSequenceIsValid(X, N, arr2);
String output2 = (isValid2) ? "Yes" : "No" ;
System.out.println(output2);
}
} // this is contributed by uttamdp_10 |
def probe_sequence_is_valid(X, N, arr):
# Variable to keep track of last element
# visited that was greater than
# the search key
last_greater_element = float ( 'inf' )
# variable to keep track of last element
# visited that was less than the search key
last_smaller_element = float ( '-inf' )
is_valid = True
# Traverse the given probe sequence
for i in range (N):
# If the current element is less than X
if arr[i] < X:
# It must be greater than the last
# smaller element we visited, because
# the elements less than X must be
# in increasing order
if arr[i] < = last_smaller_element:
return False
last_smaller_element = arr[i]
# If the current element is greater than X
else :
# It must be less than the last
# greater element we visited,
# because the elements greater than
# X must be in decreasing order
if arr[i] > = last_greater_element:
return False
last_greater_element = arr[i]
# The search key must be present at
# the last of the given sequence
return arr[N - 1 ] = = X
# Driver code if __name__ = = '__main__' :
X = 43
N = 6
# For probe sequence 1
arr1 = [ 10 , 65 , 31 , 48 , 37 , 43 ]
is_valid = probe_sequence_is_valid(X, N, arr1)
output1 = "Yes" if is_valid else "No"
print (output1)
# For probe sequence 2
arr2 = [ 2 , 3 , 50 , 40 , 60 , 43 ]
is_valid = probe_sequence_is_valid(X, N, arr2)
output2 = "Yes" if is_valid else "No"
print (output2)
#Code addition by flutterfly
|
public class ProbeSequenceIsValid
{ public static bool IsValid( int X, int N, int [] arr)
{
// Variable to keep track of last element
// visited that was greater than
// the search key
int lastGreaterElement = int .MaxValue;
// variable to keep track of last element
// visited that was less than the search key
int lastSmallerElement = int .MinValue;
bool isValid = true ;
// Traverse the given probe sequence
for ( int i = 0; i < N; i++)
{ // If the current element is less than X
if (arr[i] < X)
{ // It must be greater than the last
// smaller element we visited, because
// the elements less than X must be
// in increasing order
if (arr[i] <= lastSmallerElement)
{
return false ;
}
lastSmallerElement = arr[i];
}
// If the current element is greater than X
else if (arr[i] > X)
{ // It must be less than the last
// greater element we visited,
// because the elements greater than
// X must be in decreasing order
if (arr[i] >= lastGreaterElement)
{
return false ;
}
lastGreaterElement = arr[i];
}
}
// The search key must be present at
// the last of the given sequence
return arr[N - 1] == X;
}
public static void Main( string [] args)
{
int X = 43;
int N = 6;
// For probe sequence 1
int [] arr1 = new int [] { 10, 65, 31, 48, 37, 43 };
bool isValid = IsValid(X, N, arr1);
string output1 = isValid ? "Yes" : "No" ;
Console.WriteLine(output1);
// For probe sequence 2
int [] arr2 = new int [] { 2, 3, 50, 40, 60, 43 };
isValid = IsValid(X, N, arr2);
string output2 = isValid ? "Yes" : "No" ;
Console.WriteLine(output2);
}
} |
function probeSequenceIsValid(X, N, arr) {
// Variable to keep track of the last element
// visited that was greater than
// the search key
let lastGreaterElement = Number.MAX_VALUE;
// Variable to keep track of the last element
// visited that was less than the search key
let lastSmallerElement = Number.MIN_VALUE;
let isValid = true ;
// Traverse the given probe sequence
for (let i = 0; i < N; i++) {
// If the current element is less
if (arr[i] < X) {
// It must be greater than the last
// smaller element we visited, because
// the elements less than X must be
// in increasing order// If the current element is less than X
if (arr[i] <= lastSmallerElement)
return false ;
lastSmallerElement = arr[i];
}
// If the current element is greater than X
else if (arr[i] > X) {
// It must be less than the last
// greater element we visited,
// because the elements greater than
// X must be in decreasing order
if (arr[i] >= lastGreaterElement)
return false ;
lastGreaterElement = arr[i];
}
}
// The search key must be present at
// the last of the given sequence
return arr[N - 1] === X;
} const X = 43; const N = 6; // For probe sequence 1 const arr1 = [10, 65, 31, 48, 37, 43]; const isValid1 = probeSequenceIsValid(X, N, arr1); const output1 = (isValid1) ? "Yes" : "No" ;
console.log(output1); // For probe sequence 2 const arr2 = [2, 3, 50, 40, 60, 43]; const isValid2 = probeSequenceIsValid(X, N, arr2); const output2 = (isValid2) ? "Yes" : "No" ;
console.log(output2); // by phasing17 |
Yes No
Time Complexity: O(N)
Auxiliary Space: O(1)