Count number of triplets with product not exceeding a given number
Last Updated :
20 Jul, 2022
Given a positive integer N, the task is to find the number of triplets of positive integers (X, Y, Z), whose product is at most N.
Examples:
Input: N = 2
Output: 4
Explanation: Below are the triplets whose product is at most N(= 2):
- (1, 1, 1): Product is 1*1*1 = 1.
- (1, 1, 2): Product is 1*1*2 = 2.
- (1, 2, 1): Product is 1*2*1 = 2.
- (2, 1, 1): Product is 2*1*1 = 2.
Therefore, the total count is 4.
Input: 6
Output: 25
Naive Approach: The simplest approach to solve the given problem is to generate all possible triplets whose values lie over the range [0, N] and count those triplets whose product of values is at most N. After checking for all the triplets, print the total count obtained.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by generating all possible pairs (i, j) over the range [1, N] and increment the count of all possible pairs by N / (i * j). Follow the steps below to solve the problem:
- Initialize a variable, say ans, that stores the count of all possible triplets.
- Generate all possible pairs (i, j) over the range [1, N] and if the product of the pairs is greater than N, then check for the next pairs. Otherwise, increment the count of all possible pairs by N/(i*j).
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to count the number of
// triplets whose product is at most N
int countTriplets(int N)
{
// Stores the count of triplets
int ans = 0;
// Iterate over the range [0, N]
for (int i = 1; i <= N; i++) {
// Iterate over the range [0, N]
for (int j = 1; j <= N; j++) {
// If the product of
// pairs exceeds N
if (i * j > N)
break;
// Increment the count of
// possible triplets
ans += N / (i * j);
}
}
// Return the total count
return ans;
}
// Driver Code
int main()
{
int N = 10;
cout << countTriplets(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count the number of
// triplets whose product is at most N
static int countTriplets(int N)
{
// Stores the count of triplets
int ans = 0;
// Iterate over the range [0, N]
for(int i = 1; i <= N; i++)
{
// Iterate over the range [0, N]
for(int j = 1; j <= N; j++)
{
// If the product of
// pairs exceeds N
if (i * j > N)
break;
// Increment the count of
// possible triplets
ans += N / (i * j);
}
}
// Return the total count
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
System.out.print(countTriplets(N));
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to count the number of
# triplets whose product is at most N
def countTriplets(N):
# Stores the count of triplets
ans = 0
# Iterate over the range [0, N]
for i in range(1, N + 1):
# Iterate over the range [0, N]
for j in range(1, N + 1):
# If the product of
# pairs exceeds N
if (i * j > N):
break
# Increment the count of
# possible triplets
ans += N // (i * j)
# Return the total count
return ans
# Driver Code
if __name__ == "__main__":
N = 10
print(countTriplets(N))
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
class GFG{
// Function to count the number of
// triplets whose product is at most N
static int countTriplets(int N)
{
// Stores the count of triplets
int ans = 0;
// Iterate over the range [0, N]
for(int i = 1; i <= N; i++)
{
// Iterate over the range [0, N]
for(int j = 1; j <= N; j++)
{
// If the product of
// pairs exceeds N
if (i * j > N)
break;
// Increment the count of
// possible triplets
ans += N / (i * j);
}
}
// Return the total count
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int N = 10;
Console.Write(countTriplets(N));
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// JavaScript program for the above approach
// Function to count the number of
// triplets whose product is at most N
function countTriplets( N){
// Stores the count of triplets
let ans = 0;
// Iterate over the range [0, N]
for (let i = 1; i <= N; i++) {
// Iterate over the range [0, N]
for (let j = 1; j <= N; j++) {
// If the product of
// pairs exceeds N
if (i * j > N)
break;
// Increment the count of
// possible triplets
ans += Math.floor(N / (i * j));
}
}
// Return the total count
return ans;
}
// Driver Code
let N = 10;
document.write( countTriplets(N));
// This code is contributed by rohitsingh07052.
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1), since no extra space has been taken.
Similar Reads
Count number of triplets with product equal to given number Given an array of distinct integers(considering only positive numbers) and a number âmâ, find the number of triplets with product equal to âmâ. Examples: Input : arr[] = { 1, 4, 6, 2, 3, 8} m = 24Output : 3{1, 4, 6} {1, 3, 8} {4, 2, 3} Input : arr[] = { 0, 4, 6, 2, 3, 8} m = 18Output : 0 Asked in :
15+ min read
Count number of triplets with product equal to given number | Set 2 Given an array of distinct integers(considering only positive numbers) and a number âmâ, find the number of triplets with the product equal to âmâ. Examples: Input: arr[] = { 1, 4, 6, 2, 3, 8} m = 24 Output: 3 Input: arr[] = { 0, 4, 6, 2, 3, 8} m = 18 Output: 0 An approach with O(n) extra space has
7 min read
Count number of triplets with product equal to given number with duplicates allowed Given an array of positive integers (may contain duplicates), the task is to find the number of triplets whose product is equal to a given number t. Examples: Input: arr = [1, 31, 3, 1, 93, 3, 31, 1, 93] t = 93 Output: 18 Input: arr = [4, 2, 4, 2, 3, 1] t = 8 Output: 4 [(4, 2, 1), (4, 2, 1), (2, 4,
15+ min read
Count number of triplets with product equal to given number with duplicates allowed | Set-2 Given an array of positive integers(may contain duplicates) and a number âmâ, find the number of unordered triplets ((Ai, Aj, Ak) and (Aj, Ai, Ak) and other permutations are counted as one only) with product equal to âmâ. Examples: Input: arr[] = { 1, 4, 6, 2, 3, 8}, M = 24 Output: 3 The triplets ar
14 min read
Count triplets such that product of two numbers added with third number is N Given a positive integer N, the task is to find the number of triplets (A, B, C) where A, B, C are positive integers such that the product of two numbers added with the third number is N i.e., A * B + C = N. Examples: Input: N = 3Output: 3Explanation:Following are the possible triplets satisfying th
7 min read