+ 1
Neededhelp.Same code in c gives right output but in c++ after completion of program nothing is storing in temp array
https://code.sololearn.com/c0gzkUI4vXxt/?ref=app This is code in c https://code.sololearn.com/cWXgKks1pJNY/?ref=app This is same code in c++
20 Antworten
+ 1
push_back() adds element to end of the vector. so you are adding at end to vector. it differences in index value.
and also if(a[x]>(long long int)2*a[y]) why need cast to long long int?
+ 2
But how to correct it in c++ ?Jayakrishna🇮🇳
I type casted it because in question there is constraints that array's value can be maximum value of integer so to avoid integer overflow I did this typecasting
+ 2
Thanks alot!!!!!!!.I was struggling in this question since 2 days to get solution .Even after getting solution ,there are lots of problems and now it was resolved
+ 2
Yes ,i got it
+ 2
https://leetcode.com/problems/reverse-pairs/description/
Jayakrishna🇮🇳 my solution is giving tle though code was almost same as strivers bhaiya code /take u forward YouTube channel .🥺But why getting tle
+ 2
I mean my solution is almost same as solution of YouTube channel named Take You Forward but mine one is giving TLE ( Time limit exceeded) .As per your solution using nested loop (brute force) ,time complexity is On² which will not be accepted in LeetCode where question is present as it is not optimized one . Solution should be within time complexity Onlogn as per constraints given
Jayakrishna🇮🇳
+ 2
I recognizes it's solution as application of mergeSort bz this question is similar to question of inverse count . Suppose we have two sorted subarray of an array and now we are comparing elements of one subarray with elements of another subarray then after comparing the elements of two subarrays but before rearranging /storing them ,we get little bit time to do count operation .At that time ,Merge sort has an imp property that if elements (say x) of first subarray > elements(say y) of second subarray then all the elements after x in the first subarray will also be greater than y of second subarray .At that time ,there is no requirement to check condition ( x>y) for all elements after x of first subarray for the same element y ,we can directly count it .It can save us from comparing all elements after x with same element y and thus reduces time complexity from On*n to Onlogn as comparing operation reduces
+ 1
Srishti Jaiswal
Sry. I actually it works same but accessing leading to wrong..
You can use same array notation for vector also simply...
vector <int> vec;
for(int i=0; i<vec.size(); i++){
vec[i]=i;
cout<<vec[i]<<endl;
}
Try this way..
Instead why cant you use array also in c++ , for no confusion...!!
+ 1
But at that time when fresh value are inserted in at the end of vector and even the first time insertion then vector block will not remain allocated so it will generate runtime errorJayakrishna🇮🇳.In merge sort we don't know when will prev value of temp array get updated or when inserted at the end
+ 1
Currently
Instead of vector<int>temp;
temp.resize(a.size());
Use just
vector<int>temp(a.size());
index is uninitialized
+ 1
Using it giving error that shows [] can't be used Jayakrishna🇮🇳
+ 1
Corrected it but not giving same output as in c.Even if we print array after merge sort function call then it will not print sorted array ,it is printing 0 as all elements,though code is same in c and c++ Jayakrishna🇮🇳
+ 1
you are not updating index value...
https://code.sololearn.com/c3VYVh5Ysl6I/?ref=app
+ 1
Just for info: Here you just using initialized vector and reassigning values.. It's like array way only.. Not using c++ vector way..
You're welcome..
+ 1
Yes i tried it worked also🙃.No need to remove type cast bz in compiler it will work bz it doesn't have any constraints.But in coding platform like leetcode ,constraints are there that will cause integer overflow if it is not being type casted
+ 1
Oh. That's good to know..
Once check also by removing type casting.. As far I know, the calculations are takes place by all operands are automatically type casted to higher types.. But to store back compatible types and not to cause overflow , you need type cast result.
0
index is uninitialized
0
Srishti Jaiswal
I don't understand your reply clearly ? What channel?
Is this code about the link problem?
Then I think you don't need mergesort Or any sort
I think, see
Input: nums = [1,3,2,3,1]
Pairs are 3,1(at indexes 1,4) and 3,1(indexes 3,4)
Output: 2
Example 2:
Input: nums = [2,4,3,5,1]
Output: 3
Pairs ( 4,1), (3,1) , (5,1)
These are only pairs which satisfying constraints... Is not it?
Just with 2 loops, it can be done..
0
Oh. Then what is your idea here using merge sort..? Is channel has solution ?
0
Oh. . Try again by removing lines number 49, 51,52 means combine 2 loops..
also remove casting