Question

Let A be an array of N distinct positive numbers such that at most sqrt(N) of...

Let A be an array of N distinct positive numbers such that at most sqrt(N) of them is larger than N.

Ex: if N=100, there are at most 10 elements in A that are greater than 100.

1. Split A into A1 and A2 such that A1 contains the elements greater than N and A2 contains the remaining elements. Do this in O(N)

2. Sort A1 in O(N)

3. Sort A2 in O(N)

4. Sort A in O(N)

I don't need actual code, more so it explained clearly. I was under the impression 3 and 4 would be impossible. Thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(1) Simply iterate the array A and if current element is smaller than or equal to n insert it into A2 else insert it into A1.this takes o(n) time.

(2)to sort A2 simply use bubble sort because size of A2 is atmost sqrt(n) hence time complexity for bubble sort will b o(sqrt(n)^2)=o(n)

(3)To sort A2 because hash map of array B of size 'n' and iterate the A2 array and increase count of current element index in B i.e B[A[i]]++ now once the whole of array A2 is processed iterate array B and replace elements of A2 by current index of array B if its counter is >0 and do this for every index of B for value of counter at that index then proceed to next index once as many as current index is inserted to A2 as value of counter at that index. this is o(n)

(4) to sort A now use technique similar to we use in merge operation for two arrays A1 and A2 as in merge sort of two array.

i=0;

j=0;

k=0;

while(i<A1.size()&&j<A2.size())

{

if(A1[i]<A2[j])

A[k++]=A1[i++];

else

A[k++]=A2[j++];

}

//please upvote if this was helpful.

Add a comment
Know the answer?
Add Answer to:
Let A be an array of N distinct positive numbers such that at most sqrt(N) of...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Someone help please Let A be an array of 5 integers, whose contents are as follows:...

    Someone help please Let A be an array of 5 integers, whose contents are as follows: 3, 2, 1, 5, 4 We will apply quick sort to sort this array. Show all of the element-wise comparisons made by the algorithm in the correct order. Here an element-wise comparison means the comparison of one element of the array with another element of the array or the key set in a particular step of the algorithm. Since the algorithm may move the...

  • Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algo...

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

  • 1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the las...

    1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the last element is not. (It could be smaller than the first element, larger than element n − 1, or anywhere in between.) Give the worst-case number of comparisons that Insertion Sort will perform in this scenario. You can give your answer in terms of big-Theta if you wish to ignore low-order terms and constants.

  • Your goal is to create an ‘Array’ class that is able to hold multiple integer values....

    Your goal is to create an ‘Array’ class that is able to hold multiple integer values. The ‘Array’ class will be given functionality through the use of various overloaded operators You will be given the main() function for your program and must add code in order to achieve the desired result. Do not change any code in main(). If something is not working, you must change your own code, not the code in main(). Assignment 5: overloading member functions. Overview:...

  • Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j...

    Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your...

  • Let S be a sequence of n distinct integers stored in an array as array elements...

    Let S be a sequence of n distinct integers stored in an array as array elements S[1], S[2], · · · , S[n]. Use the technique of dynamic programming to find the length of a longest ascending subsequence of entries in S. For example, if the entries of S are 11, 17, 5, 8, 6, 4, 7, 12, 3, then one longest ascending subsequence is 5, 6, 7, 12. Specifically: (a) define a proper function and find the recurrence for...

  • Question 6 (1 point) Let a be an array, let N be the number of elment...

    Question 6 (1 point) Let a be an array, let N be the number of elment used in the array. For the given set of code below, what does the output represent? int sum = 0; for (int i = 0; i < N;i++) if (a[i] > 0) sum = sum + 1; textBox1.text = " + sum; Execution of code with nothing being printed in textBox1. Execution of code with printing all numbers Execution of code with counting the...

  • Suppose we have an array that contains tuples. These tuple contains three positive numbers. Implement an...

    Suppose we have an array that contains tuples. These tuple contains three positive numbers. Implement an algorithm that counts how many distinct tuple that an array has(contains same number in same order). ex) [(1, 2, 1), (2, 2, 2), (3, 8, 3), (1, 2, 1), (3, 4, 3)] gives 4. 1 The algorithm should be implemented in Python3. 2 The function must have average-case runtime of O(n). You can assume Simple Uniform Random Hashing. 3 Python built-in dictionary cannot be...

  • Let us suppose that there are n elements in the un-sorted array. Answer the following? q1:...

    Let us suppose that there are n elements in the un-sorted array. Answer the following? q1: How is merge sort different from quick sort? q2: What is the split ratio in merge sort? q3: What is the worst-case/average-case/best-case running time of Merge Sort? q4: Why is the worst case running time of Merge sort O(n log n) always? q5: Why does Merge Sort use a static tree in the recursion process? (It is worth noting that the Quick Sort use...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT