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.
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...
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,...
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
A.) myNums is an array of 50 elements of type int and k is an int...
A.) myNums is an array of 50 elements of type int and k is an int variable. For which one we get the index of out of bounds? a. for (k = 0; k <= 49; k++)     cout << myNums[k] << " "; b. for (k = 1; k < 50; k++)     cout << myNums[k] << " "; c. for (k = 0; k <= 50; k++)     cout << myNums[k] << " "; d. for (k =...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...
Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] = 0;} catch(ArithmeticException e){array[2] = 0} // ? finally{ return 0;}} Could you please tell me why the line with the question mark will not report an error?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT