Question

ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real...

ALGORITHM PROBLEM:

A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence.

[Hint: Use divide-&-conquer. Do the “combine” step carefully]

B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of arbitrary positive integers. Output: The maximum-element-sum contiguous sub-array of A[1..n] whose entries form a monotone sequence (either ascending or descending)

[Hint: Use an incremental approach]

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

A.

We will divide the array into equal halves, we will count the number of significant inversions in each halves, and we will also count the number of significant inversion in which one of the element is in one half and another element is in another half. We will add these to get the result. So we will perform analogous operation to merge sort in which we will divide the array into halves, count the significant inversal in each half then combine the subarray to form sorted array but also note if at some index element in left half form significant inversal with element in right half, then all the elements in next index in left half will be significant inversal. We will use this property to count the number of significant inversal in O(n log n) time.

int Divide (A,begin,end) 1. count- keep count of significant inversal 2. If end-begin< 2: then if A[begin]>Alend] then if A[b

Now merge step is very important to observe.

Note in step 4 of merge where we are incrementing count very smartly.

Complexity = O(n log n) just like merge sort

B.

We will divide the array into equal halves, we will check the maximum sum of monotone array in each halves, and we will also check the maximum sum of monotone array which has some portion in left half and some portion in right half. We will return the result which is maximum of all three. Without loss of generality assume that array is of size which is some exponent of 2.

Below is the Merge procudure where we keep the name Merge just to say that we are searching for monotone sequence which passed through middle of the array.

Time complexity T(n) = 2T(n/2)+O(n) = O( n log n)

Please comment for any clarification

Add a comment
Know the answer?
Add Answer to:
ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real...
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
  • Algorithm Problem - Counting Number of Inversions

    We are given a sequence ofndistinct numbers a1,...,an. We define an inversion to be a pair i<j such that ai>aj. Give an O(nlogn) algorithm to count the number of inversions. (Hint: assume that your algorithmreturns a sorted array of input sequences, as well as the number of inversions. Divide the input sequencesinto two halves, then count inversions and sort each half by doing recursive calls, and find a way to mergethese two sorted halves and to count the number of...

  • ALgorithm problem

    Recall the problem of ¯nding the number of inversions. As in the text, we are given asequence of numbers a1; : : : ; an, which we assume are all distinct, and we de¯ne an inversionto be a pair i < j such that ai > aj .We motivated the problem of counting inversions as a good measure of how di®erent twoorderings are. However, one might feel that this measure is two sensitive. Let's call a pair asigni¯cant inversion if...

  • 1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct...

    1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.

  • the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++...

    the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...

  • Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct...

    Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)

  • 1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers,...

    1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.

  • Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a...

    Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing and combining steps take a time in Θ(n n). Write a recurrence equation for the running time T (n) , and solve the equation for T (n) 2. Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing...

  • Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This...

    Convert the pseudocode into a C++ function Decrease-by-Half Algorithm We can solve the same problem using a decrease-by-half algorithm. This algorithm is based on the following ideas: In the base case, n 1 and the only possible solution is b 0, e 1 In the general case, divide V into a left and rnight half; then the maximum subarray can be in one of three places: o entirely in the left half; o entirely in the right half; or o...

  • a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n...

    a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...

  • Suppose you are given an array of n integers ai, az, ..., an. You need to...

    Suppose you are given an array of n integers ai, az, ..., an. You need to find out the length of its longest sub-array (not necessarily contiguous) such that all elements in the sub-array are sorted in non-decreasing order. For example, the length of longest sub-array for (5, 10, 9, 13, 12, 25, 19, 30) is 5 and one such sub-array is (5, 10, 13, 19, 303. Note you may have multiple solutions for this case. Use dynamic programming to...

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