31.08.2015 Views

Objectives Experiment #1 Introduction to Algorithms

Lab 1 - Rabie Ramadan

Lab 1 - Rabie Ramadan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

AnotherFibonacci<br />

AnotherFibonacci sequence begins by defining the first three elements A[0], A[1]<br />

and A[2]. The remaining elements are calculated using the following recurrence:<br />

A[i] = A[i-1] + A[i-2] + A[i-3]<br />

You are given a int[] A which contains exactly one element that is equal <strong>to</strong> -1, you must<br />

replace this element with a positive number in a way that the sequence becomes a<br />

AnotherFibonacci sequence. Return this number.<br />

If no such positive number exists, return -1.<br />

It’s required that you implement and test this following class by the example data, and write the<br />

complexity of your algorithm if you can (in terms of time, iterations, or number of basic operations)<br />

Definition<br />

Class: AnotherFibonacci<br />

Method:<br />

complete<br />

Parameters: array of integers<br />

Returns:<br />

int<br />

Method signature (in C#): int complete(int[] A)<br />

Notes<br />

- The constraints for the elements of the input int[] A do not necessarily apply for the<br />

replacement <strong>to</strong> the missing element.<br />

Constraints<br />

- A will contain between 4 and 20 elements, inclusive.<br />

- Each element of A will be -1 or between 1 and 1000000, inclusive.<br />

- Exactly one element of A will be -1.<br />

Examples<br />

0)<br />

input = {1,2,3,-1}<br />

Returns: 6<br />

Winter 2012 <strong>Experiment</strong> <strong>#1</strong> 4/5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!