paint-brush
How Do You Alternate Positive and Negative in Python?by@ishitajuneja
1,158 reads
1,158 reads

How Do You Alternate Positive and Negative in Python?

by Ishita JunejaNovember 2nd, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The alternate positive and negative numbers problem is one of the most asked data structure problems. A lot of companies ask for the concept behind the pairing of positive and negative numbers and those who don’t have any knowledge about it fail to give their answer. 
featured image - How Do You Alternate Positive and Negative in Python?
Ishita Juneja HackerNoon profile picture

Have you ever written a program for pairing alternate positive and negative numbers? If not, then you are missing some great opportunities. 

The alternate positive and negative numbers problem is one of the most asked data structure problems. A lot of companies ask for the concept behind the pairing of positive and negative numbers and those who don’t have any knowledge about it fail to give their answer. 

The problem is not very difficult for programmers if they have an understanding of the proper concept for solving it. 

To get a deep understanding of the problem, keep on reading till the end. 


What are the alternate positive and negative numbers in Python?

Before learning about the concept, it is important that we know about the problem statement and what type of output is required for it. Without knowing about these things, you will not be able to solve them. 

Alternate positive and negative numbers mean making pairs of numbers that contain one positive and negative number. 

To illustrate it, here is an example of the same.

Example 1

Problem: int arr[] = {1, 7, 3, -5, -1, 6}

Solution: int arr[] = {-5, 1, -1, 7, 3, 6}

Explanation: There are different elements that are declared in the array. Now, we have to make pairs by iterating over it in such a way that it makes a pair of a negative number and a positive number. Once there are no such pairs, then we will have to print the rest numbers.

Here is another example to make you clear about it. 

Example 2

Problem:  arr[] = {-6, -3, 9, 1, 4, 5, 1, 8, 0, -8}

Solution: arr[] = {-6, 9, -3, 1, -8, 4, 5, 1, 8, 0}

Now, let’s check what is the approach behind it to get pairs of negative and positive numbers. 

How can I alternate positive and negative numbers in Python?

We hope that you have got what the alternate positive and negative numbers in Python mean. Now, we have to learn how we will be able to do it by following the right approach. 

Beginner’s Approach

First of all, we will iterate over the array. 

We will classify and store both positive and negative numbers in different arrays. 

After it, we will print one element from both arrays by which we will be able to get our result. 

This is a beginner’s approach and it will take a lot of time and space. So, if you are thinking to solve the alternate positive and negative numbers like this, then you are not in the right direction. The problems like pairing, modular exponentiation, etc. require the best approach to solve the problem. If you are lacking in it, then you will have to face a lot of problems.

Best Approach

We all know that we have to make a pair of positive and negative numbers from the arrays.

So, we will check all the even places starting with 0 for the negative numbers whereas all the odd places for the positive numbers. 

If there is any number that is not in the correct position, then we will rearrange it, and if it is in the right position, then we will skip it. 

Once we have got all the negative numbers on the even place, then we will end the iteration. 

Dry Run

We are doing the dry run for a given array. So, check it properly to solve all of your doubts. 

Here is the problem statement.

int arr[] = { -4, -1, 4, 3, 2, 8, 5, 9, 2, -9, 8, -5 }

First of all, we will start the iteration and will check whether, in the first place, the element which is available is negative or not. So, we will check, and the element which is available is -4. So, we are having the correct element. 

Now, we will check for the next element, so, we have found that it is -1 which should be in an even place. So, we will rearrange it with the next positive element. 

Therefore, the -1 will be arranged with the 4. Now, our array looks like, {-4, 4, -1, 3, 2, 8, 9, 2, -9, 8, -5}

Again, we will iterate over the array, so, we will check for the next element which is at index 2, so the data is -1. So, it is in the right position. Now, we will check the next element which is 3 and it is also at the right position. 

Now, we will iterate over the next element, and after checking, we have found the positive number on the even place, so we will rearrange it, with the next negative number which is -9.  

Now, we will again iterate and will check for the next element. 

After iterating, we will get our next element which is 9 and it is at the wrong place. So, we will replace it with the next negative number. 

Now, we have got the array:  {-4, 4, -1, 3, -9, 8, -5, 2, 2, 8, 9}

Finally, we have rearranged all the necessary arrays which are making the pairs of the alternate positive and negative numbers. 

However, as there are no more negative numbers, so we will leave the array as it is. 

It will be our final answer. 

Now, you have to code it in a way that will check for the negative numbers at even places and positive numbers at odd places. After coding this, you will be able to solve the famous questions based on modular exponentiation, subarray with the given sum, etc.

Conclusion

Once you have got the right approach, then it will get easy for you to code the problems. 

For getting the right approach, you will have to practice a lot of DSA problems like alternate positive and negative numbers, modular exponentiation, subarray, linked list, and much more. When you have practiced all of these questions, then you will be able to write the correct algorithm with a proper dry run.