Skip to content

Commit

Permalink
Added codes for 25 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 25, 2024
1 parent dd34100 commit 02484ce
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
39 changes: 39 additions & 0 deletions GeeksForGeeks/February/25-2-24/GFG.java
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];
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/25-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/February/25-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logn)
Space complexity - O(n)
100 changes: 100 additions & 0 deletions LeetCode/February/25-2-24/Solution.java
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;
}
}

0 comments on commit 02484ce

Please sign in to comment.