Question

In: Computer Science

. As input you are given two arrays: an array of numbers ? and an array...

. As input you are given two arrays: an array of numbers ? and an array ? of queries where each query is a target number. The array ? is unsorted and may contain duplicates. Your goal is, for each query ? in the array ?, count the number of pairs in the array ? that sums up to ?; that is, the number of distinct pairs of indices [?, ?], with ? < ?, such that ?[?] + ?[?] = ?. Write a method, twoSumQuery, that given an array of numbers ? and an array ? of queries, returns an array ? with the same length as ?, such that for every index ?, ?[?] is the answer to query ?[?]. Let ? denote the length of ? and ? the length of ?. Suppose that in the actual input for this problem, we have the condition that ? > ? . For example, ? = 1000 and ? = 10,000,000. Your method must have time complexity ?(?).

The following is a few sample runs:

Input :     ? = [0, 1, 2, 3, 4],   ? = [1, 3, 6, 10]

Return:   [1, 2, 1, 0]

Explanation: One pair in ? sums up to 1, namely (0, 1). Two pairs in ? sum up to 3, namely (0, 3) and (1, 2). One pair in ? sum up to 6, namely (2, 4). No pair in ? sums up to 10.

public class TwoSumQuery {
   public static int[] twoSumQuery(int[] A, int[] Q) {
      
       //Replace this line with your return statement
       return null;
   }

}

Solutions

Expert Solution

import java.util.Arrays;
import java.util.HashMap;

public class TwoSumQuery {
        
   public static int[] twoSumQuery(int[] A, int[] Q) {
           
           // We will first create a Hashmap which contains all the possible
           // sums which can be created from the array
           // as A[i] + A[j] with i < j
           HashMap<Integer, Integer> sumCount = new HashMap<>();
           
           for(int i=0; i<A.length; i++) {
                   for(int j=i+1; j<A.length; j++) {
                           int sum = A[i] + A[j];
                           sumCount.put(sum, 1 + sumCount.getOrDefault(sum, 0));
                   }
           }
           
           int result[] = new int[Q.length];
           
           for(int i=0; i<Q.length; i++) {
                   result[i] = sumCount.getOrDefault(Q[i], 0);
           }
           
           // The complexity is O(m), Since n is very less and hence time
           // to build the hashmap is very negligible.
           // As we iterate on array Q one by one, Hence it is O(m) complexity.
           
       return result;
   }
   
   public static void main(String args[]) {
           int A[] = {0, 1, 2, 3, 4};
           int Q[] = {1, 3, 6, 10};
           
           System.out.println(Arrays.toString(twoSumQuery(A, Q)));
   }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
The program shall take two integer arrays as input. Each array represents a non-negative number. Each...
The program shall take two integer arrays as input. Each array represents a non-negative number. Each element in the array is one digit however when all digits in the array are combined a number is formed. See example below: int Array1 = {4,5,6,7} represents number 7654 int Array2 = {2,3,4,5} represents number 5432 You will add the two numbers i.e., 7654 + 5432 = 13086 and store it in a new array similar to how the numbers were stored earlier...
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers...
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers into A but not in B. For the example C=A-B= {4,7} Print the values (Use for loops) 2.Create a vector of five integers each in the range from -10 to 100 (Prompt the user for the five integer x). Perform each of the following using loops: a) Find the maximum and minimum value b)Find the number of negatives numbers Print all results
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
Write a procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in...
Write a procedure to calculate Average of numbers(integers) using Arrays. Send base address of array in register $a1 and Array length in register $a2 to the procedure and return Average in register $v0 to main program.
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT