Question

In: Computer Science

Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ).

Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ). Given a sorted array of distinct integers and a single integer value, find all pairs of indexes in the list whose product is equal to the value. Return the index pairs on the same row of a two-dimensional array. Note, m will be the number of rows and total number of pairs found, while 2 will be the number of columns along every row [the first and second index position. Prove the time complexity of your solution. If you solve the problem in O(n) time, +3 Extra Credit Points.

Example Data
Array: [ 1, 2, 4, 5, 6, 9, 10, 15, 20 ]
Value: 20

Return: 0, 8
1, 6
2, 3

Solutions

Expert Solution

The code is written in c++ 14 with complexity O(n). This program also works for unsorted array.

If you still have any queries please comment. I will definitely clarify it.

EXPLANATION ( ALGORITHM )

Create an result matrix of 2 columns.

Create a hash table (set) to store the valid elements that have been encountered.

Traverse array elements and do following for every element arr[i].

1) if (n < 2 ) // single element array
   terminate the program.

2) If ( arr[i] == 0 && x == 0 )
   add index of all elements with index of arr[i] in the result matrix.
else
   ignore arr[i] (continue).

3) If ( x % arr[i] == 0 && x/arr[i] exists in table)
   add indexes of arr[i] and x/arr[i] in the result matrix.

Finally, display the result matrix.

PROGRAM

#include
#include
using namespace std;

int result[][2]={-1}; // stores the index of elements with product x
int k=0;           // number of rows in result matrix

// stores the indexes of pairs with given product x in result matrix
void product_pair(int arr[], int n, int x, int idx[])
{
   if (n < 2)
       return;

   // Create an empty set and insert first element into it
   set s;

   for (int i=0; i    {
       // 0 case must be handles explicitly as x % 0 is undefined
       if (arr[i] == 0)
       {
           if (x == 0)
           {
               for(int j=0;j                {
                   if(i!=j)
                   {
                       result[k][0] = idx[ arr[j] ];
                       result[k][1] = idx[ arr[i] ];
                       k++;
                   }
               }
           }

           else
               continue;
       }

       // x / arr[i] exists in hash, then we found a pair
       if ((x % arr[i]) == 0)
       {
           if (s.find(x/arr[i]) != s.end())
           {
               result[k][0] = idx[ x/arr[i] ];
               result[k][1] = idx[ arr[i] ];
               k++;
           }

           // Insert arr[i]
           s.insert(arr[i]);
       }
   }
}


int main()
{
   int arr[] = { 1, 2, 4, 5, 6, 9, 10, 15, 20};    // array initialization
   int x = 20;    // required product
  
   int n = sizeof(arr)/sizeof(arr[0]);
  
   int idx[100000]; // for storing the index of array elements
   for(int i=0;i<100000;i++)
       idx[i] = -1;
  
   for(int i=0;i        idx[arr[i]] = i;  
  
   product_pair(arr, n, x, idx);
  
   // result matrix
   for(int i=0;i        cout<       
   return 0;
}

IMAGE OF CODE

IMAGE OF OUTPUT


Related Solutions

The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
The following is the pseudocode of algorithm that searches a sorted array of n items by...
The following is the pseudocode of algorithm that searches a sorted array of n items by dividing it into three sub arrays of almost n/3 items (7pt). Hint: refer Mergesort algorithm) Input: Sorted array of Keys(A) indexed from 1 to n. Output: Location, the Location of K in array A (hint: return 0 if K is no in A) index location3 (int A[1…n ], index L, index H) { index m1, m2; if (L > H) return 0; else   ...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm...
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm and quick union algorithm
1) Using algorithm or pseudocode, describe the implementation of the server side of a public chatroom...
1) Using algorithm or pseudocode, describe the implementation of the server side of a public chatroom application with the following specification: Any client that wishes to join the chatroom, must send the message “Hello” to the server’s IP address at port 2020. After sending the “Hello” message, the client is admitted into the chatroom. While in the chatroom, any message sent by any client must be received by all other clients. Finally, when a client decides to leave the chatroom,...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array...
1. Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values. 2. Modify the Exercise 1 above to use the unique_copy algorith. The unique values should be inserted into a vector that's initially empty. Use a back_inserter to enable the vector to grow as new items are added. Use the copy algorithm to display the unique values.
int compare_and_swap(int *value, int expected, int new_value) { int temp = *value; if ( *value ==...
int compare_and_swap(int *value, int expected, int new_value) { int temp = *value; if ( *value == expected ) *value = new_value; return temp; } Suppose that a machine has an atomic compare_and_swap() instruction that sets the value to new_value only if the expression ( *value == expected ) is true. Regardless, compare_and_swap() always returns the original value of the variable value. Using the compare__and_swap() instruction, give a mutual-exclusion solution to the n-process critical section problem. Use only the shared variable...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1,...
int get_value(int array[], unsigned int index) { ????? } int main() { int data[] = {1, 2, 3, 4}; get_value(data, 2) = 100; } Write ????? that returns the value in the array at index: Question Blank type your answer...
Problem 1: (10 marks) ALGORITHM COMPLEXITY:Write an algorithm (pseudocode but not a program) that takes a...
Problem 1: ALGORITHM COMPLEXITY:Write an algorithm (pseudocode but not a program) that takes a positive numberias an input, where i=a1a2...an,n>=3(numberwith 3 or more digits, every digit can have the value between 0 and 9–both inclusive). The algorithm should find the largest number jsuch that j=b1..bm, 1<=m<=n(numberwith m digits),andevery bk<=bk+1where1<=k<m and bk,bk+1ε{a1, a2, an}.Also, perform the computational complexity (time complexity)analysis of your algorithm and write the algorithm complexity in Big-Oh notation. Examples:Input: 23720, Output: 237Input: 9871, Output: 9Input: 1789, Output:1789Input: 2372891,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT