0% found this document useful (0 votes)
5 views

Ash Dsa 1

Uploaded by

ashwanth0811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Ash Dsa 1

Uploaded by

ashwanth0811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

Exp. No : 1.1
SEARCH INSERT POSITION
Date :

AIM:
To sorted array of distinct integers and a target value, return the index if the target is
found. If not, return the index where it would be if it were inserted in order.

PSEUDOCODE:

BEGIN
for( i=0;i<n;i++)
{
if(target==arr[i])
printf("%d",i);
else if(arr[i]<target&&arr[i+1]>target)
{
printf("%d",i+1);;
}

}
END

SOURCE CODE:

#include<stdio.h>
int main()
{
int i,n,target;
printf("Size :");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Target :");
scanf("%d",&target);
for( i=0;i<n;i++)
{
if(target==arr[i])
printf("%d",i);
else if(arr[i]<target&&arr[i+1]>target)
{
printf("Traget is at index %d",i+1);
}

}
}

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

OUTPUT:

RESULT:
Thus the program for finding the distinct target value is executed successfully and the
output is verified.

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

Exp. No : 1.2
MISSING NUMBER
Date :

AIM:
To find a missing number in a given array and return the only number in the range that is
missing from the array.

PSEUDOCODE:

BEGIN
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
qsort(arr,n,sizeof(int),cmp);
for( i=0;i<n;i++)
BEGIN
{
if(arr[i]-arr[i+1]!=-1)
{

printf("%d",arr[i]+1);
break;
}
END
}

END
SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
int *x=(int *)a;
int *y=(int *)b;
return *x-*y;
}
int main()
{
int n,i;
printf("Size :");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
qsort(arr,n,sizeof(int),cmp);
for( i=0;i<n;i++)
{
if(arr[i]-arr[i+1]!=-1)

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

printf("Missing number is %d",arr[i]+1);


break;
}

OUTPUT:

RESULT:
Thus the program for finding the missing number is executed successfully and the output is
verified.

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

Exp. No : 1.3
THIRD MAXIMUM NUMBER
Date :

AIM:
To find the third distinct maximum number in this array.

PSEUDOCODE:

BEGIN
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
BEGIN
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
arr[j]=-1;
}
}
END
qsort(arr,n,sizeof(int),cmp);
if(n>3)
printf("%d",arr[2]);
END

SOURCE CODE:

#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
int *x=(int *)a;
int *y=(int *)b;
return *y-*x;
}
int main()
{
int n,i,j;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
if(n<3)
{
if(arr[0]>arr[1])
{
printf("%d",arr[0]);
}
else {

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

printf("%d",arr[1]);
}
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j]){
arr[j]=-1;
}
}
}
qsort(arr,n,sizeof(int),cmp);

} {
if(arr[i]-arr[i+1]!=-1)

OUTPUT:

RESULT:
Thus the program for finding the distinct third maximum number is executed successfully
and the output is verified.

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

Exp. No : 1.4
VALID ANAGRAM
Date :

AIM:
To find whether the given two strings are anagram or not anagram.

PSEUDOCODE:

BEGIN
gets(a);
gets(b);
x=strlen(a);
y=strlen(b);
if(x!=y)
printf("false");
else
{
qsort(a,x,sizeof(char),cmp);
qsort(b,y,sizeof(char),cmp);
for( i=0;i<x;i++)
{
if(a[i]!=b[i])
f=1;
}
if(f==0)
printf("true");
else
printf("false");
}
END

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int cmp(const void *a,const void *b)


{
char *x=(char *)a;
char *y=(char *)b;
return *x-*y;
}
int main()
{
char a[20],b[20];
int x,y,f=0,i;
gets(a);
gets(b);
x=strlen(a);

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

y=strlen(b);
if(x!=y)
printf("false");
else
{
qsort(a,x,sizeof(char),cmp);
qsort(b,y,sizeof(char),cmp);
for( i=0;i<x;i++)
{
if(a[i]!=b[i])
f=1;
}
if(f==0)
{
printf("true");
}
else
{
printf("false");
}
}
}

OUTPUT:

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND ANALYSIS

RESULT:
Thus the program for finding the distinct third maximum number is executed successfully
and the output is verified.

ROLL NO:717823P106
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING 23CSR304 DATA DESIGN AND
ANALYSIS

ROLL
NO:717823P106

You might also like