Question

In: Computer Science

Given an array A[1..n] of integers such that A[j] ≥ A[i] − d for every j...

  1. Given an array A[1..n] of integers such that A[j] ≥ A[i] − d for every j > i. That means any inversion pair in A differs in value by at most d. Give an O(n + d) algorithm to sort this array.

Solutions

Expert Solution

So here is O(n + d) solution.

Inversion Count for an array means how far or close the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.

Here, two elements a[i] and a[j] form an inversion A[j] ≥ A[i] − d for every j > i

Let A[] ={4, 8, 10, 2, 16, 14}

Algorithm for counting number of inversion:

 Start

step 1: Declare an array A[] 
Step 2: initialize the array A[] = {4, 8, 10, 2, 16, 14}
Step 3: Declare a variable n such that n = sizeof(arr) / sizeof(arr[0])
Step 4: now print the total number of inversion by calling the function getCount(A, n)
Step 5: stop
step 6: declaring the function getCount(A, n)
Step 7: inside getCount(A, n) initialize a InvCounter = 0
         and declare a variable d as (maximum difference between inversion pair)
step 8: Check FOR condition for i (counter) = 0, i < n - 1, i++

step 9 : if above FOR condition is true go to step 10 otherwie go to step 14.

step 10: check FOR condition for j (another counter) = i + 1, j < n , j++

step 11 : if above condition is true go to step 12 otherwise go to step 14.

step 12 : if (arr[i] - d =< arr[j]) is true go to step 13 otherwise go to step 14.

step 13: increament the invCounter to plus one.

step 14: return to the step 5 and total number of invCounter will be printed.

step 12: stop

Algorithm for sorting:
  1. Like merge sort, divide the array into two equal or almost equal halves in each step until the base case is reached.
  2. Create a function merge that counts the number of inversions when two halves of the array are merged, create two indices i and j, i is the index for first half and j is an index of the second half. if a[j] is greater than equals to a[i] - d where d is maximum difference between inversion pair, then there are (mid – i) inversions. because left and right subarrays are sorted, so all the remaining elements in left-subarray (a[i+1], a[i+2] … a[mid]) with the maximum diference between inversion pair will be less than equals to a[j].
  3. Create a recursive function to divide the array into halves and find the answer by summing the number of inversions is the first half, number of inversion in the second half and the number of inversions by merging the two.
  4. The base case of recursion is when there is only one element in the given half.
  5. Print the answer

Related Solutions

1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Given an array A of n integers, describe an O(n log n) algorithm to decide if...
Given an array A of n integers, describe an O(n log n) algorithm to decide if the elements of A are all distinct. Why does your algorithm run in O(n log n) time?
Given an array of positive integers a, your task is to calculate the sum of every...
Given an array of positive integers a, your task is to calculate the sum of every possible a[i] ∘a[j], where a[i]∘a[j] is the concatenation of the string representations of a[i] and a[j] respectively. Example For a = [10, 2], the output should be concatenationsSum(a) = 1344. a[0] ∘a[0] = 10 ∘10 = 1010, a[0] ∘a[1] = 10 ∘2 = 102, a[1] ∘a[0] = 2 ∘10 = 210, a[1] ∘a[1] = 2 ∘2 = 22. So the sum is equal to...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j);...
#include <stdio.h> int main() { int i, j; printf("Enter two positive integers: "); scanf("%d%d", &i, &j); // need to validate both being positive while (i != j) { i > j ? (i -= j) : (j -= i); } printf("GCD: %d\n", i); return 0; } The above code returns a GCD from two numbers entered. Use the above program to design a C program to compute of integer coefficients a and b such that gcd(i, j) = a xi...
for (i=0; i<n; i++) for (j=1; j<n; j=j*2)    for (k=0; k<j; k++) ... // Constant...
for (i=0; i<n; i++) for (j=1; j<n; j=j*2)    for (k=0; k<j; k++) ... // Constant time operations end for end for end for Analyze the following code and provide a "Big-O" estimate of its running time in terms of n. Explain your analysis. Note: Credit will not be given only for answers - show all your work: (2 points) steps you took to get your answer. (1 point) your answer.
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It...
Let iqsort(A, 1, n) be an algorithm that sorts an array A with n integers. It works as follows: iqsort(A, p, q){ if p ≥ q, return; r=partition(A, p, q); //run quick sort on the low part quicksort(A, p, r − 1); //run insert sort on the high part insertsort(A, r + 1, q); } Compute the best-case, worst-case, and average-case complexities of iqsort.
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a...
Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain. Problem 2: [10 Marks] Now consider the same problem as problem 1, but this time you only have positive integer numbers...
Please do Part III Part I Problem statement: Given an array of 'n' non-duplicate integers, find...
Please do Part III Part I Problem statement: Given an array of 'n' non-duplicate integers, find pairs of integers from the array that make the sum equal to K. Eg. [1, 2, 7, 11, 6, 9, 5] K= 12 Soln: (1,11), (7,5). Part II Write an Oracle for testing this program using Data Flow Testing. As usual, write down test cases in a tabular form with reasons, inputs, expected output etc. Part III   1.       Identify the basic blocks in your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT