Question

In: Computer Science

Write a program that takes a set of m numbers up to size n and save...

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).

Solutions

Expert Solution

#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.


Related Solutions

Write a program in C or C++ that takes a number series of size n (n...
Write a program in C or C++ that takes a number series of size n (n integers) as input from the user, push all the numbers to the stack, and reverse the stack using recursion. Please note that this is not simply popping and printing the numbers, but the program should manipulate the stack to have the numbers stored in reverse order. In addition to the provided header file, the students can use the following function to print the content...
Write a C++ program that takes in a set of daily average temperatures (up to a...
Write a C++ program that takes in a set of daily average temperatures (up to a maximum of 30): 1.Ask the user for a temperature 2.If the user enters a -1 then stop asking for temperatures. 3. After the user is done entering temperatures: a. Print out the temperatures entered. b. print out the average, high and low temperatures. To get average, use: average = (sum of temps) divided by (count of temps) to get max or min, either keep...
Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Write a program in JAVA to create the move set of a Pokémon, and save that...
Write a program in JAVA to create the move set of a Pokémon, and save that move set to a file. This program should do the following: Ask for the pokemon’s name. Ask for the name, min damage, and max damage of 4 different moves. Write the move set data into a file with the pokemon’s name as the filename. The format of the output file is up to you, but keep it as simple as possible
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT)...
PART 1: WRITE A PROGRAM THAT TAKES IN TWO INTEGERS VALUE N AND M (USER INPUT) AND CALCULATES THE NTH FIBONACCI SUM USING RECURSION. EXAMPLE: OLD VERSION 0,1,1,2,3.. USING USER INPUT: 0, N, M ,N+M, (N+M)+M PART 2: WRITE THE SAME PROGRAM USING USER INPUT BUT USING A LOOP IN STEAD OF RECURSION. PYTHON
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
in Java, write a program that takes an input of 4 numbers and splits them into...
in Java, write a program that takes an input of 4 numbers and splits them into 4 separate lines. EXAMPLE: so if input is 1994 the output should be 1 9 9 4
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT