java - How do I improve the runtime complexity of this algorithm? -


public static boolean pp(int[] a){      int n = a.length;      for(int = 0; < (n-1); i++){             //finds 1 value in array         for(int j = (i+1); j < n; j++){         //compares other values 1 value             if (a[i] * a[j] == 225){                 return true;             }         }     }     return false;               //returns false if goes through entire loop without returning true } 

this code takes array , tries find 2 numbers multiply 225. if finds 2 numbers returns true. running time o(n^2) want faster running time o(nlogn) or o(n). how decrease running time of this?

here's o(n) solution. can use hashmap<integer, integer>. insert (accumulatively) elements a count in hashmap<integer, integer> c and:

for (int e : a) {      if (225 % e == 0) {          int t = 225/e;          if (c.containskey(t)) {              if (t == e) {                  if c.get(t) >= 2)                      return true;              }              else                  return true;          }      } }   return false; 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -