Question

In: Computer Science

Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a...

Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain.

Problem 2: [10 Marks] Now consider the same problem as problem 1, but this time you only have positive integer numbers ranging from 0 to (n-1) in the array. Can you find a duplicate in O(n) complexity? Implement the solution.

Plz answer 2nd only

Solutions

Expert Solution

2.


// Algorithm to check if an array of n positive integers ranging from 0 to (n-1) contains duplicate elements

Let the input array be arr[n]
Create an array of length n to contain the frequency of elements in arr and initialize all its elements to 0
   int counts[n] ; // array index starts from 0 and ends at (n-1), the elements are initialized to 0
   // loop that will continue for a maximum of n times
   for(i=0;i<n;i++)
       counts[i] = 0
Loop over the input array, incrementing th counts of elements in counts array
  
   // loop that will continue for a maximum of n times
   for(i=0;i<n;i++)
   do
       counts[arr[i]] = counts[arr[i]] + 1; // since arr contains elements from 0 to n-1 , the element at arr[i] will not exceed the index of counts
       // if counts of arr[i] element > 1 then duplicate elements is present, return true
       if((counts[arr[i]]) > 1) then
           return true
       end if  
   done      
  
return false; // no duplicate elements present in arr
      
     
Both the loops in the solution continues for a maximum of n times. The solution only contains a single loop that runs for a maximum of n times. Hence, the time complexity of the above solution is O(n).


Related Solutions

A)Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether...
A)Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain. B) Now consider the same problem as Question A, but this time you only have positive integer numbers ranging from 0 to (n-1) in the...
Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether...
Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain.
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 2. Give an efficient sorting algorithm for an array C[1, ..., n] whose elements are taken from the set {1, 2, 3, 4, 5}.
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 3. Give an efficient sorting algorithm for an array D[1, ..., n] whose elements are distinct (D[i] ̸= D[j], for every i ̸= j ∈ {1, ..., n}) and are taken from the set {1, 2, ..., 2n}.
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 1. Give an efficient sorting algorithm for a boolean1 array B[1, ..., n]. 2. Give an efficient sorting algorithm for an array C[1,...,n] whose elements are taken from the set {1,2,3,4,5}. 3. Give an efficient sorting algorithm for an array D[1, ..., n] whose elements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT