WAP in JAVA to input a number of 10 elements in array and remove all zeroes .
Do in JAVA only.
Answers
Answered by
2
Move all zeroes to end of array
Given an array of random numbers, Push all the zero’s of a given array to the end of the array. For example, if the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. The order of all other elements should be same. Expected time complexity is O(n) and extra space is O(1).
Example:
Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0};
Output : arr[] = {1, 2, 4, 3, 5, 0, 0};
Input : arr[] = {1, 2, 0, 0, 0, 3, 6};
Output : arr[] = {1, 2, 3, 6, 0, 0, 0};
Answered by
1
take into consideration u have input ed all the elements...
in array b[] of space 10
______________
class hi
{
void find{}
{
int c=0; // this is for counting how many elements are without 0
int i,j;
int a[]=new int[10]; /* create new array with space 10 as no of elements not equal to 0 cannot exceed 10*/
for(i=0;i<10;i++)
{
if(b[i]!=0)
{
a[c]=b[i];
c++;
}
for(j=0;j<c;j++)
System.out.println(a[j]); // elements without 0
}
}
hope it helps
in array b[] of space 10
______________
class hi
{
void find{}
{
int c=0; // this is for counting how many elements are without 0
int i,j;
int a[]=new int[10]; /* create new array with space 10 as no of elements not equal to 0 cannot exceed 10*/
for(i=0;i<10;i++)
{
if(b[i]!=0)
{
a[c]=b[i];
c++;
}
for(j=0;j<c;j++)
System.out.println(a[j]); // elements without 0
}
}
hope it helps
Similar questions