Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your algorithm? Justify the correctness of your algorithm and your complexity claim.
Note* Here indices are starting from 1.I am considering the first index as always happy as there are no element before the 1st number.In the code given below it starts with A[0] as programming languages index starts from 0.So please dont get confused A[n-1] in program is same as A[n] in question.The output is as required that it the output is in accordance with the question asked.It will be from 1 to n.
A = [A[1],A[2],A[3],....................A[n]]
Index j take values from 1 to n.
Index j is happy index if for all i < j A[i]<A[j]
I am providing a O(n) complexity here.
Method :
We initialize a variable called maximum which maintains the maximum upto the previous element while we are checking for jth element.If our element at jth index is greater than this maximum then it is a good index because as it is greater than the maximum number so it will be greater than all other numbers.And we update the maximum to this element and proceed to check whether j+1th index number is a good index or not.If this is not greater than the maximum number then it is not a good index and we dont this value into our happyindex list.We proceed with the net number.
Python code:
#defining main function
def main():
A = [] # List which ill have all numbers
happy = [] # list of all happy indices
x = int(input("enter number of values in the array")) # taking number of values in the list
for i in range(x): # a for loop for taking aall the inputs
y = int(input("enter a value"))
A.append(y)
maximum = A[0] # initializing maximum variable to A[0] which is the first value.this means
#A[1] in our program where our indexing starts from 1
happy.append(1) # because there is no element before 1st number
for i in range(1,x): # checking if each index is a happy index
if A[i] > maximum: # checking the condition
happy.append(i+1) # if greater than append it
maximum = A[i] # set the new maximum
else:
continue # continue with other index
print("The happy index are" + str(happy)) # print the result
# calling the main function
if __name__ == "__main__":
main()
Picture for indentation :
Screenshot of the result for a sample array :
Running time for the algorithm : O(n)
Correctness of our algorithm :
In the second for loop used to check whether the index is correct we are comparing it with the maximum upto j-1.So if jth index value is greater than this maximum then it definitely means that it is greater than all the values before it.By definition it is a happy index and we update the maximum for the next case.So all the values that we are appending in the list will be happy values.
Pre condition : N > 0 i.e we will be provided with atleast one number and A[i] != A[j] for all i and j in range 1 to n both inclusive and i!=j.
Post condition : happy list is of all index which are happy
Program terminates as we check for at maximum n elements.
Loop variant (second loop) : if we are checking for jth element then maximum is the maximum of elements upto j-1.
From loop invariant and the argument we did before we achieve post condition.So our program is correct.
Expanation :
We can see that we used only two for loops and this for loops are not nested they are in parllel and all other program statements take constant time.This for loop takes O(n) time.So our program running time complexity is O(n).
Please comment if you have any doubt regarding this question.I will solve your doubts as soon as possible.Thank you.
Note* Here indices are starting from 1.I am considering the first index as always happy as there are no element before the 1st number.In the code given below it starts with A[0] as programming languages index starts from 0.So please dont get confused A[n-1] in program is same as A[n] in question.The output is as required that it the output is in accordance with the question asked.It will be from 1 to n.
A = [A[1],A[2],A[3],....................A[n]]
Index j take values from 1 to n.
Index j is happy index if for all i < j A[i]<A[j]
I am providing a O(n) complexity here.
Method :
We initialize a variable called maximum which maintains the maximum upto the previous element while we are checking for jth element.If our element at jth index is greater than this maximum then it is a good index because as it is greater than the maximum number so it will be greater than all other numbers.And we update the maximum to this element and proceed to check whether j+1th index number is a good index or not.If this is not greater than the maximum number then it is not a good index and we dont this value into our happyindex list.We proceed with the net number.
Python code:
#defining main function
def main():
A = [] # List which ill have all numbers
happy = [] # list of all happy indices
x = int(input("enter number of values in the array")) # taking
number of values in the list
for i in range(x): # a for loop for taking aall the inputs
y = int(input("enter a value"))
A.append(y)
maximum = A[0] # initializing maximum variable to A[0] which is the
first value.this means
#A[1] in our program where our indexing starts from 1
happy.append(1) # because there is no element before 1st
number
for i in range(1,x): # checking if each index is a happy
index
if A[i] > maximum: # checking the condition
happy.append(i+1) # if greater than append it
maximum = A[i] # set the new maximum
else:
continue # continue with other index
print("The happy index are" + str(happy)) # print the result
# calling the main function
if __name__ == "__main__":
main()
Picture for indentation :
Screenshot of the result for a sample array :
Running time for the algorithm : O(n)
Correctness of our algorithm :
In the second for loop used to check whether the index is correct we are comparing it with the maximum upto j-1.So if jth index value is greater than this maximum then it definitely means that it is greater than all the values before it.By definition it is a happy index and we update the maximum for the next case.So all the values that we are appending in the list will be happy values.
Pre condition : N > 0 i.e we will be provided with atleast one number and A[i] != A[j] for all i and j in range 1 to n both inclusive and i!=j.
Post condition : happy list is of all index which are happy
Program terminates as we check for at maximum n elements.
Loop variant (second loop) : if we are checking for jth element then maximum is the maximum of elements upto j-1.
From loop invariant and the argument we did before we achieve post condition.So our program is correct.
Expanation :
We can see that we used only two for loops and this for loops are not nested they are in parllel and all other program statements take constant time.This for loop takes O(n) time.So our program running time complexity is O(n).
Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j...
1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.
6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative. Give an algorithm that can find an index i such that 1 <i<n and T[i] = i, provided such an index exists. Your algorithm should take a time in O(lg n) in the worst case. Answers must be proven (or at least well justified)
Given an array A containing 0s and 1s, such that all the 0s appear in the array before all the 1s. Write an algorithm with worst-case time complexity O(log(n)), which finds the smallest index i such that A[i] = 1. Describe your algorithm, and analyze its worst-case time complexity.
need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...
(20 points) You are given an array A of distinct integers of size n. The sequence A[1], A[2], ..., A[n] is unimodal if for some index k between 1 and n the values increase up to position k and then decrease the reminder of the way until position n. (example 1, 4, 5, 7, 9, 10, 13, 14, 8, 6, 4, 3, 2 where the values increase until 14 and then decrease until 1). (a) Propose a recursive algorithm to...
Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)
1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.
6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...
(13 pts) Given an array AlI,2,. .. ,n] integers, design and analyze an efficient Divide-and-Conquer algorithm to find some i and j, where j > 1, such that A[j]-Ali] is maximized. For example, given A 6, 1,3,8,4,5, 12,6], the maximum value of AL] - Ali] for j > i is 12-1 11 where j -7 and i 2. Give the underlying recurrence relation for your algorithm and analyze its running time. You should carefully state all details of your algorithm:...
Question3 10 pts Let A [L.n] be a max-heap with n > 1 and consider the index ị such that l 〈 i 〈 n . Assume that all the elements of A are distinct. Write the pseudocode of an algorithm which replaces A [i] by A i 100 and then re-arranges the elements of A into a max-heap. The running time of your algorithm must be O (log, n Upload Choose a File Question3 10 pts Let A [L.n]...