0

Given an array of length n, for every index i have a integer xi (xi<=i). I need to count all continous subsequences that include these two indices also there should be no repetition for eg. I have counted the subsequence [1,4] for say (i=4 && x4=0), then if for next (i=5 && x5=1) I should not include the same continous sequence twice. I need to find count of all those subsequences. I tried brute force approach which wasn't sufficient to beat the time.

Can I have any better approach? Possibly O(NLOGN) or less than that?

10
  • Can you please clarify as to what exactly is the input and what you need to do with it? Commented Aug 10, 2015 at 12:17
  • No idea what you mean by this question. pls clarify Commented Aug 10, 2015 at 12:26
  • I mean for every index i ,I have a corresponding index x[i](0<=x[i]<=i),I need to count the number of distinct continious segments which should include both these indices for eg . i have a array a[]={1,2,3,4,5} and the x[]={0,0,2,1,2} now for i=0 , i should count the segments {1,2,3,4,5},{1,2,3,4}.{1,2,3},{1,2},{1} a total of 5. for i=1 ,there a no segments which include index(0 and 1)x[i] and i ,which are not counted before total=5+0=5,and so on i need total for all indices .hope this makes it clear. Commented Aug 10, 2015 at 12:46
  • I am really sorry, but I still have no idea what you're talking about. why when i=0 should I count 5 segments, but for i=1 there are none? Commented Aug 10, 2015 at 12:51
  • for i=0,i should include both x[0]=0 and 0 (the same ) index in my subarrays that means i count all subarrays which include index 0, when we come to i=1 we should include i=1 and x[1]=0 (indices 0 and 1) that means include index 0 and 1 and count the subarrays but all that subrrays are already counted so here i have no additions Commented Aug 10, 2015 at 12:54

1 Answer 1

1

The solution is pretty simple and straightforward.

Say we are doing this for index i.

We generate the subsequences by extending our subsequence outside the limits(indices) x[i] and i because the subsequence will always take elements of array from x[i] to i and if we only expand, our subsequnce will always have the indices x[i] and i.

But of course, we will also cover the obvious subsequence from x[i] to i which is the very first subsequence.

EDIT:To avoid duplicates, we must check whether the given combination of left and right boundaries have been tried or not.

For this we will make an adjacency list which will contain

N linked lists.

and all linked lists are initially empty.

Now a given subsequence with corresponding left and right boundaries have not been tried before if and only if

linked list arr[left] does not contain element right. 

If linked list arr[left] contains element right then it means the subsequence has been printed before.

First we fix left boundary of our subsequence to be x[i] and then with new left boundary we try all possible new right boundaries which are:

i,i+1,i+2 ....... N-1 , N is equal to length of array a. Corresponding subsequenes being if(a[j] linked list does not contain i) { print the subsequence a[j],a[j+1],......a[i] add i to arr[j] } if(a[j] linked list does not contain i+1) { print the subsequence a[j],a[j+1],.........a[i+1] add i+1 to a[j] } and similar if condition before all subsequences given below. a[j],a[j+1],...............a[i+2] . . a[j],a[j+1]..........................a[N-1] j is x[i] for the above subsequences. 

Then we fix left boundary to be x[i]-1 and then with new left boundary we try all possible new right boundaries which are:

i,i+1,i+2 ....... N-1 

Corresponding subsequenes being

similar if condition as given above before all subsequences given below. and they will be printed if and only if the condition is true. a[j],a[j+1],.........a[i] a[j],a[j+1],...............a[i+1] . . a[j],a[j+1]..........................a[N-1] j is x[i]-1 for the above subsequences. 

We do this till j becomes 0 and that will be the last iteration.

Now coming to the efficiency of this algorithm, because at every step I am generating a new subsequence,and none of the steps go wasted in producing a subsequence that does not include both indices so I think it is pretty much efficient.

Sign up to request clarification or add additional context in comments.

12 Comments

unfortunately,this will not do the work.say for pair(i,x[i]) =(1 ,4) and (j,x[j])=(2,3) i think that the above algorithm will get wrong solution,won't it include some repetitions ,but i dont want to count a sequence twice .
@oflocon But you said that xi<=i and 4 is not <=1 and same applies to 2,3.
@oflocon Kindly clarify.
oh sorry ,i meant (x[i],i)=(1,4) and (x[j],j)=(2,3) in this case when you count the sequences for i=3 by your method and then go to i=4 ,it will already have some of it before .
There will be no duplicates my friend, even when u take the pair (1,4), the output will be exactly two subsequences (2,3,4,5) and (1,2,3,4,5).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.