Wipro Coding Question 3
Arrange the numbers
Here, in this page we will discuss the Wipro coding question asked in previous recruitment drive. We will write an efficient algorithm for the given question to arrange the numbers in the given array and there solution in C++ and Java.
Question 3
You are playing an online game. In the game, a list of N numbers is given. The player has to arrange the numbers so that all the odd numbers of the list come after the even numbers. Write an algorithm to arrange the given list such that all the odd numbers of the list come after the even numbers.
Input
- The first line of the input consists of an integer numbers, representing the size of the list(N).
- The second line of the input consists of N space-separated integers representing the values of the list
Output
Print N space-separated integers such that all the odd numbers of the list come after the even numbers
Example
Sample Input
8
10 98 3 33 12 22 21 11
Sample Output
10 98 12 22 3 33 21 11
C++
Java
C++
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0; i<n; i++)
cin>>a[i];
int p1=0, p2= n-1;
while(p1 < p2){
while(a[p1]%2 == 0 and p1<p2)
p1++;
while(a[p2]%2 != 0 and p1<p2)
p2--;
if(p1 < p2){
swap(a[p1], a[p2]);
p1++;
p2--;
}
}
for(int i=0; i<n; i++)
cout<<a[i]<<" ";
return 0;
}
Java
import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt ();
int p1 = 0, p2 = n - 1;
while (p1 < p2)
{
while (arr[p1] % 2 == 0 && p1 < p2)
p1++;
while (arr[p2] % 2 != 0 && p1 < p2)
p2--;
if (p1 < p2)
{
int temp = arr[p1];
arr[p1] = arr[p2];
arr[p2] = temp;
p1++;
p2--;
}
}
for (int i = 0; i < n; i++)
System.out.print (arr[i] + " ");
}
}