You’re given an array say for example arr = [0,1,-1,2,1,5]; Write a logic to determine whether the given array is in zig-zag pattern or not.
Explanation:
Array starts with 0
Then goes upward (U) direction 1
Then goes downward (D) direction -1
Then goes upward (U) direction 2
Then goes downard (D) direction 1
Then goes upward (U) direction 5
Here pattern is U -> D -> U -> D -> U, so it is in Zig-Zag pattern.
Example 2: Consider array arr = [0,1,-1,2,3,1]
Array Starts with 0
Then goes upward (U) direction 1
Then goes downward (D) direction -1
Then goes upward (U) direction 2
Then goes upward (U) direction 3
Then goes downard (D) direction 1
Here pattern is U -> D -> U -> U -> D, so it is NOT Zig-Zag pattern as we have got two updward direction together.
Same would be the case if we have two same direction together.
You can code in language of your choice.