2. 1. Prefix-Sufix balance
You are given an array A of N integers. The array A can be divided
into two parts: the first part consists of the first "i" elements of A
(where ‘i’ range from 1 to N), and the second part consists of the
last (N-i) elements of A.
Your task is to find and return a new array named result of the same
Size as A, where each element of result[i] represents the absolute
difference between the sum of the elements in the first part of A
and the sum of the elements in the second part of A.
Note: For i = N, N-i = 0. So, consider the sum of last N-i integers as 0
in this case.
3. Input Specification:
Input1: An integer value N, representing the size of the array A.
input2: An integer array A.
Output Specification:
Return a new integer array named result of the same size as A,
where each element of result[i] represents the absolute difference
between the sum of the elements in the first part of A and the sum
of the elements in the second part of A
Example 1:
Input1 : 5
input2: {1,2,3,4,5}
Output: (13,9,3,5,15)
4. Explanation:
We have been given the value of N as 5, the array A is (1,2,3,4,5).
We can create the result array by the following way:
result[1] = absolute(1-(2+3+4+5)) = 13
result[2] = absolute((1+2)-(3+4+5)) = 9
result[3] = absolute((1+2+3)-(4+5)) = 3
result[4] = absolute((1+2+3+4)-(5)) =5
result[5] = absolute((1+2+3+4+5)-(0)) = 15
Hence, (13,9,3,5,15) is returned as the output.
Example 2:
input1:3
input2: {15,45,12}
Output: (42,48,72)
5. import java.util.*;
import java.io.*;
class Main {
public static void prefixSuffix(int arr[], int n){
int sum = 0, temp = 0;
for(int i = 0; i < n; i++)
sum += arr[i];
for(int i = 0; i < n; i++) {
temp += arr[i];
int t = sum - temp;
arr[i] = Math.abs(t - temp);
System.out.print(arr[i] + " ");
}
}
6. public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
prefixSuffix(arr, n);
}
}
7. 2. Minimum Array Sum
Paul is given an array A of length N. He must perform the
following operations on the array sequentially:
• Choose any two integers from the array and calculate their
average.
• If an element is less than the average, update it to 0.
However if the element is greater than or equal to the
average, he need not update it.
Your task is to help Paul find and return an integer value,
representing the minimum possible sum of all the elements in the
array by performing the above operations.
8. Note: An exact average should be calculated even if it results in a
decimal.
Input Specification:
input1: An integer value N, representing the size of the array A
input2: An integer array A
Output Specification:
Return an integer value, representing the minimum possible sum of
all the elements in the array by performing the above operations.
Example 1:
input1 : 5
input2: (1,4,2,9,5)
output:9
9. Explanation:
Here, we have been given the array (1,4,2,9,5).
• Paul selects 5 and 9 from this array.
• The average of these two numbers is (5+9)/2=7.
• Following the process, Paul updates each element in the array to 0,
except for 9, as it is greater than 7, and all other elements are less
than 7.
• Therefore, the updated array becomes (0, 0, 0, 9, 0).
• The sum of these updated elements is 0+0+0+9+0=9.
If Paul were to select any other pair of elements, the resulting sum 9.
Hence, 9 is returned.
Example 2:
input1 : 3
input2: (1,2,3)
Output: 3
10. import java.util.*;
import java.io.*;
class Main {
public static int minimumArray(int arr[], int n) {
int i, first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (i = 0; i < n; i++){
if (arr[i] > first) {
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] != first)
second = arr[i];
}
int avg = (first + second) / 2;
int sum = 0;
11. for(i = 0; i < n; i++){
if(arr[i] <= avg)
arr[i] = 0;
sum += arr[i];
}
return sum;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println(minimumArray(arr, n));
}
}
12. 3.String swap
Given a string S, you need to perform M operations on the string
based on an array A of length M, which contains the sequence of
operations to be performed. The operations are of two types:
1. Swapping the first and last characters of the string.
2. Swapping the first N/2 and the last N/2 characters of the string
where N is the length of the string.
Your task is to find and return the final string value that will be
obtained after performing all M operations in the given sequence.
Note:
• The array contains either 1 or 2 as values representing the operation
number.
• The length of string is always even.
• Assume 0 based indexing.
13. Input Specification:
Input1:A string S, representing the initial string
input2: An integer M, representing size of the array.
input3: An integer array A, where each element represents the
number of the operation that is to be performed.
output Specification:
Return the final string value that will be obtained after performing
all M operations in the given sequence.
Example 1:
input1: ABCD
input2: 3
input3: (1,2,1)
Output: BADC
14. Explanation:
Here in this example, on the string "ABCD" we can perform the
sequence of operations in the following way:
• The first operation is of type 1, we convert "ABCD" to "DBCA“
• The second operation is of type 2, we convert "DBCA" to
"CADB".ntanm
• The third operation is of type 1, we convert "CADB" to
"BADC"String.
Therefore, the final string obtained after performing all the
operations is "BADC". Hence, BADC is returned as the output.
Example 2:
input1 : MERCER
input2 : 3
input3: (1,1,2)
Output: CERMER
15. import java.util.*;
class Main {
public static String stringSwap(String str, int arr[], int n) {
char[] ch = str.toCharArray();
int len = str.length();
for(int i = 0; i < n; i++) {
if(arr[i] == 1) {
char temp = ch[0];
ch[0] = ch[len-1];
ch[len-1] = temp;
}
if(arr[i] == 2) {
int half = len/2;
for(int j = 0; j < half; j++) {
char temp = ch[j];
ch[j] = ch[j+half];
ch[j+half] = temp;
}
16. }
}
String s = new String(ch);
return s;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println(stringSwap(str, arr, n));
}
}
17. 4. Magic String
Eva has a string S containing lowercase English letters. She wants to
transform this string into a Magic String, where all the characters in
the string are the same. To do so, she can replace any letter in the
string with another letter present in that string.
Your task is to help Eva find and return an integer value,
representing the minimum number of steps required to form a
Magic String. Return 0, if S is already a Magic String.
Input Specification:
input1: A string S containing lowercase English letters.
Output Specification:
Return an integer value, representing the minimum number of steps
required to form a Magic String. Return 0. if S is already a Magic
String
18. Example 1:
input1: aaabbbccdddd
Output: 8
Explanation:
Here in this example, the string S is aaabbbccdddd. We need perform
the following operations to make a Magic String:
1st way:
• On replacing the letters b, c d with a, the Magic string formed is
aaaaaaaaaaaa
• Here, the minimum number of steps to convert the original string
to Magic String is 9.
Optimal Way:
• On replacing the letters, a, b, c with d, the Magic string formed is
dddddddddddd.
19. • Here, the minimum number of steps required to form the Magic
String is 8.
Since 8 is smaller than 9. 8 is returned as the output.
Example 2:
Input 1: aaaa
Output: 0
20. import java.util.*;
class Main {
static final int SIZE = 26;
static void printCharWithFreq(String str) {
int n = str.length();
int[] freq = new int[SIZE];
for (int i = 0; i < n; i++) {
freq[str.charAt(i) - 'a']++;
}
int max = -1;
for (int i = 0; i < n; i++) {
if (freq[str.charAt(i) - 'a'] != 0) {
if(max < freq[str.charAt(i) - 'a']) {
max = freq[str.charAt(i) - 'a'];
freq[str.charAt(i) - 'a'] = 0;
}
}
}
System.out.println(n - max);
}
21. public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
printCharWithFreq(str);
}
}