Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the time complexity of this code?


What I have tried:

import java.util.*;
public class Main
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
	    for(int i=0; i<n; i++){
	        for(int j=0; j<n; j++){
	    
		System.out.println(i + "->"+ j);
	        }
	    }
	}
}
Posted
Updated 28-Dec-22 3:58am
v2

 
Share this answer
 
I suggest you an experimental approach, try
Java
public static void main(String[] args)  throws InterruptedException {
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      long start = System.currentTimeMillis();
      for(int i=0; i<n; i++){
          for(int j=0; j<n; j++){
              Thread.sleep(1000);
              System.out.println(i + "->"+ j);
          }
      }
      double elapsed_seconds = (System.currentTimeMillis() - start) / 1000;
      System.out.println("seconds " +  elapsed_seconds);
  }
with different input values, in order to find out the expected behaviour.
 
Share this answer
 
Comments
0x01AA 28-Dec-22 11:17am    
My small 5 Sir ;)
CPallini 28-Dec-22 11:23am    
Thank you!
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900