Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please edit the following code to have O(n). Currently, it is at O(n^2) and I need to reduce it to O(n). You can also paste

Please edit the following code to have O(n). Currently, it is at O(n^2) and I need to reduce it to O(n). You can also paste another solution that is O(N). Paste the edited code and highlight what was changed. If you meet all conditions I will give thumbs up.

image text in transcribed

#function for finding biggest value X with x frequency

def solution(A):

#assuming final output is 0

out=0

#an empty dictionary to store frequiency of every number

dic=dict()

#for loop to get frequency of every element in list A

for i in A:

#if key present then count increases by 1

if i in dic.keys():

dic[i]+=1

#if not in dict then adding to dic with count 1

else:

dic[i]=1

#loop to check whether count and number is equal

#dic.items() are the key,valu pair of dic

for i,j in dic.items():

#condition for maximum number

if i==j and i>out:

out=i

return out

image text in transcribed

#Tun 2 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 inding biggest Value with Xtrequency def solution(A): #assuming final output is o out=0 #an empty dictionary to store frequiency of every number dic-dict() #for loop to get frequency of every element in list A for i in A: #if key present then count increases by 1 if i in dic.keys(: dic[i]+=1 #if not in dict then adding to dic with count 1 else: dic[i]=1 #loop to check whether count and number is equal #dic.items() are the key, valu pair of dic for ij in dic.items(): #condition for maximum number if i==j and i>out: outui return out Write a function def solution(A) that, given an array A consisting of Nintegers, returns the biggest value X, which occurs in A exactly X times. If there is no such value, the function should return 0. Examples: 1. Given A = [3, 8, 2, 3, 3, 2), the function should return 3. The value 2 occurs exactly two times and the value 3 occurs exactly three times, so they both meet the task conditions. The value 8 occurs just once, thus it does not meet the task conditions. The maximum of 2 and 3 is 3. 2. Given A = [7, 1, 2, 8, 2], the function should return 2. The value 1 occurs exactly one time; the value 2 occurs exactly two times. 3. Given A = [3, 1,4, 1, 5), the function should return 0. There is no value which meets the task conditions. 4. Given A = [5, 5, 5, 5, 5), the function should return 5. Write an efficient algorithm for the following assumptions: Nis an integer within the range (1...100,000); each element of array A is an integer within the range (1..1,000,000,000)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions