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
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 −...
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT