Question

In: Computer Science

5. Suppose you are given an n-element array containing distinct integers that are listed in increasing...

5. Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm?




Java Language....

Solutions

Expert Solution

import java.util.Scanner;

public class solve {
    public static void findSumPairs(int[] array, int wantedSum) {
        recursiveSumPairs(array,  0, 1,wantedSum);
    }

    private static void recursiveSumPairs(
            int[] array,
            int firstIndex,
            int nextIndex,int wantedSum) {

        // Base Case for recursion: first element is the last
        // element in the array. This also handles input arrays shorter than
        // two elements.
        if (firstIndex >= array.length - 1) {
            System.out.println("No such pair exists");
            return;
        }

        // Increment the first index to the
        // next index and compare it to the rest of the elements ofthe
        // array.
        if (nextIndex >= array.length) {
            recursiveSumPairs(array, wantedSum, firstIndex + 1, firstIndex + 2);
            return;
        }

        if (array[firstIndex] + array[nextIndex] == wantedSum) {
            System.out.println(array[firstIndex] + " + " + array[nextIndex]);
        }

        // Compare first element to the next element in the array.
        recursiveSumPairs(array, wantedSum, firstIndex, nextIndex + 1);
    }


    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        int n = scn.nextInt();
        int []arr = new int[n];
        for(int i =0 ; i < n ;i ++)
            arr[i] = scn.nextInt();
        int sum = scn.nextInt();
        findSumPairs(arr,sum);

    }
}

Since this algorithm checks one element with every other element, the running time will be O(n^2) .

Please ask if you have any doubt , I used Divide and Conquer using recursion , i.e., I broke down the array into two parts in every iteration, one part contained one element and it was used to compare with other elements of the array.


Related Solutions

Suppose you are given an n-element array containing distinct integers that are listed in increasing order....
Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm?
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus...
An array A[0..n - 2] contains n-1 integers from 1 to n in increasing order. (Thus one integer in this range is missing.) Design an algorithm in ​(Theta(log n)) to find the missing integer. Your algorithm should be given in pseudo code. For example, the array A could be {1, 2, 3, 4, 6, 7, 8, 9, 10} in which 5 is missing.
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
Consider an array of length n containing positive and negative integers in random order. Write the...
Consider an array of length n containing positive and negative integers in random order. Write the C++ code that rearranges the integers so that the negative integers appear before the positive integers. write a program that includes both functions and a main() function that tests them. Name the two functions rearrangeN() and rearrangeN2().
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real 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]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT