Login

All Problems

Problem title

Difficulty

Avg time to solve

Transitive Closure Of Directed Graph

Moderate

30 mins

Trie Delete Operation

Moderate

25 mins

Distribute Items

Moderate

31 mins

Optimal Ordering Of Tasks

Moderate

15 mins

Pairs Violating BST

Moderate

25 mins

Reach the last cell in the least time

Moderate

15 mins

Ninja's Contract

Hard

45 mins

Prime Permutations

Moderate

15 mins

Arithmetic Operators

Moderate

30 mins

Minimum And Maximum

Easy

15 mins

Problem

Submissions

0

Avg. time to solve

25 min

Success Rate

75%

Problem Statement

```
1. For every node, it is greater than or equal to its left child and less than or equal to its right child.
2. Node is greater than equal to the maximum value of its left subtree and less than the minimum value of its right subtree.
3. The maximum in the left sub-tree must be less than equal to the minimum in the right subtree.
```

```
The numbers of pairs violating BST property are:-
(1,5) : 1 should be greater than its left child value.
(4,3) : 4 should be less than its right child value.
(5,4),(5,3): Maximum of left subtree of 1 is 5 greater
than 2 and 3 of its right subtree.
```

```
The first line contains an integer 'T' which denotes the number of test cases. Then the test cases are as follows.
The first line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
```

```
For example, the input for the tree depicted in the above image would be :
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
```

```
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
```

```
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
```

```
For each test case, print a single line containing a single integer denoting the number of pairs violating the BST property.
The output of each test case will be printed in a separate line.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= 'T' <= 10
1 <= Number of nodes in Tree <= 5000
1 <= Value of nodes <= 10 ^ 5
Where 'T' is the number of test cases.
Time Limit: 1 sec.
```

```
1
8 4 3 2 -1 6 7 -1 -1 -1 -1 -1 -1
```

```
5
```

```
For the above input tree is:-
```

```
Thus pairs violating BST property are:-
(8,3) :- Greater than its right child.
(8,6) :- Greater than node on its right side.
(8,7) :- Greater than node on its right side.
(3,6):- The value of 3 is not more than its left child.
(4,3):- The value in the left subtree is greater than the
value of the right subtree.
Thus the number of pairs is 5.
```

```
2
35 44 26 -1 50 37 20 -1 -1 43 36 -1 -1 -1 -1 -1 -1
49 48 26 32 36 40 31 30 -1 25 40 -1 -1 -1 40 -1 -1 -1 -1 -1 -1 -1 -1
```

```
24
25
```

Console

Sample Test Case

Custom Test Case

Download Test Cases

Test Case 1

Test Case 2

Test Case 3

Saving Code...

Full Screen Mode

Change Language

Change Theme

Solution submission not allowed

Save Code

Reset Code