Question

In: Computer Science

//2.-------------------------------------------------------------- Given an array of size N, what is the complexity? int m = A[0]; for...

//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( )

Solutions

Expert Solution

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)


Related Solutions

Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
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 int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the...
Given the existence of two-dimensional array double A[M][N], where M and N are #defined as the number of rows and columns, respectively, define a function named sqabsmax that accepts array A as an argument (i.e. input parameter) and returns the square of the maximum absolute value element in A. Use the const qualifier if appropriate. Only show the function definition. Do not write an entire program with a main function. Just write the definition for function sqabsmax. in C
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT