File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ https:www.codingninjas.com/codestudio/problems/print-all-divisors-of-a-number_1164188?utm_source=youtube&utm_medium=affiliate&utm_campaign=striver_Arrayproblems&leftPanelTab=1
2
+
3
+ Now instead of just going till n^2, we can go till square root of n sqrt(n), why?
4
+ 36:
5
+ 1 36 (i, n/i)
6
+ 2 18 (i, n/i)
7
+ 3 12 (i, n/i)
8
+ 4 9 (i, n/i)
9
+ 6 6 (i, n/i)
10
+ -----------------
11
+ 9 4 (i, n/i)
12
+ 12 3 (i, n/i)
13
+ 18 2 (i, n/i)
14
+ 36 1 (i, n/i)
15
+
16
+ but taking directly sqrt(n) will repeatedly call the function many times, so
17
+ instead we can take i*i
18
+ vector<int> ls;
19
+
20
+ O(sqrt(n))
21
+ for(int i=1; i*i<=n; i++){
22
+ if(n%i==0){
23
+
24
+ ls.push_back(i);
25
+ if((n/i)!=i)
26
+ ls.push_back(n/i);
27
+
28
+
29
+
30
+ }
31
+ }
32
+ m=no of divisors
33
+ O(mlogm)+O(m)
34
+
35
+ sort(ls.begin(),ls.end());
36
+ for(auto it:ls) cout<<it<<" ";
37
+
38
+ Total Time Complexity: O(sqrt(n))+O(mlogm)+O(m) = O(sqrt(n))+O(mlogm)
39
+ Space Complexity: O(m)
40
+
41
+
42
+
43
+
You can’t perform that action at this time.
0 commit comments