Sort by

recency

|

1075 Discussions

|

  • + 0 comments
    def beautifulTriplets(d, arr):
        # Write your code here
        
        count = 0
        lookup = set(arr)
        
        for i in arr:
            num2 = i + d
            num3 = num2 + d
            
            if num2 in lookup and num3 in lookup:
                count += 1
                
        return count
    
  • + 0 comments

    Very Simple method with Include in Javascript

    function beautifulTriplets(d, arr) { let count =0 for(let i = 0;i

    }

  • + 0 comments
    def beautifulTriplets(d, arr):
        # Write your code here
        n = len(arr)
        c = 0
        lookup = set(arr)
        for i in range(n - 2):
            first = arr[i]
            second = first + d
            third = second + d
            if second in lookup and third in lookup:
                c += 1
        return c
    
  • + 0 comments
    public static int beautifulTriplets(int d, List<Integer> arr) {
        int tripletcount = 0;
        for(int i = 0; i < arr.size(); i++) {
            for(int j=i+1; j< arr.size(); j++) {
                if(arr.get(j) - arr.get(i) == d && findThirdNumber(arr, j, d, arr.get(j))) {
                    tripletcount++;
                    break;
                }
            }
        }
        return tripletcount;
    }
    
    private static boolean findThirdNumber(List<Integer> arr, int j, int d, int jval) {
        for(int k = j+1; k < arr.size(); k++) {
            if(arr.get(k) - jval == d) return true;
        }
        return false;
    }
    
  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-beautiful-triplets-problem-solution.html