Skip to main content

Command Palette

Search for a command to run...

Rearrange Array Elements by Sign

Published
2 min read
Rearrange Array Elements by Sign
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.

You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers.

You should return the array of nums such that the the array follows the given conditions:

  1. Every consecutive pair of integers have opposite signs.

  2. For all integers with the same sign, the order in which they were present in nums is preserved.

  3. The rearranged array begins with a positive integer.

Return the modified array after rearranging the elements to satisfy the aforementioned conditions.

LeetCode Problem - 2149

class Solution {
    public int[] rearrangeArray(int[] nums) {
        // Initialize arrays to hold positive and negative numbers separately.
        int[] resultArr = new int[nums.length]; // Array to store the rearranged numbers.
        int[] posArray = new int[(nums.length)/2]; // Array to hold positive numbers.
        int[] negArray = new int[(nums.length)/2]; // Array to hold negative numbers.

        // Counters to keep track of the positions in the positive and negative arrays.
        int posCount = 0;
        int negCount = 0;

        // Loop through the original array to separate positive and negative numbers.
        for(int i=0; i<nums.length; i++){
            if(nums[i]>0){
                posArray[posCount] = nums[i]; // Store positive number in positive array.
                posCount++; // Increment positive counter.
            } else {
                negArray[negCount] = nums[i]; // Store negative number in negative array.
                negCount++; // Increment negative counter.
            }
        }

        // Reset counters for reusing.
        posCount = 0;
        negCount = 0;

        // Loop through the original array to rearrange the numbers.
        for(int i=0; i<nums.length; i++){
            if(i%2==0){
                resultArr[i]=posArray[posCount]; // Place positive numbers at even indices.
                posCount++; // Move to the next positive number.
            } else {
                resultArr[i]=negArray[negCount]; // Place negative numbers at odd indices.
                negCount++; // Move to the next negative number.
            }
        }

        // Return the rearranged array.
        return resultArr;
    }
}

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.

Rearrange Array Elements by Sign