This file contains a list of integers. Let N(s) equal the number of contiguous subsequences within the list that have a sum of s .
Let T = N(11) + N(2) + N(9) + N(14). What are the last three digits of T?
Click here to open the file.
Details and assumptions
The sum of a single number is itself.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
very VERY clever
Let n be the number of integers in the list. Then for any particular integer k, calculating N(k) can be done in O(n log n) time.
Consider the list [3,4,-6,7,-8,9]. To aid in evaluating sums of contiguous subsequences, we can calculate the running sum from the leftmost number: for the given list, we get
[0, 0+3=3,3+4=7,7+(-6)=1,1+7=8,8+(-8)=0,0+9=9]
where the leftmost zero is used to represent the 'sum' of an empty sequence. Call this sequence S 0 , S 1 , . . . , S n To calculate N ( k ) , we can inspect for pairs of integers S i , S j such that i < j and S j − S i = k
A naive O ( n 2 ) proceeds by trying all n + 1 C 2 pairs ( S i , S j ) with i < j and checking if their difference is k .
An improvement to the above solution proceeds by inspecting each S i for i = 1 , . . . , n and counting how many integers S j there are with j < i and S j = S i − k . Counting all S j ′ s can be done in O(log n) time by inserting the elements of the runnning-sum sequence in a red-black tree.
Here is my code in C++ implementing the above solution:
#include< iostream>
#include< cstdio>
#include< map>
using namespace std;
int main(){
map<int,int> H;
H[0]=1;
int n;
int curr=0;
int a[]={11,2,9,14};
int ans=0;
while(true){
if (scanf("%d",&n)==-1) break;
curr+=n;
for(int i=0; i<4; i++){
int target=curr-a[i];
map<int,int>::iterator it=H.find(target);
if (it!=H.end()) ans+=it->second;
}
if (H.find(curr)==H.end()) H[curr]=1;
else H[curr]++;
}
cout<<ans<<endl;
}
My O( n 2 ) solution in Ruby is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Python 3.5.1:
#Summing Sequences
file = open("alist.txt", "r")
def N(alist,s):
count=0
for index1 in range(len(alist)):
for index2 in range(index1,len(alist)):
asum = sum(alist[i] for i in range(index1,index2+1))
if asum==s:
count+=1
return count
biglist = [int(line.strip()) for line in file]
print(N(biglist,11) + N(biglist,2) + N(biglist,9) + N(biglist,14))
This is another rolling/sliding window problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Solution in Python; will run 'len(list)' iterations over the list, taking into account larger subsets that sum up to the number (remember that 'sublists_sum(1,[-1,1,1]) == 3' because the contiguous subsequence {-1,1,1} sums up to 1).
def sublists_sum(number,list_of_numbers):
sequences = 0
list_len = len(list_of_numbers)
for i in range(0,list_len):
total_sum = 0
for z in range(i,list_len):
total_sum += list_of_numbers[z]
if (total_sum == number):
sequences += 1
return sequences
Solution in AutoHotkey
global file := A_Desktop "\nlist.txt" ; THE FILE WITH THE LIST
msgbox % N(11) + N(2) + N(9) + N(14)
return
N(N){
o := {}
loop, read, % file
o.Insert(A_LoopReadLine)
tl := o.MaxIndex() , ans := 0
loop % tl
{
i := A_index , s := 0
loop % tl-i+1
{
s += o[i+A_index-1]
if (s==N)
ans++
}
}
return ans
}
We use the following Python 2 code to make a generator for all the subsequences of a list:
def subsequences(li):
for length in xrange(1, len(li) + 1):
for start in xrange(0, len(li) - length + 1):
yield li[start : start + length]
Then, we can store the data to a list and finish the problem with:
import urllib2
ans = 0
data = map(int, urllib2.urlopen('https://gist.githubusercontent.com/brilliant-problems/e154b00845f6c922a4b7/raw/cec07a3ae48c7b6964b16ef82e37b4268367e240/cs_subsequence_sums').read().split())
for i in subsequences(data):
if (sum(i) == 9) or (sum(i) == 11) or (sum(i) == 2) or (sum(i) == 14):
ans += 1
print ans
Our final code is: http://pastebin.com/9ZNZwN33
Problem Loading...
Note Loading...
Set Loading...