Question

In: Computer Science

Can I get a breakdown line by line of the time complexity, I know it is...

Can I get a breakdown line by line of the time complexity, I know it is O(NlogN) but I am trying to better understand time complexity as a whole.

Input :- N , Array[N]

                               function findSmallestPair()

                               sort the Array

                               int minimumDiff := INFINITY , A := -1 , B := -1;  

                               for(int i : 1 to N-1)

                               begin for

               currentDiff = Array[i]-Array[i-1]

               if(currentDiff < minimumDiff)

               begin if

                              A := Array[i-1];

                              B := Array[i];

                              minimumDiff = currentDiff;

                   end if

              end for

           return {A,B}; end function                      

Solutions

Expert Solution

Time Complexity:

function findSmallestPair()

                               sort the Array //Using standard sort method generally it takes O(nlogn) time(eg:Merge Sort)

                               int minimumDiff := INFINITY , A := -1 , B := -1; //Constant Operation O(1) time

                               for(int i : 1 to N-1) //Loop iterate from1 to n-1 ,So Time complexity O(n)

                               begin for

               currentDiff = Array[i]-Array[i-1] // Constant Operation O(1) time

               if(currentDiff < minimumDiff) //If also take constant operation time O(1)

               begin if

                              A := Array[i-1]; //O(1)

                              B := Array[i]; //O(1)

                              minimumDiff = currentDiff; //O(1)

                   end if

              end for

           return {A,B}; end function                     

So we can easily find that the maximum time complexity is O(nogn) .

If the sort line is removed from your code then the time complexity is O(n).  


Related Solutions

I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
Can I get all of the question answered in a way that I know under which...
Can I get all of the question answered in a way that I know under which sub question the answer goes please. Can I get all of the questions answered in a way that I know how it fits in the question. Some parts are explanations. Thank you. Q. When a pink aqueous solution of potassium permanganate, faintly acidified with dilute sulfuric acid was treated with 10% aq. hydrogen peroxide, the reaction took place with the evolution of gas bubbles,...
I want to know why I can not get N(should contain 1000 number) N<-vector() for (k...
I want to know why I can not get N(should contain 1000 number) N<-vector() for (k in 0:999){ y=vector() for(i in 0:999){ r1=runif(1) r2=runif(1) x1=-log(r1) x2=-log(r2) k=(x1-1)^2/2 if (x2 >= k){ r<-runif(1) if (r>0.5){ y[i]= x1 }else{ y[i]= -x1 } } y.1<- y[-which(is.na(y))] number<-length(y.1) } N[k]<-number } N
How can I know when I should use line graph, histogram, and steam-leaves? Is that gonna...
How can I know when I should use line graph, histogram, and steam-leaves? Is that gonna be mansioned in the text or I have to know by myself?
I would like to know the mortgage calculation including breakdown to come up with the monthly...
I would like to know the mortgage calculation including breakdown to come up with the monthly mortgage payment for the following: Price of Home: $550,000 Interest rate: 4.25 Length of loan: 30 years 0 down payment Please provide a written response to show calculation and not an excel spreadsheet. I need to learn the actual written calculation. Thank you
Can I get some simple pseudocode (simple descriptions as exampled in top italicized line) added to...
Can I get some simple pseudocode (simple descriptions as exampled in top italicized line) added to this small section of Java code? Thumbs up always left for answers! ------------------------------------------------------------------------------------------------------- // Check to see if two bags are equals.    public boolean equals(LinkedBag<T> aBag) {        boolean result = false; // result of comparison of bags        if (this.numberOfEntries == aBag.numberOfEntries) {            if (numberOfEntries == 0) {                return true;           ...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these functions. A step by step tutorial would be great. This is done in Python. Please not only the answer, but how you calculated it as well. Here is the code #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l)...
//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know...
//Trying to get this code with JavaScript. I could partition the subarrays, but I don't know how to check for unique elements Given an array of integers check if it is possible to partition the array into some number of subsequences of length k each, such that: Each element in the array occurs in exactly one subsequence For each subsequence, all numbers are distinct. Elements in the array having the same value must be in different subsequences If it is...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity of O(n^2)? Justify.
Please explain clearly which line means what and how can I get the answer. (correct answer...
Please explain clearly which line means what and how can I get the answer. (correct answer 2 2) public class Checker { public static int count = 0; public int number = 0; public Checker(){ count++; number = count; } public int getCount() { return count; } public int getNumber() { return number; } } What is output from the code fragment below? Checker one = new Checker(); Checker two = new Checker(); System.out.println(one.getCount() + " " + two.getCount());
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT