Given three integers A, B and C. The task is to count the number of triples (a, b, c) such that a * c > b2, where 0 < a <= A, 0 < b <= B and 0 < c <= C.
Input: A = 3, B = 2, C = 2
Following triples are counted :
(1, 1, 2), (2, 1, 1), (2, 1, 2), (3, 1, 1), (3, 1, 2) and (3, 2, 2).
Input: A = 3, B = 3, C = 3
The brute force approach is to consider all possible triples (a, b, c) and count those triples that satisfy the constraint a*c > b2.
Below is the implementation of the given approach.
Time Complexity: .
Let us count all triplets for a given value of b = k for all k from 1 to B.
- For a given b = k we need to find all a = i and c = j that satisfy i * j > k2
- For a = i, find smallest c = j that satisfies the condition.
Since c = j satisfies this condition therefore c = j + 1, c = j + 2, … and so on, will also satisfy the condition.
So we can easily count all triples in which a = i and b = k.
- Also if for some a = i, c = j is the smallest value such that the given condition is satisfied so it can be observed that a = j and all c >= i also satisfy the condition.
The condition is also satisfied by a = j + 1 and c >= i that is all values a >= j and c >= i also satisfy the condition.
- The above observation helps us to count all triples in which b = k and a >= j easily.
- Now we need to count all triples in which b = k and i < a < j.
- Thus for a given value of b = k we only need to go upto a = square root of k.
Below is the implementation of the above approach:
- Count subarrays with all elements greater than K
- Count the values greater than X in the modified array
- Count number of substrings with numeric value greater than X
- Count the number of pairs that have column sum greater than row sum
- Count the subarray with sum strictly greater than the sum of remaining elements
- Count the number of elements which are greater than any of element on right side of an array
- Count numbers with difference between number and its digit sum greater than specific value
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Kth prime number greater than N
- Least Greater number with same digit sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.