Move All Negative Number To The Left Side of an Array

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 47 :- Move All Negative Number To The Left Side of an Array

To move all negative numbers to the left side of an array, you can use the Two-Pointer technique. The idea is to maintain two pointers, one at the beginning of the array (left pointer) and another at the end of the array (right pointer). The left pointer will move towards the right, and the right pointer will move towards the left until they meet.

Here's the algorithm to achieve this:

  1. Initialize two pointers left and right, where left is set to the beginning of the array (index 0), and right is set to the end of the array (index n-1, where n is the size of the array).
  2. Repeat the following until left is less than or equal to right: a. If the element at the left pointer is negative, move left pointer one step to the right. b. If the element at the right pointer is positive, move right pointer one step to the left. c. If the element at the left pointer is positive and the element at the right pointer is negative, swap the elements at left and right, and then move both pointers one step to the right and one step to the left, respectively.

After the above steps, all the negative numbers will be on the left side of the array, and all the positive numbers will be on the right side of the array.

Here's the C++ code to implement the algorithm:

cppCopy code

#include <iostream> #include <vector> void moveNegativesToLeft(std::vector<int>& nums) {    int left = 0;    int right = nums.size() - 1;    while (left <= right) {        if (nums[left] < 0) {            left++;        } else if (nums[right] >= 0) {            right--;        } else {            std::swap(nums[left], nums[right]);            left++;            right--;        }    } } int main() {    std::vector<int> nums = {-1, 2, -3, 4, -5, 6, -7, 8};    std::cout << "Original Array: ";    for (int num : nums) {        std::cout << num << " ";    }    std::cout << std::endl;    moveNegativesToLeft(nums);    std::cout << "Array after moving negatives to the left: ";    for (int num : nums) {        std::cout << num << " ";    }    std::cout << std::endl;    return 0; }

Example Output:

sqlCopy code

Original Array: -1 2 -3 4 -5 6 -7 8 Array after moving negatives to the left: -1 -7 -3 -5 4 6 2 8

In this code, the moveNegativesToLeft function rearranges the array so that all negative numbers appear on the left side. The time complexity of this algorithm is O(n), where n is the size of the array.

7. Week3 - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?