I have a question regarding time complexity (big O notation) for Java software. Is there a way to quickly calculate or test it (or any website that could calculate it for me would be welcomed). For example I would like to check it for the following snippet of code and possibly improve as well:

```
int dcount = 24423567;
int a = 0;
if (dcount == 0){
a = 1;
}
String ds = Integer.toString(dcount);
String[] sa = ds.split("(?<=.)");
HashSet hs = new HashSet();
Collections.addAll(hs, sa);
a = hs.size();
if (dcount < 0)
a--;
System.out.println(a);
```

As @emory pointed out, it is provably impossible to determine the big-O time complexity of an arbitrary piece of code automatically (the proof is a reduction from the Halting Problem). However, there are tools that can attempt to measure the complexity of a piece of code empirically by running it on several different inputs. One such tool is described **in this paper**, and works by attempting to do a regression on the program's runtime versus its input size. The tool, called **trend-prof**, is available online.

Hope this helps!

I might be solving someone's homework, but the question was begging for a sane solution...

Counting distinct digits in a number requires no strings, sets or regular expressions, just some simple arithmetics.

The following method runs in O(n) time (n = number of digits in the input) and constant space:

```
int distinctDigits(int num) {
if (num == 0) {
return 1;
}
boolean[] digits = new boolean[10];
while (num > 0) {
digits[num % 10] = true;
num /= 10;
}
int count = 0;
for (boolean digit : digits) {
if (digit) {
count++;
}
}
return count;
}
```

Making this work for negative numbers is left as an exericse to the reader ;)

Similar Questions

I am learning about algo complexity and calculating time complexity. the question is What is the time complexity of the best case to insert a new node into a minimum-level BST with n nodes? Explain. (

I am trying to understand Big-O Time complexity and am unfortunately struggling, I cannot seem to grasp the concept, I know my results are correct for the following two code fragments however how I go

I am trying to check the time complexity of the below simple program. The program replaces spaces in a string with '%20'. The loop to count spaces (O(1) time) foreach (char k in s) { if (k == ' ') {

I know that this is already asked(link) a long back for the IntelliJ previous versions. Also some how I have got information on even this. I have tried with the clover tool of atlassian. Please sugge

Couple very basic time complexity related questions here: What is the time complexity of array initialization in java? e.g., int arr[] = new int[5000]; I am guessing it to be O(n) or does the JVM in

I am taking up algorithm course on coursera,and I am stuck on this particular problem. I am supposed to find the time complexity of this code. int sum = 0 for (int i = 1; i <= N*N; i = i*2) { for (

Does anybody has any reference to the code in PHP that calculating time difference similar to stackoverflow? for example: given a time stamp '2011-08-03 07:15:22', produce less than a minute ago, giv

From an online notes, I read the following java code snippet for reversing a string, which is claimed to have quadratic time complexity. It seems to me that the “for” loop for i just iterates the whol

The time complexity of Fibonacci is O(2^n) What if I want to get time complexity of 3^n? According to my friend the time complexity of fibonacci is O(2^n) due to following step:- return fibonacci(n-1)

Does anyone know the time complexity of the operations of TreeMap like - subMap, headMap. tailMap. The time complexity of operations like get, put is O(logn). But the javadoc doesnt say much about the

I have this method, isPalindrome(), and I am trying to find the time complexity of it, and also rewrite the code more efficiently. boolean isPalindrome(String s) { boolean bP = true; for(int i=0; i<

I am having some trouble determining space and time complexities. For example, if I have a tree that has a branching factor b and will have at most a depth d, how can I calculate the time and space co

I've been doing some questions but answers not provided so I was wondering if my answers are correct a) given that a[i....j] is an integer array with n elements and x is an integer int front, back; wh

I'm trying to wrap my head around the concept of studying an algorithm's time complexity with respect to bit cost (instead of unit cost) and it seems to be impossible to find anything on the subject.

In Java, at some point in my program I have to process gigabytes of int[] arrays in memory. They are sorted and contain only natural (like 1, 2, 3, 4, ..., up to n) numbers which represent file lines.

i have my Java program and i would like to add code that show the run time for calculating, but i have no idea how can i do that?

I know that quicksort has O(n log n) average time complexity. A pseudo-quicksort (which is only a quicksort when you look at it from far enough away, with a suitably high level of abstraction) that is

I am a bit confused about time complexity of Dynamic Arrays. In this article here it states that the time complexity of insertion and deletion of Dynamic Array is O(n). I wanted to know why that is th

I am pretty sure about my answer but today had a discussion with my friend who said I was wrong. I think the complexity of this function is O(n^2) in average and worst case and O(n) in best case. Righ

I want to know the exact time complexity of my algorithm in this method. I think it is nlogn as it uses arrays.sort; public static int largestElement(int[] num) throws NullPointerException // O(1) { i

What is the the time complexity of each of python's set operations in Big O notation? I am using Python's set type for an operation on a large number of items. I want to know how each operation's perf

Is there any search algorithm with time complexity O(1)? Search Algorithm = Finding an element x from n elements.

I am having trouble understanding this time complexity O(sqrt(B)) given that B is an integer. For example if I have a function... int GetResult(int A, int B) { } ...and this function has a time compl

Is there some tool to monitor different metrics for a Java project over a longer period of time, preferrably by the data in CVS to establish trends and weak points? This would be a good starting point

Below code is to find how many times a number is shown in an array. For Example: 1,2,2,2,3,4,5,5,5,5 number 2 = 3 times number 5 = 4 times. What is the time complexity in Java for the below code? What

How do you calculate time complexity in case of recursion algorithms? for eg t(n) = t(3n/2) + 0(1) (Heapsort)

Does anyone know which algorithm MATLAB uses for matrix multiplication and what is its time complexity?

I had an assignment in school last week to implement a function for calculating the n:th number in the fibonacci sequence. A 'sub-assignment' was to implement it using accumulation(Might not be a corr

Say I wanted to calculate a (mod n). What is the time complexity of this? I'm using Matlab, and am not sure how Matlab calculates it. Does it divide a by n, subtract the integer part, and then multipl

Can you recommend free tools for calculating cyclomatic complexity. Looking for all languages. One tool/language per answer please.

This question already has an answer here: Is Big-O of the C++ statement 'delete [] Q;' O(1) or O(n)? 2 answers What is the Time Complexity of the delete[] operator? I mean how is it implemente

First, here is my code for the O(n): import java.util.*; public class BigO{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print(Enter a number: ); String u

I don't mean to be asking for help with something simple, but I can't seem to figure out how to answer this question. Compute the time complexity of the following program fragment: sum = 0; for i=1 to

I would like to know the time complexity of the following code snippet, FileReader fr = new FileReader(myfile.txt); BufferedReader br = new BufferedReader(fr); for (long i = 0; i < n-1; i++ ) { b

sum = 0; for(i=1;i<2*n;i++) for(j=1;j<i*i;j++) for(k=1;k<j;k++) if (j % i == 1) sum++; I need to calculate the time complexity of this code in terms of big O notation. I understand how to an

I'm using this basic and crude code below for calculating prime numbers then exporting them to a text file: import java.util.Scanner; import java.io.*; public class primeGenerator{ public static void

i am starting to learn time complexity , and i looked in the examples for the time complexity for some simple sort.I wanted to know how do we calculate average time complexity for a depth-first search

I have a recursive function as you can see from below. I also have the iterative version of same function. My problem is about recursive function's time complexity. Based on my knowledge it should be

A long string s contains only 0 and 1. This Ruby code counts how many 1 there are: s.gsub(/1/).count What is the time complexity in Big O notation? Is there a tool to do the calculation?

So given the following program: Is the time complexity of this program O(0)? In other words, is 0 O(0)? I thought answering this in a separate question would shed some light on this question. EDIT: Lo

From Wikipedia: The complexity of the algorithm is O(n(logn)(loglogn)) bit operations. How do you arrive at that? That the complexity includes the loglogn term tells me that there is a sqrt(n) some

What is the time complexity of computing betweenness centrality if we are given the shortest path predecessor matrix of a graph? Predecessor matrix cells look like this: if node i and node j are dire

This question already has an answer here: Time Complexity of Ternary Search Algorithm 2 answers Given a sorted array, determine if it contains a given number x: Instead of using binary search i

It is extremely difficult to illustrate the complexity of frameworks (hibernate, spring, apache-commons, ...) The only thing I could think of was to compare the file sizes of the jar libraries or even

Good afternoon, I am wondering what the time complexity of std::multimap::equal_range is? Is it Big-O(n) or BIG-0(log n). I remember reading that the time complexity of std::multimap::erase is logari

Many UML tools claim to do forward / reverse engineering of Java code. However, it turns out from prior experience, that few tools really work in this area. I haven't been doing Java projects for 3 y

I'm looking for an intuitive, real-world example of a problem that takes (worst case) exponential time complexity to solve for a talk I am giving. Here are examples for other time complexities I have

I would like to know whether this works in worst case as Θ(n+m)? n is size of i_StringForSearch, m is size of i_SubStringToFind. 2.Is there any program which is recommended to check time complexity

From Sonar Metrics complexity page the following method has a complexity of 5. public void process(Car myCar){ <- +1 if(myCar.isNotMine()){ <- +1 return; <- +1 } car.paint(red); car.change

Cyclomatic complexity is number of test cases that are necessary to achieve thorough test coverage of a particular module. Consider the following pseudo-code : If (A > B) and (C > D) then A = A