Question

In: Computer Science

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!).

Solutions

Expert Solution

Here Iam providing the code and the output for the problem

Time Complexity of the code : We traverse the array only one time so the time complexity is O(n).Its the best time complexity we can have.

Code for Main.java

Sample Output 1

Sample Output 2

Code for Main.java

import java.util.Scanner;

public class Main
{
static void checkSum(int sum){
  
int[] arr = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
int left = 0,right = 9;
  
while(left < right){
  
if(sum > arr[left] + arr[right]) //if sum is greater than sum pair we increment left pointer
left++;
if(sum < arr[left] + arr[right]) //if sum is less than sum pair we increment right pointer
right--;
if(sum == arr[left] + arr[right]){
System.out.print("("+arr[left]+","+arr[right]+") "); //we print if pair equals to sum
left++;
right--;
}
}
  
}
   public static void main(String[] args) {
         
       Scanner sc = new Scanner(System.in);
         
       System.out.print("Enter the sum : ");
         
       int sum = sc.nextInt();
         
       checkSum(sum);
   }
}

NOTE : If you got the output correct please give me a THUMBS UP. Let me know in the comments if you have any doubts.Thank you


Related Solutions

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...
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...
The following is the pseudocode of algorithm that searches a sorted array of n items by...
The following is the pseudocode of algorithm that searches a sorted array of n items by dividing it into three sub arrays of almost n/3 items (7pt). Hint: refer Mergesort algorithm) Input: Sorted array of Keys(A) indexed from 1 to n. Output: Location, the Location of K in array A (hint: return 0 if K is no in A) index location3 (int A[1…n ], index L, index H) { index m1, m2; if (L > H) return 0; else   ...
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...
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
Consider this prime sieve in which an array of numbers of size N is created (i.e....
Consider this prime sieve in which an array of numbers of size N is created (i.e. int nums[10] = {2,3,4,5,6,7,8,9,10,11};) and initialized to contain counting numbers starting with 2. The sieve works by taking each number in the array, and “crossing out” (perhaps via a similar array of Booleans) multiples of that number. So beginning with 2, we would “cross out” 4, 6, 8, 10. Then repeat the process for 3, and so on. Numbers that are already “crossed out”...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT