Question

In this programming exercise you will create an algorithm for solving the following version of the...

In this programming exercise you will create an algorithm for solving the following version of the m Smallest Numbers problem.   Instead of just returning the m smallest values as in homework 1, you want to return a list of the positions where the m smallest values are located without changing the original array.

Your algorithm should meet the following specifications:

mSmallest( L[1..n], m )

Pre: L is a list of distinct integer values. n is the number of elements in the list. 0 < mn

Post: A list [p1, p2, …, pm] has been returned where pj is the position of the jth smallest element of L. The original list L has not been changed.

Determine the time complexity and space complexity of your algorithm.

Use Java or Python to implement your algorithm. Your program should:

prompt the user to enter the list of numbers

prompt the user to enter the value for m

print a list of the positions where the m smallest values are located.

You may assume that the elements of the list are unique.

Homework Answers

Answer #1

KthSmallestelement.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

public class KthSmallestelement {

   public static void main(String[] args) {

       PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
       HashMap<Integer, Integer> map= new HashMap<Integer, Integer>();
       List<Integer> list = new ArrayList<Integer>();
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter the number of elements");
       int n= sc.nextInt();
       int values[] = new int[n];
       System.out.println("Enter Elements");
       for(int i=0;i<n;++i){
           values[i] = sc.nextInt();
           pq.offer(values[i]);
           map.put(values[i], i);
       }
      
       System.out.println("Enter the number of smallest values");
       int m= sc.nextInt();
      
       for(int i=1;i<=m && i<=n;++i){
           //System.out.println();
          
           list.add(map.get(pq.poll()));
       }
       System.out.println(list.toString());
   }
  
}
================================================================================
Note: The answer is the indexex if the array and not the values . As per the question.

Thanks, lete me know if there is anything.

Time Complexity is O(MLogN) , Because Min Heap is Used. As we know for a single extraction Heap takes log N time. For M extraction it will take O(MLOGN) Time.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an insertion sort algorithm in C language by performing following steps .make sure code is...
Write an insertion sort algorithm in C language by performing following steps .make sure code is work. thanks Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Implement the insertion sort algorithm (note that insertion sort is an in-place sort that modifies the original array). Print the sorted array.
Write an algorithm to search for and count the number of times a value is found...
Write an algorithm to search for and count the number of times a value is found in a list L of n numbers. Each item in the list can be accessed by L[i] where i is the position of the item in the list. In other words, L[O] is the first item, L[1] is the second item, and so on. The last item can be accessed by! L[n-1]. The user enters the value to search for and count, then your...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
Do not copy from others! Thank you! Design an algorithm to find all the common elements...
Do not copy from others! Thank you! Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2, 2, 3, 5, 5, 7, the output should be 2, 5, 5. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, respectively?
In C: character specified: R You are to write a program that will compare characters the...
In C: character specified: R You are to write a program that will compare characters the user enters to a known character Your code should first prompt for the number of values the user will enter, then loop to allow the user to enter the characters. After each character is entered your code should print out whether the character comes before, after, or is, the known character specified . Your code should also keep track of how many total characters...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Write an application from scratch where you are to write in Java the following methods to...
Write an application from scratch where you are to write in Java the following methods to be used by the main(): getFootValue() returning a Double value that is entered getInchValue() returning a Double value that is entered convertToCentimeters() returns a Double value using the calculated results from the getFootValue() and getInchValue() which will be used as the input values for the convertToCentimeters() method Next: The Exceptions that will be thrown if the values are invalid and these values are to...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...