Triple sum


Problem Statement :


Given 3 arrays a, b , c of different sizes, find the number of distinct triplets ( p , q , r )  where p is an element of a, written as , , and , satisfying the criteria: p <= q and q > = r .


Function Description

Complete the triplets function in the editor below. It must return the number of distinct triplets that can be formed from the given arrays.

triplets has the following parameter(s):

a, b, c: three arrays of integers .
Input Format

The first line contains 3 integers lena, lenb, lenc , the sizes of the three arrays.
The next 3 lines contain space-separated integers numbering lena, lenb, lenc  respectively.


constraints

1  <=   lena, lenb , lenc <= 10^5
1  <=  all elements in a, b , c  <= 10^8



Output Format

Print an integer representing the number of distinct triplets.


Sample Input 0

3 2 3
1 3 5
2 3
1 2 3
Sample Output 0

8



Solution :



title-img




                        Solution in C++ :

In  C ++ :







#include <bits/stdc++.h>
using namespace std;

int main()
{
    int p,q,r,i,x,y;
    cin>>p>>q>>r;

    int a[p],b[q],c[r];
    for(i=0;i<p;++i) {
        cin>>a[i];
    }
    for(i=0;i<q;++i) {
        cin>>b[i];
    }
    for(i=0;i<r;++i) {
        cin>>c[i];
    }
    sort(a,a+p);
    sort(b,b+q);
    sort(c,c+r);

    long distinct_a[p],distinct_b[q],distinct_c[r];
    set<int> s;
    for(i=0;i<p;++i) {
        s.insert(a[i]);
        distinct_a[i]=s.size();
    }
    s.clear();
    for(i=0;i<q;++i) {
        s.insert(b[i]);
        distinct_b[i]=s.size();
    }
    s.clear();
    for(i=0;i<r;++i) {
        s.insert(c[i]);
        distinct_c[i]=s.size();
    }

    long ans=0;

    x = upper_bound(a,a+p,b[0])-a;
    y = upper_bound(c,c+r,b[0])-c;

    x-=1,y-=1;
    if(x>=0 && y>=0) {
        ans += distinct_a[x]*distinct_c[y];
    }

    for(i=1;i<q;++i) {
        if(b[i]!=b[i-1]) {
            x = upper_bound(a,a+p,b[i])-a;
            y = upper_bound(c,c+r,b[i])-c;

            x-=1,y-=1;
            if(x>=0 && y>=0) {
                ans += distinct_a[x]*distinct_c[y];
            }
        }
    }
    cout<<ans<<endl;

    return 0;
}
                    


                        Solution in Java :

In    Java :






static long triplets(int[] a, int[] b, int[] c) {

        Arrays.sort(a);
        a=Arrays.stream(a).distinct().toArray();
        Arrays.sort(b);
        b=Arrays.stream(b).distinct().toArray();
        Arrays.sort(c);
        c=Arrays.stream(c).distinct().toArray();
        int aIndex = 0;
        int bIndex = 0;
        int cIndex = 0;
        long result = 0;

        while(bIndex<b.length){
            while(aIndex<a.length && a[aIndex]<=b[bIndex])
                aIndex++;

            while(cIndex<c.length && c[cIndex]<=b[bIndex])
                cIndex++;
						
            // you should convert int to long first, 
            // avoid int overflow and lead to fail the test case 4
            result += ((long)aIndex)*((long)cIndex);
            bIndex++;
        }
        return result;
    }
                    


                        Solution in Python : 
                            
In    Python3 :




    a = list(sorted(set(a)))
    b = list(sorted(set(b)))
    c = list(sorted(set(c)))
    
    ai = 0
    bi = 0
    ci = 0
    
    ans = 0
    
    while bi < len(b):
        while ai < len(a) and a[ai] <= b[bi]:
            ai += 1
        
        while ci < len(c) and c[ci] <= b[bi]:
            ci += 1
        
        ans += ai * ci
        bi += 1
    
    return ans
                    


View More Similar Problems

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →