Computer Science, asked by anoljoseph566367, 7 months ago

python program to find the triplets from a given list and print it in ascending order​

Answers

Answered by gaurikumari06209
1

Method 1: This is the naive approach towards solving the above problem.

Approach: A simple method is to generate all possible triplets and compare the sum of every triplet with the given value. The following code implements this simple method using three nested loops.

Algorithm:

Given an array of length n and a sum s

Create three nested loop first loop runs from start to end (loop counter i), second loop runs from i+1 to end (loop counter j) and third loop runs from j+1 to end (loop counter k)

The counter of these loops represent the index of 3 elements of the triplets.

Find the sum of ith, jth and kth element. If the sum is equal to given sum. Print the triplet and break.

If there is no triplet, then print that no triplet exist. please mark as brainliest

Attachments:
Similar questions