Created
October 18, 2023 10:11
-
-
Save bunnykek/76f79706cdd05750bdc00f8c0b8da726 to your computer and use it in GitHub Desktop.
good array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An array A[i] of size N and a single integer K are given.You must create a good array G[i] of size N.The good array G[i] is the array in which the i ^ (th) elementdenotes the number of elements of the array present betweenrange A[i] - K, A[i] + K ] (inclusively). | |
// Find the good array G[i] of size N. | |
vector<int> solve(vector<int> &v, int &k){ | |
auto s = v; | |
sort(s.begin(), s.end()); | |
vector<int> ans; | |
for(auto num: v){ | |
auto lp = lower_bound(s.begin(), s.end(), num-k) - s.begin(); | |
auto hp = lower_bound(s.begin(), s.end(), num+k+1) - s.begin(); | |
ans.push_back(hp-lp); | |
} | |
return ans; | |
} | |
int main() { | |
int n ,k; | |
cin>>n>>k; | |
vector<int> v(n); | |
for(int i=0; i<n; i++) cin>>v[i]; | |
for(auto num: solve(v, k)) cout<<num<<", "; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment