fork download
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6. // } Driver Code Ends
  7.  
  8. class Solution {
  9. public:
  10. int maxDistance(vector<int> &arr) {
  11. int n=arr.size();
  12. unordered_map<int,int>mp;
  13. int maxi=0;
  14. for(int i=0;i<n;i++){
  15. if(mp.find(arr[i])!=mp.end()){
  16. maxi=max(maxi,i-mp[arr[i]]);
  17. }else{
  18. mp[arr[i]]=i;
  19. }
  20. }
  21. return maxi;
  22. }
  23. };
  24.  
  25.  
  26. //{ Driver Code Starts.
  27.  
  28. int main() {
  29. int t;
  30. cin >> t;
  31. cin.ignore();
  32. while (t--) {
  33. vector<int> arr;
  34. string input;
  35. getline(cin, input);
  36.  
  37. stringstream s1(input);
  38. int num;
  39. while (s1 >> num) {
  40. arr.push_back(num);
  41. }
  42.  
  43. Solution ob;
  44. cout << ob.maxDistance(arr) << endl;
  45. cout << "~" << endl;
  46. }
  47. return 0;
  48. }
  49. // } Driver Code Ends
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty