0% found this document useful (0 votes)
11 views

ACC Coding3

The document contains ten programming problems along with their descriptions and solutions. Each problem involves manipulating arrays or strings, such as distributing chocolates, parking cars, transforming strings, finding equilibrium points, rotating arrays, searching for substrings, checking palindromes, reversing words, finding pairs with a specific sum, and calculating the maximum subarray sum. Each problem is accompanied by example inputs and outputs, along with Python function implementations.

Uploaded by

sri117537
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

ACC Coding3

The document contains ten programming problems along with their descriptions and solutions. Each problem involves manipulating arrays or strings, such as distributing chocolates, parking cars, transforming strings, finding equilibrium points, rotating arrays, searching for substrings, checking palindromes, reversing words, finding pairs with a specific sum, and calculating the maximum subarray sum. Each problem is accompanied by example inputs and outputs, along with Python function implementations.

Uploaded by

sri117537
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

WELCOME

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

def max_cars_parked(n, arr):


max_cars = 0
current_cars = 0
for status in arr:
if status == 'S':
current_cars += 1
else:
max_cars = max(max_cars, current_cars)
current_cars = 0
max_cars = max(max_cars, current_cars)
return max_cars
n = 16
arr = "XXXSXXSXXSSXXSXX"
result = max_cars_parked(n, arr)
print(result)
Question-3
Asked in Accenture OnCampus 10 Aug 2022, Slot 4

• 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

def rotate_array(arr, n, d):


temp = arr[-d:]
for i in range(n - 1, d - 1, -1):
arr[i] = arr[i - d]
arr[:d] = temp
n=5
arr = [1, 2, 3, 4, 5]
d=3
rotate_array(arr, n, d)
print("Output:", *arr)
Question-6

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

def substring_search(str1, str2):


index = str1.find(str2)
return index if index != -1 else -1
str1 = "Hello, World!"
str2 = "World"
print(substring_search(str1, str2))
Question-7

• 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

Find Two Numbers with Sum N


Problem Description:
Given an array of integers and an integer sum, find a pair of numbers (a, b)
in the array where a + b = sum.
Input: Output:
An array of two integers representing
An array of integers
the pair (a, b) or -1 if no such pair exists
An integer sum

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

def find_pair(arr, sum):


for i in range(len(arr) - 1):
for j in range(i + 1, len(arr)):
if arr[i] + arr[j] == sum:
print(f"Pair found: {arr[i]}, {arr[j]}")
return
print("Pair not found")
arr = [1, 4, 7, 8, 3, 9]
sum_val = 10
find_pair(arr, sum_val)
Question-10

• Maximum Subarray Sum


• Problem Description:
• Given an array of integers, find the maximum subarray sum. A subarray is a
contiguous subsequence of the array.
Input: Output:
An array of An integer representing the
integers maximum subarray sum

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

You might also like