Program to find largest size to truncate logs to store them completely in database in Python

Suppose we have a list of numbers called logs and another value limit. Each element in logs[i] represents the size of logs generated by the i-th user. The limit represents the total size of logs we can store in our database. We need to find the largest x such that if we truncate every log in logs to be at most size x, the sum of the truncated log sizes is at most limit. If no log needs to be truncated, then simply return the largest log size.

So, if the input is like logs = [500, 200, 10000, 500, 4000] and limit = 3000, then the output will be 900. When we truncate logs to 900, we get [500, 200, 900, 500, 900] and the sum is exactly 3000.

Approach Using Binary Search

We can solve this problem using binary search on the truncation size. The key insight is that if we can store all logs with truncation size x, we can also store them with any smaller truncation size.

The algorithm follows these steps ?

  • Set lo = 0 (minimum possible truncation size)
  • Set hi = 1 + maximum of logs (upper bound)
  • While lo + 1 < hi, do binary search:
    • Calculate mi = lo + floor of (hi − lo)/2
    • If sum of min(mi, log) for each log ? limit, then lo = mi
    • Otherwise, hi = mi
  • Return lo (the largest valid truncation size)

Example

def solve(logs, limit):
    lo, hi = 0, max(logs) + 1
    
    while lo + 1 < hi:
        mi = lo + (hi - lo) // 2
        truncated_sum = sum(min(mi, log) for log in logs)
        
        if truncated_sum <= limit:
            lo = mi
        else:
            hi = mi
    
    return lo

# Test with the given example
logs = [500, 200, 10000, 500, 4000]
limit = 3000
result = solve(logs, limit)
print(f"Maximum truncation size: {result}")

# Let's verify the result
truncated_logs = [min(result, log) for log in logs]
print(f"Original logs: {logs}")
print(f"Truncated logs: {truncated_logs}")
print(f"Sum after truncation: {sum(truncated_logs)}")
Maximum truncation size: 900
Original logs: [500, 200, 10000, 500, 4000]
Truncated logs: [500, 200, 900, 500, 900]
Sum after truncation: 3000

How It Works

The binary search works by testing different truncation sizes. For each candidate size mi, we calculate what the total would be if we truncated all logs to at most mi. If this total is within our limit, we know we can use this truncation size, so we search for potentially larger values. If the total exceeds our limit, we need a smaller truncation size.

The time complexity is O(n log m) where n is the number of logs and m is the maximum log size. The space complexity is O(1).

Alternative Example

# Example where no truncation is needed
logs2 = [100, 200, 300]
limit2 = 1000
result2 = solve(logs2, limit2)
print(f"No truncation needed - max size: {result2}")
print(f"Original sum: {sum(logs2)}")
No truncation needed - max size: 300
Original sum: 600

Conclusion

This binary search approach efficiently finds the maximum truncation size by testing candidates between 0 and the maximum log size. The algorithm guarantees we find the largest possible truncation size that keeps the total within our storage limit.

Updated on: 2026-03-26T17:37:18+05:30

157 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements