Algorithm for what should be a simple problem

  • Thread starter SpiffyEh
  • Start date
  • Tags
    Algorithm
In summary, an efficient O(ln) algorithm for determining whether there exists an index i such that ai = i in a sorted sequence of distinct integers is by recursively splitting the sequence in half and checking if the middle element satisfies the condition. This approach has a time complexity of O(ln) and is achieved by dividing and conquering the problem.
  • #1
SpiffyEh
194
0

Homework Statement



Suppose that you are given a sorted sequence of distinct integers {a1, a2, . . . , an}.
Give an O(lg n) algorithm to determine whether there exists an i index such as
ai = i. For example, in {−10,−3, 3, 5, 7}, a3 = 3. In {2, 3, 4, 5, 6, 7}, there is no
such i.

Homework Equations





The Attempt at a Solution


could this be done using a tree? For example splitting the problem in half with every iteration and checking to see if it is in the end? Or am I approaching this wrong?
 
Physics news on Phys.org
  • #2
O(ln) usually implies a divide-conquer approach
Try the half eah sequence and see what O() that has
 
  • #3
i know halving it repeatedly gives O(log n) but I don't see how that gets me anywhere
 
  • #4
Here's a kind of rough outline. You have a sorted list of n numbers. Look at the middle of the list, and see whether an/2 == n/2. If so, you're done.

If an/2 < n/2, look in the lower half of the list.
If an/2 > n/2, look in the upper half of the list.

Do the above recursively, stopping when the size of the list is 1.

For example, suppose the list is {−10,−3, 3, 5, 7, 8 11, 12}. (I added a few numbers to your first example.)

Here n = 8

a4 = 5, so we should look in the lower half of the list; i.e., a1 through a4.

Now n = 4
a2 = -3, so we should look in the upper half of this list; namely, at a3 and a4

Now n = 2, and we see that a3 == 3 and we're done.
 
  • #5


I would like to offer a solution to this problem using a binary search algorithm. This algorithm works by dividing the sorted sequence into two halves and then checking if the middle element is equal to its index. If it is, then the algorithm terminates and returns the index. If not, then the algorithm compares the middle element to its index and determines whether the target element is in the left or right half of the sequence. This process is repeated until the target element is found or the algorithm determines that it does not exist in the sequence.

The time complexity of this algorithm is O(log n) because with each iteration, the size of the problem is halved, leading to a logarithmic time complexity. This makes it an efficient solution for determining whether there exists an i index such that ai = i.

In summary, the algorithm for this problem would involve using a binary search approach to divide the sorted sequence and compare the middle element to its index until the target element is found or determined to not exist. This would result in a time complexity of O(log n) making it an efficient solution for a simple problem.
 

Related to Algorithm for what should be a simple problem

1. What exactly is an algorithm?

An algorithm is a step-by-step process or set of rules used to solve a problem or complete a task.

2. How do you determine the best algorithm for a simple problem?

To determine the best algorithm for a simple problem, you must first understand the problem and its constraints. Then, you can consider factors such as efficiency, accuracy, and scalability to select the most suitable algorithm.

3. Can an algorithm be used for any type of problem?

No, an algorithm is designed for a specific type of problem and may not be applicable to all types of problems. It is important to choose the right algorithm for the specific problem at hand.

4. What are some common mistakes made when creating an algorithm?

Some common mistakes when creating an algorithm include not considering all possible scenarios, not testing the algorithm thoroughly, and not optimizing for efficiency.

5. Is it possible for an algorithm to have multiple solutions?

Yes, an algorithm can have multiple solutions. In some cases, there may be more than one way to solve a problem, and it is up to the programmer to determine which solution is the most efficient and effective.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
972
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
9
Views
1K
Back
Top