+ 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++

20th Apr 2022, 3:27 PM
Srishti Jaiswal
Srishti Jaiswal - avatar
20 Réponses
+ 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?
20th Apr 2022, 4:07 PM
Jayakrishna 🇮🇳
+ 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
20th Apr 2022, 4:36 PM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 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
20th Apr 2022, 6:51 PM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 2
Yes ,i got it
21st Apr 2022, 1:32 AM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 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
21st Apr 2022, 1:55 AM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 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🇮🇳
21st Apr 2022, 10:20 AM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 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
21st Apr 2022, 12:25 PM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 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...!!
20th Apr 2022, 5:39 PM
Jayakrishna 🇮🇳
+ 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
20th Apr 2022, 5:42 PM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 1
Currently Instead of vector<int>temp; temp.resize(a.size()); Use just vector<int>temp(a.size()); index is uninitialized
20th Apr 2022, 5:49 PM
Jayakrishna 🇮🇳
+ 1
Using it giving error that shows [] can't be used Jayakrishna🇮🇳
20th Apr 2022, 5:51 PM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 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🇮🇳
20th Apr 2022, 5:54 PM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 1
you are not updating index value... https://code.sololearn.com/c3VYVh5Ysl6I/?ref=app
20th Apr 2022, 6:47 PM
Jayakrishna 🇮🇳
+ 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..
20th Apr 2022, 7:03 PM
Jayakrishna 🇮🇳
+ 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
22nd Apr 2022, 6:47 AM
Srishti Jaiswal
Srishti Jaiswal - avatar
+ 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.
22nd Apr 2022, 8:45 AM
Jayakrishna 🇮🇳
0
index is uninitialized
20th Apr 2022, 5:53 PM
Jayakrishna 🇮🇳
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..
21st Apr 2022, 9:58 AM
Jayakrishna 🇮🇳
0
Oh. Then what is your idea here using merge sort..? Is channel has solution ?
21st Apr 2022, 11:55 AM
Jayakrishna 🇮🇳
0
Oh. . Try again by removing lines number 49, 51,52 means combine 2 loops.. also remove casting
21st Apr 2022, 8:15 PM
Jayakrishna 🇮🇳