## Yahoo Interview Question

**Country:**-

THERE WILL BE NO ANOTHER ARRAY a[] i think so.....YOU SHOULD ARRANGE THEM IN THE MAIN ARRAY ITSELF

instead of using another array use 2 pointers i and j such that i=0 and j=last index

now traverse the array with k pointer and if i==k do no-op and if k=j end the loop else if arr[k] returns 0 swap with a[i] and i++ and if it returns 1 swap with arr[j] and j-- .

space complexity O(1) and time complexity O(n)

Counting sort will do this in O(N + 2), but it will also require O(N + 2) extra space.

if (1<<N > UINT_MAX) then, below procedure can be followed.

```
void arrange(int Arr[], int Arr_len)
{
unsigned int chk = 0;
for (int i=0; i<Arr_len; i++)
{
chk = chk | 1<<i;
}
int a = 0;
int b = 0;
while(1)
{
if(chk&(1<<a)<1)
{
a++;
}
if(chk&(1<<b)>1)
{
b++;
}
if(a>=Arr_len && b>=Arr_len) break;
swap(Arr[a], Arr[b]);
chk = chk | 1<<b;
}
}
```

Since no one has actually posted code for it, here is a solution using a 2 color Dutch National Flag algorithm. O(N) time, O(1) space

```
public class YahooPartitionArray {
public static void main(String[] args) {
int a[] = new int[] {4,5,2,4,3,9,8,1};
partition(a, new SomeInterface() {
@Override
public int someFunction(int i) {
if (i % 2 == 0) {
return 1;
}
else {
return 0;
}
}
});
System.out.println(Arrays.toString(a));
}
interface SomeInterface {
public int someFunction(int i);
}
// Use Dutch National Flag algorithm with 2 colors.
//
// O(N) time, O(1) space
static void partition(int a[], SomeInterface s) {
int i = 0, j = a.length - 1;
while (i < j) {
int result = s.someFunction(a[i]);
if (result == 0) {
i++;
}
else {
swap(a, i, j--);
}
}
}
static void swap(int a[], int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
```

We can update the values in the same input array only. Let assume a condition of number is even/odd and the function returns 1 if that is odd else 0. Now start traversing array from the 0th index and check util it reached to endIndex. Keep all the even on the left side and odd values to the right side. If the number is even , just increment startIndex and proceed else swap the values and decrement endIndex and proceed.

```
// odd or even
bool isOdd(int n)
{
return (n % 2 == 0 ? 0: 1);
}
// Arrange element of the array
void arrangeArray(int * A, int size )
{
// Start from left
int startIndex = 0; // start index
int endIndex = size -1; // end index
int temp = 0;
while ( startIndex <= endIndex)
{
if ( isOdd(A[startIndex]))
{ // Store odd number at the end , decrement end index
temp = A[endIndex];
A[endIndex] = A[startIndex];
A[startIndex] = temp;
endIndex--;
}
else{
startIndex++;
}
}
}
```

#include<stdio.h>

int main()

{

int arr[10]={7,6,4,3,2,9,0,1,5,8};

int i=0,j=9;

while(i<j)

{

if(!func(arr[i]) || func(arr[j]))

{

if(!func(arr[i]) && func(arr[j]))

{

swapper(arr,i,j);

i++;

j--;

}

else if (!func(arr[i]))

{

j--;

}

else

{

i++;

}

}

else

{

i++;j--;

}

}

int k;

for(k=0;k<10;k++)

printf("%d ",arr[k]);

}

int func(int a)

{

return a%2;

}

void swapper(int arr[],int i,int j)

{

int temp;

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}

public class Solution

{

public static void main(String arg[])

{

int array[]={45,43,12,78,12,15,14};

int i=0;

int j=array.length-1;

while(i<j)

{

while(func(array[i])==1)

i++;

while(func(array[j])==0)

j--;

if(i<j)

{

int temp=array[j];

array[j]=array[i];

array[i]=temp;

}

}

for(int num : array)

{

System.out.print(num + " ");

}

}

public static int func(int array)

{

System.out.println(array%2);

return (array%2);

}

}

Use Insertion sort properties i.e if function return 1 insert it into the place after number whose return is 1 otherwise leave at its own place.

let a[] be d array...where v have 2 store the number.

- Anonymous October 25, 2011let i point's to 0th loc of a[] and j point's to last index(last index is nothing but d last index of given array.)

check if func returns 1 then store that number at ith loc and increment i by 1.

if func returns 0 then store that number at jth loc and decrement j by 1.

finally array a[] wil have required result.