Beautiful Arrangement II


  • 0
    C
    class Solution {
        public int[] constructArray(int n, int k) {
            int[] arr = new int[n];
            arr[0]= 1;
            int x=k, y=1;
            for (int i=1; i<=k;i++){
                arr[i] = arr[i-1] + x*y;
                x--;
                y=-y;
            }
            for (int i = k+1;i<n;i++){
                arr[i]=i+1;
            }
            return arr;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.