Beautiful arrangement || C++ Fast and slow pointer approach - easy best explanation
class Solution {\npublic:\n\n    vector constructArray(int n, int k) {\n        \n        //vector having the beautiful arrangement of numbers\n        vector r
class Solution {\npublic:\n\n    vector<int> constructArray(int n, int k) {\n        \n        //vector having the beautiful arrangement of numbers\n        vector<int> result(n);\n        \n        //we are solving the array by dividing it into two parts \n        // covering till k+1 and after that simply incrementing the number and storing it.\n        for(int i = 0 , a = 1 , z = k+1 ; i <= k; i++)\n        {\n            \n            //here we are dealing with the extremes and the maximum and unique differences can be calculated using the extremes only\n            //for ex : [1,2,3,4,5]\n            //max diff of 4 can be from (1,5) max diff of 3 can be from (1,4) OR (2,5)\n            //max diff of 2 can be from (1,3) OR (2,5) and so on\n            //here i%2 is being used to alternatively choose one extreme\n            result[i] = i % 2 ? z-- : a++;\n        }\n        \n        //after covering till k+1 elements we have found all the distinct differences and for the rest of elements of array , we can simply add them to the last\n        for(int i = k+1; i < n; i++)\n        {\n            result[i] = i + 1;\n        }\n        \n        //now return the resultant array\n        return result;\n    }\n};
[]