In this post, we will learn How to write a program to move all negative elements to end in an int array with or without using existing data Structures in java?
Approach 1 : without using existing Data Structures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.kkjavatutorials.client; /** * @author KK JavaTutorials * Write a program to move all negative elements to end in an int array * without using existing data structures */ public class MoveNegativeArrayElementsAtLast_1 { public static void main(String[] args) { //input Array int arr[] = {3, -3, 45, 2, -19, -7, 56, 45 }; int n = arr.length; moveAllNegativeElementsAtLastInArray(arr,n); //Printing array after moving all negative elements in last for (int i : arr) { System.out.print(i+" "); } } private static void moveAllNegativeElementsAtLastInArray(int[] arr, int n) { //Creating temporary array with same size as input array int tempAll[] = new int[n]; //Start index of tempAll[] array int j =0; //Storing positive or zero elements in tempAll[] array for (int i = 0; i < arr.length; i++) { if(arr[i]>=0) { tempAll[j++] = arr[i]; } } //If all numbers are either positive or zeros if(j == n || j == 0) return; //Storing negative elements in tempAll[] array for (int i = 0; i < arr.length; i++) { if(arr[i] < 0) { tempAll[j++] = arr[i]; } } //Copy all elements from tempAll[] to arr[] array for (int i = 0; i < tempAll.length; i++) { arr[i] = tempAll[i]; } } } |
Approach 2: with using existing Data Structures
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.kkjavatutorials.client; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @author KK JavaTutorials * Write a program to move all negative elements to end in an int array * with using existing data structures */ public class MoveNegativeArrayElementsAtLast_2 { public static void main(String[] args) { //End of the program allNumbers will have final output List<Integer> allNumbers = new ArrayList<>(); //We will store all negative elements in this list List<Integer> negativeNumbers = new ArrayList<>(); //Input array int arr[] = {3, -3, 45, 2, -19, -7, 56, 45 }; for (Integer data : arr) { if(data >=0) { //Storing positive or zero elements allNumbers.add(data); }else { //Storing negative elements negativeNumbers.add(data); } } //Adding all negative elements in allNumbers list allNumbers.addAll(negativeNumbers); //Printing output for (int data : allNumbers) { System.out.print(data+" "); } } } |
Output of above program:
3 45 2 56 45 -3 -19 -7
You may also like :
Difference between List and Set in Java Collection ?
When should we choose LinkedList over ArrayList for a given Scenario and Why?
Is there concurrent version for TreeMap and TreeSet in Java Collections Framework?
What is difference between Collections. Sort() and Arrays.sort()? Which one is better with respect to time efficiency?
What are fail-fast and fail-safe Iterator?
What is difference between Iterator and LisIterator?
Why Prime Numbers are considered in writing certain algorithms like hashcode()?
That’s all about Write a program to move all negative elements to end in an int array ?
If you have any feedback or suggestion please feel free to drop in blow comment box.