- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Python program to find list of triplets for which i+j+k is not same as n

Suppose we have three numbers i, j and k and another number n. We shall have to find the list of all triplets (i, j, k) for which i+j+k not same as n. We shall have to solve this problem using list comprehension strategy.

So, if the input is like i = 1, j = 1, z = 2 and n = 3, then the output will be [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 2]]

To solve this, we will follow these steps −

arr = an array of triplets [x, y, z] for all x in range from 0 to i, for all y in range from 0 to j and for all z in range from 0 to k, when x+y+z is not same as n

## Example

Let us see the following implementation to get better understanding −

def solve(i, j, k, n): arr = [[x, y, z] for x in range(i+1) for y in range(j+1) for z in range(k+1) if x+y+z != n] return arr i = 1 j = 1 k = 2 n = 3 print(solve(i, j, k, n))

## Input

1, 1, 2, 3

## Output

[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 2]]

- Related Questions & Answers
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
- Program to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++
- Construct a Turing machine for L = {aibjck | i*j = k; i, j, k ≥ 1}
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized in C++
- Program to find smallest index for which array element is also same as index in Python
- Program to check we can find four elements whose sum is same as k or not in Python
- Program to check we can find three unique elements ose sum is same as k or not Python
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++
- C++ program to find range whose sum is same as n
- C++ program to find permutation for which sum of adjacent elements sort is same as given array
- Program to check n can be shown as sum of k or not in Python
- Program to find higher number with same number of set bits as n in Python?\n
- C++ program to find number of l-r pairs for which XORed results same as summation
- Count the triplets such that A[i] < B[j] < C[k] in C++

Advertisements