✅✅ 🔥🔥O(N) solution | Easy Explanation ( 3 cases + Line Sweep).
Line Sweep Technique:\n- Used to increment a range of index values of an array.\n\n## Intuition:\n- Add +2 to the count as we are allowed to have (i,j) and (j,i
## Line Sweep Technique:\n- Used to increment a range of index values of an array.\n\n## Intuition:\n- Add +2 to the count as we are allowed to have (i,j) and (j,i) as pairs and have d distance between them.\n- The mid of x and y is used to find the minimum distances between two points where x and y occurs between them.\n- We have three cases:\n- $$i<=x$$\n- $$x<i<mid$$\n- $$i>=mid$$\n## Example:\n\nNumbers till n: 1 2 3 4 5 6 7 8 9 10 11 12\nTake x=3, y=9\nThen mid= 6\nNow, Let us consider a case where We have to increment x from a range{l,r}\nSo, I used as ans[l]+=x ans[r]-=x\nFor i>=mid, we can directly implement above line.\n```\nCase-1: i<=x\nWe can go as usual till mid, but after mid we can go to any point\nwith minDist by using the x to y route.\nSo again we have following sequence of increments.\nRoute i to mid: ans[0]+=2 , ans[mid-i]-=2\nThen from to other elements would take a path from y.\n\nCase-3:i>=mid\nBasic increment, ans[i]+=2 , ans[n-i]-=2\n\nCase-2: i>x and i<mid \nA tricky case. Find a tricky point till which basic increment thing works.\ntricky point = mid+(i-x);\nFor elements after tricky point are visited by going to x and then going from y.\n\n```\n\n\n# Code\n```\nclass Solution {\npublic:\n    using ll=long long;\n    vector<long long> countOfPairs(int n, int x, int y) {\n        vector<long long> ans(n);\n        if(x>y)swap(x,y);\n        if(x==y){\n            for(int i=1;i<n;i++){\n                ans[0]+=2;\n                ans[n-i]-=2;\n            }\n            for(int i=1;i<n;i++)ans[i]+=ans[i-1];\n            return ans;\n        }\n        int mid=x+(y-x+1)/2;\n        for(int i=1;i<n;i++){\n            if(i>=mid){\n                ans[0]+=2;\n                ans[n-i]-=2;\n                continue;\n            }\n            if(i<=x){\n                ans[0]+=2;\n                ans[mid-i]-=2;\n\n                int reach_y=x-i+1;\n                ans[reach_y-1]+=2;\n                int yTon=reach_y + n-y;\n                if(yTon<n)\n                ans[yTon]-=2;\n\n                ans[reach_y]+=2;\n                ans[reach_y+y-mid-1]-=2;\n                continue;\n            }\n            int reach_y=i-x+1;\n            ans[reach_y-1]+=2;\n            int yTon=reach_y+n-y;\n            if(yTon<n)ans[yTon]-=2;\n\n            int trickypoint=mid+(i-x);\n            ans[0]+=2;\n            ans[trickypoint-i]-=2;\n\n            ans[reach_y]+=2;\n            ans[reach_y+y-trickypoint-1]-=2;\n\n        }\n        for(int i=1;i<n;i++){\n            ans[i]+=ans[i-1];\n        }\n        return ans;\n    }\n};\n```
['Line Sweep', 'Counting', 'C++']