- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Program to find length of smallest sublist that can be deleted to make sum divisible by k in Python

Suppose we have a list with positive values, called nums and also have a positive number k. We have to find the length of the shortest sublist (may be empty) that we can delete from nums, such that sum of the remaining elements is divisible by k. But we cannot remove the entire list. If there is no such sublist to delete, return -1.

So, if the input is like nums = [5,8,6,3] k = 8, then the output will be 1, because current sum of the elements of [5,8,6,3] is 22. If we remove sublist [6] of length 1, then sum is 16, which is divisible by 8.

To solve this, we will follow these steps −

- rem := (sum of all elements present in nums + k) mod k
- if rem is same as 0, then
- return 0

- n := size of nums
- presum := 0
- mp := a dictionary, initially store -1 for key 0
- res := n
- for i in range 0 to n - 1, do
- presum := presum + nums[i]
- m :=(presum + k) mod k
- mp[m] := i
- if (m - rem + k) mod k is present in mp, then
- res := minimum of res and (i - mp[(m - rem + k) mod k])

- return res if res is not same as n otherwise -1

## Example

Let us see the following implementation to get better understanding −

def solve(nums, k): rem = (sum(nums) + k) % k if rem == 0: return 0 n, presum = len(nums), 0 mp = {0: -1} res = n for i in range(n): presum += nums[i] m = (presum + k) % k mp[m] = i if (m - rem + k) % k in mp: res = min(res, i - mp[(m - rem + k) % k]) return res if res != n else -1 nums = [5,8,6,3] k = 8 print(solve(nums, k))

## Input

[5,8,6,3], 8

## Output

1

- Related Articles
- Program to count minimum k length sublist that can be flipped to make all items of list to 0 in Python
- Program to find the sum of largest K sublist in Python
- Program to find length of longest sublist containing repeated numbers by k operations in Python
- Program to make sum divisible by P in Python
- Program to find size of smallest sublist whose sum at least target in Python
- Smallest Integer Divisible by K in Python
- Program to find length of longest sublist whose sum is 0 in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Python Program for Smallest K digit number divisible by X
- Program to find length of longest distinct sublist in Python
- Program to find minimum digits sum of deleted digits in Python
- Program to Find Out the Largest K-Divisible Subsequence Sum in Python
- Program to find sum of contiguous sublist with maximum sum in Python
- Removing smallest subarray to make array sum divisible in JavaScript
- Program to find length of contiguous strictly increasing sublist in Python

Advertisements