# Problem 25

### 18 comments on “Problem 25”

• Gourab

#include
int main(){
int a[20],n,i,c=0;
scanf(“%d”,&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
int m=n;
//shift if zero found and count zero
for(i=0;i<n;){
if(a[i]==0){
c++;
for(int j=i;j<n-1;j++){
a[j] = a[j+1];
}
n–;
}else i++;
}
for(i=m-c;i<m;i++){
a[i] = 0;
}
for(i=0;i<m;i++){
printf("%d ",a[i]);
}
return 0;
}

• Raman

#include
void main()
{
int a[20];
int b[20];
int n,i,j,count=0;

printf(“enter number of element”);
scanf(“%d”,&n);
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
j=0;
for(i=0;i<n;i++){
if(a[i]!=0){
b[j]=a[i];
j++;
}
else{
count++;
}
}
j=j-1; //becase j get incremented at the time of last assingment

for(i=0;i0){
b[++j]=0;
count–;
}
for(i=0;i<n;i++){
printf("%d",b[i]);
}
}

• rohan

#include
int main()
{
int i,j,l,n;
int x[]= {1, 2, 0, 4, 3, 0, 5, 0};
n=sizeof(x)/sizeof(x[0]);
l=n;
for(i=0;i<l;++i)
{
if(x[i]==0)
{
for(j=i;j<l-1;++j)
{
x[j]=x[j+1];
}
l–;
i–;
}
}
for(i=l;i<n;++i)
{
x[i]=0;
}
for(i=0;i<n;++i)
{
printf("%d",x[i]);
}

}

• Chandra Chandu

#include
using namespace std;
int main()
{
int i,j,n,p;
cin>>n;
vectorv;
int a[n];
for(i=0;i>a[i];
p=count(a,a+n,0);
for(i=0;i<n;i++)
{
if(a[i]!=0)
v.push_back(a[i]);
}
for(i=0;i<p;i++)
v.push_back(0);
for(i=0;i<v.size();i++)
cout<<v[i]<<" ";

}

• The Bong

public class Pushallthezero {
public static void main(String[] args) {
int a[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0};
a = pushAllZeroToEnd(a);
for(int nums : a) {
System.out.print(nums + ” “);
}
}
static int[] pushAllZeroToEnd(int a[]) {
int count = 0;
for (int i = 0; i < a.length; i++) {
if(a[i] != 0) {
a[count++] = a[i];
}
}
while (count < a.length) {
a[count++] = 0;
}
return a;
}
}

• RKS

Java Code———————————————————–
import java.util.*;
public class RotateZeroesInArray {
public static void main(String[] args) {
int[] arr = new int[] {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0};
ArrayList arr1 = new ArrayList();
for(int i=0;i<arr.length;i++)
{
if(arr[i]!=0)
{
}
}
for(int i=0;i<arr.length;i++)
{
if(arr[i]==0)
{
}
}
System.out.println(arr1);
}
}

• sakshi

#include
using namespace std;

void swap(int* x,int* y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int main()
{
int arr[] = {1,9,8,4,0,0,2,7,0,6,0};
int size = sizeof(arr)/sizeof(arr[0]);
int i,j;

for(i=0; i<size; i++)
{
if(arr[i]==0)
{
for( j=i+1; j<size; j++)
{
if(arr[j]!=0)
{
swap(arr[i],arr[j]);
break;
}
}
}
cout<<arr[i]<<" ";
}
return 0;
}

• Mohd Saif

#python easy code for this

arr=[1,9,8,4,0,0,2,7,0,6,0]
for i in arr:
if(i==0):
arr.remove(i)
arr.append(i)
print(arr)

• Mohd Saif

arr=[1,9,8,4,0,0,2,7,0,6,0]
count=0

for i in range(0,len(arr)):
if(arr[i]!=0):
arr[count]=arr[i]
count=count+1
while(count<len(arr)):
arr[count]=0
count=count+1

print(arr)

• rishabh trivedi

#python
def zero(arr):
for i in arr:
if i==0:
arr.remove(i)
arr.append(i)
print(arr)
arr=[]
for i in range(7):
a=int(input())
arr.append(a)
zero(arr)

• Ayantika

#include
void main()
{
int count=0,j=0;
int arr[]={12,56,0,1,2,3,0,9,7};
int n=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<n;i++)
{
if(arr[i]!=0)
{
arr[j]=arr[i];
j++;
}
}
count=n-j;
while(count)
{
arr[j]=0;
j++;
count–;
}
printf("\n\n\n");
for(int i=0;i<n;i++)
printf("\t%d",arr[i]);
}

• Ravisankar ka

//Java program

public class NewClass9 {
public static void main(String[] args) {
int arr[]={5,4,6,0,1,2,12,0,0,98,7,8,9,0};
int index=0;
for(int i=0;i<arr.length;i++){
if(arr[i]!=0){
arr[index]=arr[i];
index++;
}
}
while(index<arr.length){
arr[index]=0;
index++;
}
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}

}