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:
Post a Comment