In: Computer Science
//2.--------------------------------------------------------------
Given an array of size N, what is the complexity?
int m = A[0];
for (int i=1; i<N; i++)
{ if (A[i] > m) m = A[i]; }
int k = 0;
for (int i=0; i<N; i++)
{ if (A[i] == m) k++; }
What is a (name one) most frequent operation performed?______________________
Expression showing the number of times it is performed ___________
Time Complexity in Big-O Notation O( )
_____________________________________________________
//3.--------------------------------------------------------------
int mult(int N, int M)
{
int s = 0;
while (N > 0)
{
s = s + M;
N = N-1;
}
return s;
}
What is a (name one) most frequent operation performed?______________________
Expression showing the number of times it is performed___________
Time Complexity in Big-O Notation O( )
Solution
===================
Part 2)- Most frequent operation is i++
explanation:- here both for loop will execute N times from i=0 to i<N and each time loop execute i will be incremented by one that is i++ statement will be executed.
-Expression showing the number of times it is performed :- 2N
here we have two for loop and in each for loop i++ will be executed exactly N times thus two number of time i++ executed is 2N.
-Time Complexity in Big-O Notation O( ):- Here we have two for loop and each loop will executed exactly N times thus time complexity is O(2N) which is asymptomatic equal to O(N).
Part3)Most frequent operation is :- N=N-1
Here while loop will executed from N to 1 That is loop will execute N times and each time loop execute N=N-1 will also execute. Hence N=N-1 is most frequent operation
-Expression showing the number of times it is performed :- N
here we have one while loop and in each loop run N=N-1 will be executed exactly N times thus N=N-1 will execute N times
-Time Complexity in Big-O Notation O( ):- Here while loop will execute N times thus time complexity is O(N)