From a726e7f1493f81713dfe3fd8868bb29fc8dba056 Mon Sep 17 00:00:00 2001 From: Mayank Padshala Date: Wed, 22 Oct 2025 01:01:52 +0000 Subject: [PATCH 1/2] Add max subarray sum problem --- maxSubArraySum.py | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 maxSubArraySum.py diff --git a/maxSubArraySum.py b/maxSubArraySum.py new file mode 100644 index 00000000..081e1da8 --- /dev/null +++ b/maxSubArraySum.py @@ -0,0 +1,138 @@ +""" +Problem: Maximum Subarray and Subsequence Sum + +We define: +- A **subsequence** as any subset of an array. +- A **subarray** as a contiguous subsequence within an array. + +Task: +Given an integer array, find the maximum possible sum among: +1. All non-empty subarrays. +2. All non-empty subsequences. + +Print the two values as space-separated integers on one line. + +Note: +Empty subarrays or subsequences should NOT be considered. + +--- + +Function Description: +--------------------- +def maxSubarray(arr: List[int]) -> List[int]: + +Parameters: +- arr (List[int]): an array of integers. + +Returns: +- List[int]: a list of two integers representing: + [maximum subarray sum, maximum subsequence sum] + +--- + +Input Format: +------------- +- The first line contains an integer T, the number of test cases. +- For each test case: + - The first line contains an integer n (size of the array). + - The second line contains n space-separated integers (the elements of the array). + +--- + +Constraints: +------------ +- 1 ≤ T ≤ 10 +- 1 ≤ n ≤ 10^5 +- -10^4 ≤ arr[i] ≤ 10^4 +- Each subarray/subsequence considered must have at least one element. + +--- + +Sample Input: +-------------- +2 +4 +1 2 3 4 +6 +2 -1 2 3 4 -5 + +Sample Output: +--------------- +10 10 +10 11 + +Explanation: +------------ +Case 1: +All numbers are positive, so both sums are simply the total sum of the array → 10. + +Case 2: +- Maximum subarray: [2, -1, 2, 3, 4] → 10 +- Maximum subsequence: [2, 2, 3, 4] → 11 + +Case 3 (All negatives example): +Input: +1 +5 +-2 -3 -1 -4 -6 + +Output: +-1 -1 +Explanation: +The maximum sum for both is the single largest element (-1). +""" + +#!/bin/python3 + +import math +import os +import random +import re +import sys + +# +# Complete the 'maxSubarray' function below. +# +# The function is expected to return an INTEGER_ARRAY. +# The function accepts INTEGER_ARRAY arr as parameter. +# + +def maxSubarray(arr): + if len(arr) < 1: + return [arr[0], arr[0]] + maxSubArraySum = arr[0] + maxRunningSum = arr[0] + for i, num in enumerate(arr[1:]): + # If the num is greater than the current maxRunningSum, we start fresh + maxRunningSum = max(num, maxRunningSum+num) + # Here there will be cases where we start with max, then we might find something that breaks the max, then we have to only keep max subArraySum + maxSubArraySum = max(maxSubArraySum, maxRunningSum) + maxSubsequenceSum = 0 + for num in arr: + if num > 0: + maxSubsequenceSum += num + + if maxSubsequenceSum == 0: + # This means we did not add anything to maxSubsequenceSum because we only add if num is greater than 0 + # In this case we should find the max value in the array and then just return that + maxSubsequenceSum = max(arr) + + return [maxSubArraySum, maxSubsequenceSum] + + +if __name__ == '__main__': + fptr = open(os.environ['OUTPUT_PATH'], 'w') + + t = int(input().strip()) + + for t_itr in range(t): + n = int(input().strip()) + + arr = list(map(int, input().rstrip().split())) + + result = maxSubarray(arr) + + fptr.write(' '.join(map(str, result))) + fptr.write('\n') + + fptr.close() \ No newline at end of file From 95ee120dc6292978575042537215c15150d6bd3a Mon Sep 17 00:00:00 2001 From: Mayank Padshala Date: Wed, 22 Oct 2025 01:22:01 +0000 Subject: [PATCH 2/2] Add Hackerland Radio Transmitters --- hackerlandRadioTransmitter.py | 164 ++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 hackerlandRadioTransmitter.py diff --git a/hackerlandRadioTransmitter.py b/hackerlandRadioTransmitter.py new file mode 100644 index 00000000..71fb8367 --- /dev/null +++ b/hackerlandRadioTransmitter.py @@ -0,0 +1,164 @@ +""" +Problem: Hackerland Radio Transmitters +-------------------------------------- + +Hackerland is a one-dimensional city with houses located at integer positions along a road. +The Mayor wants to install radio transmitters on the roofs of some houses. +Each transmitter has a fixed range `k`, meaning it can transmit signals to all houses +within `k` units distance (to both the left and right). + +Goal: +----- +Determine the **minimum number of transmitters** required so that every house is +within range of at least one transmitter. +Each transmitter must be installed **on top of an existing house**. + +--- + +Function Description: +--------------------- +def hackerlandRadioTransmitters(x: List[int], k: int) -> int + +Parameters: +- x (List[int]): the locations of houses along the road. +- k (int): the effective transmission range of each transmitter. + +Returns: +- int: the minimum number of transmitters required to cover all houses. + +--- + +Input Format: +------------- +- The first line contains two space-separated integers: + - n: the number of houses in Hackerland. + - k: the range of each transmitter. +- The second line contains n space-separated integers describing house locations. + +--- + +Output Format: +-------------- +Print a single integer — the minimum number of transmitters needed. + +--- + +Constraints: +------------ +- 1 ≤ n ≤ 10^5 +- 1 ≤ k ≤ 10^4 +- 1 ≤ x[i] ≤ 10^5 +- There may be more than one house at the same location. + +--- + +Subtasks: +--------- +For 30% of the maximum score: +- n ≤ 1000 + +--- + +Examples: +---------- +Sample Input 0: +--------------- +5 1 +1 2 3 4 5 + +Sample Output 0: +---------------- +2 + +Explanation 0: +-------------- +We can cover all houses by installing transmitters at locations 2 and 4. +Coverage: +- Transmitter at 2 → covers houses [1, 2, 3] +- Transmitter at 4 → covers houses [3, 4, 5] + +--- + +Sample Input 1: +--------------- +8 2 +7 2 4 6 5 9 12 11 + +Sample Output 1: +---------------- +3 + +Explanation 1: +-------------- +We can install transmitters at locations 4, 9, and 12. +Coverage: +- Transmitter at 4 → covers houses [2, 3, 4, 5, 6] +- Transmitter at 9 → covers houses [7, 8, 9, 10, 11] +- Transmitter at 12 → covers houses [10, 11, 12, 13, 14] +""" +#!/bin/python3 + +import math +import os +import random +import re +import sys + +# +# Complete the 'hackerlandRadioTransmitters' function below. +# +# The function is expected to return an INTEGER. +# The function accepts following parameters: +# 1. INTEGER_ARRAY x +# 2. INTEGER k +# + +def hackerlandRadioTransmitters(x, k): + lenX = len(x) + if lenX < 1: + return 0 + # First we need to sort all the houses in x, so that we know that the houses are being read in sequence. + x.sort() + + # Now that we have all houses in sequence, we know that we can check if a given house if there are antenas places, how many houses it can cover. + # For this we can assume that we place the antena at the first house. This means it will cover next k houses. + # So we move to the next house which will be the last one to recieve signal, if antena places at the first house. + # This also means that if the antena is places at this last house, the first house on the left will recieve the signal. So instead of placing the antena at the first house, we place it at the current house. This gives us an advantage that we can use this to calculate which is the next house the antena can be placed and which houses current antena will cover. + currHouse = 0 + count = 0 + while currHouse < lenX: + # Find the place where we can place the antena + tillWeCanPlace = x[currHouse] + k + while currHouse < lenX and x[currHouse] <= tillWeCanPlace: + currHouse += 1 + # At this point the curr is at a house where if we placed the antena the first house will not get signal. + # So we just move our curr to one house earlier + currHouse -= 1 + + # We can safely place an antena here, becase we know that the houses on the left will all recieve signal + count += 1 + + # Now that we have placed the antena check for all the houses that this antena will give signal till on the right side + tillWeCanPlace = x[currHouse] + k + while currHouse < lenX and x[currHouse] <= tillWeCanPlace: + currHouse += 1 + # Now curr is at a place where there is no signal. So we continue in the while loop and find the next place where we can place the antena. This is a greedy way of solving the problem. + + return count + +if __name__ == '__main__': + fptr = open(os.environ['OUTPUT_PATH'], 'w') + + first_multiple_input = input().rstrip().split() + + n = int(first_multiple_input[0]) + + k = int(first_multiple_input[1]) + + x = list(map(int, input().rstrip().split())) + + result = hackerlandRadioTransmitters(x, k) + + fptr.write(str(result) + '\n') + + fptr.close()