From d95c37f1ba8b77fbcd1e5bdcd260624d2ddf2cc6 Mon Sep 17 00:00:00 2001
From: Bhairavi Bhuyar <115082146+bhairavibhuyar@users.noreply.github.com>
Date: Mon, 30 Oct 2023 02:14:20 +0530
Subject: [PATCH 1/4] Create README.md
---
Medium/823. Binary Trees With Factors/README.md | 1 +
1 file changed, 1 insertion(+)
create mode 100644 Medium/823. Binary Trees With Factors/README.md
diff --git a/Medium/823. Binary Trees With Factors/README.md b/Medium/823. Binary Trees With Factors/README.md
new file mode 100644
index 00000000..8b137891
--- /dev/null
+++ b/Medium/823. Binary Trees With Factors/README.md
@@ -0,0 +1 @@
+
From 886263d94e9c7e98d5c90ef8b97ce269c28dfea0 Mon Sep 17 00:00:00 2001
From: Bhairavi Bhuyar <115082146+bhairavibhuyar@users.noreply.github.com>
Date: Mon, 30 Oct 2023 02:19:57 +0530
Subject: [PATCH 2/4] README.md
---
.../823. Binary Trees With Factors/README.md | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/Medium/823. Binary Trees With Factors/README.md b/Medium/823. Binary Trees With Factors/README.md
index 8b137891..9d41c494 100644
--- a/Medium/823. Binary Trees With Factors/README.md
+++ b/Medium/823. Binary Trees With Factors/README.md
@@ -1 +1,26 @@
+
823. Binary Trees With Factors
+Medium
+Given an array of unique integers, arr, where each integer arr[i] is strictly greater than 1.
+
+We make a binary tree using these integers, and each number may be used for any number of times. Each non-leaf node's value should be equal to the product of the values of its children.
+
+Return the number of binary trees we can make. The answer may be too large so return the answer modulo 109 + 7.
+
+
+Example 1:
+Input: arr = [2,4]
+Output: 3
+Explanation: We can make these trees: [2], [4], [4, 2, 2]
+
+
+Example 2:
+Input: arr = [2,4,5,10
+Output: 7
+Explanation: We can make these trees: [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, 5, 2].
+
+
+Constraints:
+1. <= arr.length <= 1000
+2. <= arr[i] <= 109
+All the values of arr are unique.
From e2ec942a751ec5a2cf47d49ce8bdf807b4c1d304 Mon Sep 17 00:00:00 2001
From: Bhairavi Bhuyar <115082146+bhairavibhuyar@users.noreply.github.com>
Date: Mon, 30 Oct 2023 02:20:53 +0530
Subject: [PATCH 3/4] Binary Trees With Factors
---
.../Solution.java | 54 +++++++++++++++++++
1 file changed, 54 insertions(+)
create mode 100644 Medium/823. Binary Trees With Factors/Solution.java
diff --git a/Medium/823. Binary Trees With Factors/Solution.java b/Medium/823. Binary Trees With Factors/Solution.java
new file mode 100644
index 00000000..47a091a3
--- /dev/null
+++ b/Medium/823. Binary Trees With Factors/Solution.java
@@ -0,0 +1,54 @@
+package hacktoberfest;
+import java.util.*;
+
+public class Solution {
+
+ // Method to count the number of binary trees that can be formed
+ public static int countBinaryTrees(int[] arr) {
+
+ // Sort the array
+ Arrays.sort(arr);
+
+ // Create a map to store the count of subtrees for each element
+ Map subtreeCount = new HashMap<>();
+
+ for (int root : arr) {
+ subtreeCount.put(root, 1L);
+
+ for (int factor : arr) {
+ if (factor >= root) {
+ break;
+ }
+
+ /* Check if the current factor is a factor of the root
+ * and if the root/factor is present in the map */
+ if (root % factor == 0 && subtreeCount.containsKey(root / factor)) {
+ // Update the subtree count for the current root
+ subtreeCount.put(root,
+ (subtreeCount.get(root) + subtreeCount.get(factor) * subtreeCount.get(root / factor)));
+ }
+ }
+ }
+
+ long totalTrees = 0L;
+ for (int key : subtreeCount.keySet()) {
+ totalTrees = (totalTrees + subtreeCount.get(key)) % 1_000_000_007;
+ }
+
+ return (int) totalTrees;
+ }
+
+ // Driver Code
+ public static void main(String[] args) {
+
+ int[] arr = {2, 4, 5, 10};
+
+ // Calculate the number of binary trees that can be formed
+ int ans = countBinaryTrees(arr);
+
+ // Print the result
+ System.out.println("Number of binary trees we can make = " + ans);
+
+ }
+
+}
From d05f60cf0daabdd67b52cf499eb650564d57989c Mon Sep 17 00:00:00 2001
From: Jarrian Gojar
Date: Mon, 30 Oct 2023 09:04:26 +0800
Subject: [PATCH 4/4] Update Solution.java
---
.../Solution.java | 74 +++++++------------
1 file changed, 26 insertions(+), 48 deletions(-)
diff --git a/Medium/823. Binary Trees With Factors/Solution.java b/Medium/823. Binary Trees With Factors/Solution.java
index 47a091a3..eedf221b 100644
--- a/Medium/823. Binary Trees With Factors/Solution.java
+++ b/Medium/823. Binary Trees With Factors/Solution.java
@@ -1,54 +1,32 @@
-package hacktoberfest;
-import java.util.*;
-
public class Solution {
-
- // Method to count the number of binary trees that can be formed
- public static int countBinaryTrees(int[] arr) {
-
+ // Method to count the number of binary trees that can be formed
+ public int numFactoredBinaryTrees(int[] arr) {
// Sort the array
- Arrays.sort(arr);
-
- // Create a map to store the count of subtrees for each element
- Map subtreeCount = new HashMap<>();
-
- for (int root : arr) {
- subtreeCount.put(root, 1L);
-
- for (int factor : arr) {
- if (factor >= root) {
- break;
- }
-
- /* Check if the current factor is a factor of the root
- * and if the root/factor is present in the map */
- if (root % factor == 0 && subtreeCount.containsKey(root / factor)) {
- // Update the subtree count for the current root
- subtreeCount.put(root,
- (subtreeCount.get(root) + subtreeCount.get(factor) * subtreeCount.get(root / factor)));
- }
- }
- }
-
- long totalTrees = 0L;
- for (int key : subtreeCount.keySet()) {
- totalTrees = (totalTrees + subtreeCount.get(key)) % 1_000_000_007;
- }
-
- return (int) totalTrees;
- }
+ Arrays.sort(arr);
+ // Create a map to store the count of subtrees for each element
+ Map subtreeCount = new HashMap<>();
+ for (int root : arr) {
+ subtreeCount.put(root, 1L);
+ for (int factor : arr) {
+ if (factor >= root) {
+ break;
+ }
- // Driver Code
- public static void main(String[] args) {
+ /* Check if the current factor is a factor of the root
+ * and if the root/factor is present in the map */
+ if (root % factor == 0 && subtreeCount.containsKey(root / factor)) {
+ // Update the subtree count for the current root
+ subtreeCount.put(root,
+ (subtreeCount.get(root) + subtreeCount.get(factor) * subtreeCount.get(root / factor)));
+ }
+ }
+ }
+
+ long totalTrees = 0L;
+ for (int key : subtreeCount.keySet()) {
+ totalTrees = (totalTrees + subtreeCount.get(key)) % 1_000_000_007;
+ }
- int[] arr = {2, 4, 5, 10};
-
- // Calculate the number of binary trees that can be formed
- int ans = countBinaryTrees(arr);
-
- // Print the result
- System.out.println("Number of binary trees we can make = " + ans);
-
+ return (int) totalTrees;
}
-
}