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;
3 Antworten
+ 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;
}
+ 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.
0
what it means there m[arr[i-1]].second?
What are you trying in code?
Can you share full code?