Merge 2 Sorted array without using Extra Space | Love Babbar DSA Sheet Q12 | Arrays

preview_player
Показать описание
✅ Useful Links

SUBSCRIBE AND HIT BELL ICON TO CHECK MORE OF MY CONTENT

Merge 2 Sorted array without using Extra Space | Love Babbar DSA Sheet Q12 | Arrays

𝐌𝐔𝐒𝐈𝐂 𝐂𝐑𝐄𝐃𝐈𝐓
Track: Julius Dreisig & Zeus X Crona - Invisible [NCS Release]
Music provided by NoCopyrightSounds.

Track: Axollo - Silence (ft. Josh Bogert) [NCS Release]
Music provided by NoCopyrightSounds.
Рекомендации по теме
Комментарии
Автор

Thank you so much for this solution I tried to get it from various websites, even leetcode discussion i was not able to understand. Thanks for clarifying.
God Bless Everyone😇

sakshiaggarwal
Автор

you can optimize this fixArray function by introducing a bool variable cause you are iterating the full array but need to iterate till the elements shifts though on gfg it will give error cause it, for gfg cycle sort approach will work and one another thing you have used bubble sort approach in code not insertion sort

rahulprasad
Автор

i never know ki arrays aise bhi print ki ja sakti thi.... thanks a lot...

aniruddhachunne
Автор

bhai doing great work .just keep it up

omkarbarbadikar
Автор

bhai tum while loop me function call kr re ho aur uske TC O(n) hai to TC in total O(n*m) hogai, ye to heavy hai time complexity ke hisab se . Not and optimized solution.

urge-bull
Автор

for some reason its giving TLE to me:

Test Cases Passed:
1110 /1119

Here is the Code:

class Solution
{
//Function to merge the arrays.
public static void merge(long arr1[], long arr2[], int n, int m)
{
// code here
int i=0, j=0;
long temp=0;
while(i<n){
if(arr1[i]>arr2[j]){
temp=arr1[i];
arr1[i]=arr2[j];
arr2[j]=temp;
fixArray2(arr2, m);
}
i++;
}
}
static void fixArray2(long arr2[], int m){
long temp=0;
for(int i=1; i<m; i++){
if(arr2[i]<arr2[i-1]){
temp=arr2[i];
arr2[i]=arr2[i-1];
arr2[i-1]=temp;
}
}
}

}

rahulgupta
Автор

public static void merge(long arr1[], long arr2[], int n, int m)
{
// code here
int left = n-1;
int right = 0;
while(left >=0 && right <m){
if(arr1[left] > arr2[right]){
long temp = arr1[left];
arr1[left] = arr2[right];
arr2[right] = temp;
left--;
right++;
}else{
break;
}

}
Arrays.sort(arr1);
Arrays.sort(arr2);
}
}

anushkaagrawal
Автор

U didn't merge the elements into a single array

bhushanp
Автор

Bekr hai bhai ye TLE dega iska code isiliye isne gfg pai nahi run Kiya code apne ide mai chala rha hai🧐

bartosz