samedi 30 juillet 2016

Determining Big O: Find 4 different numbers in array summing up to S

Given an array of numbers arr and a number S, find 4 different numbers in arr that sum up to S.

Write a function that gets arr and S and returns an array with 4 indices of such numbers in arr.

The solution I came up with involves recursively building up combinations of indices while making sure no combinations are counted more than once. I also prune the search tree by making sure the size of the solution set is no more than 4.

#include <iostream>
#include <vector>
using namespace std;

bool find_sol(vector<int> &N, vector<int> &I, int S, int sum){
  if (I.size() == 4)
    if (S == sum)
      return true;
    else
      return false;
  for (int i = 0; i < N.size(); ++i){
    if (I.empty() || I[I.size()-1] < i){
      I.push_back(i);
      if (find_sol(N,I,S,sum+N[i]))
        return true;
      else {
        I.pop_back();
      }
    }
  }
  return false;
}

int main(){
  int S = 23;
  vector<int> numbers = {1,3,5,6,7,8,9};
  vector<int> indices;
  find_sol(numbers,indices,S,0);

  // prints 0, 2, 5, 6
  for (int i = 0; i < indices.size(); ++i)
    cout << indices[i] <<" ";
  cout<<endl;

  return 0;
}

How can I determine the run time of this algorithm? I feel like it is something like O(n choose 4) but am not sure. The solution tells me the optimal run time is O(n^2).

Aucun commentaire:

Enregistrer un commentaire