16
3
Introduction
I want to find the substring with the most 1
's in a sequence of 0
's and 1
's.
Input
Your program has two inputs, the sequence and the substring length.
The sequence is any number of 0
's and 1
's:
01001010101101111011101001010100010101101010101010101101101010010110110110
The substring length is any positive non-zero integer:
5
Output
Your program should output the starting index of the first substring of the given length that contains the most 1
's. With the above input, the output is:
10
The first character in the string starts at an index of 0
.
Scoring
Shortest code wins!
Rules
- Your program must always output the correct index for any valid inputs.
- You can pick your input/output method from any answer with positive score on the default options. Please specify the method you pick in your answer.
Your title and introduction says "find the substring with the most 1's". But your program description says you are giving a substring length and looking for the index of the first substring. So should we assume that the title and introduction are wrong? Most people seem to be solving the first part. Who wins? – swstephe – 2015-01-23T17:43:56.513
@swstephe I'm not sure I understand your confusion. If there are more than one substring tied for the most
1
's, you output the first substring you found. You identify the substrings with the index of the first character in that substring. Does that help? – hmatt1 – 2015-01-23T17:52:15.097Okay, so you are breaking the sequence in substrings and returning the index of the first substring with the most 1's? It sounded like you were looking for substrings of 1's. – swstephe – 2015-01-23T17:58:58.480
Does requirement "must always output the correct index for any given inputs" still apply if we give infeasible lengths, e.g. length=99? – smci – 2015-01-27T03:52:35.383
@smci you can assume for valid input. You don't have to handle a case where the substring length is longer than the sequence. – hmatt1 – 2015-01-28T17:00:32.950