A graph consisting of n nodes, where there is an edge from 1 to 2, 2 to 3, 3 to 4, ........, n-1 to n.
Now, there is an array consisting of permutation of 1 to n and there are number of queries given based on array segments. Determine number of connected components formed by the nodes(indicated by the array elements) for the given segment. For e.g.,
Array : 4 5 3 2 1 Queries are : [1, 5], [1, 4], [2, 4]
For [1, 5], array elements are 1 2 3 4 5 and all are connected and forms a single connected component.
For [1, 4], array elements are 2 3 4 5, they also forms a single connected component.
For [2, 4], array elements are 2 3 5, so 2 and 3 forms a single component and 5 forms a single component so total of 2 connected components are there in [2, 4].