0

There always comes an error when this code is executed. It says that request for member 'second

int n, arr[1000], maxm = 0; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; map <int, int> m; for (int i = 0; i < n; i++) m[arr[i]]++; sort(arr, arr + n); for (int i = 1; i < n; i++) { int temp; if (arr[i] - arr[i - 1] <= 1) temp = m[arr[i]].second + m[arr[i - 1]].second; if (maxm < temp) maxm = temp; } cout << maxm;

13th Aug 2020, 8:12 AM
Morningstar
Morningstar - avatar
3 odpowiedzi
+ 8
Check it try this #include <bits/stdc++.h> using namespace std; int main() { int n, arr[1000], maxm = 0; cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; map <int, int> m; for (int i = 0; i < n; i++) m[arr[i]]++; sort(arr, arr + n); for (int i = 1; i < n; i++) { int temp; if (arr[i] - arr[i - 1] <= 1) temp = m[arr[i]] + m[arr[i - 1]]; if (maxm < temp) maxm = temp; } cout << maxm; return 0; }
14th Aug 2020, 6:51 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
You don't need to use the .second member when using ::operator[] with a map. m[arr[i]] will output the value held in the map at the location of the arr[i] key. Remove .second and your code works.
13th Aug 2020, 9:14 AM
ChaoticDawg
ChaoticDawg - avatar
0
what it means there m[arr[i-1]].second? What are you trying in code? Can you share full code?
13th Aug 2020, 8:58 AM
Jayakrishna 🇮🇳