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

You are given an array of arrays a. Your task is to group the arrays a[i]...
You are given an array of arrays a. Your task is to group the arrays a[i] by their mean values, so that arrays with equal mean values are in the same group, and arrays with different mean values are in different groups. Each group should contain a set of indices (i, j, etc), such that the corresponding arrays (a[i], a[j], etc) all have the same mean. Return the set of groups as an array of arrays, where the indices within...
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...
You will need to use only two arrays:  One array of type int to store...
You will need to use only two arrays:  One array of type int to store all the ID's  One two‐dimensional array of type double to store all the scores for all quizzes Note: The two dimensional array can be represented also with the rows being the students and the columns being the quizzes. How to proceed: 1. Declare the number of quizzes as a constant, outside the main method. (Recall that identifiers for constants are all in CAPITAL_LETTERS.)...
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.
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