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
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?
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...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
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...
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