Question

#include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int...

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {

int N = nums.size();
vector<int> result;
vector<int> a(101);
vector<int> b(101);

for (int i = 0; i < N; i++) {
a[nums[i]]++; // what does this mean?
}
for (int i = 1; i < 101; i++) {
b[i] = a[i - 1] + b[i - 1];
}
for (int i = 0; i < N; i++) {
result.push_back(b[nums[i]]);
}
for (int i = 0; i < N; i++)
{
cout << result[i] << " ";
}
return result;
}
};

int main()
{
Solution solution;
vector<int> vect = { 8,1,2,2,3 };
solution.smallerNumbersThanCurrent(vect);
return 0;
}

what does this line mean? a[nums[i]]++;
is there any example or other way to write that?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

we can write a[num[i]]++ as

a[num[i]] = a[num[i]] + 1;

num[i] return the value from vector num at ith position then this value is taken as the index value for the vector a and at this index, lets say we get value of num[i] = k, then  value at a[k] will be incremented by one.

Note: Let me know if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <vector> using namespace std; class Solution { public: vector<int> smallerNumbersThanCurrent(vector<int>& nums) { int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT