Sum of List<Integer> recursively - java

Hello fellow programmers.
I am having a very silly problem.. I'm supposed to sum all the integers in a List, recursively. I know that there is an easier way to do this, and I actually made that method too (see class below). But the meaning of this assignment is that I have to split the list up into 2 halves and then calculate the sum recursively on both halves, and last I just return half1 + half2.
The problem is that the advanced method does not return the sum of all values. Can anyone please help me?
The method sum is the simple method. Summer(Summarize in danish) is the more advanced method.
package opgave1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class BinærSøgning {
public static void main(String[] args) {
Random random = new Random();
int tal = 3;
List<Integer> liste = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
liste.add(random.nextInt(10));
Collections.sort(liste);
System.out.println(liste);
// System.out.println(binærSøgning(liste, 0, tal));
System.out.println(summer(liste, 0));
}
public static int binærSøgning(List<Integer> liste, int start, int find) {
if (liste.size() > 0) {
int midt = liste.size() / 2;
if (liste.get(midt) == find)
return start + midt;
else if (liste.size() > 1) {
if (find < liste.get(midt))
return binærSøgning(liste.subList(0, midt), start, find);
else
return binærSøgning(liste.subList(midt + 1, liste.size()), start + midt + 1, find);
}
}
return -1;
}
public static int sum (List<Integer> list, int i)
{
if (i == list.size())
return 0;
else
return list.get(i) + sum(list, i+1);
}
public static int summer(List<Integer> list, int start){
int right = 0;
int left = 0;
if(start == list.size()){
return 0;
} else {
int mid = list.size() / 2;
if(start < mid){
left += list.get(start) + summer(list.subList(0, mid), start+1);
} else if(mid < list.size()){
right += list.get(mid) + summer(list.subList(mid+1, list.size()), mid+1);
}
}
return right + left;
}
}

Ii's much easier with two base cases, there is no need for an extra 'start' parameter if you use sublists. (Because it's homework, I won't fill in the details.)
public static int summer(List<Integer> list) {
//base 1
if (list.size() == 0) {
}
//base 2
else if (list.size() == 1) {
}
else
{
//no need for if statements now!
int left = summer(list.sublist(/* */))
int right = summer(list.sublist(/* */))
return left + right;
}
}

Are you sure that you have to split the list? Usually, when you get this task for homework, the meaning is something like:
public static int sum(List<Integer> l,int start) {
if (start==l.size()) return 0;
return l.get(start)+sum(l,start+1);
}

You are missing out elements of list when you are sending start+1 in the recursion.
left += list.get(start) + summer(list.subList(0, mid), start+1);
You should rather send
left += list.get(0) + summer(list.subList(1, mid), 0);
and
right += list.get(mid) + summer(list.subList(mid+1, list.size()), 0);
I dont see the need to sending any start value. Every recursion should take the 0 in place of start.

I think it can be solved in a simpler way, by keeping track of both the first and last index of the range, like this:
public static int sum(List<Integer> list, int i, int j) {
if (i == j)
return list.get(i);
int half = (i + j) / 2;
return sum(list, i, half) + sum(list, half + 1, j);
}
It's called like this:
List<Integer> list = Arrays.asList(1, 2, 3);
System.out.println(sum(list, 0, list.size()-1));
It will work fine for non-empty lists. If the list is empty, you'll need to check it before calling sum.

First of all, you don't need to cut the list in half if all you need to do is use recursion. The sum of the elements of a list is the sum of its first element + the sum of all the elements of the rest of the list.
Second, your method is too complex. The sum of the elements of the list is 0 if the list is empty, the unique element is it contains 1 element, and the sum of the results of the method applied to the two sublists if it contains more than 1. You don't need any start argument.
This should get you started.
EDIT: since Oscar Lopez gave you the answer, but I find it too complex, here's mine:
public static int sum(List<Integer> list) {
if (list.isEmpty()) {
return 0;
}
else if (list.size() == 1) {
return list.get(0);
}
else {
int half = list.size() / 2;
return sum(list.subList(0, half)) + sum(list.subList(half, list.size()));
}
}

I am wondering a reason of getting a "half" of the List.The solution for the question is the one and only:
public int sum_list(List<Integer> list) {
if (list == null || list.isEmpty()) {
return 0;
}
return list.remove(0) + sum_list(list);
}

import java.util.*;
public class SumListByRecursion {
public static int sumByRec(List<Integer> list) {
int size = list.size();
if(size > 0) {
return list.get(0) + sumByRec(list.subList(1, size));
}
return 0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println("List:"+" "+list);
int sum = sumByRec(list);
System.out.println("Sum of the list:"+" "+sum);
}
}

You can use this method for sum all ement of given list.
public static Integer sumOfList(List<Integer> list){
try {
return list.get(0) + sumOfList(list.subList(1, list.size()));
}catch (Exception e) {
return 0;
}
}

Related

Find an element in a list/array (a big list)

I'm actually doing an easy CodinGame --> I have to find if an element exists in a list.
I've tested a first solution, it was working but it wasn't really optimized (according to the machine).
So I've tried another solution but :
When I test my code for this 2nd solution, it returns the right answers but when I'm submitting my code, it tells me that my solution is completely wrong (it doesn't work if the list is empty, and also if the list is huge, ...).
Please can you help me ?
Here is my first naive solution :
public static boolean check(int[] ints, int k) {
boolean res = false;
for(int i : ints){
if(i == k){
res = true;
break;
}
}
return res;
}
Here is the code of my 2nd solution that is supposed to be optimized:
static boolean exists(int [] ints, int k){
boolean res = false;
int first = 0;
int last = ints.length;
int mid = (first + last)/2;
while(first <= last){
if( ints[mid] < k){
first = mid +1;
}else if (ints[mid] == k){
res = true;
break;
}else{
last = mid -1;
}
mid = (first + last)/2;
}
if(first > last){
res = false;
}
return res;
}
Finally I've found the solution to my problem !!!!!
Here it is :
import java.util.Arrays;
class A{
static boolean exists(int[] ints, int k){
boolean res = false;
int index = Arrays.binarySearch(ints, k);
if (index<0){
res = false;
}else{
res = true;
}
return res;
}
}
I suppose you are trying to implement Binary search in the second solution.
If so, please check this answer. Your input array must be sorted in non-decreasing order, because Binary Search works only with sorted input data. For example, you can simply type Arrays.sort(arr); and then pass your array into exists() method. But the overall time&space complexities will be O(n log n).
Fixed some bugs in your implementation:
public static boolean exists(int[] ints, int k) {
int first = 0;
int last = ints.length - 1;
while (first <= last) {
int mid = first + (last - first) / 2; // to avoid integer overflow in extremely large arrays
if (ints[mid] < k) {
first = mid + 1;
} else if (ints[mid] == k) {
return true;
} else {
last = mid - 1;
}
}
return false;
}

perfect squares leetcode - recursive solution with memoization

Trying to solve this problem with recursion and memoization but for input 7168 I'm getting wrong answer.
public int numSquares(int n) {
Map<Integer, Integer> memo = new HashMap();
List<Integer> list = fillSquares(n, memo);
if (list == null)
return 1;
return helper(list.size()-1, list, n, memo);
}
private int helper(int index, List<Integer> list, int left, Map<Integer, Integer> memo) {
if (left == 0)
return 0;
if (left < 0 || index < 0)
return Integer.MAX_VALUE-1;
if (memo.containsKey(left)) {
return memo.get(left);
}
int d1 = 1+helper(index, list, left-list.get(index), memo);
int d2 = 1+helper(index-1, list, left-list.get(index), memo);
int d3 = helper(index-1, list, left, memo);
int d = Math.min(Math.min(d1,d2), d3);
memo.put(left, d);
return d;
}
private List<Integer> fillSquares(int n, Map<Integer, Integer> memo) {
int curr = 1;
List<Integer> list = new ArrayList();
int d = (int)Math.pow(curr, 2);
while (d < n) {
list.add(d);
memo.put(d, 1);
curr++;
d = (int)Math.pow(curr, 2);
}
if (d == n)
return null;
return list;
}
I'm calling like this:
numSquares(7168)
All test cases pass (even complex cases), but this one fails. I suspect something is wrong with my memoization but cannot pinpoint what exactly. Any help will be appreciated.
You have the memoization keyed by the value to be attained, but this does not take into account the value of index, which actually puts restrictions on which powers you can use to attain that value. That means that if (in the extreme case) index is 0, you can only reduce what is left with one square (1²), which rarely is the optimal way to form that number. So in a first instance memo.set() will register a non-optimal number of squares, which later will get updated by other recursive calls which are pending in the recursion tree.
If you add some conditional debugging code, you'll see that map.set is called for the same value of left multiple times, and with differing values. This is not good, because that means the if (memo.has(left)) block will execute for cases where that value is not guaranteed to be optimal (yet).
You could solve this by incorporating the index in your memoization key. This increases the space used for memoization, but it will work. I assume you can work this out.
But according to Lagrange's four square theorem every natural number can be written as the sum of at most four squares, so the returned value should never be 5 or more. You can shortcut the recursion when you get passed that number of terms. This reduces the benefit of using memoization.
Finally, there is a mistake in fillSquares: it should add n itself also when it is a perfect square, otherwise you'll not find solutions that should return 1.
Not sure about your bug, here is a short dynamic programming Solution:
Java
public class Solution {
public static final int numSquares(
final int n
) {
int[] dp = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[0] = 0;
for (int i = 1; i <= n; i++) {
int j = 1;
int min = Integer.MAX_VALUE;
while (i - j * j >= 0) {
min = Math.min(min, dp[i - j * j] + 1);
++j;
}
dp[i] = min;
}
return dp[n];
}
}
C++
// Most of headers are already included;
// Can be removed;
#include <iostream>
#include <cstdint>
#include <vector>
#include <algorithm>
// The following block might slightly improve the execution time;
// Can be removed;
static const auto __optimize__ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return 0;
}();
#define MAX INT_MAX
using ValueType = std::uint_fast32_t;
struct Solution {
static const int numSquares(
const int n
) {
if (n < 1) {
return 0;
}
static std::vector<ValueType> count_perfect_squares{0};
while (std::size(count_perfect_squares) <= n) {
const ValueType len = std::size(count_perfect_squares);
ValueType count_squares = MAX;
for (ValueType index = 1; index * index <= len; ++index) {
count_squares = std::min(count_squares, 1 + count_perfect_squares[len - index * index]);
}
count_perfect_squares.emplace_back(count_squares);
}
return count_perfect_squares[n];
}
};
int main() {
std::cout << std::to_string(Solution().numSquares(12) == 3) << "\n";
return 0;
}
Python
Here we can simply use lru_cache:
class Solution:
dp = [0]
#functools.lru_cache
def numSquares(self, n):
dp = self.dp
while len(dp) <= n:
dp += min(dp[-i * i] for i in range(1, int(len(dp) ** 0.5 + 1))) + 1,
return dp[n]
Here are LeetCode's official solutions with comments:
Java: DP
class Solution {
public int numSquares(int n) {
int dp[] = new int[n + 1];
Arrays.fill(dp, Integer.MAX_VALUE);
// bottom case
dp[0] = 0;
// pre-calculate the square numbers.
int max_square_index = (int) Math.sqrt(n) + 1;
int square_nums[] = new int[max_square_index];
for (int i = 1; i < max_square_index; ++i) {
square_nums[i] = i * i;
}
for (int i = 1; i <= n; ++i) {
for (int s = 1; s < max_square_index; ++s) {
if (i < square_nums[s])
break;
dp[i] = Math.min(dp[i], dp[i - square_nums[s]] + 1);
}
}
return dp[n];
}
}
Java: Greedy
class Solution {
Set<Integer> square_nums = new HashSet<Integer>();
protected boolean is_divided_by(int n, int count) {
if (count == 1) {
return square_nums.contains(n);
}
for (Integer square : square_nums) {
if (is_divided_by(n - square, count - 1)) {
return true;
}
}
return false;
}
public int numSquares(int n) {
this.square_nums.clear();
for (int i = 1; i * i <= n; ++i) {
this.square_nums.add(i * i);
}
int count = 1;
for (; count <= n; ++count) {
if (is_divided_by(n, count))
return count;
}
return count;
}
}
Java: Breadth First Search
class Solution {
public int numSquares(int n) {
ArrayList<Integer> square_nums = new ArrayList<Integer>();
for (int i = 1; i * i <= n; ++i) {
square_nums.add(i * i);
}
Set<Integer> queue = new HashSet<Integer>();
queue.add(n);
int level = 0;
while (queue.size() > 0) {
level += 1;
Set<Integer> next_queue = new HashSet<Integer>();
for (Integer remainder : queue) {
for (Integer square : square_nums) {
if (remainder.equals(square)) {
return level;
} else if (remainder < square) {
break;
} else {
next_queue.add(remainder - square);
}
}
}
queue = next_queue;
}
return level;
}
}
Java: Most efficient solution using math
Runtime: O(N ^ 0.5)
Memory: O(1)
class Solution {
protected boolean isSquare(int n) {
int sq = (int) Math.sqrt(n);
return n == sq * sq;
}
public int numSquares(int n) {
// four-square and three-square theorems.
while (n % 4 == 0)
n /= 4;
if (n % 8 == 7)
return 4;
if (this.isSquare(n))
return 1;
// enumeration to check if the number can be decomposed into sum of two squares.
for (int i = 1; i * i <= n; ++i) {
if (this.isSquare(n - i * i))
return 2;
}
// bottom case of three-square theorem.
return 3;
}
}

Fibonacci sequence pruning using java

Here is my implementation of the fibonacci sequence using java
/**
* Returns nth fibonacci number
*/
public class fib {
public static void main(String[] args){
System.out.println(fibonacci(12));
}
public static int fibonacci(int n) {
if (n == 0){
return 0;
} else if (n == 1){
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
But visualization of this method using recursion made me think it would be a lot faster.
Here is a visualization of fib(5). http://imgur.com/a/2Rgxs
But this got my thinking, notice at the bottom when we bubble up from the bottom of the recursive path we calculate fib(2), fib(3) and fib(4) but then we recalculate fib(3) on the very top right branch. So I was thinking when we are bubbling back up why not save fib(3) calculated from the left branch so we don't do any calculations on the right branch like my method currently does, like a hashtable while coming back up.
My question is, how do I implement this idea?
When you want to add the HashMap and stay with your original approach, try this:
static HashMap<Integer, Integer> values = new HashMap<Integer, Integer>();
public static void main(String[] args){
values.put(0, 0);
values.put(1, 1);
System.out.println(fibonacci(12));
}
public static int fibonacci(int n) {
if (values.containsKey(n)){
return values.get(n);
} else {
int left = values.containsKey(n - 1) ? values.get(n - 1) : fibonacci(n - 1);
values.put(n - 1, left);
int right = values.containsKey(n - 2) ? values.get(n - 2) : fibonacci(n - 2);
values.put(n - 2, right);
return left + right;
}
}
This approach could be very fast if you call it more often because the fibonacci results are already stored in the values (for sure this could also be done with other approaches):
public static void main(String[] args){
values.put(0, 0);
values.put(1, 1);
System.out.println(fibonacci(12));
System.out.println(fibonacci(11));
System.out.println(fibonacci(10));
}
For a fast calculation, you do not need recursion at all - just shift the intermediate results
public static int fibonacci(int n) {
if (n == 0) {
return 0;
} else {
int npp = 0; // pre-previous number
int np = 1; // previouse number
int r = 1; // current number, eventually the result
for (int i = 2; i <= n; i++) {
r = np + npp;
npp = np;
np = r;
}
return r;
}
}
In order to avoid repeated calculations, you can use dynamic programming. BTW, this is not a memory optimized solution, but it can be faster than the recursive one.
public static int fibonacci(int n)
{
int f[] = new int[n+1];
for (int i = 0; i <= n; i++) {
if(i == 0 || i == 1) {
f[i] = i;
}
else {
f[i] = f[i-1] + f[i-2];
}
}
return f[n];
}

Maximum minus minimum in an array

I am trying to find range(max - min) of an array using recursion.
Since, there can be only one return value, I am kind of confused how to go about this problem.
What I have done so far is to find maximum and minimum recursively and then use this in range function to find the range. I was wondering if it was possible to do everything in just range function somehow recursively.
public static int max(int[] array, int N) {
int maximum;
if (N >= array.length) {
maximum = Integer.MIN_VALUE;
} else {
maximum = max(array, N + 1);
if (array[N] > maximum) {
maximum = array[N];
}
}
return maximum;
}
public static int min(int[] array, int N) {
int minimum;
if (N >= array.length) {
minimum = Integer.MAX_VALUE;
} else {
minimum = min(array, N + 1);
if (array[N] < minimum) {
minimum = array[N];
}
}
return minimum;
}
public static int range(int [] array)
{
int max1 = max(array , 0);
System.out.println(max1);
int min1 = min(array , 0);
System.out.println(min1);
int range = max1 - min1;
return range;
}
If recursion really is a requirement, and you just need the range, then this should do it:
public static int range(int [] array, int index, int min, int max)
{
if (index == array.length) {
if (index == 0)
return 0;
else
return max - min;
}
else {
int value = array[index];
return range(array, index + 1, Math.min(value, min), Math.max(value, max));
}
}
public static int range(int [] array)
{
return range(array, 0, Integer.MAX_VALUE, Integer.MIN_VALUE);
}
Your algorithm seems waaaay too complicated for what you're trying to do.
It's not clear if using recursion is a requirement. If it is not, what about this?
public int range (int[] array) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int elem : array) {
if (elem < min) min = elem;
if (elem > max) max = elem;
}
return (max - min);
}
On mobile so I cannot test any code, but it should work.
EDIT: Ok sorry, re-reading your question I see you just want to know how to do it using recursion. Maybe you'd like to make that clear in the title itself ;-)
You could provide a int[] with min (index 0) and max (index 1) within the method and do all in the same method.
public class Test {
#org.junit.Test
public void test() {
int[] array = new int[] { -5,2,4,6,-10,44};
int[] minmax = new int[ 2 ];
minmax( array, minmax, 0 );
assertEquals( "[-10, 44]", Arrays.toString( minmax ) );
}
public static void minmax(int[] array, int[] minmax, int N) {
if (N >= array.length) {
return;
} else {
minmax(array, minmax, N + 1);
if (array[N] < minmax[0]) {
minmax[0] = array[N];
}
if (array[N] > minmax[1]) {
minmax[1] = array[N];
}
}
}
}
You could for example return an array (0 would have min and 1 have max) that way you can return both values.
A nicer way would be to pass a callback function to you method and call that once done.
findMinMax(array, (min, max) -> { System.out.println(min + " " + max);});
private void findMinMax(int[] array, BiFunction<Integer, Integer, Void> callback) {
//Do things
callback.apply(min, max);
}
This problem doesn't have any improvement from recursion, since it is a iterative problem that - instead - can result in performance issue due to the growing of stack size, in particular for big arrays.
Anyway, a more classical example in java 7 can be the following, where you can use a minimal "Couple" class to store min/max values
public class MaxMinRecurse {
void evalMaxMinInInterval(Couple maxmin, int pos, int[] values) {
if (pos >= values.length)
return;
int x = values[pos];
if (x < maxmin.min) {
maxmin.min = x;
} else if (x > maxmin.max) {
maxmin.max = x;
}
evalMaxMinInInterval(maxmin, pos + 1, values);
}
public static void main(String[] args) {
MaxMinRecurse mmr = new MaxMinRecurse();
int[] values = { 1, 5, 3, 4, 7, 3, 4, 13 };
Couple result = mmr.new Couple();
mmr.evalMaxMinInInterval(result, 0, values);
System.out.println("Max: " + result.max + ", Min:" + result.min);
}
class Couple {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
}
}

A recursive algorithm to find two integers in an array that sums to a given integer

I need an algorithm to determine if an array contains two elements that sum to a given integer.
The array is sorted.
The algorithm should be recursive and runs in O(n).
The recursive step should be based on the sum, meaning the method passes the sum and return true or false depending on the end result (if two elements are found - return true, else - return false)
Only linear data structures can be used.
Any ideas are appreciated..
You can convert any iterative algorithm into a recursive one by using (for instance) tail recursion. I'd be more expansive, if it weren't homework. I think you'll understand it from the other post.
Normally I'd use a Map, but since one of the requirements is to use a linear data structure, I think that's excluded, so I'd go about using a boolean array.
public boolean hasSum( int[] numbers, int target )
{
boolean[] hits = new boolean[ target + 1 ];
return hasSumRecursive( 0, numbers, target, hits );
}
public boolean hasSumRecursive( int index, int[] numbers, int target, boolean[] hits )
{
...
}
Hopefully this is a good enough hint.
I think hash is ok, for example, 1,3,7,9,12,14,33...
if we want sum=21, we hash the numbers into a hash table, So, O(n).
we iterator them, when we get 7, we let 21-7=14, so we hash 14, we can find it. so 7+14=21,
we got it!
Here is a solution witch takes into account duplicate entries. It is written in javascript and assumes array is sorted. The solution runs in O(n) time and does not use any extra memory aside from variable.
var count_pairs = function(_arr,x) {
if(!x) x = 0;
var pairs = 0;
var i = 0;
var k = _arr.length-1;
if((k+1)<2) return pairs;
var halfX = x/2;
while(i<k) {
var curK = _arr[k];
var curI = _arr[i];
var pairsThisLoop = 0;
if(curK+curI==x) {
// if midpoint and equal find combinations
if(curK==curI) {
var comb = 1;
while(--k>=i) pairs+=(comb++);
break;
}
// count pair and k duplicates
pairsThisLoop++;
while(_arr[--k]==curK) pairsThisLoop++;
// add k side pairs to running total for every i side pair found
pairs+=pairsThisLoop;
while(_arr[++i]==curI) pairs+=pairsThisLoop;
} else {
// if we are at a mid point
if(curK==curI) break;
var distK = Math.abs(halfX-curK);
var distI = Math.abs(halfX-curI);
if(distI > distK) while(_arr[++i]==curI);
else while(_arr[--k]==curK);
}
}
return pairs;
}
I solved this during an interview for a large corporation. They took it but not me.
So here it is for everyone.
Start at both side of the array and slowly work your way inwards making sure to count duplicates if they exist.
It only counts pairs but can be reworked to
use recursion
find the pairs
find pairs < x
find pairs > x
Enjoy!
It is pretty easy. It is important for array to be sorted.
Correct algorithm with O(n) time complexity and no additional space is:
public static boolean isContainsSum(int[] arr, int sum) {
for (int i = 0, j = arr.length - 1; i < j; ) {
if (arr[i] + arr[j] == sum)
return true;
if (arr[i] + arr[j] < sum)
i++;
else
j--;
}
return false;
}
To make it recursive, you need just replace i and j iterations with recursive call:
public static boolean isContainsSumRecursive(int[] arr, int sum) {
return isContainsSumRecursive(arr, sum, 0, arr.length - 1);
}
private static boolean isContainsSumRecursive(int[] arr, int sum, int i, int j) {
if (i == j)
return false;
if (arr[i] + arr[j] == sum)
return true;
if (arr[i] + arr[j] < sum)
return isContainsSumRecursive(arr, sum, i + 1, j);
return isContainsSumRecursive(arr, sum, i, j - 1);
}
Here is my solution: I iterate until the first number is greater than the expected sum, then until to second one or the sum of two is greater than the expected sum. If I do not want a correct answer, I return {-1,-1} (assuming all numbers are positive integers)
{
private static int[] sumOfTwo(int[] input, int k) {
for (int i = 0; i < input.length - 1; i++) {
int first = input[i];
for (int j = 1; j < input.length; j++) {
int second = input[j];
int sum = first + second;
if (sum == k) {
int[] result = {first, second};
return result;
}
if (second > k || sum > k) {
break;
}
}
if (first > k) {
break;
}
}
int[] begin = {-1, -1};
return begin;
}
}
Here is the recursion method to perform the groupSum
public boolean groupSum(int start, int[] nums, int target)
{
if (start >= nums.length) {
return (target == 0);
}
return groupSum(start + 1, nums, target - nums[start]) || groupSum(start +
1,nums,target)
}
def ExistsSum(A, i, j, Target):
if i >= j:
return False # Failure, all candidate pairs exhausted
if A[i] + A[j] < Target:
return ExistsSum(A, i+1, j, Target) # Try a larger sum
if A[i] + A[j] > Target:
return ExistsSum(A, i, j-1, Target) # Try a smaller sum
return True # Success
Run with
ExistsSum(A, 0, len(A)-1, Target)
bool solve(vector<int> &sorted_array, int l, int r, int target) {
if(l>=r) {
return false;
}
if(sorted_array[l] + sorted_array[r] == target) {
return true;
}
if(sorted_array[l] + sorted_array[r] > target) {
return solve(sorted_array, l, r-1, target);
}
if(sorted_array[l] + sorted_array[r] < target) {
return solve(sorted_array, l+1, r, target);
}
}
int main() {
vector<int> a = ...
solve(a, 0, a.size() - 1, target)
}
Here is my solution using recursion
pair<int,int> twoSum(int arr[], int s, int e, int target, pair<int, int>p){
//base case
if(arr[s] + arr[e] == target){
// pair<int,int>p;
p.first = arr[s];
p.second = arr[e];
return p;
}
while(s < e-1){
if(arr[s] + arr[e] < target){
s++;
return twoSum(arr, s, e, target, p);
}
if(arr[s] + arr[e] > target){
e--;
return twoSum(arr, s, e, target, p);
}
}
//if there is no pair possible
cout<<"pair is not possible" <<endl;
return p;
}
Sort the array. Search for the complement of each number (sum-number). Complexity O(nlogn).

Categories