One pass - 0 ms solution - No Two pointers
IntuitionPattern based, do some examples and you'll understancdApproachStep 1: If k=1, you need to either keep increasing/decreasing the values based upon the m
# Intuition
Pattern based, do some examples and you'll understancd

# Approach
**Step 1:** If k=1, you need to either keep increasing/decreasing the values based upon the maxima value.

**Step 2:** Else, you need keep altering your next value.
ie. if you are currently a maxima then next value should be a minima (less than current), but at what distance?
To get distance you use a distance which you +/- to current value base on minima/maxima respectively.
After finding next value you reverse your maxima/minima and decrement the k and distance till it becomes 1. Then you do step 1.

# Complexity
- Time complexity:
O(n)

- Space complexity:
O(1)

# Code
```cpp []
class Solution {
public:
    vector<int> constructArray(int n, int k) {
        int isMaxima=0,distance=n-1,i;
        vector<int>ans(n);
        ans[0]=1;
        for(i=0;i<n-1;){
            if(k>1){
                ans[i+1]=ans[i]+((isMaxima)?-distance:+distance);
                k--;
                isMaxima=!isMaxima;
                distance--;
                i++;
            }
            if(k==1)
                break; 
        }
        distance=((isMaxima)?-1:1);
        for(;i<n-1;i++)
            ans[i+1]=ans[i]+distance;
        return ans;
    }
};
```
['C++']