No. of Distinct Elements in a sorted array

No. of Distinct Elements in a sorted array

 Write a program to find the number of distinct elements in a sorted array.
Input Format: Input consists of n+1 integers.
The first integer corresponds to n, the number of elements in the array.
The next n integers correspond to the elements in the array.
Assume that the maximum value of n is 15.
Output Format: Output consists of a single integer which corresponds to the number of distinct elements in the array.
Sample Input:
5
3
3
3
78
90
Sample Output: 3

Image result for NO. OF DISTINCT ELEMENTS IN A SORTED ARRAY

#include < stdio.h >
#include < stdlib.h >
int *createarray(int n)
{
int *a;
a=(int *)malloc(sizeof(int)*n);
return a;
}
void readarray(int *a,int n)
{
int i;
for( n > 0 )
 {
scanf("%d",(a+i));
 }
}
void countdistinct(int *a,int n)
{
 int i,count=0;
for( n )
{
if( )
count++;
 }
 printf("%d",count);
 }
int main()
{
int *a,n;
 scanf("%d",&n);
a=createarray(n);
 readarray(a,n);
countdistinct(a,n);
return 0;
}

No comments