Solutions for Zig-Zag

    • @kristeencoupp91
    • @Ethan, submitted in Python 3, achived 100% score
    def solve(arr):  
        # Flag true indicates relation "<" is expected,  
        # else ">" is expected. The first expected relation  
        # is "<"  
        n = len(arr)  
        flag = True
        for i in range(n-1):  
            # "<" relation expected  
            if flag is True:  
                # If we have a situation like A > B > C,  
                # we get A > B < C  
                # by swapping B and C  
                if arr[i] > arr[i+1]:  
                    arr[i],arr[i+1] = arr[i+1],arr[i]  
                # ">" relation expected  
            else:  
                # If we have a situation like A < B < C,  
                # we get A < C > B  
                # by swapping B and C      
                if arr[i] < arr[i+1]:  
                    arr[i],arr[i+1] = arr[i+1],arr[i]  
            flag = bool(1 - flag)  
        return arr