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
In Python You must create a flowchart, and its corresponding Python program, to solve the following...
In Python You must create a flowchart, and its corresponding Python program, to solve the following problem: 1. Using a repetition structure, evaluate the factorial of a positive whole number n: n! = n · (n - 1) · (n - 2) · ... · 1, where n >=1 Your program must take into account the cases in which the user enters an incorrect number and provide an error message for such cases.
You must create a flowchart, and its corresponding Python program, to solve the following problem: 1....
You must create a flowchart, and its corresponding Python program, to solve the following problem: 1. Using a repetition structure, evaluate the factorial of a positive whole number n: n! = n · (n - 1) · (n - 2) · ... · 1, where n >=1 Your program must take into account the cases in which the user enters an incorrect number and provide an error message for such cases.
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...
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)...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm...
HIMT 345 Homework 06: Iteration Overview: Examine PyCharm’s “Introduction to Python” samples for Loops. Use PyCharm to work along with a worked exercise from Chapter 5. Use two different looping strategies for different purposes. Prior Task Completion: 1. Read Chapter 05. 2. View Chapter 05’s video notes. 3. As you view the video, work along with each code sample in PyCharm using the Ch 5 Iteration PPT Code Samples.zip. Important: Do not skip the process of following along with the...
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...
Project 10-3 In this hands-on project, you view the configuration of the System Log Daemon and...
Project 10-3 In this hands-on project, you view the configuration of the System Log Daemon and the logrotate utility on Ubuntu Server Linux. 1. Boot your Ubuntu Server Linux virtual machine. After your Linux system has been loaded, log into tty1 using the user name of root and the password of LNXrocks!. 2. At the command prompt, type ls –l /dev/log and press Enter. What is the file type? Which daemon on Ubuntu Server Linux uses this file and what...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but mostly readable way called “leet” – short for “elite”. In essence, it was a simple character replacement algorithm, where a single “regular” character was replaced by one or more “leet” characters; numbers remained the same. Here’s one of the most readable versions: a 4 g 9 m /\\/\\ s $ y ‘/ b B h |-| n |\\| t 7 z Z c (...
This programming task will be a bit different. It will be more like what you would...
This programming task will be a bit different. It will be more like what you would receive if you were a maintenance engineer working in a corporate information systems or technology department. In other words, you will have some prebuilt source code provided for you that you must alter so it will meet the given programming specification.. Build a program from what you have been given as a starting-point. Rename the “starter code”. Do not add any modules; just, use...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT