SlideShare a Scribd company logo
1 of 21
Virtusa content questions
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.
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)
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)
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] + " ");
}
}
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);
}
}
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.
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
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
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;
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));
}
}
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.
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
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
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;
}
}
}
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));
}
}
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
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.
• 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
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);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
printCharWithFreq(str);
}
}

More Related Content

Similar to Virtusa questions placement preparation guide

1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
aroraopticals15
 
Problem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdfProblem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdf
JUSTSTYLISH3B2MOHALI
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
vannagoforth
 

Similar to Virtusa questions placement preparation guide (20)

C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Problem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdfProblem 2.19. How much time is required to add or subtract two large.pdf
Problem 2.19. How much time is required to add or subtract two large.pdf
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
ARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptxARRAY OPERATIONS.pptx
ARRAY OPERATIONS.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdfAdvanced Datastructures and algorithms CP4151unit1b.pdf
Advanced Datastructures and algorithms CP4151unit1b.pdf
 
ObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docxObjectivesMore practice with recursion.Practice writing some tem.docx
ObjectivesMore practice with recursion.Practice writing some tem.docx
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
NUMPY-2.pptx
NUMPY-2.pptxNUMPY-2.pptx
NUMPY-2.pptx
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 

Recently uploaded

Top profile Call Girls In Belgaum [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Belgaum [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In Belgaum [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Belgaum [ 7014168258 ] Call Me For Genuine Models W...
nirzagarg
 
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
gajnagarg
 
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Menggugurkan Kandungan 087776558899
 
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
K Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CVK Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CV
K VENKAT NAVEEN KUMAR
 
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
nirzagarg
 
Baranagar ) High Class Call Girls Kolkata - Phone 8005736733 Escorts Service ...
Baranagar ) High Class Call Girls Kolkata - Phone 8005736733 Escorts Service ...Baranagar ) High Class Call Girls Kolkata - Phone 8005736733 Escorts Service ...
Baranagar ) High Class Call Girls Kolkata - Phone 8005736733 Escorts Service ...
HyderabadDolls
 
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 

Recently uploaded (20)

Top profile Call Girls In Belgaum [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Belgaum [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In Belgaum [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Belgaum [ 7014168258 ] Call Me For Genuine Models W...
 
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
Athwa gate \ Call Girls Service Ahmedabad - 450+ Call Girl Cash Payment 80057...
 
Cheap Call Girls Maharajganj { 9332606886 } VVIP NISHA Call Girls Near 5 Star...
Cheap Call Girls Maharajganj { 9332606886 } VVIP NISHA Call Girls Near 5 Star...Cheap Call Girls Maharajganj { 9332606886 } VVIP NISHA Call Girls Near 5 Star...
Cheap Call Girls Maharajganj { 9332606886 } VVIP NISHA Call Girls Near 5 Star...
 
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Anantapur [ 7014168258 ] Call Me For Genuine Models...
 
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In godhra [ 7014168258 ] Call Me For Genuine Models We...
 
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Ratnagiri [ 7014168258 ] Call Me For Genuine Models...
 
UXPA Boston 2024 Maximize the Client Consultant Relationship.pdf
UXPA Boston 2024 Maximize the Client Consultant Relationship.pdfUXPA Boston 2024 Maximize the Client Consultant Relationship.pdf
UXPA Boston 2024 Maximize the Client Consultant Relationship.pdf
 
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
Cara Gugurkan Kandungan Awal Kehamilan 1 bulan (087776558899)
 
9352852248 Call Girls Sanand Escort Service Available 24×7 In Sanand
9352852248 Call Girls  Sanand Escort Service Available 24×7 In Sanand9352852248 Call Girls  Sanand Escort Service Available 24×7 In Sanand
9352852248 Call Girls Sanand Escort Service Available 24×7 In Sanand
 
7737669865 Call Girls In Ahmedabad Escort Service Available 24×7 In In Ahmedabad
7737669865 Call Girls In Ahmedabad Escort Service Available 24×7 In In Ahmedabad7737669865 Call Girls In Ahmedabad Escort Service Available 24×7 In In Ahmedabad
7737669865 Call Girls In Ahmedabad Escort Service Available 24×7 In In Ahmedabad
 
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Shillong [ 7014168258 ] Call Me For Genuine Models ...
 
K Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CVK Venkat Naveen Kumar | GCP Data Engineer | CV
K Venkat Naveen Kumar | GCP Data Engineer | CV
 
Novo Nordisk Kalundborg. We are expanding our manufacturing hub in Kalundborg...
Novo Nordisk Kalundborg. We are expanding our manufacturing hub in Kalundborg...Novo Nordisk Kalundborg. We are expanding our manufacturing hub in Kalundborg...
Novo Nordisk Kalundborg. We are expanding our manufacturing hub in Kalundborg...
 
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Shivamogga [ 7014168258 ] Call Me For Genuine Model...
 
Baranagar ) High Class Call Girls Kolkata - Phone 8005736733 Escorts Service ...
Baranagar ) High Class Call Girls Kolkata - Phone 8005736733 Escorts Service ...Baranagar ) High Class Call Girls Kolkata - Phone 8005736733 Escorts Service ...
Baranagar ) High Class Call Girls Kolkata - Phone 8005736733 Escorts Service ...
 
TEST BANK For Growth and Development Across the Lifespan, 3rd Edition By Glor...
TEST BANK For Growth and Development Across the Lifespan, 3rd Edition By Glor...TEST BANK For Growth and Development Across the Lifespan, 3rd Edition By Glor...
TEST BANK For Growth and Development Across the Lifespan, 3rd Edition By Glor...
 
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime MysoreMysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
Mysore Escorts Service Girl ^ 9332606886, WhatsApp Anytime Mysore
 
B.tech civil major project by Deepak Kumar
B.tech civil major project by Deepak KumarB.tech civil major project by Deepak Kumar
B.tech civil major project by Deepak Kumar
 
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Jabalpur [ 7014168258 ] Call Me For Genuine Models ...
 
Ganga Path Project (marine drive project) Patna ,Bihar .pdf
Ganga Path Project (marine drive project) Patna ,Bihar .pdfGanga Path Project (marine drive project) Patna ,Bihar .pdf
Ganga Path Project (marine drive project) Patna ,Bihar .pdf
 

Virtusa questions placement preparation guide

  • 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); } }