Question

In: Computer Science

(Programming: Counting threshold inversions) You’ll be given an array (of integers) and a threshold value as...

(Programming: Counting threshold inversions)

You’ll be given an array (of integers) and a threshold value as input, write a program to return the number of threshold inversions in the array. An inversion between indices i < j is a threshold inversion if ai > t ∗ aj , where t is the threshold value given as input.

Solutions

Expert Solution

There is no programming language mentioned in the problem. Therefore, the chosen programming language is C++. Hence, the program is provided below:

Screenshot of the code:

Sample Output:

Code To Copy:

//Include the header files.

#include <bits/stdc++.h>

using namespace std;

//Define the function.

int Retrive_inversion_count(int arr[], int ele_count, float t)

{

//Declare the variable to store the

//count of inversions.

int count_inversions = 0;

//Begin the for loop.

for (int i = 0; i < ele_count - 1; i++)

{

    for (int j = i + 1; j < ele_count; j++)

    {

      //Compare the values of array i and j.

      if (arr[i] > (t*arr[j]))

      {

        //Increase the count.

        count_inversions++;

      }       

    }

}

//Return the inversion counts.

return count_inversions;

}

//Define the main function.

int main()

{

//Declare the variables.

int num;

float t;

//Prompt the user to enter the number of

//elements of the array.

cout << "Enter the number of elements: ";

cin >> num;

int array_ele[num];

cout << "Enter the elements: ";

//Begin the for loop to store the elements

//of the array.

for(int i = 0; i < num; i++)

{

    cin >> array_ele[i];

}

cout << "Enter the threshold value: ";

cin >> t;

//Display the number of inversions returned by

//the function Retrive_inversion_count().

cout << "Number of inversions are: "

       << Retrive_inversion_count(array_ele, num, t)

       << endl;

//Return the value for the main function.

return 0;

}

//Kindly comment for any query. Ready to help!!!


Related Solutions

please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find...
please write in c++ Algorithm Design problem: Counting inversions: given an array of n integers, find out the total number of inversions in the given sequence. For example, for the sequence of 2, 4, 1, 3, 5, there are three inversions (2,1), (4,1), and (4,3). Give a brute-force algorithm with running time of O(n^2). Using the technique of divide-and-conquer, design an algorithm with running time of O(nlog n).
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
Given an array of integers, and given a specific value k (not equal to 0), produce...
Given an array of integers, and given a specific value k (not equal to 0), produce all unique pairs of values in the array which differ by k. For example, if the array has [1,4,9,12, 6, 15, 5, 13,17] and k=3, the answer would be (1,4 ) ( 9,12), ( 9,6), (12,15). If k=4, the answer would be (1,5), (9,5), (13,17), (9.13). Notice that you do not print the same answer twice, for instance (9,13), and (13,9).
Fix the C++ code to count the number of inversions using the given array. The answer...
Fix the C++ code to count the number of inversions using the given array. The answer is 8 inversions, but I am only getting 6 inversions. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; int mergeInversion(int arr[], int l, int m, int r) {     // size of the two arrays     int n1 = m - l + 1;     int n2 = r - m;     // create temporary arrays     int L[n1];     int R[n2];     // Copy the...
Problem5: (5 pts) using c programming Given an array of integers named “simple” of length 8...
Problem5: (5 pts) using c programming Given an array of integers named “simple” of length 8 and filled as follows: 5 20 13 8 0 16 55 2 int x = 5; int *p = &simple[3] ; Write the values of x after each of the following executes (write unknown if cannot be determined): (hint: pointer moves to new location after each line) x = *(p++);                                                   x = ___8_______________ p->simple[4]            x = *( p + 1) ;                                               x = _____16______________...
Given an array storing integers ordered by value, modify the binary search routine to return the...
Given an array storing integers ordered by value, modify the binary search routine to return the position of the integer with the greatest value less than K when K itself does not appear in the array. Return ERROR if the least value in the array is greater than K.
Given an array ? of ? integers. Divide ? into three subsets such that the total...
Given an array ? of ? integers. Divide ? into three subsets such that the total absolute difference of subset sums between them is minimum. Provide python source code, time complexity, and pseudocode. thank you
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...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b...
Problem Definition: Problem: Given an array of integers print all pairs of integers a and b where a + b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a + b = 16 A= [ 10, 4, 6, 15, 3, 5, 1, 13] The following are pairs that sum to 16: 13, 3 6, 10 15, 1 Your program should print these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT