0

I have a list (up to 10,000 long) of numbers 0, 1, or 2. I need to see how many consecutive subsequences have a total which is NOT 1. My current method is to for each list do:

cons = 0
for i in range(seqlen+1):
    for j in range(i+1, seqlen+1):
        if sum(twos[i:j]) != 1:
            cons += 1

So an example input would be:

[0, 1, 2, 0]

and the output would be

cons = 8

as the 8 working subsequences are:

[0] [2] [0] [1,2] [2, 0] [0, 1, 2] [1, 2, 0] [0, 1, 2, 0]

The issue is that simply going through all these subsequences (the i in range, j in range) takes almost more time than is allowed, and when the if statement is added, the code takes far too long to run on the server. (To be clear, this is only a small part of a larger problem, I'm not just asking for the solution to an entire problem). Anyway, is there any other way to check faster? I can't think of anything that wouldn't result in more operations needing to happen every time.

Theo
  • 613
  • 4
  • 22

2 Answers2

1

I think I see the problem: your terminology is incorrect / redundant. By definition, a sub-sequence is a series of consecutive elements.

Do not sum every candidate. Instead, identify every candidate whose sum is 1, and then subtract that total from the computed quantity of all sub-sequences (simple algebra).

All of the 1-sum candidates are of the regular expression form 0*10*: a 1 surrounded by any quantity of 0s on either or both sides.

Identify all such maximal-length strings. FOr instance, in

210002020001002011

you will pick out 1000, 000100, 01, and 1. For each string compute the quantity of substrings that contain the 1 (a simple equation on the lengths of the 0s on each side). Add up those quantities. Subtract from the total for the entire input. There's you answer.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Whats the smartest way to pick out these 0-separated 1s? keeping in mind that just splitting at twos will not work as two consecutive ones must be split down the middle as well? – Theo Apr 11 '20 at 05:42
  • *"By definition, a sub-sequence is a series of consecutive elements."* - That's not true. I challenge you to find any book or website that defines it like that. – Kelly Bundy Apr 25 '23 at 14:02
  • @KellyBundy A simple browser search of the term brings up the general usage, for both operations and data. In particular, the data structure sequence is built with implicit contiguity. Also by general usage, a "sub" sequence would be a sequence in its own right. In any case, OP's example clarifies that the terms must be contiguous. – Prune Apr 26 '23 at 19:34
  • So you can't actually find any. And [Wikipedia](https://en.wikipedia.org/wiki/Subsequence?wprov=sfla1) for example shows already in its first paragraph the counter-example of being a subsequence of . – Kelly Bundy Apr 26 '23 at 19:47
-1

Use sliding window technique to solve these type of problem. Take two variable to track first and last to track the scope of window. So you start with sum equal to first element. If the sum is larger than required value you subtract the 'first' element from sum and increment sum by 1. If the sum is smaller than required you add next element of 'last' pointer and increment last by 1. Every time sum is equal to required increment some counter.

As for NOT, count number of sub-sequence having '1' sum and then subtract from total number of sub-sequence possible, i.e. n * (n + 1) / 2

Ashish Kumar
  • 184
  • 3