Java Program to Set Positive Elements at Even and Negative at Odd Positions

In the previous article, we have seen Java Program to Rearrange Array such that Even Index Elements are Smaller and Odd Index Elements are Greater

In this article we are going to see how to Set Positive Elements at Even and Negative at Odd Positions using java programming language.

Java Program to Set Positive Elements at Even and Negative at Odd Positions

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Approach:

  1. Initialize two pointers, even = 0 and odd = 1.
  2. Iterate over the array.
  3. If the element at even index is even or zero increment the even pointer by 2.
  4. If the element at odd index is odd or zero increment the odd pointer by 2.
  5. If the element at even index is odd and the element at odd index is even, swap the elements and increment both pointers by two.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        // call rearrangeOddEven function
        rearrangeOddEven(arr);
    }

    public static boolean isPositive(int n) 
    {
        return n > 0;
    }

    public static void rearrangeOddEven(int[] arr) 
    {
        // initialize even and odd pointers
        int even = 0;
        int odd = 1;
        while (even < arr.length && odd < arr.length) 
        {
            // if even element is positive or zero then increment even pointer
            if (isPositive(arr[even]) || arr[even] == 0) 
            {
                even += 2;
            } 
            // if odd element is negative or zero then increment odd pointer
            else if (!isPositive(arr[odd]) || arr[odd] == 0) 
            {
                odd += 2;
            } 
            else 
            {
                swap(arr, even, odd);
                even += 2;
                odd += 2;
            }
        }
        System.out.println("Array after rearranging: " + Arrays.toString(arr));

    }

    //swap() method
    private static void swap(int[] arr, int even, int odd) 
    {
        int temp = arr[even];
        arr[even] = arr[odd];
        arr[odd] = temp;
    }

}

Output:

Enter the size of array: 7
Enter array elements: 6 -7 2 -4 3 1 -5
Array after rearranging: [6, -7, 2, -4, 3, -5, 1]

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: