
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find number of unique four indices where they can generate sum less than target from four lists in python
Suppose we have four list of numbers A, B, C and D and also have another number target. We have to find the number of different unique indices i, j, k, l such that A[i] + B[j] + C[k] + D[l] ≤ target.
So, if the input is like A = [3, 2] B = [5, 3] C = [1] D = [2, 3] target = 9, then the output will be 3, as We can pick the following combinations: [3, 3, 1, 2] [3, 3, 1, 2] [2, 3, 1, 3]
To solve this, we will follow these steps:
- temp_list := a new list
- for i in range 0 to size of A, do
- for j in range 0 to size of B, do
- insert (A[i] + B[j]) at the end of temp_list
- for j in range 0 to size of B, do
- sort the list temp_list
- ans := 0
- for i in range 0 to size of C, do
- for j in range 0 to size of D, do
- sum_cd := C[i] + D[j]
- sum_ab := target - sum_cd
- ans := ans + number of elements in temp_list whose sum <= sum_ab
- for j in range 0 to size of D, do
- return ans
Let us see the following implementation to get better understanding:
Example
from bisect import bisect_right class Solution: def solve(self, A, B, C, D, target): temp_list = [] for i in range(len(A)): for j in range(len(B)): temp_list.append(A[i] + B[j]) temp_list.sort() ans = 0 for i in range(len(C)): for j in range(len(D)): sum_cd = C[i] + D[j] sum_ab = target - sum_cd ans += bisect_right(temp_list, sum_ab) return ans ob = Solution() A = [3, 2] B = [5, 3] C = [1] D = [2, 3] target = 9 print(ob.solve(A, B, C, D, target))
Input
[3, 2], [5, 3], [1], [2, 3], 9
Output
3
- Related Articles
- Program to find sum of two numbers which are less than the target in Python
- Program to check number of triplets from an array whose sum is less than target or not Python
- MySQL query to find all rows where string contains less than four characters?
- Program to find lowest sum of pairs greater than given target in Python
- (a) Write four negative integers greater than \( -20 \).(b) Write four integers less than $– 10$.
- Program to find number of unique subsequences same as target in C++
- Program to find number of sublists whose sum is given target in python
- Program to find number of distinct quadruple that forms target sum in python
- Write four rational numbers less than $\frac{-3}{5}$
- Program to find number of solutions for given equations with four parameters in Python
- Program to check we can find four elements whose sum is same as k or not in Python
- Find the smallest sum of all indices of unique number pairs summing to a given number in JavaScript
- Program to find sum of unique elements in Python
- Program to find number of starting point from where we can start travelling in Python
- Program to find number of ways we can arrange symbols to get target in Python?

Advertisements