Question

In: Computer Science

An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i <...

An abs-sorted array is an array of numbers in which |A[i]| <= |A[j]| whenever i < j.

For example, the array A = [-49, 75, 103, -147, 164, -197, -238, 314, 348, -422],

though not sorted in the standard sense, is abs-sorted. Design an algorithm that

takes an abs-sorted array A and a number k, and returns a pair of indices of elements

in A that sum up to k. For example if k = 167 your algorithm should output (3, 7).

Output (-1, -1) if the is no such pair.

Solutions

Expert Solution

A simple solution is to traverse each element and check if there’s another number in the array which can be added to it to give sum. But it's complexity will be O(n^2)
// Consider all possible pairs and check if theor sum = k
String pair = '';
for (int i = 0; i < arr.length; i++)
for (int j = i + 1; j < arr.length; j++)
if ((arr[i] + arr[j]) == sum){
   pair = "(" + i + "," + j + ")";
return pair;   
}


TO solve in O(n) -
Concept used -
   a + b = k
   a = b - k


for every element 'a' taken from the array, check if it already exist in the map.
   If it does not exist,
       store the map with the pair (index, k-a)
   If it already exists,
       print it's index and the index of the number already present in the map.


Pseudo code :
HashMap<Integer, Integer> map = new HashMap<>();
for (int i=0; i<n; i++){
   if(!map.containsKey(A[i]))
System.out.println("(" + i + "," + map.get(A[i]) + ")");
else
map.put(A[i],i);
}


Related Solutions

Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
Please, I want a paragraph about ABS materiel, with a table of properties of ABS. I...
Please, I want a paragraph about ABS materiel, with a table of properties of ABS. I need it in an experimental measurement and techniques project. NO PLAGIARISM PLEASE
Written in MIPS assembly language If given an array which is sorted in ascending order, as...
Written in MIPS assembly language If given an array which is sorted in ascending order, as well as its length and a target value. You are asked to find the index of this target value in the array using binary search algorithm. Here is an example: array 1, 4, 6, 8, 10, 12 length 6 target 8 2 In this case, the returned result should be assigned to be 3, as array[3] is equal to target. (Note: The target will...
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:...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
Project: Given an array numbers. We define a running sum of an array as runningSum[i] =...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of numbers. Example: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. You need to do: Create a class called RunningSumArray to perform the main method. Create a class called ArrayOperationto hold the actions for array. In this project, you will need 4methods: 1. Public static Int[] getArray() --- inside ArrayOperationclass,...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
i j(i) i j(i) i j(i) i j(i) 0 -0.0499 7 -0.08539 13 0.144812 19 0.08021...
i j(i) i j(i) i j(i) i j(i) 0 -0.0499 7 -0.08539 13 0.144812 19 0.08021 1 0.107506 8 0.062922 14 -0.0499 20 -0.30103 2 -0.06719 9 -0.04444 15 -0.18366 21 -0.33834 3 -0.04717 10 0.219422 16 -0.02898 22 0.058373 4 -0.09176 11 0.083849 17 0.08021 23 0.79083 5 -0.25918 12 -0.02261 18 -0.14271 24 0.130254 6 0.055643 25 -0.10177 (3 points) Please encipher the following plaintext with Caesar Cipher and the encryption key of 10: TODAYISTUESDAY; (3 points) In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT