Question: Please read carefully - The problem You are given an array of arrays a. Your task is to group the arrays a[i] by their
Please read carefully -
The problem "
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values, so that arrays with equal mean values are in the same group, and arrays with different mean values are in different groups.
Each group should contain a set of indices (i, j, etc), such that the corresponding arrays (a[i], a[j], etc) all have the same mean. Return the set of groups as an array of arrays, where the indices within each group are sorted in ascending order, and the groups are sorted in ascending order of their minimum element.
Example
- For
- a = [[3, 3, 4, 2],
- [4, 4],
- [4, 0, 3, 3],
- [2, 3],
- [3, 3, 3]]
the output should be
meanGroups(a) = [[0, 4],
[1],
[2, 3]]
- mean(a[0]) = (3 + 3 + 4 + 2) / 4 = 3;
- mean(a[1]) = (4 + 4) / 2 = 4;
- mean(a[2]) = (4 + 0 + 3 + 3) / 4 = 2.5;
- mean(a[3]) = (2 + 3) / 2 = 2.5;
- mean(a[4]) = (3 + 3 + 3) / 3 = 3.
There are three groups of means: those with mean 2.5, 3, and 4. And they form the following groups:
- Arrays with indices 0 and 4 form a group with mean 3;
- Array with index 1 forms a group with mean 4;
- Arrays with indices 2 and 3 form a group with mean 2.5
"
I was able to save all means without duplicated on Unordered_map and all the means on vector track.
as track has exact index of arrays of array. I want to utilize unorderedmap and save the indexs of the same per array within the array res. but I got stuck.
This is c++ program
#include
using namespace std;
int main() {
vector
for (auto x : c) { int sum=0; int aver=0; for (auto z : x) { // cout << "z val->" << z << endl; sum += z; } //cout << "sum val->" << sum << endl; //cout << "x size->" << x.size() << endl; int sz = x.size(); aver =sum/sz; //cout << "aver->" << aver << endl; maps[aver]++; track.push_back(aver); }
cout << "show res val:" << endl;
for (auto j : maps)cout << j.first << " " << endl;
vector
for (auto z : res){ for(auto x:z) cout << x << " ";
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
