Rearrange the array in alternating positive and negative items🔥🔥 | Array | O(1) space | Hindi
Time Complexity : O(n)
Space Complexity : O(1)
Problem Link : www.geeksforgeeks.org/rearran...
C++ Code Link : github.com/Ayu-99/Love-Babbar...
Python Code Link: github.com/Ayu-99/Love-Babbar...
Love Babbar DSA Sheet : drive.google.com/file/d/1FMdN...
Please like, share and subscribe if you found the video useful. Feel free to ask in comments section if you have any doubts. :)
#DataStructuresAndAlgorithms
#LoveBabbarDSASheet
#interviewpreparation
Rearrange the array in alternating positive and negative items solution
Rearrange the array in alternating positive and negative items Leetcode
Rearrange the array in alternating positive and negative items C++
Rearrange the array in alternating positive and negative items C++ Hindi
Rearrange the array in alternating positive and negative items Hindi
Checkout the series: 🔥🔥🔥
👉 Array: • Arrays
👉 Recursion : • Recursion
👉 Stack and Queue : • Stack And Queue
👉 Greedy : • Greedy
👉 Leetcode contests : • Leetcode contests
👉 Leetcode June Challenge : • Leetcode June Challenge
👉 Leetcode July Challenge : • Leetcode July Challenge
LIKE | SHARE | SUBSCRIBE 🔥🔥😊
Пікірлер: 81
used partition method for seprating +ve and -ve int i = 0; int j = 0; while (i if (arr[i] >= 0) { swap(arr, i, j); i++; j++; } else { i++; } } j lies on first negative element
in GFG this same approach is giving me TLE , if you could help please?
In line 15-16 Where you're swapping arr[i] with arr[j], why we aren't incrementing i and decrementing j?
wah didi apka solution always best hota hai
Doing great work mam
can we do like this let array = [9, 4 ,-2, -1, 5, 0 ,-5, -3, 2]; rearrange = function(arr,n){ arr.sort((a,b)=> b-a); let answer = []; for(let i = 0 ; i answer.push(arr[i]); if(i !== (n-1)-i){ answer.push(arr[(n-1) - i]); } } return answer ; } console.log(rearrange(array,array.length));
#CODE ISSUE⁉ there is a small mistake in code after swapping the values we have to increase i and decrease j otherwise it will fall into infinite loop..
Check if this approach working for an array when it consist of positive and negative are not equal in numbers(ie 5 positive and 2 negative number)
Input: arr[] = {1, 2, 3, -4, -1, 4} Output: arr[] = {-4, 1, -1, 2, 3, 4} It is not working on this example, the approach that you said
@mr_arun_019
5 ай бұрын
only applicable equal positive and equal negative
Could you please make video on next case i.e. maintaining order?
But here order of appearance is not getting follow for positive intergers.
Thanks mam for adding Python code 😊😊😊...
@AyushiSharmaDSA
2 жыл бұрын
😀😀 always welcome
i am not able to solve it using brute force can someone help??
great video it was really helpful
@AyushiSharmaDSA
2 жыл бұрын
Thank you Shivam😊
thankyou for the idea at 5:21 ayushi, i was able to solve myself, with 0(1) and O(n) without seeing your solution : )
@AyushiSharmaDSA
2 жыл бұрын
Awesome Rohan, glad it was helpful :)
@rohandevaki4349
2 жыл бұрын
@@AyushiSharmaDSA should we do in positive ,negative, positive elements way? or negative ,positive,negative elements order way?
@rohandevaki4349
2 жыл бұрын
@@AyushiSharmaDSA can you show an example where the edge case is there, i didnt understand that code for the edge case
Great explanation ma'am . Can you please explain the problem in which the order also matters .
@AyushiSharmaDSA
2 жыл бұрын
Sure :)
thanks keep uploading these videos
@AyushiSharmaDSA
2 жыл бұрын
Sure😊
But what if order matter..
U are teaching very well. Keep making more video on latest techo
@AyushiSharmaDSA
4 ай бұрын
Thank you, I will
nice one
Aapne DSA kese itna strong kr liya 😮 please tell me
I felt like i am learning from alia Bhatt your voice is so good
@AyushiSharmaDSA
2 ай бұрын
ohh is it 😛 btw I love her :)
Thank u didi❤
@AyushiSharmaDSA
Ай бұрын
welcome :)
Helpful tha👍👍
@AyushiSharmaDSA
2 жыл бұрын
Thank you. 😀
can we apply sorting to make negative and positive seperate?🙏🏻 plz reply di 🥺
@AyushiSharmaDSA
2 жыл бұрын
We can do , but here it should be alternate , make sure that , you need to find where positive elements are starting and switch positive with negative alternatively :)
@anubhavjoria1632
2 жыл бұрын
sorting will take O(n log n) time
wrong initialization ,,i=0 and j=n-1; and function rearrange comes first after that print array .
@priyanshupatel02
2 жыл бұрын
thanks vro ! by take i = -1 & j =n ...output will get wrong
This will only work if both positive and negative elements are equal in numbers.
great explanation
@AyushiSharmaDSA
2 жыл бұрын
Thank you Gourav :)
Fabulous explaination 🎉
@AyushiSharmaDSA
2 жыл бұрын
Thank you :))
@DeviDevi-yr2sv
2 жыл бұрын
@@AyushiSharmaDSA mam..agar DSA 450 sheet ache se solve karli toh placement ho jayegi ache company mein??
@AyushiSharmaDSA
2 жыл бұрын
@@DeviDevi-yr2sv Java interview ka kuch ni pta kya problem puche, but agar sheet ache se karoge to kafi variety of approaches aayengi
@DeviDevi-yr2sv
2 жыл бұрын
@@AyushiSharmaDSA thanks for replying mam
@AyushiSharmaDSA
2 жыл бұрын
@@DeviDevi-yr2sv 💞
Again first visiter😁
@AyushiSharmaDSA
2 жыл бұрын
Thank you for supporting😊😊
@RohitKumar-pp9qn
2 жыл бұрын
😁😁😜🤪🤭🤗🥰😍🤩🥳🙂🙃😗😙😚😀😃
💯💯💯🤩
@AyushiSharmaDSA
2 жыл бұрын
Thank you :)
output ye nahi aa raha
NIce
@AyushiSharmaDSA
2 жыл бұрын
Thanks🙂
Cpp code is not work properly
@priyanshupatel02
2 жыл бұрын
try this ur output get crct initialization i=0 and j=n-1; and *function rearrange comes first after that print array* .
i am seaching for the solution where space complexity does not matters
hey, it gives the wrong output. when positive and negative elements are equal then this code will work fine,,,, but if -ve elements are greater than positive elements, the code gives a wrong output...plz check it once..
@mehaknautiyal9810
2 жыл бұрын
How can negative number be greater than positive number ?
@rahulkumarsingh7615
2 жыл бұрын
@@mehaknautiyal9810 Here I mean to say greater than means size of -ve elements are greater than +ve elements...
@priyanshupatel02
2 жыл бұрын
try this ur output get crct initialization i=0 and j=n-1; and function rearrange comes first after that print array .
why did u take i=-1and not 0
@ris99
2 жыл бұрын
wrong initialization ,,i=0 and j=n-1; and function rearrange comes first after that print array .
i==a -ve element condition is not true in every case.finding the position of first -ve element in array after segregation will always work btw nice explanation maam :)
@sambhavsharma2780
2 жыл бұрын
just initialise i = 0 and do while(i
In code i should be initialized with 0.
@shourjyajitmaiti2490
2 жыл бұрын
noo
Getting wrong output for c++ code
@AyushiSharmaDSA
2 жыл бұрын
try printing the array, after rearrange function
@NitinKumar-gv4hs
2 жыл бұрын
@@AyushiSharmaDSA Its giving wrong output , Please check the code again, thanks
@ecs185_shaileshbharti3
2 жыл бұрын
@@NitinKumar-gv4hs just change global initialisation with int i=0 ; int j=n-1; that's
@NitinKumar-gv4hs
2 жыл бұрын
@@ecs185_shaileshbharti3 thanks shailesh
also upload a video of this problem with maintaining order
@AyushiSharmaDSA
2 жыл бұрын
Sure✌🏻
// my code 😅😅😅😅 #include using namespace std; int main(){ int n; cin>>n; int arr[n]; for(int i = 0;i>arr[i]; } int i = 0, j = n-1; while(i=0){ i++; continue; } else if(arr[i]