Can I solve it using Moore’s Algo.
Can I solve it using Moore's Algo
Then any other method you thought after just seeing the Question.
Can you please tell all the approaches which you thought from Brute Force till Most Optimized.
Why so?
How to find which can be solved by that.
only two problems are solvable by moore vooting.
one is n/2 majority and other is n/3 majority
u can use hashing to count freq of each number and then print the required one.
or
u can sort the array and can count the consecutive element
etc.
Yeh fir O(nlogn) mein krega
This one will too take o(nlogn) time as sorting kro
o(nlogn+n) hojayega
it is given that array is sorted so time complexity will be O(N).
ANOTHER APPROACH->
it’s guaranteed that only one number will appear more than 25% times, that number will definitely appear at one of the three positions in the array: quarter, half, and three quarters. We see them as candidates, and then using binary search to check each of them.
Ohkkayyy
Bhut shi method rhega yeh ton
Bro ek yeh check krdo
kyu galt aarha
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n=nums.size();
vector<pair<int,int> > v;
for(int i=0;i<n;i++)
{
v.push_back({nums[i],i});
}
int i=0;
int j=n-1;
sort(nums.begin(),nums.end());
vector<int> res;
while(i<j)
{
int sum=nums[i]+nums[j];
if(sum==target)
{
int first=v[i].second;//This is wrong, How to implement this one.
int second=v[j].second;//This is wrong, How to implement this one.
res.push_back(first);
res.push_back(second);
break;
}
else if(sum>target)
{
j--;
}
else
{
i++;
}
}
return res;
}
};
Han Bhai wo smjh gya that galti pta laggyi thi
Here I will actually use nums only but sorting galt krdi thi
ab bhi same issue aarha kuch
idk why
[3,2,4]
6
iske liye
pta nhi kya dikkat hai
if u want to use nums then u need to sort both nums as well as v.
chekc now>
aisa kyu
sort ton sirf num ko nhi krna pdega
bcoz v ton like ek trah map ka kaam krrha
that I just used for indexing
so that ki jab muzhe uss element ka index chahiye ho direct whi se uthaalu.
Ya vector mein mene i,j use kiya wo issue krrha
iss trah se jaise mene socha uss trah se map se krna pdega
@cbcao263
ya to map use karo fir .
otherwise vector of pair ko sort karna jaruri hai.
kunki nums[i] sorted hai iska actual index lene ke liye v[i].second (ye tabhi kar paowoye jab dono array ke elements same sequence mein right?)
Okayy Sir
Very much right.
Thanks for this detailed explanation.
Sir
Yeh kyu galt aarha
bro create a new thread , for new question
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.