All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > General
String length differences (Posted on 2005-02-15) Difficulty: 3 of 5
Consider the sequence 0100111100
There are 5 zeroes and 5 ones in this sequence.
The longest string of ones is 4.
The longest string of zeroes is 2.
The difference in string lengths is 2.

Create a sequence consisting of 100 zeroes and 100 ones which maximizes the difference in lengths between the longest string of 1's and the longest string of 0's.

Find a formula for the maximum difference in string lengths with n zeroes and n ones.

See The Solution Submitted by Jer    
Rating: 4.0000 (3 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution Without peeking | Comment 3 of 5 |

I'm going to assume Charlie's solution is correct, but here's my try anyway.   I get 82.

I'm going to do this in steps.  First imagine all the 1's grouped together in the center of all the zeroes:
0's, 1's, 0's
n/2, n, n/2  stringlengthdiff:  n-n/2

0's, 1's, 0's, 1's, 0's
n/3, 1, n/3, n-1, n/3    stringlengthdiff: (n-1) - n/3

0's, 1's, 0's, 1's, 0's, 1's, 0's
n/4, 1, n/4, 1, n/4, n-2, n/4   stringlengthdiff: (n-2) - n/4

So each time I break off one more 1 to split up the bunches of zeroes.  Make the bunches of zeroes all the same, since if not then there would be a longer string of zeroes and that would decrease our string length difference.

So there is some i such that stringlengthdiff is maximized.
stringlengthdiff  = (n-i) - n/(i+2)
take derivative with respect to i and set to zero
d/di(stringlengthdiff) = -1 + n/(i+2)^2  = 0 if
      n = (i+2)^2
  i = sqrt(n) -2

stringlengthdiff = n - 2*sqrt(n) +2

which for n=100 is 82
the formula would require some tweaking if sqrt(n) didn't happen to be an integer


  Posted by Larry on 2005-02-17 21:01:07
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (16)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information