-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd34100
commit 02484ce
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class GFG { | ||
public static void main(String[] args) { | ||
Scanner in = new Scanner(System.in); | ||
int t=in.nextInt(); | ||
while(t > 0) | ||
{ | ||
int n = in.nextInt(); | ||
Geeks obj = new Geeks(); | ||
System.out.println(obj.count(n)); | ||
t--; | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
// Complete this function! | ||
|
||
class Geeks { | ||
public long count(int n) | ||
{ | ||
// Add your code here. | ||
int[] ways = new int[n + 1]; | ||
|
||
ways[0] = 1; | ||
|
||
int[] moves = { 3, 5, 10 }; | ||
|
||
for (int i = 0; i < 3; i++) | ||
for (int j = moves[i]; j <= n; j++) | ||
ways[j] += ways[j - moves[i]]; | ||
|
||
return ways[n]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n * logn) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
class UnionFind | ||
{ | ||
public UnionFind(int n) | ||
{ | ||
id = new int[n]; | ||
sz = new int[n]; | ||
|
||
for (int i = 0; i < n; ++i) | ||
id[i] = i; | ||
|
||
for (int i = 0; i < n; ++i) | ||
sz[i] = 1; | ||
} | ||
|
||
public void unionBySize(int u, int v) | ||
{ | ||
int i = find(u); | ||
int j = find(v); | ||
|
||
if (i == j) | ||
return; | ||
|
||
if (sz[i] < sz[j]) | ||
{ | ||
sz[j] += sz[i]; | ||
id[i] = j; | ||
} | ||
else | ||
{ | ||
sz[i] += sz[j]; | ||
id[j] = i; | ||
} | ||
} | ||
|
||
public int getSize(int i) | ||
{ | ||
return sz[i]; | ||
} | ||
|
||
private int[] id; | ||
private int[] sz; | ||
|
||
private int find(int u) | ||
{ | ||
return id[u] == u ? u : (id[u] = find(id[u])); | ||
} | ||
} | ||
|
||
class Solution | ||
{ | ||
public boolean canTraverseAllPairs(int[] nums) | ||
{ | ||
int n = nums.length; | ||
int maxNum = Arrays.stream(nums).max().getAsInt(); | ||
int[] minPrimeFactors = sieveEratosthenes(maxNum + 1); | ||
Map<Integer, Integer> primeToFirstIndex = new HashMap<>(); | ||
UnionFind uf = new UnionFind(n); | ||
|
||
for (int i = 0; i < n; ++i) | ||
for (final int primeFactor : getPrimeFactors(nums[i], minPrimeFactors)) | ||
if (primeToFirstIndex.containsKey(primeFactor)) | ||
uf.unionBySize(primeToFirstIndex.get(primeFactor), i); | ||
else | ||
primeToFirstIndex.put(primeFactor, i); | ||
|
||
for (int i = 0; i < n; ++i) | ||
if (uf.getSize(i) == n) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
private int[] sieveEratosthenes(int n) | ||
{ | ||
int[] minPrimeFactors = new int[n + 1]; | ||
for (int i = 2; i <= n; ++i) | ||
minPrimeFactors[i] = i; | ||
|
||
for (int i = 2; i * i < n; ++i) | ||
if (minPrimeFactors[i] == i) | ||
for (int j = i * i; j < n; j += i) | ||
minPrimeFactors[j] = Math.min(minPrimeFactors[j], i); | ||
|
||
return minPrimeFactors; | ||
} | ||
|
||
private List<Integer> getPrimeFactors(int num, int[] minPrimeFactors) | ||
{ | ||
List<Integer> primeFactors = new ArrayList<>(); | ||
while (num > 1) | ||
{ | ||
int divisor = minPrimeFactors[num]; | ||
primeFactors.add(divisor); | ||
while (num % divisor == 0) | ||
num /= divisor; | ||
} | ||
|
||
return primeFactors; | ||
} | ||
} |