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?

oflocon
  • 11
  • 4
  • Can you please clarify as to what exactly is the input and what you need to do with it? – shapiro yaacov Aug 10 '15 at 12:17
  • No idea what you mean by this question. pls clarify –  Aug 10 '15 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. – oflocon Aug 10 '15 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? – shapiro yaacov Aug 10 '15 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 – oflocon Aug 10 '15 at 12:54
  • Could you perhaps provide a link to it? – Rohcana Aug 10 '15 at 13:43
  • @oflocon Hope this helps, feel free for any queries. – Sumeet Aug 10 '15 at 13:45
  • @anchor this is not the original question but after gaining some insight into it the question reduced to this . – oflocon Aug 10 '15 at 14:59
  • @oflocon I do not understand, your comment contradicts your question. – Sumeet Aug 10 '15 at 18:16
  • yes ,i was taking about time comlexity – oflocon Aug 13 '15 at 13:09

1 Answers1

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.

Sumeet
  • 8,086
  • 3
  • 25
  • 45
  • 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 Aug 10 '15 at 15:07
  • @oflocon But you said that xi<=i and 4 is not <=1 and same applies to 2,3. – Sumeet Aug 10 '15 at 15:09
  • @oflocon Kindly clarify. – Sumeet Aug 10 '15 at 15:14
  • 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 . – oflocon Aug 11 '15 at 04:47
  • 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). – Sumeet Aug 12 '15 at 10:34
  • I don't see any duplicates here. – Sumeet Aug 12 '15 at 10:34
  • thank you for ur help but i am afraid that that i can't mark every start nd end in case N=10^5 ,2d array that large is not possible – oflocon Aug 13 '15 at 08:25
  • @oflocon ok, do you want a solution that minimizes space? It can be achieved. – Sumeet Aug 13 '15 at 08:38
  • since your solution needs marking pairs,if you can somehow mark then without using an array of 10^5*10^5 it would be awesome – oflocon Aug 13 '15 at 08:43
  • @oflocon using adjacency list instead of adjacency matrix greatly reduces the space required. – Sumeet Aug 13 '15 at 08:58
  • the complexity of above in O(n^2)? – oflocon Aug 13 '15 at 12:19
  • @oflocon are you talking about time complexity? – Sumeet Aug 13 '15 at 12:52