Question

I am trying to calculate the runtime complexity of a function that does not have fixed...

I am trying to calculate the runtime complexity of a function that does not have fixed size input, but uses several helper methods that do have fixed size input. I was unsure of how to include the helper methods in my calculations.

If I have an array with a fixed size of 32 indices, and I have a function that sums up the elements in that array, will that function be O(n), or O(1)? I think that a function that sums up the elements of an array is O(n) because each element of an n-length array must be visited, but if the function only sums up arrays of length 32, is it still O(n)?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There are some things to consider here.

Conceptually, an algorithm that iterates once over the input array has runtime ?(n) (and O(n) if it does constant-time work per element), n being the size of the array.

In application, if the algorithm is only ever called with arrays of length n?n0 for some constant n0, you may treat the used time as O(n)?O(n0)=O(1). Note that the n here is probably not the one you use for your whole algorithm!

In practice, every algorithm runs constant time as real computers have finite memory. This is not an interesting model in the sense that it does not help to differentiate between algorithms that do behave very differently in practice, so we don't use it.

You may be interested in some of our reference questions on asymptotics, runtime analysis and the time used by arithmetic operations.

Add a comment
Answer #2

#include<iostream>

int subArraySum(int arr[], int n, int sum)

{

int curr_sum, i, j;

    for (i = 0; i < n; i++)

    {

        curr_sum = arr[i];

}

for (j = i+1; j <= n; j++)

        {

            if (curr_sum == sum)

            {

                printf ("Sum found between indexes %d and %d", i, j-1);

                return 1;

            }

            if (curr_sum > sum || j == n)

                break;

           curr_sum = curr_sum + arr[j];

        }

    }

printf("No subarray found");

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
I am trying to calculate the runtime complexity of a function that does not have fixed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Determine the runtime complexity of the following. Use O() notation, but the function that you place within the parenthe...

    Determine the runtime complexity of the following. Use O() notation, but the function that you place within the parentheses of O() should be the upper bound on the runtime of the algorithm or function. Unless otherwise specified, N is the problem size or the number of elements in the container (even if N does not appear in the code or algorithm you are given), and all other values are constants. For graphs, use |V| and |E| for the size of...

  • Find the space complexity of Merge Sort below as a function of n (the length of...

    Find the space complexity of Merge Sort below as a function of n (the length of A). Assume: • The elements of A require (1) space. • Merge takes 2 sorted arrays as input and merges them into one sorted array containing both inputs' elements in (n) space. A (there is no index trickery allowing Al and A2 Note that Al and A2 are independent arrays from to be "in" A; A is not being sorted in place). Merge Sort...

  • Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of...

    Write in Java. Program need to have runtimes < n^2 to satisfy the runtime efficiency of some of the testsets. Question 2: Understanding Orders Given an array A of size N, find the number of ordered pairs (i, j) such that i < j and A[i] < A[j]. Input: • First line contains one integer, N, size of array. • Second line contains N space separated integers denoting the elements of the array A. Output: Print the total number of...

  • Question One (2 marks): What is the complexity of the given code as a function of the problem size n? Show the (complete) details of your analysis. Note: a [ i] s an array with n elements. for (int i...

    Question One (2 marks): What is the complexity of the given code as a function of the problem size n? Show the (complete) details of your analysis. Note: a [ i] s an array with n elements. for (int i- 0; i < n; i++) if (Math.random) > 0.5) if (i%2-0) Insertionsort (a[i]); else Quicksort (a[i]) else for (int j = 0; j < i; j++) for (int k 0: k 〈 i; k++) o (logn) Question One (2 marks):...

  • In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as...

    In python, PART A: I am trying to get a dictionary with size(4, 5, 6) as keys and an array for key containing a list of values (words from file of respective size) associated with those keys I am reading from a file of strings, where I am only interested with words of length 4, 5, and 6 to compute my program reading the text file line by line: At first, I have an empty dictionary then to that I...

  • Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int...

    Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int j) that exchanges the values stored at indices i and j in the array a. You do not need to worry about cases where either i or j is an invalid index. Give the best estimate you can for its time complexity (b) In an ordered array of n items, how can we determine whether or not an item belongs to the list using...

  • I have the below code that I need help with fixing this Ruby code. I am...

    I have the below code that I need help with fixing this Ruby code. I am trying to iterate through these three arrays and print the array out with a label. The animals array should have the Animals header, the people should have the People header and the friends array should have the Friends header. I would also like to adjust the printout size based on the size of the elements in the array. I would like for it to...

  • The task was to find the recurrence relation for this function and then find the complexity...

    The task was to find the recurrence relation for this function and then find the complexity class for it as well. Provided is my work and the function. My question is, I feel like I'm missing some step in the recurrence relation and complexity class. Is this correct? The following code is in JavaScript. function divideAndConquerSum(x){ if(x.length<1){ return 0; } if(x.length == 1){ return x[0]; } var third = Math.floor((x.length-1)/3); var next = (third *2)+1; var y = x.slice(0, third+1);...

  • In class we wrote a method closestPairFast that on an input array of numbers, finds the...

    In class we wrote a method closestPairFast that on an input array of numbers, finds the distance of the closest pair by first sorting the input array and then finding the closest adjacent pair. (See the file ClosestPair1D.java in the Code folder on D2L.) In this problem, you are asked to modify the method so that it returns an integer array consisting of the indices of the closest pair in the original array. If there is a tie, just return...

  • I am working on my java but I keep getting this one wrong. Method Name: arrayContains...

    I am working on my java but I keep getting this one wrong. Method Name: arrayContains Access modifier: Private Parameters: 1 integer named number Return type: boolean Purpose: This method returns a true or a false indicating if the number is present in an array or not. In order for this method to work, there must be an array already declared. Therefore, in the CLASS BLOCK, declare an array as follows: static int[] listof Numbers = {1, 2, 3, 4,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT