kdn251 / interviews

Everything you need to know to get the job.
https://www.youtube.com/channel/UCKvwPt6BifPP54yzH99ff1g?view_as=subscriber
MIT License
63.47k stars 12.89k forks source link

Reformat files #195

Open shivam83184 opened 3 years ago

shivam83184 commented 3 years ago

import java.util.*;

class Triplet {

// function to print triplets with 0 sum
static void findTriplets(int arr[], int n)
{
    boolean found = false;

    for (int i = 0; i < n - 1; i++)
    {
        // Find all pairs with sum equals to
        // "-arr[i]"
        HashSet<Integer> s = new HashSet<Integer>();
        for (int j = i + 1; j < n; j++)
        {
            int x = -(arr[i] + arr[j]);
            if (s.contains(x))
            {
                System.out.printf("%d %d %d\n", x, arr[i], arr[j]);
                found = true;
            }
            else
            {
                s.add(arr[j]);
            }
        }
    }

    if (found == false)
    {
        System.out.printf(" No Triplet Found\n");
    }
}

// Driver code
public static void main(String[] args)
{
    int arr[] = {0, -1, 2, -3, 1};
    int n = arr.length;
    findTriplets(arr, n);
}

}