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

  • @aahanaganjewar9951
    @aahanaganjewar99512 жыл бұрын

    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

  • @anweshaaddya5459
    @anweshaaddya5459 Жыл бұрын

    in GFG this same approach is giving me TLE , if you could help please?

  • @kshitijgupta592
    @kshitijgupta5922 жыл бұрын

    In line 15-16 Where you're swapping arr[i] with arr[j], why we aren't incrementing i and decrementing j?

  • @thebeatifulnature3142
    @thebeatifulnature31422 жыл бұрын

    wah didi apka solution always best hota hai

  • @ANUJ-we3su
    @ANUJ-we3su2 жыл бұрын

    Doing great work mam

  • @prakashjaiswal2517
    @prakashjaiswal25177 ай бұрын

    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));

  • @harshitsharma1880
    @harshitsharma1880 Жыл бұрын

    #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..

  • @ArulPrakash-wp1pu
    @ArulPrakash-wp1pu11 ай бұрын

    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)

  • @shubham8835
    @shubham88352 жыл бұрын

    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

    @mr_arun_019

    5 ай бұрын

    only applicable equal positive and equal negative

  • @Timeless_Trader
    @Timeless_Trader2 ай бұрын

    Could you please make video on next case i.e. maintaining order?

  • @Y_bhav
    @Y_bhav6 ай бұрын

    But here order of appearance is not getting follow for positive intergers.

  • @shubhasheeshkundu9933
    @shubhasheeshkundu99332 жыл бұрын

    Thanks mam for adding Python code 😊😊😊...

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    😀😀 always welcome

  • @siddhantgoyal5103
    @siddhantgoyal51032 жыл бұрын

    i am not able to solve it using brute force can someone help??

  • @ShivamKendre-fc3su
    @ShivamKendre-fc3su2 жыл бұрын

    great video it was really helpful

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Thank you Shivam😊

  • @rohandevaki4349
    @rohandevaki43492 жыл бұрын

    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

    @AyushiSharmaDSA

    2 жыл бұрын

    Awesome Rohan, glad it was helpful :)

  • @rohandevaki4349

    @rohandevaki4349

    2 жыл бұрын

    @@AyushiSharmaDSA should we do in positive ,negative, positive elements way? or negative ,positive,negative elements order way?

  • @rohandevaki4349

    @rohandevaki4349

    2 жыл бұрын

    @@AyushiSharmaDSA can you show an example where the edge case is there, i didnt understand that code for the edge case

  • @nirajjain8099
    @nirajjain80992 жыл бұрын

    Great explanation ma'am . Can you please explain the problem in which the order also matters .

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Sure :)

  • @codehustler8582
    @codehustler85822 жыл бұрын

    thanks keep uploading these videos

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Sure😊

  • @willturner3440
    @willturner34402 жыл бұрын

    But what if order matter..

  • @siddharthpandey5371
    @siddharthpandey53716 ай бұрын

    U are teaching very well. Keep making more video on latest techo

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    4 ай бұрын

    Thank you, I will

  • @abdussamad0348
    @abdussamad03482 жыл бұрын

    nice one

  • @Annu63946
    @Annu639467 ай бұрын

    Aapne DSA kese itna strong kr liya 😮 please tell me

  • @kunjdave3676
    @kunjdave367611 ай бұрын

    I felt like i am learning from alia Bhatt your voice is so good

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 ай бұрын

    ohh is it 😛 btw I love her :)

  • @vivekmalviya3036
    @vivekmalviya30362 ай бұрын

    Thank u didi❤

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    Ай бұрын

    welcome :)

  • @pratikbhange2757
    @pratikbhange27572 жыл бұрын

    Helpful tha👍👍

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Thank you. 😀

  • @anshumansharma6650
    @anshumansharma66502 жыл бұрын

    can we apply sorting to make negative and positive seperate?🙏🏻 plz reply di 🥺

  • @AyushiSharmaDSA

    @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

    @anubhavjoria1632

    2 жыл бұрын

    sorting will take O(n log n) time

  • @ris99
    @ris992 жыл бұрын

    wrong initialization ,,i=0 and j=n-1; and function rearrange comes first after that print array .

  • @priyanshupatel02

    @priyanshupatel02

    2 жыл бұрын

    thanks vro ! by take i = -1 & j =n ...output will get wrong

  • @animeshjain4594
    @animeshjain45942 жыл бұрын

    This will only work if both positive and negative elements are equal in numbers.

  • @gouravkumarshaw5467
    @gouravkumarshaw54672 жыл бұрын

    great explanation

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Thank you Gourav :)

  • @DeviDevi-yr2sv
    @DeviDevi-yr2sv2 жыл бұрын

    Fabulous explaination 🎉

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Thank you :))

  • @DeviDevi-yr2sv

    @DeviDevi-yr2sv

    2 жыл бұрын

    @@AyushiSharmaDSA mam..agar DSA 450 sheet ache se solve karli toh placement ho jayegi ache company mein??

  • @AyushiSharmaDSA

    @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

    @DeviDevi-yr2sv

    2 жыл бұрын

    @@AyushiSharmaDSA thanks for replying mam

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    @@DeviDevi-yr2sv 💞

  • @mayanksharma9827
    @mayanksharma98272 жыл бұрын

    Again first visiter😁

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Thank you for supporting😊😊

  • @RohitKumar-pp9qn

    @RohitKumar-pp9qn

    2 жыл бұрын

    😁😁😜🤪🤭🤗🥰😍🤩🥳🙂🙃😗😙😚😀😃

  • @aakashvishwakarma4653
    @aakashvishwakarma46532 жыл бұрын

    💯💯💯🤩

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Thank you :)

  • @samudra-2231
    @samudra-2231 Жыл бұрын

    output ye nahi aa raha

  • @tanvithshenoy4499
    @tanvithshenoy44992 жыл бұрын

    NIce

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Thanks🙂

  • @teamcoders557
    @teamcoders5572 жыл бұрын

    Cpp code is not work properly

  • @priyanshupatel02

    @priyanshupatel02

    2 жыл бұрын

    try this ur output get crct initialization i=0 and j=n-1; and *function rearrange comes first after that print array* .

  • @tusharvaish8096
    @tusharvaish8096 Жыл бұрын

    i am seaching for the solution where space complexity does not matters

  • @rahulkumarsingh7615
    @rahulkumarsingh76152 жыл бұрын

    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

    @mehaknautiyal9810

    2 жыл бұрын

    How can negative number be greater than positive number ?

  • @rahulkumarsingh7615

    @rahulkumarsingh7615

    2 жыл бұрын

    @@mehaknautiyal9810 Here I mean to say greater than means size of -ve elements are greater than +ve elements...

  • @priyanshupatel02

    @priyanshupatel02

    2 жыл бұрын

    try this ur output get crct initialization i=0 and j=n-1; and function rearrange comes first after that print array .

  • @shourjyajitmaiti2490
    @shourjyajitmaiti24902 жыл бұрын

    why did u take i=-1and not 0

  • @ris99

    @ris99

    2 жыл бұрын

    wrong initialization ,,i=0 and j=n-1; and function rearrange comes first after that print array .

  • @fatehpreetsingh1440
    @fatehpreetsingh14402 жыл бұрын

    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

    @sambhavsharma2780

    2 жыл бұрын

    just initialise i = 0 and do while(i

  • @prateeksharma3698
    @prateeksharma36982 жыл бұрын

    In code i should be initialized with 0.

  • @shourjyajitmaiti2490

    @shourjyajitmaiti2490

    2 жыл бұрын

    noo

  • @NitinKumar-gv4hs
    @NitinKumar-gv4hs2 жыл бұрын

    Getting wrong output for c++ code

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    try printing the array, after rearrange function

  • @NitinKumar-gv4hs

    @NitinKumar-gv4hs

    2 жыл бұрын

    @@AyushiSharmaDSA Its giving wrong output , Please check the code again, thanks

  • @ecs185_shaileshbharti3

    @ecs185_shaileshbharti3

    2 жыл бұрын

    ​@@NitinKumar-gv4hs just change global initialisation with int i=0 ; int j=n-1; that's

  • @NitinKumar-gv4hs

    @NitinKumar-gv4hs

    2 жыл бұрын

    @@ecs185_shaileshbharti3 thanks shailesh

  • @abhishekkumbhani4403
    @abhishekkumbhani44032 жыл бұрын

    also upload a video of this problem with maintaining order

  • @AyushiSharmaDSA

    @AyushiSharmaDSA

    2 жыл бұрын

    Sure✌🏻

  • @parul8334
    @parul8334 Жыл бұрын

    // 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]

Келесі