Count of sorted triplets (a, b, c) whose product is at most N
Given an integer N, the task is to find the count of triplets (a, b, c) such that a <= b <= c and a * b * c <= N.
Examples:
Input: N = 5
Output: 6
Explanation: The triplets that follow the required conditions are (1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 1, 5) and (1, 2, 2). Hence the count of value triplets is 6.Input: N = 20
Output: 40
Approach: The given problem can be solved based on the following observations:
- Since a <= b <=c, it can be observed that the value of a must lie in the range [1, N1/3].
- Similarly, for a given a the value of b must lie in the range [a, (N/a)1/2].
- Similarly, when the value of a and b is fixed, the value of c must lie in the range [b, N/(a*b)]. Hence the number of possible values of c is the count of integers in the range [b, N/(a*b)]. Therefore, for each valid a and b, the number of possible values of c are N/(a*b) – b + 1.
Therefore, using the above observations below is the implementation of the above approach:
C++
// C++ implementation of the above approach #include <iostream> using namespace std; // Function to find the count of valid // triplets (a, b, c) such that the value // of a * b * c <= N and a <= b <= c long long validTriplets( int N) { // Stores count of triplets long long ans = 0; // Loop to iterate in the // range [1, N^(1/3)] for ( long long a = 1; a * a * a <= N; a++) { // Loop to iterate in the // range [a, (N/a)^(1/2)] for ( long long b = a; a * b * b <= N; b++) { // Add the count of valid // values of c for a fixed // value of a and b ans += N / a / b - b + 1; } } // Return Answer return ans; } // Driver Code int main() { int N = 5; cout << validTriplets(N); return 0; } |
Java
// Java implementation of the above approach import java.util.*; class GFG{ // Function to find the count of valid // triplets (a, b, c) such that the value // of a * b * c <= N and a <= b <= c static long validTriplets( int N) { // Stores count of triplets long ans = 0 ; // Loop to iterate in the // range [1, N^(1/3)] for ( long a = 1 ; a * a * a <= N; a++) { // Loop to iterate in the // range [a, (N/a)^(1/2)] for ( long b = a; a * b * b <= N; b++) { // Add the count of valid // values of c for a fixed // value of a and b ans += N / a / b - b + 1 ; } } // Return Answer return ans; } // Driver Code public static void main(String[] args) { int N = 5 ; System.out.print(validTriplets(N)); } } // This code is contributed by 29AjayKumar |
Python3
# Python implementation of the above approach # Function to find the count of valid # triplets (a, b, c) such that the value # of a * b * c <= N and a <= b <= c def validTriplets(N): # Stores count of triplets ans = 0 ; # Loop to iterate in the # range [1, N^(1/3)] for a in range ( 1 , int (N * * ( 1 / 3 )) + 1 ): # Loop to iterate in the # range [a, (N/a)^(1/2)] b = a; while (a * b * b < = N): # Add the count of valid # values of c for a fixed # value of a and b ans + = N / a / b - b + 1 ; b + = 1 # Return Answer return int (ans); # Driver Code N = 5 ; print (validTriplets(N)); # This code is contributed by gfgking |
C#
// C# implementation of the above approach using System; class GFG { // Function to find the count of valid // triplets (a, b, c) such that the value // of a * b * c <= N and a <= b <= c static long validTriplets( int N) { // Stores count of triplets long ans = 0; // Loop to iterate in the // range [1, N^(1/3)] for ( long a = 1; a * a * a <= N; a++) { // Loop to iterate in the // range [a, (N/a)^(1/2)] for ( long b = a; a * b * b <= N; b++) { // Add the count of valid // values of c for a fixed // value of a and b ans += N / a / b - b + 1; } } // Return Answer return ans; } // Driver Code public static void Main() { int N = 5; Console.WriteLine(validTriplets(N)); } } // This code is contributed by ukasp. |
Javascript
<script> // JavaScript implementation of the above approach // Function to find the count of valid // triplets (a, b, c) such that the value // of a * b * c <= N and a <= b <= c function validTriplets(N) { // Stores count of triplets let ans = 0; // Loop to iterate in the // range [1, N^(1/3)] for (let a = 1; a * a * a <= N; a++) { // Loop to iterate in the // range [a, (N/a)^(1/2)] for (let b = a; a * b * b <= N; b++) { // Add the count of valid // values of c for a fixed // value of a and b ans += N / a / b - b + 1; } } // Return Answer return Math.floor(ans); } // Driver Code let N = 5; document.write(validTriplets(N)); // This code is contributed by Potta Lokesh </script> |
Output
6
Time Complexity: O(N2/3)
Auxiliary Space: O(1)
Please Login to comment...