6 kyu

Zeros and Ones

449 of 811kodejuice

Description:

Given an array containing only zeros and ones, find the index of the zero that, if converted to one, will make the longest sequence of ones.

For instance, given the array:

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

replacing the zero at index 10 (counting from 0) forms a sequence of 9 ones:

[1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1]
                  '------------^------------'

Your task is to complete the function that determines where to replace a zero with a one to make the maximum length subsequence.

Notes:

  • If there are multiple results, return the last one:

[1, 1, 0, 1, 1, 0, 1, 1] ==> 5

  • The array will always contain only zeros and ones.

Can you do this in one pass?

Puzzles
Logic
Algorithms

Stats:

CreatedNov 6, 2017
PublishedNov 26, 2017
Warriors Trained2416
Total Skips90
Total Code Submissions7973
Total Times Completed811
JavaScript Completions449
Python Completions346
Ruby Completions38
Total Stars81
% of votes with a positive feedback rating93% of 182
Total "Very Satisfied" Votes159
Total "Somewhat Satisfied" Votes20
Total "Not Satisfied" Votes3
Total Rank Assessments4
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • kodejuice Avatar
  • anter69 Avatar
  • myjinxin2015 Avatar
  • KenKamau Avatar
  • hobovsky Avatar
Ad