I'm having difficulty understanding when can we know if the time complexity of an algorithm is n+m or n*m

Is the time complexity of the following algo O(n+m) or O(n*m)

Can you please point me to a resource that can give some sample examples of code patterns for different variations of time complexity?

For example:

for(String str : strs) {
   char[] charArray = str.toCharArray();
   Array.sort(charArray); //ignoring the TC of this one, which would be nLogn in java
   System.out.println("sorted: "+ new String(charArray));

Read The Algorithm Design Manual.

For this specific question, refer Section 2.5.

Basically you need to count the number of atomic operations your algorithm is performing.

  • 1
    $\begingroup$ Please include the gist of your answer in your post. $\endgroup$ – greybeard Aug 16 '20 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.