Write a function longest_run that takes as input an array of 0's and 1's and outputs the length and index of the longest consecutive run of either 0's or 1's. If there are multiple runs of the same length, it should output the first occurence.
Examples:
1. longest_run([1 1 0 1]) should output [2 1], since the longest consecutive run is 11, which starts at index 1
2. longest_run([1 1 0 1 0 0 0 0 1 1 1]) should ouptut [4 5], since the longest consecutive run is 0000, which starts at index 5
3. longest_run([1 0]) should ouptut [1 1], since the first longest consecutive run is 1, which starts at index 1
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers90
Suggested Problems
-
1832 Solvers
-
Matrix indexing with two vectors of indices
781 Solvers
-
Get the elements of diagonal and antidiagonal for any m-by-n matrix
519 Solvers
-
Calculate the Number of Sign Changes in a Row Vector (No Element Is Zero)
955 Solvers
-
538 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!