AP Computer Science Exam Question
// You are given an array of integers named "nums" and an integer "target".
// Your task is to write a method "traverseAndManipulate" that will perform the following operations on the array:
// 1. Traverse the array and find all pairs of numbers whose sum equals the target.
// 2. Multiply each of the pairs found in step 1 by a factor of 2.
// 3. Sort the array in ascending order.
// The method should return the modified array as the result. If no pairs are found, or if the array is empty, the method should return an empty array.
// You may assume that the array may contain duplicate numbers, but the same pair should not be counted more than once.
// You may use any additional methods or classes as needed.
// Answer the following questions and write the code providing detailed explanations for the implementation.
// a) What is the time complexity of your solution?
// b) What is the space complexity of your solution?
// c) Write the code for the "traverseAndManipulate" method.
public class ArrayTraversalManipulation {
public static int[] traverseAndManipulate(int[] nums, int target) {
// Step 1: Traverse the array and find all pairs of numbers whose sum equals the target.
ArrayList<Integer[]> pairs = new ArrayList<>();
int n = nums.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (nums[i] + nums[j] == target) {
pairs.add(new Integer[]{nums[i], nums[j]});
}
}
}
// If no pairs found, return empty array
if (pairs.isEmpty()) {
return new int[]{};
}
// Step 2: Multiply each of the pairs found in step 1 by a factor of 2.
for (Integer[] pair : pairs) {
pair[0] *= 2;
pair[1] *= 2;
}
// Step 3: Sort the array in ascending order.
Arrays.sort(nums);
//Convert the integer 2D array to a 1D array and return
int[] result = new int[2 * pairs.size()];
int index = 0;
for (Integer[] pair : pairs) {
result[index++] = pair[0];
result[index++] = pair[1];
}
return result;
}
public static void main(String[] args) {
// Test the traverseAndManipulate method
int[] nums = {1, 4, 2, 7, 5, 3, 11};
int target = 8;
int[] result = traverseAndManipulate(nums, target);
System.out.println("Modified Array: " + Arrays.toString(result));
}
}
a) The time complexity of the solution is O(n^2 log n), where n is the length of the given array. The nested for loop runs in O(n^2) time, and the sorting step takes O(n log n) time.
b) The space complexity of the solution is O(n), where n is the length of the given array. We use an ArrayList to store pairs, which can have a maximum of n/2 pairs. The result array also has a maximum size of 2n.
c)
The traverseAndManipulate
method performs the following steps:
pairs
to store pairs of numbers whose sum equals the target.pairs
list as an Integer array containing the two numbers.pairs
list by a factor of 2.Arrays.sort()
method.pairs
to the result array, converting from the Integer array to individual integers.In the given sample code, the main
method tests the traverseAndManipulate
method by providing a sample array nums
and a target value of 8. The modified array is printed as the output.