Question

In: Computer Science

The following algorithm returns true if the n items in an array are all distinct, i.e.,...

The following algorithm returns true if the n items in an array are all distinct, i.e., if no two items are equal to each other, otherwise it returns false. 1 ALGORITHM UniqueElements(A[1..n]) 2 // Determine whether all the elements in an array are distinct 3 // Input: Array A[1..n] 4 // Output: “true” if all elements of A are distinct, “false” otherwise 5 for i ← 1 to n – 1 do 6 for j ← i + 1 to n do 7 if A[i] = A[j] return false 8 return true The basic operation is the comparison of two array elements at line 7. Determine the number of basic operations performed for an input of size n. Write your answer in simplest form, if possible.

Solutions

Expert Solution

So, as you said I will try to be as simple as I can.

So, algorithm for the given problem is :

for i ← 1 to n – 1 do 
      for j ← i + 1 to n do 
             if A[i] = A[j] 
                   return false 
return true

Now, let's talk about the number of operations performed in terms of n.

for i ← 1 to n – 1 do             //------------> O(n){as loop is running from 1 to n) that means n times loop is running and checking the condition also that the loop condition holds good and loop will continues to run. Hence number of operations are n.
      for j ← i + 1 to n do       //------------> O(n*n)(as loop is running from i+1 to n) that means in worst case it will run n times which will be similarly takes O(n) time and this same O(n) time will be repeating n times . Hence total operations will be O(n*n) 
             if A[i] = A[j]       //------------> O(n*n)(as it is written in the nested loop which is performing O(n*n) operations, so this will also runs O(n*n) times)
                   return false  //-------------> O(1) it will executed only 1 time
return true                       //-------------> O(1) it will executed only 1 time



Hence, total operations of this algorithm will be:

                              O(n) + O(n*n) + O(n*n) + O(1) + O(1)

Hence, as we know O will be written as the maximum operation in the expression:


So, O(n*n) will be the total operations performed in the algorithm

So, this was the solutions of the problem.
I hope I am able to solve your problem, if yes then do give it a like.
It really helps :)


Related Solutions

Let A be an array of non-decreasing N integers. Write an algorithm that returns the number...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number of pairs of integers in A that are equal. Analyze your algorithm thoroughly. Your analysis should include a thorough examination of both the best and the worst-case scenarios. This includes a description of what the best and worst cases would look like. You should include both space and time in your analysis, but keep in mind that space refers to “extra space,” meaning in...
An array A of n distinct numbers are said to be unimodal if there exists an...
An array A of n distinct numbers are said to be unimodal if there exists an index k, 1 ≤ k ≤ n, such that A[1] < A[2] < · · · < A[k − 1] < A[k] and A[k] > A[k + 1] > · · · > A[n]. In other words, A has a unique peak and are monotone on both sides of the peak. Design an efficient algorithm that finds the peak in a unimodal array of...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
give an algorithm where given as input an array A[1...n] withthe following property. There exists...
give an algorithm where given as input an array A[1...n] with the following property. There exists an index I such that if we append A[1...i−1] after A[i...n], we get an array in sorted increasing order. For simplicity you can assume that n is a power of 2.Give an efficient algorithm that returns the smallest element in A. Analyze the time complexity of your algorithm.Hint: you may want to compare A[1] and A[n/2].
Prove that the following is true for all positive integers n: n is odd if and...
Prove that the following is true for all positive integers n: n is odd if and only if n2 is odd.
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT