📌  相关文章
📜  计算乘积等于给定数的三元组数

📅  最后修改于: 2021-10-27 07:45:59             🧑  作者: Mango

给定一组不同的整数(仅考虑正数)和一个数字“m”,找出乘积等于“m”的三元组的数量。
例子:

Input : arr[] = { 1, 4, 6, 2, 3, 8}  
            m = 24
Output : 3
{1, 4, 6} {1, 3, 8} {4, 2, 3}

Input : arr[] = { 0, 4, 6, 2, 3, 8}  
            m = 18
Output : 0

提问:微软

一种朴素的方法是一个一个地考虑每个三元组,并计算它们的乘积是否等于 m。

C++
// C++ program to count triplets with given
// product m
#include 
using namespace std;
 
// Function to count such triplets
int countTriplets(int arr[], int n, int m)
{
    int count = 0;
 
    // Consider all triplets and count if
    // their product is equal to m
    for (int i = 0; i < n - 2; i++)
        for (int j = i + 1; j < n - 1; j++)
            for (int k = j + 1; k < n; k++)
                if (arr[i] * arr[j] * arr[k] == m)
                    count++;
 
    return count;
}
 
// Drivers code
int main()
{
    int arr[] = { 1, 4, 6, 2, 3, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 24;
 
    cout << countTriplets(arr, n, m);
 
    return 0;
}


Java
// Java program to count triplets with given
// product m
 
class GFG {
    // Method to count such triplets
    static int countTriplets(int arr[], int n, int m)
    {
        int count = 0;
 
        // Consider all triplets and count if
        // their product is equal to m
        for (int i = 0; i < n - 2; i++)
            for (int j = i + 1; j < n - 1; j++)
                for (int k = j + 1; k < n; k++)
                    if (arr[i] * arr[j] * arr[k] == m)
                        count++;
 
        return count;
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int arr[] = { 1, 4, 6, 2, 3, 8 };
        int m = 24;
 
        System.out.println(countTriplets(arr, arr.length, m));
    }
}


Python3
# Python3 program to count
# triplets with given product m
 
# Method to count such triplets
def countTriplets(arr, n, m):
     
    count = 0
 
    # Consider all triplets and count if
    # their product is equal to m
    for i in range (n - 2):
        for j in range (i + 1, n - 1):
            for k in range (j + 1, n):
                if (arr[i] * arr[j] * arr[k] == m):
                    count += 1
    return count
 
# Driver code
if __name__ == "__main__":
   
    arr = [1, 4, 6, 2, 3, 8]
    m = 24
    print(countTriplets(arr,
                        len(arr), m))
 
# This code is contributed by Chitranayal


C#
// C# program to count triplets
// with given product m
using System;
 
public class GFG {
     
    // Method to count such triplets
    static int countTriplets(int[] arr, int n, int m)
    {
        int count = 0;
 
        // Consider all triplets and count if
        // their product is equal to m
        for (int i = 0; i < n - 2; i++)
            for (int j = i + 1; j < n - 1; j++)
                for (int k = j + 1; k < n; k++)
                    if (arr[i] * arr[j] * arr[k] == m)
                        count++;
 
        return count;
    }
 
    // Driver method
    public static void Main()
    {
        int[] arr = { 1, 4, 6, 2, 3, 8 };
        int m = 24;
 
        Console.WriteLine(countTriplets(arr, arr.Length, m));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


C++
// C++ program to count triplets with given
// product m
#include 
using namespace std;
 
// Function to count such triplets
int countTriplets(int arr[], int n, int m)
{
    // Store all the elements in a set
    unordered_map occ;
    for (int i = 0; i < n; i++)
        occ[arr[i]] = i;
 
    int count = 0;
 
    // Consider all pairs and check for a
    // third number so their product is equal to m
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            // Check if current pair divides m or not
            // If yes, then search for (m / arr[i]*arr[j])
            if ((arr[i] * arr[j] <= m) && (arr[i] * arr[j] != 0) && (m % (arr[i] * arr[j]) == 0)) {
                int check = m / (arr[i] * arr[j]);
                auto it = occ.find(check);
 
                // Check if the third number is present
                // in the map and it is not equal to any
                // other two elements and also check if
                // this triplet is not counted already
                // using their indexes
                if (check != arr[i] && check != arr[j]
                    && it != occ.end() && it->second > i
                    && it->second > j)
                    count++;
            }
        }
    }
 
    // Return number of triplets
    return count;
}
 
// Drivers code
int main()
{
    int arr[] = { 1, 4, 6, 2, 3, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 24;
 
    cout << countTriplets(arr, n, m);
 
    return 0;
}


Java
// Java program to count triplets with given
// product m
 
import java.util.HashMap;
 
class GFG {
    // Method to count such triplets
    static int countTriplets(int arr[], int n, int m)
    {
        // Store all the elements in a set
        HashMap occ = new HashMap(n);
        for (int i = 0; i < n; i++)
            occ.put(arr[i], i);
 
        int count = 0;
 
        // Consider all pairs and check for a
        // third number so their product is equal to m
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                // Check if current pair divides m or not
                // If yes, then search for (m / arr[i]*arr[j])
                if ((arr[i] * arr[j] <= m) && (arr[i] * arr[j] != 0) && (m % (arr[i] * arr[j]) == 0)) {
                    int check = m / (arr[i] * arr[j]);
 
                    occ.containsKey(check);
 
                    // Check if the third number is present
                    // in the map and it is not equal to any
                    // other two elements and also check if
                    // this triplet is not counted already
                    // using their indexes
                    if (check != arr[i] && check != arr[j]
                        && occ.containsKey(check) && occ.get(check) > i
                        && occ.get(check) > j)
                        count++;
                }
            }
        }
 
        // Return number of triplets
        return count;
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int arr[] = { 1, 4, 6, 2, 3, 8 };
        int m = 24;
 
        System.out.println(countTriplets(arr, arr.length, m));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find the triplet
def countTriplets(li,product):
    flag = 0
    count = 0
     
    # Consider all pairs and check
    # for a third number so their
    # product is equal to product
    for i in range(len(li)):
         
        # Check if current pair
        # divides product or not
        # If yes, then search for
        # (product / li[i]*li[j])
        if li[i]!= 0 and product % li[i] == 0:
             
            for j in range(i+1, len(li)):
                
                # Check if the third number is present
                # in the map and it is not equal to any
                # other two elements and also check if
                # this triplet is not counted already
                # using their indexes
                if li[j]!= 0 and product % (li[j]*li[i]) == 0:
                    if product // (li[j]*li[i]) in li:
                     
                        n = li.index(product//(li[j]*li[i]))
                     
                        if n > i and n > j:
                            flag = 1
                            count+=1
    print(count)
    
# Driver code
li = [  1, 4, 6, 2, 3, 8 ]
product = 24
 
# Function call
countTriplets(li,product)


C#
// C# implementation of the above
// approach
using System;
using System.Collections.Generic;
class GFG{
     
// Method to count such triplets
static int countTriplets(int[] arr,
                         int n, int m)
{
  // Store all the elements
  // in a set
  Dictionary occ = new Dictionary(n); 
 
  for (int i = 0; i < n; i++)
    occ.Add(arr[i], i);
 
  int count = 0;
 
  // Consider all pairs and
  // check for a third number
  // so their product is equal to m
  for (int i = 0; i < n - 1; i++)
  {
    for (int j = i + 1; j < n; j++)
    {
      // Check if current pair divides
      // m or not If yes, then search
      // for (m / arr[i]*arr[j])
      if ((arr[i] * arr[j] <= m) &&
          (arr[i] * arr[j] != 0) &&
          (m % (arr[i] * arr[j]) == 0))
      {
        int check = m / (arr[i] * arr[j]);
 
        //occ.containsKey(check);
        // Check if the third number
        // is present in the map and
        // it is not equal to any
        // other two elements and also
        // check if this triplet is not
        // counted already using their indexes
        if (check != arr[i] &&
            check != arr[j] &&
            occ.ContainsKey(check) &&
            occ[check] > i &&
            occ[check] > j)
          count++;
      }
    }
  }
 
  // Return number of triplets
  return count;
}
 
// Driver code
static void Main()
{
  int[] arr = {1, 4, 6,
               2, 3, 8};
  int m = 24;
  Console.WriteLine(countTriplets(arr,
                                  arr.Length, m));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
 

3

时间复杂度:O(n 3 )
一种有效的方法是使用散列。

  1. 将所有元素及其索引存储在 hash_map 中。
  2. 考虑所有对 (i, j) 并检查以下内容:
    • 如果 (arr[i]*arr[j] !=0 && (m % arr[i]*arr[j]) == 0), 如果是,则搜索 ( m / (arr[i]*arr[ j])在地图中。
    • 还要检查 m / (arr[i]*arr[j]) 不等于 arr[i] 和 arr[j]。
    • 此外,通过使用存储在映射中的索引,检查当前三元组是否之前没有被计算在内。
    • 如果满足上述所有条件,则增加计数。
  3. 返回计数。

C++

// C++ program to count triplets with given
// product m
#include 
using namespace std;
 
// Function to count such triplets
int countTriplets(int arr[], int n, int m)
{
    // Store all the elements in a set
    unordered_map occ;
    for (int i = 0; i < n; i++)
        occ[arr[i]] = i;
 
    int count = 0;
 
    // Consider all pairs and check for a
    // third number so their product is equal to m
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            // Check if current pair divides m or not
            // If yes, then search for (m / arr[i]*arr[j])
            if ((arr[i] * arr[j] <= m) && (arr[i] * arr[j] != 0) && (m % (arr[i] * arr[j]) == 0)) {
                int check = m / (arr[i] * arr[j]);
                auto it = occ.find(check);
 
                // Check if the third number is present
                // in the map and it is not equal to any
                // other two elements and also check if
                // this triplet is not counted already
                // using their indexes
                if (check != arr[i] && check != arr[j]
                    && it != occ.end() && it->second > i
                    && it->second > j)
                    count++;
            }
        }
    }
 
    // Return number of triplets
    return count;
}
 
// Drivers code
int main()
{
    int arr[] = { 1, 4, 6, 2, 3, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 24;
 
    cout << countTriplets(arr, n, m);
 
    return 0;
}

Java

// Java program to count triplets with given
// product m
 
import java.util.HashMap;
 
class GFG {
    // Method to count such triplets
    static int countTriplets(int arr[], int n, int m)
    {
        // Store all the elements in a set
        HashMap occ = new HashMap(n);
        for (int i = 0; i < n; i++)
            occ.put(arr[i], i);
 
        int count = 0;
 
        // Consider all pairs and check for a
        // third number so their product is equal to m
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                // Check if current pair divides m or not
                // If yes, then search for (m / arr[i]*arr[j])
                if ((arr[i] * arr[j] <= m) && (arr[i] * arr[j] != 0) && (m % (arr[i] * arr[j]) == 0)) {
                    int check = m / (arr[i] * arr[j]);
 
                    occ.containsKey(check);
 
                    // Check if the third number is present
                    // in the map and it is not equal to any
                    // other two elements and also check if
                    // this triplet is not counted already
                    // using their indexes
                    if (check != arr[i] && check != arr[j]
                        && occ.containsKey(check) && occ.get(check) > i
                        && occ.get(check) > j)
                        count++;
                }
            }
        }
 
        // Return number of triplets
        return count;
    }
 
    // Driver method
    public static void main(String[] args)
    {
        int arr[] = { 1, 4, 6, 2, 3, 8 };
        int m = 24;
 
        System.out.println(countTriplets(arr, arr.length, m));
    }
}

蟒蛇3

# Python3 program for the above approach
 
# Function to find the triplet
def countTriplets(li,product):
    flag = 0
    count = 0
     
    # Consider all pairs and check
    # for a third number so their
    # product is equal to product
    for i in range(len(li)):
         
        # Check if current pair
        # divides product or not
        # If yes, then search for
        # (product / li[i]*li[j])
        if li[i]!= 0 and product % li[i] == 0:
             
            for j in range(i+1, len(li)):
                
                # Check if the third number is present
                # in the map and it is not equal to any
                # other two elements and also check if
                # this triplet is not counted already
                # using their indexes
                if li[j]!= 0 and product % (li[j]*li[i]) == 0:
                    if product // (li[j]*li[i]) in li:
                     
                        n = li.index(product//(li[j]*li[i]))
                     
                        if n > i and n > j:
                            flag = 1
                            count+=1
    print(count)
    
# Driver code
li = [  1, 4, 6, 2, 3, 8 ]
product = 24
 
# Function call
countTriplets(li,product)

C#

// C# implementation of the above
// approach
using System;
using System.Collections.Generic;
class GFG{
     
// Method to count such triplets
static int countTriplets(int[] arr,
                         int n, int m)
{
  // Store all the elements
  // in a set
  Dictionary occ = new Dictionary(n); 
 
  for (int i = 0; i < n; i++)
    occ.Add(arr[i], i);
 
  int count = 0;
 
  // Consider all pairs and
  // check for a third number
  // so their product is equal to m
  for (int i = 0; i < n - 1; i++)
  {
    for (int j = i + 1; j < n; j++)
    {
      // Check if current pair divides
      // m or not If yes, then search
      // for (m / arr[i]*arr[j])
      if ((arr[i] * arr[j] <= m) &&
          (arr[i] * arr[j] != 0) &&
          (m % (arr[i] * arr[j]) == 0))
      {
        int check = m / (arr[i] * arr[j]);
 
        //occ.containsKey(check);
        // Check if the third number
        // is present in the map and
        // it is not equal to any
        // other two elements and also
        // check if this triplet is not
        // counted already using their indexes
        if (check != arr[i] &&
            check != arr[j] &&
            occ.ContainsKey(check) &&
            occ[check] > i &&
            occ[check] > j)
          count++;
      }
    }
  }
 
  // Return number of triplets
  return count;
}
 
// Driver code
static void Main()
{
  int[] arr = {1, 4, 6,
               2, 3, 8};
  int m = 24;
  Console.WriteLine(countTriplets(arr,
                                  arr.Length, m));
}
}
 
// This code is contributed by divyeshrabadiya07

Javascript


输出:

3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程