Ash Dsa 1
Ash Dsa 1
Ash Dsa 1
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
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>
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