Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Triple sum problem solution

YASH PAL, 31 July 202410 September 2024

In this HackerRank Triple sum interview preparation kit problem You are 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 p E a, q E b, and r E c, satisfying the criteria: p <= q and q => r.

HackerRank Triple sum interview preparation kit solution

Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the triplets function below.
def triplets(a, b, c):
    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

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    lenaLenbLenc = input().split()

    lena = int(lenaLenbLenc[0])

    lenb = int(lenaLenbLenc[1])

    lenc = int(lenaLenbLenc[2])

    arra = list(map(int, input().rstrip().split()))

    arrb = list(map(int, input().rstrip().split()))

    arrc = list(map(int, input().rstrip().split()))

    ans = triplets(arra, arrb, arrc)

    fptr.write(str(ans) + 'n')

    fptr.close()

Problem solution in Java Programming.

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the triplets function below.
   static long triplets(int[] a, int[] b, int[] c) {
      int[] ar = Arrays.stream(a).sorted().distinct().toArray();
      int[] br = Arrays.stream(b).sorted().distinct().toArray();
      int[] cr = Arrays.stream(c).sorted().distinct().toArray();

      long left = 0; 
      long right = 0; 
      int l = 0; 
      int r = 0; 
      long sum = 0; 

      for(int i=0; i<br.length; i++) {
        while(l<ar.length&&ar[l]<=br[i]) {
          left++;
          l++; 
        }
        while(r<cr.length&&cr[r]<=br[i]) {
          right++;
          r++; 
        }
        sum += left*right;
      }
        return sum;    
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] lenaLenbLenc = scanner.nextLine().split(" ");

        int lena = Integer.parseInt(lenaLenbLenc[0]);

        int lenb = Integer.parseInt(lenaLenbLenc[1]);

        int lenc = Integer.parseInt(lenaLenbLenc[2]);

        int[] arra = new int[lena];

        String[] arraItems = scanner.nextLine().split(" ");
        scanner.skip("(rn|[nru2028u2029u0085])?");

        for (int i = 0; i < lena; i++) {
            int arraItem = Integer.parseInt(arraItems[i]);
            arra[i] = arraItem;
        }

        int[] arrb = new int[lenb];

        String[] arrbItems = scanner.nextLine().split(" ");
        scanner.skip("(rn|[nru2028u2029u0085])?");

        for (int i = 0; i < lenb; i++) {
            int arrbItem = Integer.parseInt(arrbItems[i]);
            arrb[i] = arrbItem;
        }

        int[] arrc = new int[lenc];

        String[] arrcItems = scanner.nextLine().split(" ");
        scanner.skip("(rn|[nru2028u2029u0085])?");

        for (int i = 0; i < lenc; i++) {
            int arrcItem = Integer.parseInt(arrcItems[i]);
            arrc[i] = arrcItem;
        }

        long ans = triplets(arra, arrb, arrc);

        bufferedWriter.write(String.valueOf(ans));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

Problem solution in C++ programming.

#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;
}
                 

coding problems solutions interview prepration kit

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes