Norway


#include <iostream>

#include <algorithm>

using namespace std;

 

// Function to all distinct in the with

// less than or to given

int printAllTriplets(int A[], int n, int sum)

{

    // sort the array in ascending order

    sort(A, A + n);

 

    // check if triplet is formed by A[i] and a pair from

    // sub-array A[i+1..n)

    for (int i = 0; i <= n 3; i++)

    {

        // maintain two indexes pointing to end-points of the

        // sub-array A[i+1..n)

        int low = i + 1, high = n 1;

 

        // loop till low is less than high

        while (low < high)

        {

            // decrement high is total is more than the remaining sum

            if (A[i] + A[low] + A[high] > sum)

                highR;;

 

            else

            {

                // if total is less than or equal to the remaining sum,

                // print all triplets

                for (int x = low + 1; x <= high; x++)

                    cout << “(“ << A[i] << “, “ << A[low] << “, “ << A[x] << “)”;

 

                low++;  // increment low

            }

        }

    }

}

 

// main function

int main()

{

    int A[] = { 2, 7, 4, 9, 5, 1, 3 };

    int sum = ;

 

    int n = sizeof(A) / sizeof(A[0]);

 

    printAllTriplets(A, n, sum);

 

    return 0;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here