Question

In: Computer Science

Is there any way to do this function //Recursive method to check if two numbers form...

Is there any way to do this function

//Recursive method to check if two numbers form a given number
private static boolean addition(int[] array, int start, int end, int n) {
if(start < end) {
if (array[start] + array[end] == n) {
return true;
}
if (array[start] + array[end] > n) {
return addition(array, start, end-1, n);
}
if (array[start] + array[end] < n) {
return addition(array, start+1, end, n);
}
}
return false;
}

Solutions

Expert Solution

Function corrected:

private static boolean addition(int[] array, int start, int end, int n) {
       Arrays.sort(array,start,end+1); //sort the array
if(start < end)
{
if (array[start] + array[end] == n) {
return true;
}
if (array[start] + array[end] > n) {
return addition(array, start, end-1, n);
}
if (array[start] + array[end] < n) {
return addition(array, start+1, end, n);
}
       }  
       return false;
   }

whole program to test

// Java program to check if given array
// has 2 elements whose sum is equal
// to the given value
import java.util.*;

class Solution {
   // Function to check if array has 2 elements
   // whose sum is equal to the given value
   private static boolean addition(int[] array, int start, int end, int n) {
       Arrays.sort(array,start,end+1); //sort the array
if(start < end)
{
if (array[start] + array[end] == n) {
return true;
}
if (array[start] + array[end] > n) {
return addition(array, start, end-1, n);
}
if (array[start] + array[end] < n) {
return addition(array, start+1, end, n);
}
       }  
       return false;
   }

   // Driver code
   public static void main(String args[])
   {
   Solution s = new Solution();
       int A[] = { 1, 4, 45, 6, 10, -8 };
       int n = 16;
       int arr_size = A.length;

       // Function calling
       if (s.addition(A, 0,arr_size-1, n))
           System.out.println("Array has two "
                           + "elements with given sum");
       else
           System.out.println("Array doesn't have "
                           + "two elements with given sum");
   }
}


Related Solutions

Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of...
Add a recursive Boolean function called bool isGreater(int compare) to class IntegerLinkedList to check if any of the linked list data values is greater than the given parameter, compare. For example, isGreater(25) should return true and isGreater(100) should return false for the the linked list shown below. A main function (prob3.cpp) is given to you to add data values to the linked list and test your function. Other examples are given in the main function. // ADD ANSWER TO THIS FILE...
Write a recursive function to check if a string whose each character is stored in a...
Write a recursive function to check if a string whose each character is stored in a separate node in a doubly linked list, is a palindrome. Use the code available from DoublyLinkedList.hpp on our Github. // // Doubly-linked list with 2 dummy nodes // #pragma once #include <stdexcept> template<typename T> struct Node { T data; Node<T>* next; Node<T>* prev; Node() = delete; // Intentionally no default constructor Node( const T & element ) : data( element ), next( nullptr ),...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write...
Part 1: Write a recursive function that will calculate Fibonacci numbers using a recursive definition. Write a short program to test it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n) Part 2: Write an iterative function to calculate Fibonacci numbers. Write a test driver for it. The input of this program must be a positive integer n; the output is the corresponding Fibonacci number F(n). Part 3: Write a...
** * Write a recursive function that removes the first k even numbers * from the...
** * Write a recursive function that removes the first k even numbers * from the stack. If there are less than k even elements in the stack, * just remove all even elements. Do not use any loops or data structures * other than the stack passed in as a parameter. * @param stack * @param k * @return Returns the number of elements removed from the stack. */ public static int removeEvenNumbers(Stack<Integer> stack, int k) { return 0;...
Given a stream of random binary numbers(*) Is there any way to differentiate if they came...
Given a stream of random binary numbers(*) Is there any way to differentiate if they came from a Truly Random or from a formula/algorithm ? how? if there is no way to decide this, then, I can't find any basis, to keep denying that behind the "truly random" of quantum mechanics can be a hidden algorithm. I know I am talking about the posibility of a "hidden variables" theory, but I can't find any other explanation. (*) Is known that...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
Provide a recursive definition of some sequence of numbers or function (e.g. log, exponent, polynomial). Choose...
Provide a recursive definition of some sequence of numbers or function (e.g. log, exponent, polynomial). Choose one different from that of any posted thus far. Write a recursive method that given n, computes the nth term of that sequence. Also provide an equivalent iterative implementation. How do the two implementations compare?
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
Write a MIPS assembly program to calculate the Fibonacci numbers from 1..n using the recursive method....
Write a MIPS assembly program to calculate the Fibonacci numbers from 1..n using the recursive method. The definition of a Fibonacci number is F(n) = F(n-1) + F(n-2). The implementation must follow the following guidelines: Prompt the user for a number n Allocate heap memory to hold the exact number of elements in the Fibonacci sequence for n Implement recursive Fibonacci method as a subprogram Print the Fibonacci sequence array
Please write code in Scala Write a function which check that numbers in provided list are...
Please write code in Scala Write a function which check that numbers in provided list are correctly sorted. Please use tail recursion
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT