DUET PG Score Card 2021
NHAI Recruitment 2021
Bihar Board 12th Dummy Admit Card 2022
RBI Assistant Notification 2021
Jee Advanced 2021 Result
NIACL Assistant Recruitment 2021
SBI PO Online Form 2021

Write a class ReorderArray with a public method reorder that takes one parameter arr of type int[] and returns the arr such that all even numbers in the array come to the front of the arr

Write a class ReorderArray with a public method reorder that takes one parameter arr of type int[] and returns the arr such that all even numbers in the array come to the front of the arr.

Assumptions:

  1. arr is never null

Here is an example:

Enter no of elements in the array:
6
Enter elements in the array seperated by space:
3 5 6 4 2 4
6
4
2
4
3
5

Solution:

package q11090;

public class ReorderArray {
  /** *Arrange all even numbers to infront of the array
  *
  *
  *
  * 
  @return result */
  public int[] reorder(int[] arr) { 
      //Write your code here
      int size = arr.length;
      int temp=0,count=0;
      for(int i = 0; i < size; i++){
          if(arr[i]%2==0){
              for(int j=i; j > count; j--){
                  temp = arr[j-1];
                  arr[j-1] = arr[j];
                  arr[j] = temp;
                  
              } count++;
              
          }
          
      }
      return arr;
      
  }
    
}

Candidates having any kind of query so he/she can ask in our comment section. Our Panel will b reverting back as soon as possible. Thank You. For more details visit our website (https://speedjobalert.com/)

Leave a Comment