Java Program to Store Even & Odd Elements of an Array into Separate Arrays Last Updated : 13 Apr, 2022 Comments Improve Suggest changes 5 Likes Like Report 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) Create Quiz Comment D dhatriganda07 Follow 5 Improve D dhatriganda07 Follow 5 Improve Article Tags : Java Java Programs Java-Array-Programs Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like