(5/5)

# For each of your solutions please provide a short explanation as to why you believe your solution works properly

INSTRUCTIONS TO CANDIDATES

data structures and algorithms:#

For each of your solutions please provide a short explanation as to why you believe your solution works properly. This is not formally required, so I will not reduce marks if you choose not to provide any explanations. However, in case the explanations are provided, my feedback will be more meaningful as I will be able to see your line of thought.

1 Most frequently occurring number:

Design an O(n) algorithm whose input is a sorted array A of integer numbers. The program should print the most frequent element of A. For example, in the array A = [1,2,2,5,7,7,7,8], 1 occurs once, 2 occurs twice, 5 occurs once, 7 occurs three times,and 8 occurs once. Hence, the most frequently occurring element is 7. If there are two or more most frequently occurring elements, the algorithm can be print anyone of them (e.g. the ﬁrst one).

Solution to Question 1

2 Number of distinct elements in a sorted array

Design an O(n) algorithm whose input is a sorted array A of integer numbers. The program should print the number of distinct elements of A. For example, in the array A = [1,2,2,5,7,7,7,8], as in the previous section, the number of distinct elements is 5 which are 1,2,5,7,8.

Solution to Question 2

3 Number of distinct elements in an arbitrary array

Design an O(n2) algorithm whose input is an array A of integer numbers (not necessarily sorted). The program should print the number of distinct elements of A

Solution to Question 3

4 Three distinct elements of a sorted array

Design an O(logn) algorithm whose input is a sorted array A of integer numbers. The algorithm should print YES if A contains at least 3 distinct numbers and NO otherwise. Hint: Compare the ﬁrst and the last elements and then use binary search.

(5/5)

## Related Questions

##### . Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

##### . The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

##### . Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

##### . SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

##### . Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Hire Me