Search This Blog

C Program to Burst Consecutively Repeated Numbers From Array - Zoho Programming Round Question

This was one of the programming round (hands on coding) questions in recruitment process of Zoho, an IT company. The question is to write a c program to remove all consecutive repetition of numbers in given array. We should also keep in mind that, if we remove one set of consecutive repeated number, this removal may cause another repetition as illustrated in following example.

example array: 2  3  5  3  8  8  8 3  3 5  3  1  2

Solving: 2  3  5  3  8  8  8  3  3  5  3  1  2   ->  2  3  5  3  3  3  5  3  1  2 -> 2  3  5  5  3  1  2 -> 2  3   3  1  2   ->  2  1  2
output:  2  1  2
I hope that you understood how the removal of consecutive appearance of numbers should be. I hope the program will explain you more.



#include<stdio.h>

void main()

{
int a[20],i,j,k,n;
printf("Enter Number of elements");
scanf("%d",&n);
printf("Enter elements");
for(i=0;i<n;i++)
 scanf("%d",&a[i]);
i=0;
for(;i<n-1;)
 {
 j=1;
 while(i+j<n&&a[i+j]==a[i])
  j++;
 if(j==1)
  {
  i++;
  continue;
  }

 for(k=0;k<n-(i+j);k++)
  a[i+k]=a[i+j+k];
 n-=j;
 if(i>0)
  i--;
 }

printf("Output:\n");
for(i=0;i<n;i++)
 printf("%d",a[i]);
getch();
}

No comments: