Skip to main content

Command Palette

Search for a command to run...

Efficiently Separate Positive and Negative Numbers in an Array (Preserves Order)

Published
1 min read
Efficiently Separate Positive and Negative Numbers in an Array (Preserves Order)
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Q - Given an unsorted array arr[] of size N having both negative and positive integers. The task is to place all negative elements at the end of the array without changing the order of positive elements and negative elements.

GeeksForGeeks Question - Link | Click Here

class Solution {

    public void segregateElements(int arr[], int n) {

        // Create a temporary array to hold elements in a new order
        int[] temp = new int[n];
        // Keep track of the position in the new array
        int count = 0;

        // Rearrange positive numbers to the start of the new array
        for (int number : arr) {
            if (number >= 0) {
                temp[count] = number; // Store positive numbers
                count++; // Move to the next available spot in the new array
            }
        }

        // Put negative numbers after the positive ones in the new array
        for (int number : arr) {
            if (number < 0) {
                temp[count] = number; // Store negative numbers
                count++; // Move to the next available spot in the new array
            }
        }

        // Copy the rearranged elements back to the original array
        for (int i = 0; i < n; i++) {
            arr[i] = temp[i]; // Update the original array with rearranged elements
        }
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.