GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Ask a Question

 

Start Coding Today

   

GeeksQuiz

GeeksforGeeksIDE

Data Structures

Algorithms

Interview Experiences

C Programming

C++ Programming

Java Programming

GATE CS

Books

Find way to divide array into parts whose total is less or equal than k

I have this problem, however I still don't get the answer. Please help me! They give me an array of n numbers (n<=10^5). They asked me to divide the array into consecutive parts that each total is less or equal than k (k<=10^9).

However, number of parts is minimum.

For ex: Input:

11 5 (n=11, k=5)

9 -1 2 -6 1 2 3 -4 3 9 -4

Output: 3 parts.

  • Part 1: 9, -1, 2, -6, 1

  • Part 2: 2, 3, -4, 3

  • Part 3: 9, -4

asked Feb 1 by Awson Wertan

1 Answer

n = 11
k = 5

array = [9, -1, 2, -6, 1, 2 ,3 ,-4 ,3 ,9 ,-4]

i = 0

k = 1
while(i<n):
    while(k<= n-i-1):
        j = i+k
        if(sum(array[i:j]) <= k) :
            
            print(array[i:j])
        k += 1
    i += 1

 

answered Feb 4 by Prateek Chanda

Can you tell me the complexity of this algorithm plss? Because I have to solve this problem under 1s

You output requirement is not clear. Are you asking for only the number of minimum partitions or you asking for the partitioned sub-arrays to be printed as output?

I am asked for the output of partitioned sub-arrays one

...