In: Computer Science
Write a program that takes a set of m numbers up to size n and save them in an array. The program then allows you to search for any number in the array with O(1).
#include<stdio.h>
int main()
{
int n,m,i,ele;
printf("enter the size of the array: ");
scanf("%d",&n);
printf("enter the numer of elemnts you want: ");
scanf("%d",&m);
int arr[n];
if(m>n)
return;
else
{
for(i=0;i<n;i++)
arr[i]=-1;
for(i=0;i<n;i++)
{
printf("enter
the number: ");
scanf("%d",&ele);
if(arr[ele%n]==-1)
{
arr[ele%n]=ele;
}
else
{
printf("search is not possible with
o(1)");
return;
}
}
}
}
search of element is not possible in o(1) if user enter the
value
1) which we want to insert into the position and position which is already filled with another element
2)which is greater than the size of the array and that position is already filled with another element.