VMWare Inc Interview Question for SDETs


Country: India




Comment hidden because of low score. Click to expand.
4
of 4 vote

Simple approach using O(n) algo.
Just need to keep two pointers for odd and even index and increase by 2 each, swap odd and even as required

#include "stdafx.h"
#include <iostream>

bool ArrangeEvenOddIndex(int evenOddArray[], int length)
{
	if (length % 2 != 0)
	{
		return false;
	}

	if (evenOddArray == NULL || length < 2)
	{
		return true;
	}

	int oddIndex = 1;
	int evenIndex = 0;
	while (oddIndex < length && evenIndex < length)
	{
		while (evenIndex < length && evenOddArray[evenIndex] % 2 == 0)
		{
			evenIndex += 2;
		}
		while (oddIndex < length && evenOddArray[oddIndex] % 2 == 1)
		{
			oddIndex += 2;
		}

		// Swap Even and Odd
		if (evenIndex < length && oddIndex < length)
		{
			int temp = evenOddArray[evenIndex];
			evenOddArray[evenIndex] = evenOddArray[oddIndex];
			evenOddArray[oddIndex] = temp;
		}
	}
	
	return true;
}

void PrintArray(int arr[], int length)
{
	for (int index = 0; index < length; index++)
	{
		std::cout << arr[index] << " ";
	}

	std::cout << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int oddVernArray[] = {2, 5, 8, 6, 11, 23, 18, 1, 9, 10};
	std::cout << "Before Arranging even and Odd" << std::endl;
	PrintArray(oddVernArray, 10);
	ArrangeEvenOddIndex(oddVernArray, 10);
	std::cout << "After Arranging even and Odd" << std::endl;
	PrintArray(oddVernArray, 10);
	return 0;
}

- pc May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

There is O(n) solution for it. take two pointers for odd and even index, keep moving both pointers by 2 and stop when it violates the rule. Swap both odd and even index till indexes reach the end

Here is a C++ working code for it

#include "stdafx.h"
#include <iostream>

bool ArrangeEvenOddIndex(int evenOddArray[], int length)
{
	if (length % 2 != 0)
	{
		return false;
	}

	if (evenOddArray == NULL || length < 2)
	{
		return true;
	}

	int oddIndex = 1;
	int evenIndex = 0;
	while (oddIndex < length && evenIndex < length)
	{
		while (evenIndex < length && evenOddArray[evenIndex] % 2 == 0)
		{
			evenIndex += 2;
		}
		while (oddIndex < length && evenOddArray[oddIndex] % 2 == 1)
		{
			oddIndex += 2;
		}

		// Swap Even and Odd
		if (evenIndex < length && oddIndex < length)
		{
			int temp = evenOddArray[evenIndex];
			evenOddArray[evenIndex] = evenOddArray[oddIndex];
			evenOddArray[oddIndex] = temp;
		}
	}
	
	return true;
}

void PrintArray(int arr[], int length)
{
	for (int index = 0; index < length; index++)
	{
		std::cout << arr[index] << " ";
	}

	std::cout << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int oddVernArray[] = {2, 5, 8, 6, 11, 23, 18, 1, 9, 10};
	std::cout << "Before Arranging even and Odd" << std::endl;
	PrintArray(oddVernArray, 10);
	ArrangeEvenOddIndex(oddVernArray, 10);
	std::cout << "After Arranging even and Odd" << std::endl;
	PrintArray(oddVernArray, 10);
	return 0;
}

- Anonymous May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Working java code. in O(n)

static void ArrangeOddAndEven(int[] arr) 
	{
		int OI=0;
		int EI=1;
		while(OI <= arr.length-2 && EI<= arr.length-1)
		{
			if( arr[OI] % 2 == 1 )
			{
				OI = OI+2;
			}
			else 
			{
				if( arr[EI] % 2 == 1)
				{
					int temp = arr[OI];
					arr[OI]=arr[EI];
					arr[EI]=temp;
				}
				else
				{
					EI = EI+2;
				}
			}				
		}
    }

- vbala1981 May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayEvenOdd {
	
	private static int[] array = {2,4,3,45,21,6,22,5,9,111};
	
	public static void main(String[] s)
	{
		int[] tempArray = new int[array.length];
		int oddCounter = 0;
		
		int evenCounter = 1;
		for(int i=0;i<array.length;i++)
		{
			boolean isOdd = false;
			if(i==array.length-1)
			{
				tempArray[i] = array[i];
			}
			else if(array[i]%2 != 0)
			{
				isOdd = true;
				tempArray[oddCounter] = array[i];
			}
			else if(array[i]%2 == 0)
			{
				tempArray[evenCounter] = array[i];
			}
			if(isOdd)
				oddCounter+=2;
			else
				evenCounter+=2;
		}
		for(int i=0;i<array.length;i++)
		{
			System.out.print(tempArray[i] + ",");
		}
	}

}

- Ramanathan May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayEvenOdd {
	
	private static int[] array = {2,4,3,45,21,6,22,5,9,111};
	
	public static void main(String[] s)
	{
		int[] tempArray = new int[array.length];
		int oddCounter = 0;
		
		int evenCounter = 1;
		for(int i=0;i<array.length;i++)
		{
			boolean isOdd = false;
			if(i==array.length-1)
			{
				tempArray[i] = array[i];
			}
			else if(array[i]%2 != 0)
			{
				isOdd = true;
				tempArray[oddCounter] = array[i];
			}
			else if(array[i]%2 == 0)
			{
				tempArray[evenCounter] = array[i];
			}
			if(isOdd)
				oddCounter+=2;
			else
				evenCounter+=2;
		}
		for(int i=0;i<array.length;i++)
		{
			System.out.print(tempArray[i] + ",");
		}
	}

}

- Ramanathan May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayEvenOdd {
	
	private static int[] array = {2,4,3,45,21,6,22,5,9,111};
	
	public static void main(String[] s)
	{
		int[] tempArray = new int[array.length];
		int oddCounter = 0;
		
		int evenCounter = 1;
		for(int i=0;i<array.length;i++)
		{
			boolean isOdd = false;
			if(i==array.length-1)
			{
				tempArray[i] = array[i];
			}
			else if(array[i]%2 != 0)
			{
				isOdd = true;
				tempArray[oddCounter] = array[i];
			}
			else if(array[i]%2 == 0)
			{
				tempArray[evenCounter] = array[i];
			}
			if(isOdd)
				oddCounter+=2;
			else
				evenCounter+=2;
		}
		for(int i=0;i<array.length;i++)
		{
			System.out.print(tempArray[i] + ",");
		}
	}

}

- Ramanathan May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayEvenOdd {
	
	private static int[] array = {2,4,3,45,21,6,22,5,9,111};
	
	public static void main(String[] s)
	{
		int[] tempArray = new int[array.length];
		int oddCounter = 0;
		
		int evenCounter = 1;
		for(int i=0;i<array.length;i++)
		{
			boolean isOdd = false;
			if(i==array.length-1)
			{
				tempArray[i] = array[i];
			}
			else if(array[i]%2 != 0)
			{
				isOdd = true;
				tempArray[oddCounter] = array[i];
			}
			else if(array[i]%2 == 0)
			{
				tempArray[evenCounter] = array[i];
			}
			if(isOdd)
				oddCounter+=2;
			else
				evenCounter+=2;
		}
		for(int i=0;i<array.length;i++)
		{
			System.out.print(tempArray[i] + ",");
		}
	}

}}

- Ramanathan May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayEvenOdd {
	
	private static int[] array = {2,4,3,45,21,6,22,5,9,111};
	
	public static void main(String[] s)
	{
		int[] tempArray = new int[array.length];
		int oddCounter = 0;
		
		int evenCounter = 1;
		for(int i=0;i<array.length;i++)
		{
			boolean isOdd = false;
			if(i==array.length-1)
			{
				tempArray[i] = array[i];
			}
			else if(array[i]%2 != 0)
			{
				isOdd = true;
				tempArray[oddCounter] = array[i];
			}
			else if(array[i]%2 == 0)
			{
				tempArray[evenCounter] = array[i];
			}
			if(isOdd)
				oddCounter+=2;
			else
				evenCounter+=2;
		}
		for(int i=0;i<array.length;i++)
		{
			System.out.print(tempArray[i] + ",");
		}
	}

}

- RAMANATHAN May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

l1 = [2, 1, 3, 4, 7, 9, 24, 98]
l2 = []
even_index = 0
odd_index = 1

for i in range(len(l1)):
     if l1[i]%2 == 0:
         l2.insert(even_index, l1[i])
         even_index = even_index + 2
     else:
         l2.insert(odd_index, l1[i])
         odd_index = odd_index + 2

print l2

- puneetha May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void oddEvenNos(int iArr[])
{
	int resulArr[SIZE];
	for(int k=0,i=0,j=1;k<SIZE;k++)
	{
		if(iArr[k]%2 ==1)
		{
			resulArr[i] = iArr[k];
			i+=2;
		}
		else
		{
			resulArr[j] = iArr[k];
			j+=2;
		}
	}
}

- sv May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayEvenOdd {
	
	private static int[] array = {2,24,98, 1,3,4,7,9};
	
	public static void main(String[] s)
	{
		int evenArray[] = new int[array.length/2];
		int oddArray[] = new int[array.length/2];
		int evenIdx=0;
		int oddIdx=0;
		
		for(int i=0; i<array.length; i++){
			if(array[i]%2==0)
				evenArray[evenIdx++] = array[i];
			else
				oddArray[oddIdx++] = array[i];
		}
		
		for(int i=0; i<array.length; i++){
			if(i%2==0) 
				array[i]= oddArray[i/2];
			else
				array[i] = evenArray[i/2];
		}
		
		for(int i=0;i<array.length;i++)
		{
			System.out.print(array[i] + ",");
		}
	}
	 
	 
}

- Anonymous May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A python script with the same logic as applied in rest of the answers

l = [2, 5, 8, 6, 11, 23, 18, 1, 9, 10]

even_ptr = 0
odd_ptr = 1

l_len = len(l)

while even_ptr < l_len and odd_ptr < l_len:
	if l[even_ptr]%2 == 0:
		even_ptr += 2
		continue
	if l[odd_ptr]%2 == 1:
		odd_ptr += 2
		continue
	l[even_ptr], l[odd_ptr] = l[odd_ptr], l[even_ptr]
	even_ptr += 2
	odd_ptr += 2

print l

- Anonymous May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python script with the same principal as applied in rest of the answers:-

l = [2, 5, 8, 6, 11, 23, 18, 1, 9, 10]

even_ptr = 0
odd_ptr = 1

l_len = len(l)

while even_ptr < l_len and odd_ptr < l_len:
	if l[even_ptr]%2 == 0:
		even_ptr += 2
		continue
	if l[odd_ptr]%2 == 1:
		odd_ptr += 2
		continue
	l[even_ptr], l[odd_ptr] = l[odd_ptr], l[even_ptr]
	even_ptr += 2
	odd_ptr += 2

print l

- barun sharma May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void rearrange(int[] arr){
    if(arr == null){
        throw new NullPointerException();
    }
    
    int oddIndex = 0;
    int evenIndex = 1;
    while(evenIndex < arr.length && oddIndex < arr.length){
        //find the next odd value to move
        while(arr[evenIndex] % 2 == 0 && evenIndex< arr.length){
            evenIndex+=2;
        }
        //find the next even value to move
        while(arr[oddIndex] % 2 == 1 && oddIndex < arr.length){
            oddIndex += 2;
        }
        //swap positions
        if(oddIndex < arr.length && evenIndex < arr.length){
            int temp = arr[evenIndex];
            arr[evenIndex] = arr[oddIndex];
            arr[oddIndex] = arr[evenIndex];
        }
    }
}

- zortlord May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void rearrange (int [] num) {		
		int odd = 0 , even = 1 ;
		for (int i = 0 ; i < num.length ; ++i) {
			while (odd < num.length && (num[odd] % 2) != 0) {
				odd +=2 ;
			}
			while (even < num.length && (num[even] % 2) == 0 ) {
				even +=2 ;
			}
			
			if (odd < num.length && (i % 2 != 0 && num[i] % 2 != 0)) {
				swap (num,odd, i) ;
			}
			
			if (even < num.length && (i % 2 == 0 && num[i] % 2 == 0)) {
				swap (num,even, i) ;
			}
		}
	}
	
	private void swap (int [] data , int i , int j){
		int tmp = data [i] ;
		data[i] = data[j] ;
		data[j] = tmp ;
	}

- Scott May 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void fun(int *a,int n)
{
int c1,c2,temp,temp1,index,index1,j,i;
c1=0;
c2=0;
for(j=0;j<n;j++)
{
if(a[j]%2==0 && (j+1)%2==0)
continue;
else
{
if(a[j]%2>0 && (j+1)%2>0)
continue;
else
{
if(a[j]%2==0)
{
c1++;
temp=a[j];
index=j;
if(c2>0)
{
a[index1]=a[j];
a[j]=temp1;
c2=0;
}
}
else
{
c2++;
temp1=a[j];
index1=j;
if(c1>0)
{
a[index]=a[j];
a[j]=temp;
c1=0;
}
}
}
}
}
printf(" now required array:=>");
for(i=0;i<n;i++)
printf("%d ",a[i]);

}

- mithleshtechno May 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void arrange_even_and_odd_numbers(vector<int>& arr)
{
    int odd = 0;
    int even = 1;

    while (odd < arr.size() && even < arr.size())
    {
        if (arr[odd] & 1)
            odd += 2;
        else if (!(arr[even] & 1))
            even += 2;
        else
            swap(arr[even], arr[odd]);
    }
}

- Sergey Dorofeev June 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Correction:

void arrange_even_and_odd_numbers(vector<int>& arr)
{
    int odd = 0;
    int even = 1;

    while (odd < arr.size() && even < arr.size())
    {
        if (arr[odd] & 1 == 1)
            odd += 2;
        else if ((arr[even] & 1) == 0)
            even += 2;
        else
        {
            swap(arr[even], arr[odd]);
            even += 2;
            odd += 2;
        }
    }
}

- Sergey Dorofeev August 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class ArrangeEvenOddNumbers
{
public static void main(String[] args){
  int arr = [];
  int anotherArr = [], odd=0, even=1;
  for (int i=0; i<arr.length; i++){
    if (arr[i] % 2 == 1){
     anotherArr[odd] = arr[i];
     odd = odd + 2; 
   } else {
     anotherArr[even] = arr[i];
     even = even + 2;
   }
   
}
}

- Simple Java code for this problem June 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[2,1,3,4,7,9,24,98]
def switch(a):
	array_len=len(a)
	i=0
	for i in range(len(a)):
		if(i%2==0):
			if a[i] % 2 !=0 :
				for j in reversed(range(len(a))):
					print j
					if j % 2 != 0:
						if a[j] % 2 ==0:
							temp=a[j]
							a[j]=a[i]
							a[i]=temp
							break


	
switch(a)
print a

- mallok July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[2,1,3,4,7,9,24,98]
def switch(a):
	array_len=len(a)
	i=0
	for i in range(len(a)):
		if(i%2==0):
			if a[i] % 2 !=0 :
				for j in reversed(range(len(a))):
					print j
					if j % 2 != 0:
						if a[j] % 2 ==0:
							temp=a[j]
							a[j]=a[i]
							a[i]=temp
							break


	
switch(a)
print a

- mallok July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[2,1,3,4,7,9,24,98]

def switch(a):

array_len=len(a)

i=0

for i in range(len(a)):

if(i%2==0):

if a[i] % 2 !=0 :

for j in reversed(range(len(a))):

print j

if j % 2 != 0:

if a[j] % 2 ==0:

temp=a[j]

a[j]=a[i]

a[i]=temp

break

switch(a)

print a

- mallok July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a=[2,1,3,4,7,9,24,98]

def switch(a):

array_len=len(a)

i=0

for i in range(len(a)):

if(i%2==0):

if a[i] % 2 !=0 :

for j in reversed(range(len(a))):

print j

if j % 2 != 0:

if a[j] % 2 ==0:

temp=a[j]

a[j]=a[i]

a[i]=temp

break

switch(a)

print a

- mallok July 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package algorithm;

public class question1 {

public void reArrangeArray(int[] data) {
boolean iFlip = false;
boolean jFlip = false;
for(int i = 0, j =1; (j < data.length && i < data.length); ) {
if(data[i]%2 == 0) {
i += 2;
} else {
iFlip = true;
}
if(data[j]%2 == 1) {
j += 2;
} else {
jFlip = true;
}
if(iFlip && jFlip) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
jFlip = false;
iFlip = false;
i += 2;
j += 2;
}
}

for(int i : data) {
System.out.println("data: " + i);
}
}

public static void main(String[] args) {
question1 q1 = new question1();
int[] data = {2,1,33,4,2,4,6,8,1,3,5,7,12,23,23,44};
q1.reArrangeArray(data);
}
}

- Devang July 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package algorithm;

public class question1 {

public void reArrangeArray(int[] data) {
boolean iFlip = false;
boolean jFlip = false;
for(int i = 0, j =1; (j < data.length && i < data.length); ) {
if(data[i]%2 == 0) {
i += 2;
} else {
iFlip = true;
}
if(data[j]%2 == 1) {
j += 2;
} else {
jFlip = true;
}
if(iFlip && jFlip) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
jFlip = false;
iFlip = false;
i += 2;
j += 2;
}
}

for(int i : data) {
System.out.println("data: " + i);
}
}

public static void main(String[] args) {
question1 q1 = new question1();
int[] data = {2,1,33,4,2,4,6,8,1,3,5,7,12,23,23,44};
q1.reArrangeArray(data);
}
}

- Devang July 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package algorithm;

public class question1 {

public void reArrangeArray(int[] data) {
boolean iFlip = false;
boolean jFlip = false;
for(int i = 0, j =1; (j < data.length && i < data.length); ) {
if(data[i]%2 == 0) {
i += 2;
} else {
iFlip = true;
}
if(data[j]%2 == 1) {
j += 2;
} else {
jFlip = true;
}
if(iFlip && jFlip) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
jFlip = false;
iFlip = false;
i += 2;
j += 2;
}
}

for(int i : data) {
System.out.println("data: " + i);
}
}

public static void main(String[] args) {
question1 q1 = new question1();
int[] data = {2,1,33,4,2,4,6,8,1,3,5,7,12,23,23,44};
q1.reArrangeArray(data);
}
}

- Devang July 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package algorithm;

public class question1 {

public void reArrangeArray(int[] data) {
boolean iFlip = false;
boolean jFlip = false;
for(int i = 0, j =1; (j < data.length && i < data.length); ) {
if(data[i]%2 == 0) {
i += 2;
} else {
iFlip = true;
}
if(data[j]%2 == 1) {
j += 2;
} else {
jFlip = true;
}
if(iFlip && jFlip) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
jFlip = false;
iFlip = false;
i += 2;
j += 2;
}
}

for(int i : data) {
System.out.println("data: " + i);
}
}

public static void main(String[] args) {
question1 q1 = new question1();
int[] data = {2,1,33,4,2,4,6,8,1,3,5,7,12,23,23,44};
q1.reArrangeArray(data);
}
}

- Devang July 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def arrangeOddEven(list):
rList = [0 for x in range(len(list))]
(oddIndex,evenIndex) = (0,1)
if (len(list) <=1):
return(list)
for i in list:
if (i % 2 == 0):
rList[evenIndex] = i
evenIndex+=2
else:
rList[oddIndex] = i
oddIndex+=2
return(rList)


list = [2,1,3,4,7,9,24,98]
print arrangeOddEven(list)

- Anonymous September 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{def arrangeOddEven(list):
rList = [0 for x in range(len(list))]
(oddIndex,evenIndex) = (0,1)
if (len(list) <=1):
return(list)
for i in list:
if (i % 2 == 0):
rList[evenIndex] = i
evenIndex+=2
else:
rList[oddIndex] = i
oddIndex+=2
return(rList)


list = [2,1,3,4,7,9,24,98]
print arrangeOddEven(list)
}

- Anonymous September 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The simplest O(N) solution would be :

public static void main(String[] args) {
    int[] a = new int[] { 2, 1, 3, 4, 7, 9, 24, 98 };
    evenoddNumbers(a);
    for (int i = 0; i < a.length; i++) {
      System.out.print(a[i] + " ");
    }
  }

  public static boolean evenoddNumbers(int[] a) {
    int odd = 0;
    int even = 1;
    int flag = 0;
    for (int i = 0; i < a.length; i++) {
      while (odd < a.length && even < a.length) {
        if (a[odd] % 2 != 0 && a[even] % 2 == 0) {
          odd = odd + 2;
          even = even + 2;
        } else if (a[odd] % 2 != 0) {
          odd = odd + 2;
        } else if (a[even] % 2 == 0) {
          even = even + 2;
        } else {
          int temp = a[odd];
          a[odd] = a[even];
          a[even] = temp;
          odd = odd + 2;
          even = even + 2;
        }

      }
    }
    return true;
  }

- Ajay October 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is O(m*n) which is O(N^2)

- Ajay October 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def placeEvenOdd(list1):
    for i in xrange(len(list1)-1):
        rem = 0
        if (list1[i]%2)==((i+1)%2):
            continue
        else:
            if list1[i] % 2 == 0:
                rem = 1
            for j in xrange(i+1, len(list1)):
                if list1[j]%2 == rem:
                    list1[i], list1[j] = list1[j], list1[i]
                    break

    print list1

if __name__ == "__main__":
    placeEvenOdd([2,1,3,4,7,9,24,98])

- Sunny January 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Working code in C++

#include <iostream>

using namespace std;

void RearrangeArray(int iArr[], int iNum)
{
    int iEvenIndex, iOddIndex, iEvenIndexFlag, iOddIndexFlag, iTempNum;
    iEvenIndex = 1;
    iOddIndex = 0;
    iEvenIndexFlag = 0;
    iOddIndexFlag = 0;

    while(iEvenIndex < iNum && iOddIndex < iNum)
    {
        if(iArr[iEvenIndex]%2 == 0)
        {
            //Number is even
            iEvenIndex += 2;
            iEvenIndexFlag = 0;
        }
        else
        {
            iEvenIndexFlag = 1;
        }

        if(iArr[iOddIndex]%2 == 1)
        {
            //Number is odd
            iOddIndex += 2;
            iOddIndexFlag = 0;
        }
        else
        {
            iOddIndexFlag = 1;
        }

        if(iEvenIndexFlag == 1 && iOddIndexFlag == 1)
        {
            iTempNum = iArr[iEvenIndex];
            iArr[iEvenIndex] = iArr[iOddIndex];
            iArr[iOddIndex] = iTempNum;
            iEvenIndex += 2;
            iOddIndex += 2;
        }
    }
}

void PrintArray(int iArr[], int iNum)
{
    for(int i=0; i<iNum; i++)
    {
        cout<<iArr[i]<<" ";
    }
    cout<<"\n";
}

int main()
{
    int iArr[] = {2, 1, 3, 4, 7, 9, 24, 98};
    int iNumOfElements;

    iNumOfElements= sizeof(iArr)/sizeof(int);

    //Print original array
    cout<<"\nOriginal array: ";
    PrintArray(iArr, iNumOfElements);

    //Rearrange arary with even and odd numbers at respective places
    RearrangeArray(iArr, iNumOfElements);

    //Print rearranged array
    cout<<"\nRearranged array: ";
    PrintArray(iArr, iNumOfElements);

    return 0;
}

- Tarun June 05, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class RearrangeOddEven
{
public static void main (String[] args) throws java.lang.Exception
{
int[] a = {2, 1, 3, 4, 7, 9, 24, 98};
rearrange(a);
System.out.println(Arrays.toString(a));
}

private static void swap(int i, int j, int[] a) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}


public static void rearrange(int[] a) {
for (int i=0; i<a.length-1; i++) {
if (a[i] % 2 == 0 && (i + 1 ) % 2 != 0) {
int oddIndex = getFirstOddIndex(a, i+1);
if (oddIndex != -1) {
swap(oddIndex, i, a);
}
} else if (a[i] % 2 != 0 && (i + 1 ) % 2 == 0) {
int evenIndex = getFirstEvenIndex(a, i+1);
if (evenIndex != -1) {
swap(evenIndex, i, a);
}
}


}
}

private static int getFirstOddIndex(int[] a, int startIndex) {
for (int i=startIndex; i<a.length; i++) {
if (a[i] % 2 != 0 && (i + 1) % 2 == 0) {
return i;
}

}
return -1;
}

private static int getFirstEvenIndex(int[] a, int startIndex) {
for (int i=startIndex; i<a.length; i++) {
if (a[i] % 2 == 0 && (i + 1) % 2 != 0) {
return i;
}

}
return -1;
}

}

- Pintu Das August 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		int[] a = {2, 1, 3, 4, 7, 9, 24, 98};
		rearrange(a);
		System.out.println(Arrays.toString(a));
	}
	
	private static void swap(int i, int j, int[] a) {
		int temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
	
	
	public static void rearrange(int[] a) {
		for (int i=0; i<a.length-1; i++) {
			if (a[i] % 2 == 0 && (i + 1 ) % 2 != 0) {
				int oddIndex = getFirstOddIndex(a, i+1);
				if (oddIndex != -1) {
					swap(oddIndex, i, a);
				}
			} else if (a[i] % 2 != 0 && (i + 1 ) % 2 == 0) {
				int evenIndex = getFirstEvenIndex(a, i+1);
				if (evenIndex != -1) {
					swap(evenIndex, i, a);
				}
			}
			
			
		}
	}
	
	private static int getFirstOddIndex(int[] a, int startIndex) {
		for (int i=startIndex; i<a.length; i++) {
			if (a[i] % 2 != 0 && (i + 1) % 2 == 0) {
				return i;
			}
			
		}
		return -1;
	}
	
	private static int getFirstEvenIndex(int[] a, int startIndex) {
		for (int i=startIndex; i<a.length; i++) {
			if (a[i] % 2 == 0 && (i + 1) % 2 != 0) {
				return i;
			}
			
		}
		return -1;
	}
	
}

- Pintu Das August 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		int[] a = {2, 1, 3, 4, 7, 9, 24, 98};
		rearrange(a);
		System.out.println(Arrays.toString(a));
	}
	
	private static void swap(int i, int j, int[] a) {
		int temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
	
	
	public static void rearrange(int[] a) {
		for (int i=0; i<a.length-1; i++) {
			if (a[i] % 2 == 0 && (i + 1 ) % 2 != 0) {
				int oddIndex = getFirstOddIndex(a, i+1);
				if (oddIndex != -1) {
					swap(oddIndex, i, a);
				}
			} else if (a[i] % 2 != 0 && (i + 1 ) % 2 == 0) {
				int evenIndex = getFirstEvenIndex(a, i+1);
				if (evenIndex != -1) {
					swap(evenIndex, i, a);
				}
			}
			
			
		}
	}
	
	private static int getFirstOddIndex(int[] a, int startIndex) {
		for (int i=startIndex; i<a.length; i++) {
			if (a[i] % 2 != 0 && (i + 1) % 2 == 0) {
				return i;
			}
			
		}
		return -1;
	}
	
	private static int getFirstEvenIndex(int[] a, int startIndex) {
		for (int i=startIndex; i<a.length; i++) {
			if (a[i] % 2 == 0 && (i + 1) % 2 != 0) {
				return i;
			}
			
		}
		return -1;
	}

}

- Pintu Das August 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrangArray {

public static void main(String[] args) {

int[] input = { 2, 1, 3, 4, 7, 9, 24, 98, 100 };

int[] temparray = new int[input.length];

int odd_index = 0, even_index = 1;

if (validateinputArray(input)) {

for (int i : input) {

if (i % 2 == 0) {
temparray[even_index] = i;
even_index += 2;
} else {
temparray[odd_index] = i;
odd_index += 2;
}
}
System.out.println("new array::" + Arrays.toString(temparray));
} else
System.out.println("invalid input::" + Arrays.toString(input));

}

public static boolean validateinputArray(int[] input) {

boolean rValue = false;
int evenNumbrs = 0;
int oddNumbers = 0;

for (int i : input) {

if (i % 2 == 0)
evenNumbrs++;
else
oddNumbers++;

}

if (input.length % 2 == 0) {
if (evenNumbrs == oddNumbers)
rValue = true;
} else if (oddNumbers == (evenNumbrs + 1))
rValue = true;

return rValue;
}

}

- sandeepmailid October 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.my.practice;

public class ReArrangeNumbers {

public static void main(String[] args) {

int[] a = { 1, 3, 2, 5, 6, 8, 9 };

for(int each : a){
System.out.print( each + " ");
}
System.out.println();
System.out.println("---------After----------");

int totalOddArrayPositions = 0, totalOddNumbers = 0;
int totalEvenArrayPositions = 0, totalEvenNumbers = 0;

for (int i = 1; i < a.length + 1; i++) {

if (i % 2 == 0) {
totalEvenArrayPositions++;
} else {
totalOddArrayPositions++;
}

if (a[i - 1] % 2 == 0) {
totalEvenNumbers++;
} else {
totalOddNumbers++;
}
}

if ((totalEvenArrayPositions == totalEvenNumbers) && (totalOddArrayPositions == totalOddNumbers)) {

for (int i = 1; i < a.length + 1; i++) {

if ((i % 2 == 0) && (a[i - 1] % 2 == 0)) {
continue;
} else if ((i % 2 == 1) && (a[i - 1] % 2 == 1)) {
continue;
}
else {
int temp = a[i-1];
a[ i - 1] = a [i];
a[i] = temp;
}
}
} else {
System.out.println("Re-Arrangement is NOT Possible");
}

for(int each : a){
System.out.print( each + " ");
}
}

}

- Ranganath November 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.my.practice;

public class ReArrangeNumbers {

public static void main(String[] args) {

int[] a = { 1, 3, 2, 5, 6, 8, 9 };

for(int each : a){
System.out.print( each + " ");
}
System.out.println();
System.out.println("---------After----------");

int totalOddArrayPositions = 0, totalOddNumbers = 0;
int totalEvenArrayPositions = 0, totalEvenNumbers = 0;

for (int i = 1; i < a.length + 1; i++) {

if (i % 2 == 0) {
totalEvenArrayPositions++;
} else {
totalOddArrayPositions++;
}

if (a[i - 1] % 2 == 0) {
totalEvenNumbers++;
} else {
totalOddNumbers++;
}
}

if ((totalEvenArrayPositions == totalEvenNumbers) && (totalOddArrayPositions == totalOddNumbers)) {

for (int i = 1; i < a.length + 1; i++) {

if ((i % 2 == 0) && (a[i - 1] % 2 == 0)) {
continue;
} else if ((i % 2 == 1) && (a[i - 1] % 2 == 1)) {
continue;
}
else {
int temp = a[i-1];
a[ i - 1] = a [i];
a[i] = temp;
}
}
} else {
System.out.println("Re-Arrangement is NOT Possible");
}

for(int each : a){
System.out.print( each + " ");
}
}

}

- Ranganath November 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
package com.my.practice;

public class ReArrangeNumbers {

public static void main(String[] args) {

int[] a = { 1, 3, 2, 5, 6, 8, 9 };

for(int each : a){
System.out.print( each + " ");
}
System.out.println();
System.out.println("---------After----------");

int totalOddArrayPositions = 0, totalOddNumbers = 0;
int totalEvenArrayPositions = 0, totalEvenNumbers = 0;

for (int i = 1; i < a.length + 1; i++) {

if (i % 2 == 0) {
totalEvenArrayPositions++;
} else {
totalOddArrayPositions++;
}

if (a[i - 1] % 2 == 0) {
totalEvenNumbers++;
} else {
totalOddNumbers++;
}
}

if ((totalEvenArrayPositions == totalEvenNumbers) && (totalOddArrayPositions == totalOddNumbers)) {

for (int i = 1; i < a.length + 1; i++) {

if ((i % 2 == 0) && (a[i - 1] % 2 == 0)) {
continue;
} else if ((i % 2 == 1) && (a[i - 1] % 2 == 1)) {
continue;
}
else {
int temp = a[i-1];
a[ i - 1] = a [i];
a[i] = temp;
}
}
} else {
System.out.println("Re-Arrangement is NOT Possible");
}

for(int each : a){
System.out.print( each + " ");
}
}

}

}

- Ranganath November 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.my.practice;

public class ReArrangeNumbers {

    public static void main(String[] args) {

        int[] a = { 1, 3, 2, 5, 6, 8, 9 };

        for(int each : a){
            System.out.print( each + " ");
        }
        System.out.println();
        System.out.println("---------After----------");

        int totalOddArrayPositions = 0, totalOddNumbers = 0;
        int totalEvenArrayPositions = 0, totalEvenNumbers = 0;

        for (int i = 1; i < a.length + 1; i++) {

            if (i % 2 == 0) {
                totalEvenArrayPositions++;
            } else {
                totalOddArrayPositions++;
            }

            if (a[i - 1] % 2 == 0) {
                totalEvenNumbers++;
            } else {
                totalOddNumbers++;
            }
        }

        if ((totalEvenArrayPositions == totalEvenNumbers) && (totalOddArrayPositions == totalOddNumbers)) {

            for (int i = 1; i < a.length + 1; i++) {

                if ((i % 2 == 0) && (a[i - 1] % 2 == 0)) {
                    continue;
                } else if ((i % 2 == 1) && (a[i - 1] % 2 == 1)) {
                    continue;
                }
               else {
                       int temp = a[i-1];
                       a[ i - 1] = a [i];
                       a[i] = temp;
                }
            }
        } else {
            System.out.println("Re-Arrangement is NOT Possible");
        }
        
        for(int each : a){
            System.out.print( each + " ");
        }
    }

}

- Ranganath November 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void swap(int[] a, int i, int j)
        {
            var temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
        static void Main(string[] args)
        {
            int[] a = { 2, 1, 3, 4, 7, 9, 24, 28 };
            int lastie = -1, lastio = -1, ie, io;
            for (ie=0,io=1; ie < a.Length && io < a.Length; ie+=2, io += 2)
            {
                if (a[ie] % 2 != 0)
                {
                    if (lastie >= 0)
                    {
                        swap(a, ie, lastie);
                        lastie = -1;
                    }
                    else lastio = ie;
                }
                if (a[io] % 2 == 0)
                {
                    if (lastio >= 0)
                    {
                        swap(a, io, lastio);
                        lastio = -1;
                    }
                    else lastie = io;
                }
            }
        }

- Sattar Mustafin December 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#In python, I have written this code for this purpose -
# Idea is basically create a table of column index_type(even/odd), value_type and action

index_type value_type action
even even continue
odd odd continue
odd even seek for next even index, stop when first odd value is found
even odd seek for next odd index, stop when first even value is found

Implementation is below - (pre-condition can be added : len(all even) == len(all odd), i haven't added this for simplicity)

def even_at_even_odd_at_odd(a):
n = len(a)
for i in enumerate(a):
index = i[0]
item = i[1]
if (index+1)%2 == 0 and item%2 == 0:
continue
elif (index+1)%2 != 0 and item%2 != 0:
continue
elif (index+1)%2 == 0 and item%2 != 0:
next_odd=index+1
while next_odd<n-2 and a[next_odd]%2 != 0:
next_odd = next_odd +2
a[index], a[next_odd] = a[next_odd], a[index]
elif (index+1)%2 != 0 and item%2 == 0:
next_even=index+1
while next_even<n-2 and a[next_even]%2 == 0:
next_even = next_even +2
a[index], a[next_even] = a[next_even], a[index]
print a

if __name__ == "__main__"
a = [36, 2, 17, 19, 5, 4, 6, 8, 7, 9, 16, 18, 15, 13]
even_at_even_odd_at_odd(a)

- Mh Raies August 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void rearrange(int[] A){

List<Integer> l = new ArrayList<Integer>();

for(int n=A.length-1; n>=1; n--){


if(((n+1)%2 ==1 && A[n]%2==0) || ((n+1)%2 ==0 && A[n]%2==1))

l.add(n);

for(int j=0; j<A.length; j++)
{
if(l.contains(j) && n!=0 && A[(j-1)]%2 == 0)
{
int temp = A[j];
A[j] = A[j-1];
A[j-1] = temp;

}
}

}

}

- coolplus March 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void rearrange(int[] A){

        List<Integer> l = new ArrayList<Integer>();

        for(int n=A.length-1; n>=1; n--){


            if(((n+1)%2 ==1 && A[n]%2==0) ||  ((n+1)%2 ==0 && A[n]%2==1))

                l.add(n);

            for(int j=0; j<A.length; j++)
            {
                if(l.contains(j) && n!=0 && A[(j-1)]%2 == 0)
                {
                    int temp = A[j];
                    A[j] = A[j-1];
                    A[j-1] = temp;
                    
                }
            }

        }

    }

- coolplus March 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def rearrange(arr):
    n = len(arr)
    if n == 0 or n == 1:
        return
    odd = 0
    even = 1
    while odd < n and even < n:
        while odd < n and arr[odd] % 2 == 1:
            odd += 2
        while even < n and arr[even] % 2 == 0:
            even += 2
        if odd < n and even < n:
            arr[odd], arr[even] = arr[even], arr[odd]

- pb August 02, 2021 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More