Problem 16. Return the largest number that is adjacent to a zero
This example comes from Steve Eddins' blog: Learning Lessons from a One-Liner
Write a function that takes a list or array of numbers as input and return the largest number that is adjacent to a zero.
Example:
Input x = [1 5 3 0 2 7 0 8 9 1 0] Output y is 8
This problem was originally posed by Greg Wilson of Software Carpentry.
Solution Stats
Problem Comments
-
4 Comments
Two more tests:
1. Input x = [-4 -2 0 -4 -1 0]
Output y is -1.
2. Input x = [-9 -3 0 0 -5 0 -2 -1]
Output y is -2.
Good comment, Gaik. A lot of solutions (including mine) will break with two consecutive zeros.
Why: "Input x = [-9 -3 0 0 -5 0 -2 -1] Output y is -2."???
Output should be 0. It doesn't say anywhere that the number adjacent to a zero must be nonzero!
This was challenging
Solution Comments
Show commentsProblem Recent Solvers5507
Suggested Problems
-
Maximum running product for a string of numbers
2251 Solvers
-
How to find the position of an element in a vector without using the find function
2806 Solvers
-
Given an unsigned integer x, find the largest y by rearranging the bits in x
1978 Solvers
-
We love vectorized solutions. Problem 1 : remove the row average.
882 Solvers
-
Fahrenheit to Celsius converter
596 Solvers
More from this Author96
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!