Suppose that you are given an array of N elements. Develop an optimum algorithm that finds the minimum k elements of this array in at most nlogn time. Try your algorithm on an example N-sized array and some value of k.
Below is the implementation in C++.
CODE
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// Function to find the K'th smallest element in the
// array using max-heap
int findKthSmallest(vector<int> const &v, int k)
{
// create an max-heap using std::priority_queue
and
// insert first k elements of the array into the
heap
priority_queue<int, vector<int>>
pq(v.begin(), v.begin() + k);
// do for remaining array elements
for (int i = k; i < v.size(); i++)
{
// if current element is less than
the root of the heap
if (v[i] < pq.top())
{
// replace root
with the current element
pq.pop();
pq.push(v[i]);
}
}
// return the root of max-heap
return pq.top();
}
// Find K'th smallest element in an array
int main()
{
vector<int> vec = { 7, 4, 6, 3, 9, 1 };
const size_t k = 3;
cout << "K'th smallest element in the array
is " <<
findKthSmallest(vec, k);
return 0;
}
Suppose that you are given an array of N elements. Develop an optimum algorithm that finds...
need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...
You will be given an array of N elements sorted small to large. For the X and Y values that satisfy the X ≤ Y condition, draw the flow diagram of the algorithm that finds the start and end addresses of the region with the numbers greater than X and less than Y with the divide and manage approach and with O (logN) complexity. Also code the algorithm in c language. (not c++) Example: A[0…8] array 3, 5, 7, 9,...
Write just Algorithm for Given an array of n elements, write an algorithm to find a number that has a duplicate.
3. Suppose you have an array of n random elements. You are required to perform n different searches on the array. What is best big-oh time for your entire task? Explain how to achieve that time. 4. Suppose you are given two sorted integer arrays int[] A and int[] B. Write a method that returns an array which contains only the common elements (elements that are present in both A and B) of these two sorted arrays. Indicate the big-Oh...
Given as input an array A of n positive integers and another positive integer x, describe an O(nlogn)-time algorithm that determines whether or not there exist two elements Ai and AONn the array A such that is exactly x.
Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...
It is due in 2 hours.. Thanks ! Suppose that an algorithm runs on a tree containing n nodes. What is the time complexity of the algorithm if the time spent per node in the tree is proportional to the number of grandchildren of the node? (Assume that the algorithm spends O(1) time for every node that does not have a grandchild.) In modern software development, a useful utility called make is usually employed to manage the compilation order of...
(you can use any language) Write an algorithm that works with an array of size N and shows the user the next menu: 1. Fill array. 2. Get sum of array elements 3. Get the maximum value of the array elements 4. Get the minimum value of the array elements. 5. Exit
the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...