Question

In: Computer Science

Write a method, twoSumSorted2, that solves the following variant of the Two-Sum problem: Given a sorted...

Write a method, twoSumSorted2, that solves the following variant of the Two-Sum problem:

Given a sorted array of integers where each element is unique and a target integer, return in an Array List, the indices of all pairs of elements that sum up to the target. Each pair of indices is also represented as an Array List (of two elements). Therefore, the method returns an Array List of an Array List of size 2. If no pair in the input array sums up to the target, then the method should return an empty list.

public class Hw2_p1 {
  
   // HW2 Problem 1 Graded Method
   public static ArrayList<ArrayList<Integer>> twoSumSorted(int[] A, int target) {
       ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
      
       // Your code starts
      
      
       // Your code ends
      
       return ans;
   }

  
   // Test driver for twoSumSorted2
   public static void main(String[] args) {
      
   }

}

The following are two sample runs:

Input:   ? = [−7, −5, −2, 0, 1, 6, 7, 8, 9],   ?????? = 1

Return value:   [0,7], [1, 5], [3, 4]

Explanation: The pairs in the input array that sum up to 1 are [−7, 8], [−5, 6] and [0, 1]. Their indices are [0, 7], [1, 5] and [3, 4] respectively.

Input:   ? = [−2, 0, 1, 6, 7, 8],   ?????? = 3

Return value:   [ ]

Explanation: No pair of elements in input array sums up to 3. The returned list is thus empty.

Your method must have time complexity ?(?), where ? is the length of the input array.

Solutions

Expert Solution

The code is

import java.util.*;
public class Hw2_p1 {
  
// HW2 Problem 1 Graded Method
public static ArrayList<ArrayList<Integer>> twoSumSorted(int[] A, int target) {
ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
  
// Your code starts
int l=0,r=A.length-1;
while(l<r){
if(A[l]+A[r]==target){
// System.out.println("Here");
ArrayList<Integer> temp=new ArrayList<Integer>();
temp.add(l);
temp.add(r);
ans.add(temp);
l++;
}
else if(A[l]+A[r]<target){
l++;
}
else r--;
}
  
// Your code ends
  
return ans;
}

  
// Test driver for twoSumSorted2
public static void main(String[] args) {
int[] A={-7,-5,-2,0,1,6,7,8,9};
System.out.println(twoSumSorted(A,1));
int[] B={-2,0,1,6,7,8};
System.out.println(twoSumSorted(B,3));
}

}

The output is


Related Solutions

Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one...
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one sorted array. Note the algorithm is not the one for merge sort. 2) What is the best asymptotic upper bound for the algorithm? List reasoning steps.
Given a sorted array with lot of duplicates, write a problem to search specific element m....
Given a sorted array with lot of duplicates, write a problem to search specific element m. If it’s included in the A, return its minimal index, otherwise return -1. For example A = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4}, if we search 4, you program should return 8. You are only allowed to use binary search. Linear search is not allowed here.
Write a program that solves the Knapsack problem. Code to the following standards. Your source of...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of items to put into the knapsack should consist of five randomly generated integers in the range of 1 to 20. Your knapsack can hold 20 lbs.
Write a method that takes two Sorted Arrays of different sizes and merges them into one...
Write a method that takes two Sorted Arrays of different sizes and merges them into one sorted array, and use the method to write a full recursive Merge Sort Algorithm.
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars. Students must send the code and outputs
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT