Home > Software engineering >  Partition negative and positive in an array in java using shift operator
Partition negative and positive in an array in java using shift operator

Time:05-11

You will be given an array of n integers, both negative and positive. You need to partition the array into positive and negative numbers. Add all the positive integers of the array to one array (or any data structure) and add all the negative to another array (or any data structure). If the first element of the input array is a positive number, then print all the positive numbers in the given order in the first line of output, and then all the negative numbers in the given order in the second line of output, vice - versa.

Notes:

Consider 0 as a positive number.

The positive and negative numbers in the output should follow the order of the elements in the given array. Each number in each line of the output should be separated by a space.

If the array contains only positive numbers then print the positive numbers in the first line and in the second line print “Array doesn't have negative numbers”.

If the array contains only negative numbers then print the negative numbers in the first line and in the second line print “Array doesn't have positive numbers”.

Input:

10

2 6 9 -1 -4 10 -7 3 5 -8

Output:

2 6 9 10 3 5

-1 -4 -7 -8

Here, I have done the code but it does not print the message "Array doesn't have positive numbers" at all. Where am I going wrong?

import java.util.*;

public class Graded3 {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
       int arr[] = new int[n];
       for (int i = 0; i < n; i  ) {
           arr[i] = sc.nextInt();
       }
       partitionNegativeAndPositive(n,arr);
    }
    static void partitionNegativeAndPositive(int n, int[] arr) {
       ArrayList<Integer> pos = new ArrayList<Integer>();
       ArrayList<Integer> neg = new ArrayList<Integer>();
       int first = 1 (arr[0]>>31)-(-arr[0]>>31);
       if (first==0) {
         List<List<Integer>> posNeg = Arrays.asList(neg, pos);
         for(int i: arr) posNeg.get(i >>> 31).add(i);
       }
       else {
         List<List<Integer>> posNeg = Arrays.asList(pos, neg);
         for(int i: arr) posNeg.get(i >>> 31).add(i);
       }

       if(pos.isEmpty()==false) {
         for(int i =0; i<pos.size(); i  )
           System.out.print(pos.get(i)   " ");
         System.out.println("");
       }

       if(neg.isEmpty()==false) {
         for (int i = 0; i < neg.size(); i  )
           System.out.print(neg.get(i)   " ");
         System.out.println("");
       }
       if(pos.isEmpty()) {
         System.out.println("Array doesn't have positive numbers");
       }
       if(neg.isEmpty()) {
         System.out.println("Array doesn't have negative numbers");
       }

}
}

enter image description here

CodePudding user response:

You're over-complicating things:

public static void partition(int[] data) {
    List<Integer> posList = new ArrayList<>();
    List<Integer> negList = new ArrayList<>();
    
    for (int i : data) {
        if (i < 0) {
            negList.add(i);
        } else {
            posList.add(i);
        }
    }
    
    StringBuilder output = new StringBuilder();
    if (posList.isEmpty()) {
        output.append("Array doesn't have any positive numbers.").append(System.lineSeparator());
    } else {
        for (int i : posList) {
            output.append(i).append(' ');
        }
        output.append(System.lineSeparator());
    }
    
    if (negList.isEmpty()) {
        output.append("Array doesn't have any negative numbers.").append(System.lineSeparator());
    } else {
        for (int i : negList) {
            output.append(i).append(' ');
        }
        output.append(System.lineSeparator());
    }
    
    System.out.println(output);
}

public static void main(String [] args) throws IOException {
    partition(new int[] {2, 6, 9, -1, -4, 10, -7, 3, 5, -8});
}

CodePudding user response:

Based on your code, below line was having issue, because after first==0 condition satisfies, it created list but was adding negative numbers into positive block.

if (first==0) {
List<List<Integer>> posNeg = Arrays.asList(neg, pos);
for(int i: arr) posNeg.get(i >>> 31).add(i);

To simplify this, I am suggesting some modification to your partitionNegativeAndPositive:

static void partitionNegativeAndPositive(int n, int[] arr) {
        ArrayList<Integer> pos = new ArrayList<Integer>();
        ArrayList<Integer> neg = new ArrayList<Integer>();  
        //using single `posNeg` list.
        List<List<Integer>> posNeg = Arrays.asList(pos, neg);
            for (int i : arr)
                posNeg.get(i >>> 31).add(i);        
            
        if (pos.isEmpty() == false) {
            for (int i = 0; i < pos.size(); i  )
                System.out.print(pos.get(i)   " ");
            System.out.println("");
        }

        if (neg.isEmpty() == false) {
            for (int i = 0; i < neg.size(); i  )
                System.out.print(neg.get(i)   " ");
            System.out.println("");
        }
        if (pos.isEmpty()) {
            System.out.println("Array doesn't have positive numbers");
        }
        if (neg.isEmpty()) {
            System.out.println("Array doesn't have negative numbers");
        }
    }
  • Related