Interview Question: Move All Negative Numbers To Beginning And Positive To End C++

Kartikeya Mishra
6 min readOct 21, 2023

--

Company Interviews Using C++ Data Structures and Algorithms

#include <bits/stdc++.h> 
vector<int> separateNegativeAndPositive(vector<int> &nums){
int i =0;
for(int j =0; j< nums.size(); j++){
if(nums[j]<0){
swap(nums[i], nums[j]);
i++;
}
}
return nums;
}

English :

This C++ code defines a function called `separateNegativeAndPositive` that takes a reference to a vector of integers as its input and rearranges the elements in the vector in such a way that all the negative numbers come before the positive numbers. Here’s a step-by-step explanation of what the code does:

1. `#include <bits/stdc++.h>`: This line includes a common C++ library that provides various data structures and algorithms.

2. `vector<int> separateNegativeAndPositive(vector<int> &nums)`: This line defines a function named `separateNegativeAndPositive` that takes a reference to a vector of integers named `nums` as its parameter. The function returns a vector of integers.

3. `int i = 0;`: This initializes an integer variable `i` to 0. This variable will be used to keep track of the position where the next negative number should be placed.

4. The code enters a `for` loop, iterating through the elements of the input vector `nums` using the loop variable `j`.

5. `for(int j = 0; j < nums.size(); j++)`: This loop runs from 0 to the size of the input vector `nums` — 1.

6. `if (nums[j] < 0)`: This conditional checks if the element at index `j` in the vector `nums` is negative.

7. Inside the conditional block, `swap(nums[i], nums[j]);` is used to swap the element at index `i` with the element at index `j`. This effectively moves the negative number at index `j` to the position indicated by `i`, which is the next available slot for negative numbers.

8. `i++;`: After the swap operation, `i` is incremented to point to the next available position for a negative number.

9. The loop continues to iterate through the elements of the vector `nums`, and this process repeats for each negative element found in the vector.

10. Once the loop finishes, all the negative numbers will have been moved to the front of the vector, and the positive numbers will remain in their original order at the end of the vector.

11. Finally, the function returns the modified vector `nums` with negative numbers at the beginning and positive numbers at the end.

In summary, this code reorganizes the elements of the input vector, pushing all the negative numbers to the front while maintaining their original relative order, and all the positive numbers to the back.

Hindi :

यह C++ कोड एक फ़ंक्शन को परिभाषित करता है जिसे `separateNegativeAndPositive` नाम दिया गया है, जो एक पूर्णांकों के एक वेक्टर को उसके इनपुट के रूप में एक संदर्भ लेता है और वेक्टर में मौजूद तत्वों को एक ऐसे तरीके से पुनर्व्यवस्थित करता है कि सभी नकारात्मक संख्याएँ सकारात्मक संख्याओं से पहले आती हैं। यहां कोड की कार्यवस्तु क्या करती है, इसकी चरण-बचरण व्याख्या है:

1. `#include <bits/stdc++.h>`: इस पंक्ति में एक सामान्य C++ पुस्तकालय को शामिल किया गया है जो विभिन्न डेटा संरचनाओं और एल्गोरिदम्स प्रदान करता है।

2. `vector<int> separateNegativeAndPositive(vector<int> &nums)`: इस पंक्ति में एक फ़ंक्शन का नाम `separateNegativeAndPositive` परिभाषित किया गया है, जो एक पूर्णांकों के एक वेक्टर के एक संदर्भ को अपने पैरामीटर के रूप में लेता है और यह एक पूर्णांकों के वेक्टर को वापस देता है।

3. `int i = 0;`: इस पंक्ति में एक पूर्णांक चर `i` को 0 से आरंभ किया जाता है। यह चरण बचरण का तात्कालिक स्थान दर्ज करने के लिए प्रयोग किया जाएगा जहाँ अगली नकारात्मक संख्या को रखा जाना चाहिए।

4. कोड एक `for` लूप में प्रवेश करता है, जिसमें इनपुट वेक्टर `nums` के तत्वों पर चक्रवृत्त के चर `j` का प्रयोग करके यातायात करता है।

5. `for(int j = 0; j < nums.size(); j++)`: यह लूप 0 से इनपुट वेक्टर `nums` के आकार — 1 तक चलता है।

6. `if (nums[j] < 0)`: इस शर्तीकृत वाक्य द्वारा यह जांचा जाता है कि वेक्टर `nums` के सूची में सूचीकरण `j` पर धनात्मक है या नकारात्मक है।

7. इस शर्तीकृत ब्लॉक के अंदर, `swap(nums[i], nums[j]);` का प्रयोग तत्व `i` पर तत्व `j` के साथ अदला-बदली करने के लिए किया जाता है। इससे नकारात्मक संख्या `j` को स्थानांतरित किया जाता है जिसे `i` द्वारा दिखाया गया स्थान है, जो नकारात्मक संख्याओं के लिए अगला उपलब्ध स्थान है।

8. `i++;`: अदला-बदली कार्यवस्तु के बाद, `i` को बढ़ा दिया जाता है ताकि नकारात्मक संख्या के लिए अगला उपलब्ध स्थान का पता चले।

9. लूप वेक्टर nums के तत्वों के माध्यम से यातायात करने के लिए जारी रहता है, और यह प्रक्रिया हर नकारात्मक तत्व के लिए दोहराती है।

तत्वों के माध्यम से यातायात करने के लिए जारी रहता है, और यह प्रक्रिया हर नकारात्मक तत्व के लिए दोहराती है।

10. एक बार लूप समाप्त हो जाते हैं, तो सभी नकारात्मक संख्याएँ वेक्टर के सिरे पर ले जाई जाएंगी, और सकारात्मक संख्याएँ वेक्टर के अंत में अपनी मूल क्रमवार्ग से बाहर रहेंगी।

11. आखिरकार, फ़ंक्शन विवरित वेक्टर `nums` को लौटाता है जिसमें नकारात्मक संख्याएँ शुरू में और सकारात्मक संख्याएँ आखिर में होती हैं।

संक्षेप में, यह कोड इनपुट वेक्टर के तत्वों को पुनर्व्यवस्थित करता है, सभी नकारात्मक संख्याएँ सिरे के आगे धकेलता है और उनके मूल आपेक्षिक क्रम को बनाए रखता है, और सभी सकारात्मक संख्याएँ वेक्टर के आखिर में बनाए रखता है।

Hinglish :

Is C++ wala code mein ek function hai, jiska naam hai `separateNegativeAndPositive`. Is function mein ek reference to integer vector liya jata hai, jise `nums` ke naam se pukara jata hai, aur is function mein vector ke elements ko aise rearrange kiya jata hai ki sabhi negative numbers positive numbers ke pehle aa jate hain. Yahaan main step-by-step samjhata hoon ki yeh code kya karta hai:

1. `#include <bits/stdc++.h>`: Yeh line ek aam C++ library ko include karti hai jo alag-alag data structures aur algorithms provide karti hai.

2. `vector<int> separateNegativeAndPositive(vector<int> &nums)`: Yeh line ek function ko define karti hai jiska naam hai `separateNegativeAndPositive`, jo ek reference lekar ek vector of integers ko input ke roop mein leta hai, jise `nums` naam se pukara jata hai. Is function se ek vector of integers return hota hai.

3. `int i = 0;`: Is line mein ek integer variable `i` ko 0 ke barabar ki shuruaat di jati hai. Is variable ka upayog kiya jata hai next negative number ko kis position par rakhna hai, uska track rakhne ke liye.

4. Code ek `for` loop mein enter karta hai, jisme input vector `nums` ke elements ko loop variable `j` ka upayog karke iterate kiya jata hai.

5. `for(int j = 0; j < nums.size(); j++)`: Yeh loop 0 se input vector `nums` ke size — 1 tak chalta hai.

6. `if (nums[j] < 0)`: Yeh condition check karti hai ki vector `nums` ke index `j` par jo element hai, wo negative hai ya nahi.

7. Is condition ke andar, `swap(nums[i], nums[j]);` ka upayog kiya jata hai taki `i` index par wale element ko `j` index par wale element ke saath swap kiya ja sake. Isse effectively `j` index par wala negative number `i` index ke position par move ho jata hai, jo next available position hoti hai negative numbers ke liye.

8. `i++;`: Swap operation ke baad, `i` ko ek increment kiya jata hai taki yeh next available position ko point kare negative numbers ke liye.

9. Loop vector `nums` ke elements par iterate karna jari rahta hai, aur yeh process har negative element ke liye dohrata hai.

10. Jab loop khatam ho jata hai, to sabhi negative numbers vector ke shuruaat mein aa chuke hote hain aur positive numbers vector ke end mein apni original order mein hote hain.

11. Ant mein, function modify kiya gaya vector `nums` ko return karta hai, jisme negative numbers shuruaat mein aur positive numbers ant mein hote hain.

Samanya roop se, yeh code input vector ke elements ko reorganize karta hai, jisse sabhi negative numbers shuruaat mein aa jate hain, aur unki original relative order maintain hoti hai, jabki sabhi positive numbers vector ke ant mein rehte hain.

--

--

Kartikeya Mishra

Computer science engineer nomad exploring digital world. Learning and teaching while having fun.