Advertisement
HjHimansh

Max difference.

Jan 11th, 2022
778
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h> // header file includes every Standard library
  2. using namespace std;
  3.  
  4. int main() {
  5.     int N;
  6.     cin >> N;
  7.  
  8.     vector<int> arr;
  9.            
  10.  
  11.     while(N--){
  12.         int temp;   cin >> temp;
  13.         arr.push_back(temp);
  14.     }
  15.  
  16.     multimap<int, int> uMap;
  17.     for(int i=0; i<arr.size(); i++){
  18.         uMap.insert(pair <int, int> (arr[i], i));
  19.         //uMap[arr[i]] = i;
  20.     }
  21.  
  22.     int answer = -1;
  23.     int maxSoFar = -1;
  24.     for (auto i = uMap.rbegin(); i != uMap.rend(); ++i) {
  25.         answer = max(answer, maxSoFar - i->second);
  26.         maxSoFar = max(maxSoFar, i->second);
  27.     }
  28.  
  29.     cout << answer << endl;
  30.  
  31.     return 0;
  32. }
Advertisement
RAW Paste Data Copied
Advertisement