ummmmâŚI will try.
Pair of Roses Wrong Answer
Can we update value in unordered_multimap? I guess not. What you think?
Even if we use unordered_multiset of pair of (value,index) ,then also there is an obstacle to find that pair in the set. So, unordered_multiset is also not an option. I mean, I donât know how to find pair as a key in the set. No Hope.
Prototype using unordered_map, only limitation is input elements should be unique then it will take O(n) time.
@Kinjal you can store frequency of the elements in the unordered_map, thatâd solve the problem right?
thatâs a good hack. I guess, I can implement it, what youâre saying
Let me try itâŚ
Oh wait, how can you store count of different values and link it to the map?
vector v //assuming it already has some values
unordered_map m<int, int> freq;
for (auto val: v) {
freq[val]++;
}
//this will give you a frequency map of all the values present in the vector
I did here, what youâre trying to say. Can you give a look? Still giving wrong answer.
int n;
cin>>n;
vector<int> v(n,0);
for(int i=0;i<n;i++){
cin>>v[i];
}
I changed it like youâve said before. Yeah, itâs efficient with less lines of code.
@Kinjal this is my implementation, please check if it is passing test cases or not https://ide.codingblocks.com/s/276343
@Kinjal one thing to note here is that sorting will also take nlogn time, so even if we reduce the searching to O(1) and bring the complexity down to O(n), the actual time complexity of the program will still be O(nlong) because of sorting. But yes it will certainly reduce some time.
your code worked. I will say like itâs way less than my coding style in number of lines of code and prissy & simple to understand. I like your way of thinking process and coding style. In short, youâre awesome.
Okay, letâs talk about whole purpose of doing so far is not a waste. I think, in this prototype of yours is better than previous one in terms of time complexity. But, still we stuck at O(nlogn). I have tried to level up to O(n) but it comes with some obstacles that I donât know.
@Kinjal any other approach (like two pointer approach, binary search) etc all rely on the vector being sorted, but since that is not the case I dont think the complexity can be further reduced. Still O(nlogn) is also pretty good, certainly way better than O(n*n)
yeah, itâs a fact. Thank you.
oh. wait. we did it. I think, itâll work in O(n) time. Right!!!
Nah, itâs you buddy! Because of you, I optimized it so far. Without your collaboration, I could have remained in O(n^2) time which is suck. LOL.
I think, you did a great job. Iâve just followed your advice and suggestions carefully. So, thank you. We did it!
O(n^2) --> O(nlogn) --> O(n)
And I think, youâre Awesome.
In your code, youâve just used
#include <bits/stdc++.h>
And, youâve never used these header files but you implemented inbuilt functions of all the header files,
#include <iostream> #include<algorithm> #include<vector> #include<unordered_map>
WHY!
@Kinjal thanks a lot for your kind words! Coding is a journey in which we never stop learning
#include <bits.stdc++.h> you can use this library for competitive programming as it contains all the other libraries and it will save time during contests
Okay. But, I think that right now Iâm a newbie so I should do it this way (hard way), right!!!
In this thread, apart from the solution of that problem I learned a lot of new stuff. Itâs a great experience. Hope we will talk like this thread in the future. 
