1 1 shinchan Asked: November 21, 20212021-11-21T16:09:12+05:30 2021-11-21T16:09:12+05:30In: Other CODECHEF ASSIGNMENT PROBLEM WEEK 7: 1 1 CODECHEF ASSIGNMENT PROBLEM WEEK 7: Share Facebook 1 Answer Voted Oldest Recent shinchan 2021-11-21T16:09:44+05:30Added an answer on November 21, 2021 at 4:09 pm This answer was edited. #include <iostream> #include <unordered_map> using namespace std; void countDistinct(int arr[], int k, int n) { unordered_map<int, int> hm; int dist_count = 0; for (int i = 0; i < k; i++) { if (hm[arr[i]] == 0) { dist_count++; } hm[arr[i]] += 1; } cout << dist_count<<" "; for (int i = k; i < n; i++) { if (hm[arr[i - k]] == 1) { dist_count--; } hm[arr[i - k]] -= 1; if (hm[arr[i]] == 0) { dist_count++; } hm[arr[i]] += 1; cout << dist_count<<" "; } cout<<endl; } int main() { int t; cin>>t; while(t--){ int n,k; cin>>n>>k; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; } countDistinct(a,k,n); } return 0; } Leave an answerCancel replyYou must login to add an answer. Username or email* Password* Remember Me! Forgot Password?
1 Answer