Java Program to Store Even & Odd Elements of an Array into Separate Arrays (original) (raw)
Last Updated : 13 Apr, 2022
Given an array with N numbers and separate those numbers into two arrays by odd numbers or even numbers. The complete operation required O(n) time complexity in the best case. For optimizing the memory uses, the first traverse through an array and calculate the total number of even and odd numbers in it. Create two arrays with size calculated after traversing and start storing them.
Below is the implementation of the above approach:
Java `
// Java Program to Store Even & Odd // Elements of an Array into Separate Arrays
public class Even_Odd {
// Print array method
public static void printArray(int[] array)
{
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
int n = 8;
// array with N size
int array[] = { 23, 55, 54, 9, 76, 66, 2, 91 };
int evenSize = 0;
int oddSize = 0;
for (int i = 0; i < n; i++) {
if (array[i] % 2 == 0)
evenSize++;
else
oddSize++;
}
// odd and even arrays with size
int[] even = new int[evenSize];
int[] odd = new int[oddSize];
// odd and even array iterator
int j = 0, k = 0;
for (int i = 0; i < n; i++) {
if (array[i] % 2 == 0)
even[j++] = array[i];
else
odd[k++] = array[i];
}
// print array method
System.out.print("Even Array contains: ");
printArray(even);
System.out.print("Odd Array contains: ");
printArray(odd);
}
}
`
Output:
Even Array contains: 54 76 66 2 Odd Array contains: 23 55 9 91
Time Complexity: O(n)
Space Complexity: O(n)
Similar Reads
- Java Program to Sort the Elements of an Array in Ascending Order Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort.1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort() 2 min read
- Java Program to Rearrange array elements into alternate even-odd sequence by anticlockwise rotation of digits Given an array arr[] consisting of N positive integers, the task is to rotate the digits of array elements in an anti-clockwise direction such that elements of the array elements are in alternate even-odd or odd-even form. If multiple solutions exists, then print any one of them. Otherwise, print -1 4 min read
- Java Program to Print the Elements of an Array Present on Even Position The task is to print all the elements that are present in even position. Consider an example, we have an array of length 6, and we need to display all the elements that are present in 2,4 and 6 positions i.e; at indices 1, 3, 5. Example: Input: [1,2,3,4,5,6] Output: 2 4 6 Input: [1,2] Output: 2 Appr 2 min read
- Java Program to Return the Elements at Odd Positions in a List Given a List, the task is to return the elements at Odd positions in a list. Let's consider the following list.Clearly, we can see that elements 20, 40, 60 are at Odd Positions as the index of the list is zero-based. Now we should return these elements.Approach 1:Initialize a temporary value with ze 3 min read
- Java Program to Print the Elements of an Array Present on Odd Position An array stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. In an array, if there are "N" elements, the array iteration starts from 0 and ends with "N-1". The odd positions in the array are those with even indexing and vice versa. E 3 min read
- Java Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples: Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6 2 min read
- Java Program for Odd-Even Sort / Brick Sort This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in 2 min read
- Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve 3 min read
- First element that appears even number of times in an array Given an array, find the first element that appears even number of times in the array. It returns the element if exists otherwise returns 0. Examples: Input : arr[] = {1, 5, 4, 7, 4, 1, 5, 7, 1, 5}; Output : 4 Explanation, 4 is the first element that appears even number of times. Input : arr[] = {2, 7 min read
- Java Program to Check if a Given Integer is Odd or Even A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7, 7 min read