#include <bits/stdc++.h> using namespace std; bool sortele(pair<int, int> a, pair<int,…
Jump to turn (10)
- 1#include using namespace std; bool sortele(pair a, pair b) { if (a.second == b.s
- 2sort(vec.begin(), vec.end(), sortele); what if bool sortele is true / false whic
- 3for above explain with example
- 4give explanation with other unique example
- 5vector > vec = {{4, 3}, {2, 2}, {6, 2}, {8, 1}, {5, 1}}; waht if it was like thi
- 6For (6, 2) vs (2, 2): Since the frequencies are tied, the function checks if 6 <
- 7when 2,2 and 6,2 no problem true and then sorting in ascending order but if 6,2
- 8sort(v.begin(),v.end(),true/false) difference when true and when false what will
- 9bool sortele(pair a, pair b) { if (a.second == b.second) return a.first b.second
- 10bool sortele(pair a, pair b) { if (a.second == b.second) return a.first b.second
- User
#include <bits/stdc++.h> using namespace std; bool sortele(pair<int, int> a, pair<int, int> b) { if (a.second == b.second) return a.first < b.first; return a.second > b.second; } void Sortelementsbyfreq(int arr[], int n) { unordered_map<int, int> map; for (int i = 0; i < n; i++) { map[arr[i]]++; } vector<pair<int, int>> vec; for (auto it = map.begin(); it != map.end(); it++) { vec.push_back({it->first, it->second}); } sort(vec.begin(), vec.end(), sortele); for (int i = 0; i < vec.size(); i++) { while (vec[i].second > 0) { cout << vec[i].first << " "; vec[i].second--; } } cout << endl; } int main() { int arr[] = {3, 3, 5, 2, 1, 1, 2}; int n = 7; Sortelementsbyfreq(arr, n); return 0; } explain with example
- Assistant