How To Print Ordered Triplets Of Natural Numbers Java
- Trouble Statement
- Basic Approach
- C++ Implementation
- Java Implementation
- Python Implementation
- Efficient Approach (Using Hashing)
- C++ Code For Hashing Method
- Java Code For Hashing Method
- Python Lawmaking For Hashing Method
- Practice Questions
- Frequently Asked Questions
Problem Statement
Given an array A[] consisting of Due north integers. The task is to count the number of triples (A[i], A[j], A[thou]), where i, j, and thousand announce the respective indices, such that ane of the integers can exist written as the summation of the other two integers.
Examples:
Input: A[] = {1, 2, three, iv, v}
Output: 4
Caption:
The valid triplets are –
Confused about your next task?
In iii elementary steps yous can find your personalised career roadmap in Software development for Gratuitous
Expand in New Tab
- (one, 2, 3) : 1 + 2 = iii
- (2, 3, 5) : two + iii = five
- (1, three, four) : 1 + 3 = four
- (1, 4, 5) : 1 + 4 = 5
Input: A[] = {2, 3, 6,9}
Output: 1
Basic Approach
The nigh basic arroyo is to run three nested loops from 1 till N and bank check if the summation of whatever ii integers equals the third integer.
C++ Implementation
Java Implementation
Python Implementation
Time Complexity: O(N^3) where N is the number of nodes of the binary tree.Space Complication: O(one)
Efficient Approach (Using Hashing)
The arroyo is to calculate all possible combinations such that the summation of whatsoever 2 integers is equal to the third integers. If we observe carefully, there can just be iv possible cases, which satisfy the in a higher place condition.
- (0, 0, 0) : As 0 + 0 = 0, it is a valid triplet. Therefore, the full possible means = freq[0]C3, equally among all possible frequencies of 0, we just need to consider triplets.
- (0, x, x) : As 0 + x = x, it is a valid triplet. Therefore, the total possible ways = freq[10]C2 * freq[0]C1, equally among all possible frequencies of 0 and x, nosotros but demand to consider the triplet containing one 0 and ii x.
- (ten, 10, 2x) : Every bit x + x = 2x, it is a valid triplet. Therefore, the full possible ways = freq[x]C2 * freq[2x]C1, equally amongst all possible frequencies of x and 2x, nosotros just need to consider the triplet containing one 2x and ane x.
- (ten, y, 10 + y) : The total possible ways = freq[10]C1 * freq[y]C1 * freq[x + y]C1, as among all possible frequencies of 10, y and 10 + y, we just need to consider the triplet containing all x, y and ten + y.
Algorithm
- Compute the value of the maximum element, mx of the array.
- Build a frequency array, freq of size mx + 1 and store the frequency of all the elements of the array A[].
- Initialise a count variable and consider the in a higher place four cases one by one:
- If the triplet is (0, 0, 0), add freq[0]C3 to count.
- If the triplet is (0, x, ten), add freq[0]C1 * freq[x]C2 to count.
- If the triplet is (ten, 10, 2x), add together freq[x]C2 * freq[2x]C1 to count.
- If the triplet is (ten, y, x + y), add freq[x]C1 * freq[y]C1 * freq[x + y]C1 to count.
- Render count.
C++ Lawmaking For Hashing Method
Java Lawmaking For Hashing Method
Python Code For Hashing Method
Fourth dimension Complexity: O(N^two) where N is the number of nodes of the binary tree.
Infinite Complexity: O(Due north), as a map is used.
Exercise Questions
Maximum Sum Triplets
3 Sum Nothing
Oft Asked Questions
How do you count triplets in an array?
The triplets tin can exist counted by running three nested loops over the size of the assortment.
What are triplets in an array?
The triplet of an assortment is a tuple of three elements of different indices, represented by (i, j, k).
Source: https://www.interviewbit.com/blog/count-the-triplets/
0 Response to "How To Print Ordered Triplets Of Natural Numbers Java"
Post a Comment