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...
Consider the following checkSum() method where N numbers are saved in an array (as sorted in...
Consider the following checkSum() method where N numbers are saved in an array (as sorted in as- cending order). In the main() function, the user will input a number. The checkSum() method will find out whether the sum of any two numbers in the array is equal to the given number by the user. You need to solve the problem with minimum time complexity (low complexity will lead to higher marks!).
Given an array A[1..n] of integers such that A[j] ≥ A[i] − d for every j...
Given an array A[1..n] of integers such that A[j] ≥ A[i] − d for every j > i. That means any inversion pair in A differs in value by at most d. Give an O(n + d) algorithm to sort this array.
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
Consider an array A[1 · · · n] which is sorted and then rotated k steps...
Consider an array A[1 · · · n] which is sorted and then rotated k steps to the right. For example, we might start with the sorted array [1, 4, 5, 9, 10], and rotate it right by k = 3 steps to get [5, 9, 10, 1, 4]. Give an O(log n)-time algorithm that finds and returns the position of a given element x in array A, or returns None if x is not in A. Your algorithm is...
Consider an array A[1 · · · n] which is sorted and then rotated k steps...
Consider an array A[1 · · · n] which is sorted and then rotated k steps to the right. For example, we might start with the sorted array [1, 4, 5, 9, 10], and rotate it right by k = 3 steps to get [5, 9, 10, 1, 4]. Give an O(log n)-time algorithm that finds and returns the position of a given element x in array A, or returns None if x is not in A. Your algorithm is...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT