## Facebook Interview Question for Software Engineer / Developers

Country: UK
Interview Type: In-Person

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

``````/*
Student, International Islamic University Chittagong
**/

#include <bits/stdc++.h>
using namespace std;

int zeroHater(int arra[100], int n) {

int i, k = 0;
for(i=0; i<n; i++) {
if(arra[i]!=0) {
arra[k++] = arra[i];
}
}

for(i=k; i<n; i++)
arra[i] = 0;
return k;

}

int main() {
int i, n, m;
int arra[100];

scanf("%d", &n);

for(i=0; i<n; i++) {
scanf("%d", &arra[i]);
}

m = zeroHater(arra, n);

for(i=0; i<n; i++)
printf("%d ", arra[i]);
printf("\nNon-zero elements: %d\n", m);

return 0;
}``````

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

int[] arr = { 1, 2, 0, 5, 3, 0, 4, 0 };

int endIndex = arr.Length - 1;
int foundZeros = 0;
for (int i = 0; i <= endIndex; i++)
{
if (arr[i] == 0)
{
foundZeros++;
while (arr[endIndex] == 0)
{
endIndex--;
foundZeros++;
}

if (i >= endIndex) break;

arr[i] = arr[endIndex];
arr[endIndex] = 0;
}
}

Console.WriteLine(foundZeros);

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

``````int ReArrange(int[] arr)
{
int endIndex = arr.Length - 1;
int foundZeros = 0;
for (int i = 0; i <= endIndex; i++)
{
if (arr[i] == 0)
{
foundZeros++;
if (i == endIndex) break;

while (arr[endIndex] == 0)
{
endIndex--;
foundZeros++;
}

if (i >= endIndex) break;

arr[i] = arr[endIndex];
arr[endIndex] = 0;
endIndex--;
}
}

return arr.Length - foundZeros;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ {{{ int ReArrange(int[] arr) { int endIndex = arr.Length - 1; int foundZeros = 0; for (int i = 0; i <= endIndex; i++) { if (arr[i] == 0) { foundZeros++; if (i == endIndex) break; while (arr[endIndex] == 0) { endIndex--; foundZeros++; } if (i >= endIndex) break; arr[i] = arr[endIndex]; arr[endIndex] = 0; endIndex--; } } return arr.Length - foundZeros; } }}}
Comment hidden because of low score. Click to expand.
0
of 0 vote

``````int ReArrange(int[] arr)
{
int endIndex = arr.Length - 1;
int foundZeros = 0;
for (int i = 0; i <= endIndex; i++)
{
if (arr[i] == 0)
{
foundZeros++;
if (i == endIndex) break;

while (arr[endIndex] == 0)
{
endIndex--;
foundZeros++;
}

if (i >= endIndex) break;

arr[i] = arr[endIndex];
arr[endIndex] = 0;
endIndex--;
}
}

return arr.Length - foundZeros;
}``````

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

``````#include<iostream>
using namespace std;
main()
{
int n;
cout<<"Number of elements : ";
cin>>n;
int *a = new int[n];

cout<<"Enter "<<n<<" numbers(with space) : ";
for(int i=0;i<n;i++)
cin>>a[i];
int k=n-1;
int p=0;
int temp;
for(int i=0;i<n-p;i++)
{

if(a[i]==0)
{
while(a[k]==0 && k!=i)
{
k-=1;
p+=1;
}
temp=a[i];
a[i]=a[k];
a[k]=temp;
k-=1;
p+=1;
}
}
cout<<"\nRearranged array : ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";

cout<<"\nTotal non-zero element	: "<<n-p<<endl;
return 0;

}``````

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

``````package vij.dynaprogramming;

import vij.dynaprogramming.common.Util;

import java.util.Arrays;

/**
* Created by Vijay on 2017, April 08.
* This class focuses on all array rearranging related problems.
* Can be sorting, arrange based on customized requirement, etc.
*/
public class ArrayRearrange {

public static void main(String[] args) {

rearrangeArrayNonzeroFirstZeroLast(new int[]{99,0,1,0,4,0,87,0,9,7,8,11,23,1,1});
}

/**
* This method will rearrange the given integer array
* by moving all the non-zero numbers to beginning of the array and
* all zeroes to end of the array.
* Note: The non-zero numbers' sorting is not mandatory
*
* @param sourceArr int[]
*/
private static void rearrangeArrayNonzeroFirstZeroLast(int[] sourceArr) {

int sourceArrLen = sourceArr.length;
System.out.println("ARRAY SIZE["+sourceArrLen+"] Source Array:" + Arrays.toString(sourceArr));

int beginIdx = 0;
int endIdx = sourceArrLen - 1;

while (beginIdx < endIdx) {

if (sourceArr[beginIdx] == 0 && sourceArr[endIdx] == 0) {
//begin part is not fine, end part is fine
//move the end part and retain the begin part to swap
--endIdx;
} else if (sourceArr[beginIdx] == 0 && sourceArr[endIdx] != 0) {
//begin part is not fine and end part is not fine
//swap both items
//move the begin and end part further
swap(sourceArr, beginIdx, endIdx);
++beginIdx;
--endIdx;
} else if (sourceArr[beginIdx] != 0 && sourceArr[endIdx] == 0) {
//begin part and end part satisfies the requirement
//move further on both sides
++beginIdx;
--endIdx;
} else if (sourceArr[beginIdx] != 0 && sourceArr[endIdx] != 0) {
//begin part is fine, end part is not
//move the begin part and retain the end part to swap
++beginIdx;
}

}

System.out.println("Array after re-arranging:" + Arrays.toString(sourceArr));
}

public static void swap(int[] sourceArr, int idx1, int idx2) {

int temp = sourceArr[idx1];
sourceArr[idx1] = sourceArr[idx2];
sourceArr[idx2] = temp;
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ {{{ {{{import java.util.Arrays; /** * Created by Vijay on 2017, April 08. * This class focuses on all array rearranging related problems. * Can be sorting, arrange based on customized requirement, etc. */ public class ArrayRearrange { public static void main(String[] args) { rearrangeArrayNonzeroFirstZeroLast(new int[]{99,0,1,0,4,0,87,0,9,7,8,11,23,1,1}); } /** * This method will rearrange the given integer array * by moving all the non-zero numbers to beginning of the array and * all zeroes to end of the array. * Note: The non-zero numbers' sorting is not mandatory * * @param sourceArr int[] */ private static void rearrangeArrayNonzeroFirstZeroLast(int[] sourceArr) { int sourceArrLen = sourceArr.length; System.out.println("ARRAY SIZE["+sourceArrLen+"] Source Array:" + Arrays.toString(sourceArr)); int beginIdx = 0; int endIdx = sourceArrLen - 1; while (beginIdx < endIdx) { if (sourceArr[beginIdx] == 0 && sourceArr[endIdx] == 0) { //begin part is not fine, end part is fine //move the end part and retain the begin part to swap --endIdx; } else if (sourceArr[beginIdx] == 0 && sourceArr[endIdx] != 0) { //begin part is not fine and end part is not fine //swap both items //move the begin and end part further swap(sourceArr, beginIdx, endIdx); ++beginIdx; --endIdx; } else if (sourceArr[beginIdx] != 0 && sourceArr[endIdx] == 0) { //begin part and end part satisfies the requirement //move further on both sides ++beginIdx; --endIdx; } else if (sourceArr[beginIdx] != 0 && sourceArr[endIdx] != 0) { //begin part is fine, end part is not //move the begin part and retain the end part to swap ++beginIdx; } } System.out.println("Array after re-arranging:" + Arrays.toString(sourceArr)); } public static void swap(int[] sourceArr, int idx1, int idx2) { int temp = sourceArr[idx1]; sourceArr[idx1] = sourceArr[idx2]; sourceArr[idx2] = temp; } } }}}
Comment hidden because of low score. Click to expand.
0
of 0 vote

``````public static int rearrange(final int[] numbers){
int zeroStartIndex = -1;
int zeroEndIndex = -1;
final int length = numbers.length;
for(int i = 0 ;i < length&&zeroStartIndex<length&&zeroEndIndex<length; i++){
if(numbers[i] != 0 ){
if(zeroStartIndex > -1) {
SwapUtil.swap(numbers, zeroStartIndex, i);
zeroStartIndex++;
zeroEndIndex++;
}
}else {
if(zeroStartIndex == -1){
zeroStartIndex = zeroEndIndex = i;
}else {
zeroEndIndex++;
}
}
}
return (zeroStartIndex == -1 ) ? 0 : (zeroEndIndex - zeroStartIndex)+1;
}``````

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

``````private static int rearrange(int[] numbers){
int zeroStartIndex = -1;
int zeroEndIndex = -1;
int length = numbers.length;
for(int i = 0 ;i < length&&zeroStartIndex<length&&zeroEndIndex<length; i++){
if(numbers[i] != 0 ){
if(zeroStartIndex > -1) {
SwapUtil.swap(numbers, zeroStartIndex, i);
zeroStartIndex++;
zeroEndIndex++;
}
}else {
if(zeroStartIndex == -1){
zeroStartIndex = zeroEndIndex = i;
}else {
zeroEndIndex++;
}
}
}
return (zeroStartIndex == -1 ) ? 0 : (zeroEndIndex - zeroStartIndex)+1;
}``````

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

``````public static int sideLineZeros(int []arr){
int offset = arr.length - 1;
for (int i = 0; i <= offset; i++ ){
if (arr[i] == 0)
offset = swapZeros(arr, i, offset);
}
return offset;
}

private static int swapZeros(int[] arr, int index, int offset) {
int j;
for (j = offset; j > index; j--){
if (arr[j] != 0){
arr[index] = arr[j];
arr[j] = 0;
break;
}
}
return j;
}

public static void main(String[] args) {

int [] arr = {1,2,0,5,3,0,4,0};

System.out.println(sideLineZeros(arr));

}``````

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

``````import java.util.*;

public class RearrangeArray {

public int rearrange(int[] nums) {
int i = 0, j = nums.length - 1;
while(i < j) {
while(i < j && nums[i] != 0) i++;
while(i < j && nums[j] == 0) j--;
if(i < j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

return i;
}

public static void main(String[] args) {
RearrangeArray r = new RearrangeArray();
int[] nums = new int[]{1, 2, 0, 5, 3, 0, 4, 0 };
int count = r.rearrange(nums);
System.out.println(count + "," + Arrays.toString(nums));
}
}``````

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

Python

``````def theArrangement(input):
count = 0
cursor = len(input) -1
for i in range(len(input)):
if i > cursor: break
if input[i] == 0:
while input[cursor] != 0:
input[cursor], input [i] = input[i], input[cursor]
cursor -= 1
count += 1
break
else:
count += 1
return count``````

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

public static int movenonzerotoleft(int iArray[])
{

try{

int iNonZeros=0;
int iZeros=0;
int endIndex=iArray.length-1;

// This is to calculate the end index non zero value

for( int iEnd=endIndex;iEnd>=0;iEnd--)
{
System.out.println("End is "+iArray[iEnd]);

if(iArray[iEnd]==0)
{
endIndex--;
iZeros++;
}
else
break;
}

System.out.println("End index is now "+endIndex);

// This is to swap the zero values to the last

for(int i=0;i<=endIndex;i++)
{

if(iArray[i] ==0)
{

int temp =iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while(temp==0) {endIndex--; temp =iArray[endIndex];iZeros++;}

iArray[endIndex]=0;
iArray[i]=temp;
endIndex--;
iZeros++;

}

}

//To Print the array
System.out.print("{");
for(int i=0;i<iArray.length;i++)
System.out.print( iArray[i]+" ");

System.out.println(" }");

System.out.println("No of zeros "+iZeros);

iNonZeros=iArray.length-iZeros;

return iNonZeros;

}catch(Exception e){}

return -1;

}

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

``````public static int movenonzerotoleft(int iArray[])
{

try{

int iNonZeros=0;
int iZeros=0;
int endIndex=iArray.length-1;

// This is to calculate the end index non zero value

for( int iEnd=endIndex;iEnd>=0;iEnd--)
{
System.out.println("End is "+iArray[iEnd]);

if(iArray[iEnd]==0)
{
endIndex--;
iZeros++;
}
else
break;
}

System.out.println("End index is now "+endIndex);

// This is to swap the zero values to the last

for(int i=0;i<=endIndex;i++)
{

if(iArray[i] ==0)
{

int temp =iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while(temp==0)	{endIndex--; temp =iArray[endIndex];iZeros++;}

iArray[endIndex]=0;
iArray[i]=temp;
endIndex--;
iZeros++;

}

}

//To Print the array
System.out.print("{");
for(int i=0;i<iArray.length;i++)
System.out.print( iArray[i]+" ");

System.out.println(" }");

System.out.println("No of zeros "+iZeros);

iNonZeros=iArray.length-iZeros;

return iNonZeros;

}catch(Exception e){}

return -1;``````

}

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

public class Movenonzeroleft {

public static int ivalues[] = new int[] { 1, 0, 0, 0, 5, 6, 0, 9, 0, 0, 0, 3, 0, 0, 0 };

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("No of Non Zeros " + movenonzerotoleft(ivalues));
}

public static int movenonzerotoleft(int iArray[]) {

try {

int iNonZeros = 0;
int iZeros = 0;
int endIndex = iArray.length - 1;

// This is to calculate the end index non zero value

for (int iEnd = endIndex; iEnd >= 0; iEnd--) {
System.out.println("End is " + iArray[iEnd]);

if (iArray[iEnd] == 0) {
endIndex--;
iZeros++;
} else
break;
}

System.out.println("End index is now " + endIndex);

// This is to swap the zero values to the last

for (int i = 0; i <= endIndex; i++) {

if (iArray[i] == 0) {

int temp = iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while (temp == 0) {
endIndex--;
temp = iArray[endIndex];
iZeros++;
}

iArray[endIndex] = 0;
iArray[i] = temp;
endIndex--;
iZeros++;

}

}

// To Print the array
System.out.print("{");
for (int i = 0; i < iArray.length; i++)
System.out.print(iArray[i] + " ");

System.out.println(" }");

System.out.println("No of zeros " + iZeros);

iNonZeros = iArray.length - iZeros;

return iNonZeros;

} catch (Exception e) {
}

return -1;

}

}

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

``````public class Movenonzeroleft {

public static int ivalues[] = new int[] { 1, 0, 0, 0, 5, 6, 0, 9, 0, 0, 0, 3, 0, 0, 0 };

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("No of Non Zeros " + movenonzerotoleft(ivalues));
}

public static int movenonzerotoleft(int iArray[]) {

try {

int iNonZeros = 0;
int iZeros = 0;
int endIndex = iArray.length - 1;

// This is to calculate the end index non zero value

for (int iEnd = endIndex; iEnd >= 0; iEnd--) {
System.out.println("End is " + iArray[iEnd]);

if (iArray[iEnd] == 0) {
endIndex--;
iZeros++;
} else
break;
}

System.out.println("End index is now " + endIndex);

// This is to swap the zero values to the last

for (int i = 0; i <= endIndex; i++) {

if (iArray[i] == 0) {

int temp = iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while (temp == 0) {
endIndex--;
temp = iArray[endIndex];
iZeros++;
}

iArray[endIndex] = 0;
iArray[i] = temp;
endIndex--;
iZeros++;

}

}

// To Print the array
System.out.print("{");
for (int i = 0; i < iArray.length; i++)
System.out.print(iArray[i] + " ");

System.out.println(" }");

System.out.println("No of zeros " + iZeros);

iNonZeros = iArray.length - iZeros;

return iNonZeros;

} catch (Exception e) {
}

return -1;

}

}``````

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

public class Movenonzeroleft {

public static int ivalues[] = new int[] { 1, 0, 0, 0, 5, 6, 0, 9, 0, 0, 0, 3, 0, 0, 0 };

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("No of Non Zeros " + movenonzerotoleft(ivalues));
}

public static int movenonzerotoleft(int iArray[]) {

try {

int iNonZeros = 0;
int iZeros = 0;
int endIndex = iArray.length - 1;

// This is to calculate the end index non zero value

for (int iEnd = endIndex; iEnd >= 0; iEnd--) {
System.out.println("End is " + iArray[iEnd]);

if (iArray[iEnd] == 0) {
endIndex--;
iZeros++;
} else
break;
}

System.out.println("End index is now " + endIndex);

// This is to swap the zero values to the last

for (int i = 0; i <= endIndex; i++) {

if (iArray[i] == 0) {

int temp = iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while (temp == 0) {
endIndex--;
temp = iArray[endIndex];
iZeros++;
}

iArray[endIndex] = 0;
iArray[i] = temp;
endIndex--;
iZeros++;

}

}

// To Print the array
System.out.print("{");
for (int i = 0; i < iArray.length; i++)
System.out.print(iArray[i] + " ");

System.out.println(" }");

System.out.println("No of zeros " + iZeros);

iNonZeros = iArray.length - iZeros;

return iNonZeros;

} catch (Exception e) {
}

return -1;

}

}

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

``````public class Movenonzeroleft {

public static int ivalues[] = new int[] { 1,4,5,0,6,7,0 };

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("No of Non Zeros " + movenonzerotoleft(ivalues));
}

public static int movenonzerotoleft(int iArray[]) {

try {

int iNonZeros = 0;
int iZeros = 0;
int endIndex = iArray.length - 1;

// This is to calculate the end index non zero value

for (int iEnd = endIndex; iEnd >= 0; iEnd--) {
System.out.println("End is " + iArray[iEnd]);

if (iArray[iEnd] == 0) {
endIndex--;
iZeros++;
} else
break;
}

System.out.println("End index is now " + endIndex);

// This is to swap the zero values to the last

for (int i = 0; i <= endIndex; i++) {

if (iArray[i] == 0) {

int temp = iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while (temp == 0) {
endIndex--;
temp = iArray[endIndex];
iZeros++;
}

iArray[endIndex] = 0;
iArray[i] = temp;
endIndex--;
iZeros++;

}

}

// To Print the array
System.out.print("{");
for (int i = 0; i < iArray.length; i++)
System.out.print(iArray[i] + " ");

System.out.println(" }");

System.out.println("No of zeros " + iZeros);

iNonZeros = iArray.length - iZeros;

return iNonZeros;

} catch (Exception e) {
}return -1;``````

}}

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

public class Movenonzeroleft {

public static int ivalues[] = new int[] { 1, 4, 5, 0, 6, 7, 0 };

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("No of Non Zeros " + movenonzerotoleft(ivalues));
}

public static int movenonzerotoleft(int iArray[]) {

try {

int iNonZeros = 0;
int iZeros = 0;
int endIndex = iArray.length - 1;

// This is to calculate the end index non zero value

for (int iEnd = endIndex; iEnd >= 0; iEnd--) {
System.out.println("End is " + iArray[iEnd]);

if (iArray[iEnd] == 0) {
endIndex--;
iZeros++;
} else
break;
}

System.out.println("End index is now " + endIndex);

// This is to swap the zero values to the last

for (int i = 0; i <= endIndex; i++) {

if (iArray[i] == 0) {

int temp = iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while (temp == 0) {
endIndex--;
temp = iArray[endIndex];
iZeros++;
}

iArray[endIndex] = 0;
iArray[i] = temp;
endIndex--;
iZeros++;

}

}

// To Print the array
System.out.print("{");
for (int i = 0; i < iArray.length; i++)
System.out.print(iArray[i] + " ");

System.out.println(" }");

System.out.println("No of zeros " + iZeros);

iNonZeros = iArray.length - iZeros;

return iNonZeros;

} catch (Exception e) {
}

return -1;

}

}

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

JAVA

``````public class Movenonzeroleft {

public static int ivalues[] = new int[] { 1, 4, 5, 0, 6, 7, 0 };

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("No of Non Zeros " + movenonzerotoleft(ivalues));
}

public static int movenonzerotoleft(int iArray[]) {

try {

int iNonZeros = 0;
int iZeros = 0;
int endIndex = iArray.length - 1;

// This is to calculate the end index non zero value

for (int iEnd = endIndex; iEnd >= 0; iEnd--) {
System.out.println("End is " + iArray[iEnd]);

if (iArray[iEnd] == 0) {
endIndex--;
iZeros++;
} else
break;
}

System.out.println("End index is now " + endIndex);

// This is to swap the zero values to the last

for (int i = 0; i <= endIndex; i++) {

if (iArray[i] == 0) {

int temp = iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while (temp == 0) {
endIndex--;
temp = iArray[endIndex];
iZeros++;
}

iArray[endIndex] = 0;
iArray[i] = temp;
endIndex--;
iZeros++;

}

}

// To Print the array
System.out.print("{");
for (int i = 0; i < iArray.length; i++)
System.out.print(iArray[i] + " ");

System.out.println(" }");

System.out.println("No of zeros " + iZeros);

iNonZeros = iArray.length - iZeros;

return iNonZeros;

} catch (Exception e) {
}

return -1;

}

}``````

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

``````public class Movenonzeroleft {
public static int ivalues[] = new int[] { 1, 4, 5, 0, 6, 7, 0 };
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("No of Non Zeros " + movenonzerotoleft(ivalues));
}
public static int movenonzerotoleft(int iArray[]) {
try {
int iNonZeros = 0;
int iZeros = 0;
int endIndex = iArray.length - 1;
// This is to calculate the end index non zero value
for (int iEnd = endIndex; iEnd >= 0; iEnd--) {
System.out.println("End is " + iArray[iEnd]);
if (iArray[iEnd] == 0) {
endIndex--;
iZeros++;
} else
break;
}
System.out.println("End index is now " + endIndex);
// This is to swap the zero values to the last
for (int i = 0; i <= endIndex; i++) {
if (iArray[i] == 0) {
int temp = iArray[endIndex];
// This is to make sure the endIndex is a non zero value
while (temp == 0) {
endIndex--;
temp = iArray[endIndex];
iZeros++;
}
iArray[endIndex] = 0;
iArray[i] = temp;
endIndex--;
iZeros++;
}
}
// To Print the array
System.out.print("{");
for (int i = 0; i < iArray.length; i++)
System.out.print(iArray[i] + " ");
System.out.println(" }");
System.out.println("No of zeros " + iZeros);
iNonZeros = iArray.length - iZeros;
return iNonZeros;
} catch (Exception e) {
}
return -1;
}``````

}

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

``public class Movenonzeroleft { public static int ivalues[] = new int[] { 1, 4, 5, 0, 6, 7, 0 }; public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("No of Non Zeros " + movenonzerotoleft(ivalues)); } public static int movenonzerotoleft(int iArray[]) { try { int iNonZeros = 0; int iZeros = 0; int endIndex = iArray.length - 1; // This is to calculate the end index non zero value for (int iEnd = endIndex; iEnd >= 0; iEnd--) { System.out.println("End is " + iArray[iEnd]); if (iArray[iEnd] == 0) { endIndex--; iZeros++; } else break; } System.out.println("End index is now " + endIndex); // This is to swap the zero values to the last for (int i = 0; i <= endIndex; i++) { if (iArray[i] == 0) { int temp = iArray[endIndex]; // This is to make sure the endIndex is a non zero value while (temp == 0) { endIndex--; temp = iArray[endIndex]; iZeros++; } iArray[endIndex] = 0; iArray[i] = temp; endIndex--; iZeros++; } } // To Print the array System.out.print("{"); for (int i = 0; i < iArray.length; i++) System.out.print(iArray[i] + " "); System.out.println(" }"); System.out.println("No of zeros " + iZeros); iNonZeros = iArray.length - iZeros; return iNonZeros; } catch (Exception e) { } return -1; }``

}

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

Can't paste my code due to some error. Putting down my logic here

1) First find the end index of the array and make sure its a non zero value . For loop the array from back till you encounter the first non zero value and make that as your end index
2) Now loop the array from starting till end index. Inside the loop if you encounter a zero value swap it with the end index value and reduce the end index by 1
3) During this swapping make sure , the end index value is a non zero [ while loop till you encounter non zero && array current position less than end index
4) Add zeroCounter in all the above scenarios and subtract it from array length during the end to get non zero count value

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

``````var a = [1,2,0,5,3,0,4,0]
var k = a.count - 1// next 0 position
var i = 0
while i < a.count && i < k {
if a[i] == 0 {
let t = a[i]; a[i] = a[k]; a[k] = t;
k -= 1
}
else {
i += 1
}
}
print(a)
let zeros = a.count - k
print("zeros: \(zeros)")``````

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

``````let nonSorted = [1,0,0,2,3,4,5,6,0,2,3].sorted(by: { \$0 > \$1 })
print(nonSorted)
if let index = nonSorted.index(of: 0) {
print(index)``````

}

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

using PHP:

\$array = array(1,2,0,5,3,0,4,0);
\$result = array();
\$foundZero = false;
foreach (\$array as \$arr) {
if (!\$foundZero) {
if (\$arr > 0) {
\$result[] = \$arr;
} else {
\$foundZero = true;
\$result[] = \$arr;
}
} else {
\$length = sizeof(\$result);
if (\$arr > 0 ) {
for (\$i = \$length; \$i > 0; \$i--) {

\$result[\$i] = \$result[\$i-1];
}
\$result[0] = \$arr;
} else {
\$result[] = \$arr;
}
}
}

print_r(\$result);

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

Using PHP:

``````\$array = array(1,2,0,5,3,0,4,0);
\$result = array();
\$foundZero = false;
foreach (\$array as \$arr) {
if (!\$foundZero) {
if (\$arr > 0) {
\$result[] = \$arr;
} else {
\$foundZero = true;
\$result[] = \$arr;
}
} else {
\$length = sizeof(\$result);
if (\$arr > 0 ) {
for (\$i = \$length; \$i > 0; \$i--) {

\$result[\$i] = \$result[\$i-1];
}
\$result[0] = \$arr;
} else {
\$result[] = \$arr;
}
}
}

print_r(\$result);``````

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

``````\$array = array(1,2,0,5,3,0,4,0);
\$result = array();
\$foundZero = false;
foreach (\$array as \$arr) {
if (!\$foundZero) {
if (\$arr > 0) {
\$result[] = \$arr;
} else {
\$foundZero = true;
\$result[] = \$arr;
}
} else {
\$length = sizeof(\$result);
if (\$arr > 0 ) {
for (\$i = \$length; \$i > 0; \$i--) {

\$result[\$i] = \$result[\$i-1];
}
\$result[0] = \$arr;
} else {
\$result[] = \$arr;
}
}
}

print_r(\$result);``````

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

``````\$array = array(1,2,0,5,3,0,4,0);
\$result = array();
\$foundZero = false;
foreach (\$array as \$arr) {
if (!\$foundZero) {
if (\$arr > 0) {
\$result[] = \$arr;
} else {
\$foundZero = true;
\$result[] = \$arr;
}
} else {
\$length = sizeof(\$result);
if (\$arr > 0 ) {
for (\$i = \$length; \$i > 0; \$i--) {

\$result[\$i] = \$result[\$i-1];
}
\$result[0] = \$arr;
} else {
\$result[] = \$arr;
}
}
}

print_r(\$result);``````

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

``````public class NonZeroOnLeft {
public static void run(int[] src) {
for (int i = -1, j = src.length; i < j;) {
while (i < src.length - 1 && src[++i] != 0)
;
while (j > 0 && src[--j] == 0)
;
if (i >= j)
break;
int t = src[i];
src[i] = src[j];
src[j] = t;
}
}
}``````

test case

``````@Test
public void test() {
int length = new Random().nextInt(20) + 10;
int[] src = new int[length];
for (int i = 0; i < length; i++) {
src[i] = new Random().nextInt(5);
}
System.out.println(Arrays.toString(src));
NonZeroOnLeft.run(src);
System.out.println(Arrays.toString(src));
boolean zeroFound = false;
for (int i = 0; i < src.length; i++) {
if (src[i] == 0)
zeroFound = true;
Assert.assertTrue(zeroFound && src[i] == 0 || !zeroFound && src[i] != 0);
}
}``````

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

# Code in Python 3

import random

num_list = []
out_list = []
count = 0
n = int(input('Please enter the number of elements : '))

while len(num_list) < n:
num_list.append(random.randrange(0, 5))

print(num_list)

for j in num_list:
if j > 0:
out_list.append(j)
count += 1

for j in num_list:
if j == 0:
out_list.append(j)

print('There are', count, 'non-zero numbers and the list is as follows\n', out_list)

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

``````# Code in Python 3

import random

num_list = []
out_list = []
count = 0
n = int(input('Please enter the number of elements : '))

while len(num_list) < n:
num_list.append(random.randrange(0, 5))

print(num_list)

for j in num_list:
if j > 0:
out_list.append(j)
count += 1

for j in num_list:
if j == 0:
out_list.append(j)

print('There are', count, 'non-zero numbers and the list is as follows\n', out_list)``````

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

``````public int rearrangeArray(int [] array){

int tempZeroIdx = array.length-1;
int count = 0;
for(int i=array.length-1;i>=0;i-- ){
if(array[i] == 0){
if(tempZeroIdx != i){
swap(array,i,tempZeroIdx);
}
tempZeroIdx--;
count++;
}
}

return count;
}

private void swap(int[] array, int a,int b){
int temp = array[a];
array[a]=array[b];
array[b]=temp;
}``````

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

``````object Solution {

val sc = new java.util.Scanner(System.in)
val n = sc.nextInt
val array = new Array[Int](n)
for (i <- 0 until n) {
array(i) = sc.nextInt
}

array
}

def rearrange(array: Array[Int]): Int = {
var z = array.length - 1
var i = 0

def swap(array: Array[Int], left: Int, right: Int) = {
if (left < right && array(left) == 0) {
array(left) = array(right)
array(right) = 0
}
}

def findRightMostNonZero(array: Array[Int], right: Int): Int = {
var r = right
while (r >= 0 && array(r) == 0) r -= 1
r
}

while (i <= z) {
if (array(i) == 0) {
z = findRightMostNonZero(array, z)
swap(array, i, z)
}

if (i <= z) i += 1
}

i
}

def main(args: Array[String]) {

println(rearrange(array))
println(array.mkString(" "))
}

}``````

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

can be done in O(n) .

Since the order of non zero numbers does not matter!

algo:

Identify the first zero from the start.
Identify the first non zero from the end.
if no cross over
switch.
continue until crossover.

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

can be done in O(n) .

Since the order of non zero numbers does not matter!

algo:

Identify the first zero from the start.
Identify the first non zero from the end.
if no cross over
switch.
continue until crossover.

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

can be done in O(n) .

Since the order of non zero numbers does not matter!

algo:

Identify the first zero from the start.
Identify the first non zero from the end.
if no cross over
switch.
continue until crossover.

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

this solution will work for begining zeros, multiple simultaneous zeros, ending zero like -> {0,1,0,5,0,0,4,0,0}

``````package me.satyen.code;

public class Integers {
public static int rearrangeInts(int[] arr){
int cnt = -1;
int indxZ = -1;
int indxNZ = -1;
boolean foundZero = false;
for(int i=0; i< arr.length; i++){
//find first zero index first time only.
if(!foundZero && arr[i] == 0){
indxZ = i;
foundZero = true;
}else if (arr[i] != 0){		//find the non zero index
indxNZ = i;
//if we already found zero and we got non zero index then swap
//and increment zero index
if(foundZero){
swap(arr, indxZ++, indxNZ);
}
}
}
return cnt;
}
public static void swap(int[] arr, int indx1, int indx2){
int tmp = arr[indx1];
arr[indx1] = arr[indx2];
arr[indx2] = tmp;
}
public static void main(String[] args){
int[] arr = new int[]{0,1,0,5,0,0,4,0,0};
rearrangeInts(arr);
}
}``````

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

Wow, I'm glad I program in Python. Those other solutions look like a headache.

``````def nzero(aa):
pp=0 #point to target
if not aa:return 0
c=0
for i in xrange(len(aa)):
if aa[i]==0:c+=1 #count
else: #Swap
temp=aa[pp]
aa[pp]=aa[i]
aa[i]=temp
pp+=1
if pp>len(aa):break
return aa``````

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

``````int Rearrange(vector<int> &a)
{
int i = 0;
int j = a.size() - 1;
while (i <= j) {
while (i <= j &&
i >= 0 &&
i < a.size() &&
a[i] != 0)
{
++i;
}
while (i <= j &&
j >= 0 &&
a[j] == 0)
{
--j;
}
if (i < j) {
swap(a[i], a[j]);
}
}
return i;
}``````

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

My python solution

``````arr = [1,0,0,0,3,0,4,0]
k = 0
for i in range(len(arr)):
if arr[i] != 0:
arr[k] = arr[i]
k += 1

for i in range(k,len(arr)):
arr[i] = 0

print arr
print len(arr)-k``````

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

Stream.concat(numbers.stream().filter(c->c!=0),numbers.stream().filter(c->c==0)).forEach(System.out::println);

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

``Stream.concat(numbers.stream().filter(c->c!=0),numbers.stream().filter(c->c==0)).forEach(System.out::println);``

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

public int findSepLoc(int[] arr) {
int n= arr.length;
int i=0, j=n-1;
while(i<j) {
while(arr[j]==0) {
j--;
}
while(arr[i]!=0) {
i++;
}
swap(arr,i,j);
i++;j--;
}
}

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

``````int zeros2Right(int[] a) {
int countZeros = 0;
int bIndex = 0;
int[] b = new int[a.length + 1];

for (int i = 0; i < a.length; i++) {
if (a[i] != 0) {
b[bIndex] = a[i];
bIndex++;
} else {
countZeros++;
}
}

a = b;
System.out.println(Arrays.toString(a));
return a.length - countZeros - 1;
}``````

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

``````int arr[] = {1,2,0,5,3,0,4,0};
int newArr[] = new int[arr.length];

for(int i=0, k=arr.length-1, j=0; j<arr.length ;j++) {
int curr = arr[j];
if(curr > 0) {
newArr[i] = curr;
i++;
}
else {
newArr[k] = curr;
k--;
}
}

System.out.println(Arrays.toString(newArr));``````

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

int main() {
//int numbers[] = {1,2,0,5,3,0,4,0};
static int numbers[100];

int i,j,n;
int size = sizeof(numbers)/sizeof(int);

scanf("%d", &n);

for (i = 0 ; i < n; i++) {
scanf("%d", &numbers[i]);
}

for (i = 0; i < n; i++) {
printf("%d,",numbers[i]);
}

printf("\n\n");

for (i = 0, j = 0; i < n; i++) {
if (numbers[i] == 0 && numbers[j] == 0)
continue;

if (numbers[i] != 0){
if (numbers[j] == 0) {
numbers[j] = numbers[i];
numbers[i] = 0;
}
j++;
}

}

for (i = 0; i < n; i++) {
printf("%d,",numbers[i]);
}

return 0;
}

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

``````int main() {
//int numbers[] = {1,2,0,5,3,0,4,0};
static int numbers[100];

int i,j,n;
int size = sizeof(numbers)/sizeof(int);

scanf("%d", &n);

for (i = 0 ; i < n; i++) {
scanf("%d", &numbers[i]);
}

for (i = 0; i < n; i++) {
printf("%d,",numbers[i]);
}

printf("\n\n");

for (i = 0, j = 0; i < n; i++) {
if (numbers[i] == 0 && numbers[j] == 0)
continue;

if (numbers[i] != 0){
if (numbers[j] == 0) {
numbers[j] = numbers[i];
numbers[i] = 0;
}
j++;
}

}

for (i = 0; i < n; i++) {
printf("%d,",numbers[i]);
}

return 0;
}``````

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

``````void rearrange(vector<int>& v) {
int start = 0, end = v.size() - 1;

for (; start < end; ) {
while (v[start] > 0 && start < end)
++start;

while (v[end] == 0 && end > start)
--end;

if (v[start] == 0 && v[end] > 0)
swap(v[start], v[end]);
}``````

}

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

``````void rearrange(vector<int>& v) {
int start = 0, end = v.size() - 1;

for (; start < end; ) {
while (v[start] > 0 && start < end)
++start;

while (v[end] == 0 && end > start)
--end;

if (v[start] == 0 && v[end] > 0)
swap(v[start], v[end]);
}
}``````

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.

### 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.