Split Array Elements into Bounded Parts

Last Updated : 8 Sep, 2026

Given an array arr[] of positive integers and an integer k, split each element into the minimum number of parts such that every part is less than or equal to k, and find the total number of parts formed from all elements of the array.

Examples:

Input: k = 3, arr[] = [5, 8, 10, 13]
Output: 14
Explanation: Each number is expressed as a sum of numbers less than or equal to k as 5 (3 + 2), 8 (3 + 3 + 2), 10 (3 + 3 + 3 + 1), and 13 (3 + 3 + 3 + 3 + 1). Therefore, the total count of parts is (2 + 3 + 4 + 5) = 14.

Input: k = 4, arr[] = [10, 2, 3, 4, 7]
Output: 8
Explanation: Each number is expressed as a sum of numbers less than or equal to k as 10 (4 + 4 + 2), 2 (2), 3 (3), 4 (4), and 7 (4 + 3). Therefore, the total count of parts is (3 + 1 + 1 + 1 + 2) = 8.

Try It Yourself
redirect icon

[Naive Approach] Using Repeated Subtraction - O(n * (max(arr) / k)) Time and O(1) Space

The idea is to repeatedly subtract k from each element and count every part formed until the remaining value is less than or equal to k.

Working of Approach:

  • Traverse every element of the array.
  • While the element is greater than k, subtract k from it.
  • Increment the count for every part of size k.
  • If a positive value remains, count it as the final part.
  • Return the total count of all parts.
C++
#include <iostream>
#include <vector>
using namespace std;

// Function to calculate the total number of parts.
int totalCount(int k, vector<int> &arr)
{
    int cnt = 0;
    int n = arr.size();

    // Iterating over all the elements.
    for (int i = 0; i < n; i++)
    {
        int num = arr[i];

        // Creating parts of size k repeatedly.
        while (num > k)
        {
            cnt++;
            num -= k;
        }

        // Counting the remaining positive part.
        if (num > 0)
        {
            cnt++;
        }
    }

    return cnt;
}

int main()
{
    int k = 4;
    vector<int> arr = {10, 2, 3, 4, 7};

    cout << totalCount(k, arr);

    return 0;
}
Java
import java.util.*;

class GFG {

    // Function to calculate the total number of parts.
    public int totalCount(int k, int[] arr)
    {
        int cnt = 0;
        int n = arr.length;

        // Iterating over all the elements.
        for (int i = 0; i < n; i++) {
            int num = arr[i];

            // Creating parts of size k repeatedly.
            while (num > k) {
                cnt++;
                num -= k;
            }

            // Counting the remaining positive part.
            if (num > 0) {
                cnt++;
            }
        }

        return cnt;
    }

    public static void main(String[] args)
    {
        int k = 4;
        int[] arr = { 10, 2, 3, 4, 7 };

        GFG obj = new GFG();

        System.out.print(obj.totalCount(k, arr));
    }
}
Python
def totalCount(k, arr):
    cnt = 0
    n = len(arr)

    # Iterating over all the elements.
    for i in range(n):
        num = arr[i]

        # Creating parts of size k repeatedly.
        while num > k:
            cnt += 1
            num -= k

        # Counting the remaining positive part.
        if num > 0:
            cnt += 1

    return cnt

if __name__ == '__main__':
    k = 4
    arr = [10, 2, 3, 4, 7]

    print(totalCount(k, arr))
C#
using System;

class GFG {
    // Function to calculate the total number of parts.
    public int totalCount(int k, int[] arr)
    {
        int cnt = 0;
        int n = arr.Length;

        // Iterating over all the elements.
        for (int i = 0; i < n; i++) {
            int num = arr[i];

            // Creating parts of size k repeatedly.
            while (num > k) {
                cnt++;
                num -= k;
            }

            // Counting the remaining positive part.
            if (num > 0) {
                cnt++;
            }
        }

        return cnt;
    }

    public static void Main(string[] args)
    {
        int k = 4;
        int[] arr = { 10, 2, 3, 4, 7 };

        GFG obj = new GFG();

        Console.Write(obj.totalCount(k, arr));
    }
}
JavaScript
function totalCount(k, arr)
{
    let cnt = 0;
    let n = arr.length;

    // Iterating over all the elements.
    for (let i = 0; i < n; i++) {
        let num = arr[i];

        // Creating parts of size k repeatedly.
        while (num > k) {
            cnt++;
            num -= k;
        }

        // Counting the remaining positive part.
        if (num > 0) {
            cnt++;
        }
    }

    return cnt;
}

// Driver Code
let k = 4;
let arr = [ 10, 2, 3, 4, 7 ];

console.log(totalCount(k, arr));

Output
8

[Expected Approach] Using Direct Ceiling Division - O(n) Time and O(1) Space

The idea is to directly calculate the minimum number of parts for each element using division and add the quotient (or quotient + 1) to the answer.

Working of Approach:

  • Traverse every element of the array.
  • Check whether the current element is divisible by k.
  • If divisible, add its quotient to the count.
  • Otherwise, add the quotient plus one to the count.
  • Return the total count after processing all elements.

Let us understand with an example:
Input: k = 4, arr[] = [10, 2, 3, 4, 7]

  • Initialize cnt = 0 and k = 4.
  • For 10, it is not divisible by 4, so add 10 / 4 + 1 = 3; cnt = 3.
  • For 2, 3, and 4, add 1, 1, and 1 respectively; cnt = 6.
  • For 7, it is not divisible by 4, so add 7 / 4 + 1 = 2; cnt = 8.
  • Finally, the function returns 8.
C++
#include <iostream>
#include <vector>
using namespace std;

// Function to calculate the total count of elements when divided by k.
int totalCount(int k, vector<int> &arr)
{
    int cnt = 0;
    int n = arr.size();

    // iterating over all the elements.
    for (int i = 0; i < n; i++)
    {

        // if element is divisible by k, adding quotient to count.
        if (arr[i] % k == 0)
        {
            cnt += arr[i] / k;
        }
        else
        {

            // if not divisible, adding quotient + 1 to count.
            cnt += (arr[i] / k + 1);
        }
    }
    return cnt;
}

int main()
{
    int k = 4;
    vector<int> arr = {10, 2, 3, 4, 7};

    cout << totalCount(k, arr);

    return 0;
}
Java
import java.util.*;

class GFG {

    // Function to calculate the total count of elements
    // when divided by k.
    public int totalCount(int k, int[] arr)
    {
        int cnt = 0;
        int n = arr.length;

        // Iterating over all the elements.
        for (int i = 0; i < n; i++) {

            // If element is divisible by k, adding quotient
            // to count.
            if (arr[i] % k == 0) {
                cnt += arr[i] / k;
            }
            else {

                // If not divisible, adding quotient + 1 to
                // count.
                cnt += (arr[i] / k + 1);
            }
        }

        return cnt;
    }

    public static void main(String[] args)
    {
        int k = 4;
        int[] arr = { 10, 2, 3, 4, 7 };

        GFG obj = new GFG();
        System.out.print(obj.totalCount(k, arr));
    }
}
Python
def totalCount(k, arr):
    cnt = 0
    n = len(arr)

    # iterating over all the elements.
    for i in range(n):

        # if element is divisible by k, adding quotient to count.
        if arr[i] % k == 0:
            cnt += arr[i] // k
        else:

            # if not divisible, adding quotient + 1 to count.
            cnt += (arr[i] // k + 1)
    return cnt


if __name__ == '__main__':
    k = 4
    arr = [10, 2, 3, 4, 7]

    print(totalCount(k, arr))
C#
using System;

class GFG {
    // Function to calculate the total count of elements
    // when divided by k.
    public int totalCount(int k, int[] arr)
    {
        int cnt = 0;
        int n = arr.Length;

        // Iterating over all the elements.
        for (int i = 0; i < n; i++) {
            // If element is divisible by k, adding quotient
            // to count.
            if (arr[i] % k == 0) {
                cnt += arr[i] / k;
            }
            else {
                // If not divisible, adding quotient + 1 to
                // count.
                cnt += (arr[i] / k + 1);
            }
        }

        return cnt;
    }

    public static void Main(string[] args)
    {
        int k = 4;
        int[] arr = { 10, 2, 3, 4, 7 };

        GFG obj = new GFG();
        Console.Write(obj.totalCount(k, arr));
    }
}
JavaScript
function totalCount(k, arr)
{
    let cnt = 0;
    let n = arr.length;

    // iterating over all the elements.
    for (let i = 0; i < n; i++) {

        // if element is divisible by k, adding quotient to
        // count.
        if (arr[i] % k === 0) {
            cnt += Math.floor(arr[i] / k);
        }
        else {

            // if not divisible, adding quotient + 1 to
            // count.
            cnt += Math.floor(arr[i] / k + 1);
        }
    }
    return cnt;
}

// Driver Code
let k = 4;
let arr = [ 10, 2, 3, 4, 7 ];

console.log(totalCount(k, arr));

Output
8
Comment