
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print triplets with sum less than or equal to k in C Program
Given array with set of elements and the task is to find out set with exactly three elements having sum less than or equals to k.
Input − arr[]= {1,2,3,8,5,4}
Output − set → {1, 2, 3} {1, 2, 5} {1, 2, 4} {1, 3, 5} {1, 3, 4} {1, 5, 4} {2, 3, 5} {2, 3, 4}
In this, first task is to calculate the size of array depending upon which for loop of i is iterated till size-2 and for loop of j is iterated till size-1 and for loop of k is iterated till size
Algorith
START Step 1 -> declare int variable sum to k (e.g. 10), i, j, k Step 2 -> declare and initialise size with array size using sizeof(arr)/sizeof(arr[0]) Step 3 -> Loop For i to 0 and i<size-2 and i++ Loop For j to i+1 and j<size-1 and j++ Loop For k to j+1 and k<size and k++ IF arr[i]+ arr[j] + arr[k] <= sum Print arr[i] and arr[j] and arr[k] End IF End Loop for End Loop For Step 4 -> End Loop For STOP
Example
#include <stdio.h> int main(int argc, char const *argv[]) { int arr[] = {1, 2, 3, 8, 5, 4}; int sum = 10; int i, j, k; int size = sizeof(arr)/sizeof(arr[0]); for (i = 0; i < size-2; i++) { for (j = i+1; j < size-1; j++) { for (k = j+1; k < size; k++) { if( arr[i]+ arr[j] + arr[k] <= sum ) printf( "{%d, %d, %d}
",arr[i], arr[j], arr[k] ); } } } return 0; }
Output
If we run the above program then it will generate the following output.
{1, 2, 3} {1, 2, 5} {1, 2, 4} {1, 3, 5} {1, 3, 4} {1, 5, 4} {2, 3, 5} {2, 3, 4}
- Related Articles
- Print all prime numbers less than or equal to N in C++
- Print all Semi-Prime Numbers less than or equal to N in C++
- Maximum sum subarray having sum less than or equal to given sums in C++
- Maximum Side Length of a Square with Sum Less than or Equal to Threshold in C++
- Print all triplets with given sum in C++
- Find maximum sum array of length less than or equal to m in C++
- Minimum swaps required to bring all elements less than or equal to k together in C++
- Program to check number of triplets from an array whose sum is less than target or not Python
- Two Sum Less Than K in Python
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit in C++
- Linear magnification produced by a concave mirror may be:(a) less than 1 or equal to 1 (b) more than 1 or equal to 1(c) less than 1, more than 1 or equal to 1 (d) less than 1 or more than 1
- Linear magnification produced by a convex lens can be:(a) less than 1 or more than 1.(b) less than 1 or equal to 1.(c) more than 1 or equal to 1.(d) less than 1, equal to 1 or more than 1.
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++
- JavaScript Program to Count triplets with sum smaller than a given value
- Find all factorial numbers less than or equal to n in C++

Advertisements