20 Coding Q Ans A
20 Coding Q Ans A
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-(n=5),(arr=10,4,12,3,1)
Output-3
import java.util.Arrays;
Arrays.sort(arr);
return minDiff;
System.out.println(result);
}
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.
Example:
Input: Output:
n: 16 7
arr: XXXSXXSXXSSXXSXX
int maxCars = 0;
int currentCars = 0;
if (arr[i] == 'S') {
currentCars++;
} else {
currentCars = 0;
return maxCars;
}
int n = 16;
System.out.println(result);
3. 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:
Example:
if (charArray[i] == 'a')
charArray[i] = 'b';
charArray[i] = 'a';
System.out.println(transformString(str));
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.
Example:
Input: Output:
n: 5 3
arr: 1 3 5 7 3
totalSum += arr[i];
totalSum -= arr[i];
if (leftSum == totalSum)
return i;
leftSum += arr[i];
return -1;
int n = 5;
System.out.println(findEquilibrium(arr, n));
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.
Example:
Input: Output:
n: 5 34512
arr: 1 2 3 4 5
d: 3
arr[i] = temp[i];
int n = 5;
int d = 3;
rotateArray(arr, n, d);
System.out.print("Output: ");
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.
Example:
Input: Output:
str2: “World”
System.out.println(substringSearch(str1, str2));
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.
Example:
Input: Output:
str: “madam” 1
Solution:
public class PalindromeCheck {
if (isPalindrome(str)) {
System.out.println("1");
} else {
System.out.println("0");
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.
Example:
Input: Output:
Solution:
reverseWords(str);
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.
Example:
Input: Output:
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 :
return;
findPair(arr, sum);
Problem Description:
Given an array of integers, find the maximum subarray sum. A subarray is a contiguous subsequence
of the array.
Eample:
Input: Output:
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 :
return max_so_far;
}
11. Character Replacement
Problem Description:
Given a string str, a character ch1, and a character ch2, replace all occurrences of ch1 in str with ch2
and vice versa.
Input: Output:
A character
ch1
A character
ch2
Example:
Input: Output:
Solution :
if (charArray[i] == ch1)
charArray[i] = ch2;
charArray[i] = ch1;
}
return new String(charArray);
12. Find the Minimum Value and Its Index in the Array
Problem Description:
Given an integer array, find the minimum value and its index in the array.
Input: Output:
Example:
Input: Output:
[5, 2, 4, 1, 3] 13
Solution :
public class MinValueAndIndex {
min = arr[i];
index = i;
Problem Description:
Given an array of integers, find the average of all positive numbers in the array.
Input: Output:
Example:
Input: Output:
[5, 2, -4, 1, 3] 3
Solution :
if (num > 0) {
sum += num;
count++;
}
14. Count the Occurrences of a Given Element in an Array
Problem Description:
Given an integer array and an integer element, count the number of occurrences of the element in
the array.
Input: Output:
An integer element
Example:
Input: Output:
[5, 2, 4, 1, 2], 2 2
Solution :
int count = 0;
if (num == element) {
count++;
return count;
}
public static void main(String[] args) {
int element = 2;
System.out.println(result);
Problem Description:
Given an integer array and an integer element, check if the array contains the element.
Input: Output:
An integer element
Example:
Input: Output:
Solution :
if (num == element) {
return true;
}
return false;
int element = 2;
Calculate and return the sum of prime numbers between ‘m’ and ‘n’ (inclusive).
Example:
Input: Output:
m : 10 158
n : 50
Solution :
if (num <= 1) {
return false;
}
if (num % i == 0) {
return false;
return true;
int sum = 0;
if (isPrime(i)) {
sum += i;
return sum;
System.out.println(result);
}
17. Problem Statement: Digit Sum Difference
Calculate and return the absolute difference between the sum of digits of numbers divisible by 4 and
the sum of digits of numbers divisible by 7, in the range from ‘m’ to ‘n’ (inclusive).
Example:
Input: Output:
m : 50 2
n : 120
Solution :
int sumDivisibleBy4 = 0;
int sumDivisibleBy7 = 0;
if (i % 4 == 0) {
int num = i;
num /= 10;
} else if (i % 7 == 0) {
int num = i;
num /= 10;
}
int m = 50;
int n = 120;
System.out.println(result);
Calculate and return the sum of Fibonacci numbers in the range from ‘m’ to ‘n’ (inclusive).
Example:
Input: Output:
m:5 52
n : 20
Solution :
if (b >= m) {
sum += b;
temp = a + b;
a = b;
b = temp;
return sum;
int m = 5, n = 20;
System.out.println(result);
Calculate and return the sum of numbers obtained by reversing the digits of each number in the
range from ‘m’ to ‘n’ (inclusive).
Example:
Input: Output:
m : 21 288
n : 35
Solution :
int sum = 0;
int reversedNum = 0;
int temp = i;
temp /= 10;
sum += reversedNum + i;
return sum;
int m = 21;
int n = 35;
System.out.println(result);
Calculate and return the difference between the sum of square roots of even numbers and the sum
of square roots of odd numbers in the range from ‘m’ to ‘n’ (inclusive).
Example:
Input: Output:
m:1 2.29416
n : 10
Solution :
if (i % 2 == 0) {
evenSum += squareRoot;
} else {
oddSum += squareRoot;
int m = 1, n = 10;
System.out.printf("%.5f\n", result);