0% found this document useful (0 votes)
10 views4 pages

Array Problems: Max Subarray & Pairs

The document outlines five programming problems, each requiring a specific function to be implemented. Problem 1 focuses on finding the maximum subarray sum, Problem 2 involves identifying pairs of elements that sum to a target, Problem 3 counts pairs whose sum is divisible by a given number, Problem 4 counts non-overlapping adjacent pairs with even sums, and Problem 5 reverses only alphabetic characters in a string while keeping non-alphabetic characters in place. Each problem includes input/output specifications, examples, and constraints.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Array Problems: Max Subarray & Pairs

The document outlines five programming problems, each requiring a specific function to be implemented. Problem 1 focuses on finding the maximum subarray sum, Problem 2 involves identifying pairs of elements that sum to a target, Problem 3 counts pairs whose sum is divisible by a given number, Problem 4 counts non-overlapping adjacent pairs with even sums, and Problem 5 reverses only alphabetic characters in a string while keeping non-alphabetic characters in place. Each problem includes input/output specifications, examples, and constraints.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem 1: Maximum Subarray Sum

Problem Statement: Write a function subArray that finds the maximum sum of a contiguous
subarray within an array of integers. If the array contains only negative numbers, return the
highest value (least negative) element.

Input:

●​ An array of integers arr

Output:

●​ The maximum sum of any contiguous subarray in the given array

Example:

●​ For input array {2,3,-8,7,-1,2,3}, the maximum subarray sum is 11 (elements


7,-1,2,3)
●​ For input array {-2,-4}, the maximum subarray sum is -2 (the single element -2)

Constraints:

●​ The array may contain both positive and negative integers


●​ The array will have at least one element

Problem 2: Two Sum Problem


Problem Statement: Write a function sum that finds a pair of elements in an array that add up
to a specified target sum. If such a pair exists, print the indexes and the values of these
elements.

Input:

●​ An array of integers inputArray


●​ A target sum expectedSum

Output:

●​ The indexes of the two elements that add up to the target sum
●​ The values of these two elements
Example:

●​ For input array {1, 2, 3, 5} and target sum 4, the function should print:
○​ "Indexes are: 0, 2"
○​ "Values are: 1, 3"

Constraints:

●​ Assume there is exactly one solution


●​ You cannot use the same element twice

Problem 3: Divisible Pairs Count – Similar


to previous problem (try to solve as
assignment)
Problem Statement: Write a function divisor_pair that counts the number of pairs in an
array whose sum is divisible by a given number.

Input:

●​ An array of integers arr


●​ A divisor divisor

Output:

●​ The count of pairs (i, j) where i < j and (arr[i] + arr[j]) is divisible by the divisor

Example:

●​ For input array {5,9,36,74,52,31,42} and divisor 3, the function should return 8

Constraints:

●​ The array will contain only positive integers


●​ The divisor will be a positive integer
●​ Each pair should be counted exactly once (i.e., order matters)

Problem 4: Adjacent Pair Grouping


Problem Statement: Write a function evenSum that processes an array of integers by
examining adjacent pairs of elements. The function should count how many non-overlapping
pairs with an even sum can be formed while traversing the array from left to right.

Input:

●​ An array of integers arr

Output:

●​ The count of non-overlapping adjacent pairs whose sum is even

Algorithm Details:

●​ The function traverses the array from left to right


●​ When it finds a pair of adjacent elements whose sum is even, it:
○​ Increments the counter
○​ Skips the next pair (moves the index by 2)
●​ If the adjacent pair doesn't have an even sum, it simply moves to the next position

Example:

●​ For input array {4,2,5,8,7,3,7}:


○​ Pair (4,2): Sum is 6 (even), increment counter to 1, move to index 2
○​ Pair (5,8): Sum is 13 (odd), move to index 4
○​ Pair (7,3): Sum is 10 (even), increment counter to 2, move to index 6
○​ Index 6 is the last element, so there are no more pairs to check
○​ The function returns 2

Constraints:

●​ The array will contain at least one element


●​ The function only considers adjacent pairs (elements at positions i and i+1)
●​ The function manages index traversal to ensure non-overlapping pairs

Problem 5: Reverse Letters Only


Problem Statement: Write a function reverseLetters that reverses only the alphabetic
characters in a string while keeping all non-alphabetic characters (digits, symbols, spaces) in
their original positions.

Input:
●​ A string input containing a mix of alphabetic and non-alphabetic characters

Output:

●​ A new string where only the alphabetic characters are reversed while non-alphabetic
characters remain in place

Example:

●​ For input string "1ab2", the function should return "1ba2"

Constraints:

●​ The string may contain any character (letters, digits, special characters, spaces)
●​ Only alphabetic characters (a-z, A-Z) should be reversed
●​ The function should preserve the case of each letter

You might also like