ACC Coding3
ACC Coding3
Question-1
Chocolate Distribution
Problem Description:
The function accepts an integer array ‘arr’ of size ‘n’ as its argument.
Each element of ‘arr’ represents the number of chocolates distributed
to a person. The function needs to return the minimum number of
chocolates that need to be distributed to each person so that the
difference between the chocolates of any two people is minimized.
Input: Output:
n: 5 3
arr: 10 4 12 3 1
Solution
def min_chocolates(arr):
arr.sort()
min_diff = arr[-1] - arr[0]
for i in range(len(arr) - 4):
min_diff = min(min_diff, arr[i + 4] - arr[i])
return min_diff
arr = [10, 4, 12, 3, 1]
result = min_chocolates(arr)
print(result)
Question-2
• Parking Lot
• Problem Description:
The function accepts a character array ‘arr’ of size ‘n’ as its argument.
Each element of ‘arr’ represents the status of a parking slot, where ‘S’
represents an empty slot and ‘X’ represents an occupied slot. The
function needs to return the maximum number of cars that can be
parked in the parking lot. It is assumed that two cars cannot occupy the
same slot and cars can only park in consecutive empty slots.
Input: Output:
n: 16 7
arr:
XXXSXXSXXSSXXSXX
Solution
• String Transformation
Problem Description:
The function accepts a string ‘str’ as its argument. The function needs to return
the transformed string by replacing all occurrences of the character ‘a’ with the
character ‘b’ and vice versa.
Input: Output:
str: abaabbcc bbbbaaac
Solution
def transform_string(str):
transformed_string = ""
for i in range(len(str)):
if str[i] == 'a':
transformed_string += 'b'
elif str[i] == 'b':
transformed_string += 'a'
else:
transformed_string += str[i]
return transformed_string
str = "abaabbcc"
transformed_string = transform_string(str)
print(transformed_string)
Question-4
Array Equilibrium
Problem Description:
The function accepts an integer array ‘arr’ of size ‘n’ as its argument. The function
needs to return the index of an equilibrium point in the array, where the sum of
elements on the left of the index is equal to the sum of elements on the right of
the index. If no equilibrium point exists, the function should return -1.
Input: Output:
n: 5 3
arr: 1 3 5 7 3
Solution
def find_equilibrium(arr):
total_sum = sum(arr)
left_sum = 0
for i in range(len(arr)):
total_sum -= arr[i]
if left_sum == total_sum:
return i
left_sum += arr[i]
return -1
arr = [1, 3, 5, 7, 3]
print(find_equilibrium(arr))
Question-5
Array Rotation
• Problem Description:
The function accepts an integer array ‘arr’ of size ‘n’ and an integer ‘d’ as its
argument. The function needs to rotate the array ‘arr’ by ‘d’ positions to the
right. The rotation should be done in place, without using any additional
memory.
Input: Output:
n: 5 34512
arr: 1 2 3 4 5
d: 3
Solution
Substring Search
Problem Description:
The function accepts two strings ‘str1’ and ‘str2’ as its argument. The
function needs to return the index of the first occurrence of substring ‘str2’
in string ‘str1’ or -1 if the substring is not found.
Input: Output:
str1: “Hello,
7
World!”
str2: “World”
Solution
• Palindrome Check
Problem Description:
The function accepts a string ‘str’ as its argument. The function needs to
determine whether the string is a palindrome or not. A palindrome is a word
or phrase that reads the same backward as forward.
Input: Output:
str: “madam” 1
Solution
def isPalindrome(s):
return s == s[::-1]
str_input = "madam"
if isPalindrome(str_input):
print("1")
else:
print("0")
Question-8
Reverse Words
• Problem Description:
The function accepts a string ‘str’ as its argument. The function needs to
reverse the order of the words in the string.
Input: Output:
str: “Hello,
!dlroW ,olleH
World!”
Solution
def reverse_words(s):
words = s.split()
reversed_words = " ".join(reversed(words))
+print(reversed_words)
str_input = "Hello, World!"
reverse_words(str_input)
Question-9
Explanation:
Given an array of integers, such as [5, 2, 4, 1, 3], and an integer sum,
such as 9, the algorithm should determine that the pair (a, b) = (2, 7) or
(4, 5) satisfies the condition a + b = sum. If no such pair exists, the
algorithm should return -1.
Solution
Explanation:
Given an array of integers, such as [-2, 1, -3, 4, -1, 2, 1, -5, 4], the algorithm
should determine that the maximum subarray sum is 6 ([4, -1, 2, 1]).
Solution
def max_sub_array_sum(arr):
max_so_far = max_ending_here = arr[0]
for num in arr[1:]:
max_ending_here = max(num, max_ending_here + num)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
arr = [-2, -3, 4, -1, -2, 1, 5, -3]
print("Maximum Subarray Sum is", max_sub_array_sum(arr))
THANK YOU