CRT - Assessment
CRT - Assessment
You are given a string which contains at least one special character, one even digit,
and one odd digit. Your task is to count the number of special characters in the
string. Depending on whether this count is even or odd, you should print all the even
and odd digits alternatively.
● If the count of special characters is even, print the digits starting with an even
digit.
● If the count of special characters is odd, print the digits starting with an odd
digit.
Input
● A single string s (1 ≤ ∣𝑠∣≤10^5), where s contains at least one special
character, one even digit, and one odd digit.
Output
● Print a single string representing the digits arranged according to the rules
specified.
Example
Example 1
Input:
A5c67r21i@p#8t
Output:
652781
Explanation
Example 1:
● The special characters are '@' and '#', making the count 2 (even).
● Since the count of special characters is even, we start with an even digit.
● The even digits in the string are 6, 2, and 8.
● The odd digits in the string are 5, 7, and 1.
● Arranging them alternatively starting with an even digit gives "652781".
Note
● Special characters are defined as any character that is not a letter (a-z, A-Z)
or a digit (0-9).
Constraints
The string will always contain at least one special character, one even digit, and one
odd digit.
Sample Testcases:
Input Result
A5c67r21i@p#8t 652781
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
List<Integer> even = new ArrayList<>();
List<Integer> odd = new ArrayList<>();
int specialCharacterCount = 0;
for (char c : input.toCharArray()) {
if (!Character.isLetterOrDigit(c)) {
specialCharacterCount++;
}
}
for (char c : input.toCharArray()) {
if (Character.isDigit(c)) {
int digit = Character.getNumericValue(c);
if (digit % 2 == 0) {
even.add(digit);
} else {
odd.add(digit);
}
}
}
if (specialCharacterCount % 2 != 0) {
List<Integer> temp = odd;
odd = even;
even = temp;
}
int maxLen = Math.max(odd.size(), even.size());
for (int b = 0; b < maxLen; b++) {
if (b < even.size()) {
System.out.print(even.get(b));
}
if (b < odd.size()) {
System.out.print(odd.get(b));
}
}
}
}
Problem Statement
You are given a list of numbers. For each number in the list, check if the sum of its
factors (excluding the number itself) is present in the list. If it is present, print the
number. If no such numbers exist, print -1.
Input
● The first line contains an integer 𝑛n (1 ≤ 𝑛≤10^4), representing the number of
elements in the list.
● The second line contains 𝑛n integers separated by spaces, representing the
elements of the list. Each element in the list is between 0 and 10^5.
Output
● Print the numbers whose sum of factors (excluding the number itself) is
present in the list, separated by spaces.
● If no such numbers exist, print -1.
Example
Example 1
Input:
3
0 1 6
Output:
0 1 6
Explanation
Example 1:
● For number 0, the sum of factors (excluding itself) is 0.
● For number 1, the sum of factors (excluding itself) is 0.
● For number 6, the sum of factors (excluding itself) is 1 + 2 + 3 = 6.
● All sums are present in the list, so the output is "0 1 6".
Note
● Factors of a number include all positive integers that divide the number
exactly.
● The sum of factors for each number should exclude the number itself.
Constraints
● 1≤𝑛≤10^4
● 0 ≤ elements of the list ≤ 10^5
Sample Testcases:
Input Result
3 0 1 6
0 1 6
long sumFactors = 0;
long sqrtN = (int) Math.sqrt(n);
if (sqrtN * sqrtN == n) {
sumFactors -= sqrtN;
}
sumFactors -= n;
return sumFactors;
}
// Read input
int n = scanner.nextInt();
long[] listOfNumbers = new long[n];
for (int i = 0; i < n; i++) {
listOfNumbers[i] = scanner.nextLong();
}
scanner.close();
}
}
Problem Statement:
Given a string, remove duplicate characters and reverse it.
Input:
● A string str of length 𝑛 (1≤𝑛≤10^5).
Output:
● A string representing the result after removing duplicates and reversing the
input string.
Example:
Input:
google
Output:
elog
Input:
infosys
Output:
ysofni
Sample Testcases:
Input Result
google elog
System.out.println(result.toString());
}
}
Output:
● Return 0 if all the brackets are properly matched. Otherwise, return the
position of the first mismatched bracket (1-indexed).
Constraints:
● 1≤∣𝑠∣≤10^5
● The string s may contain any combination of brackets {}, (), and [].
Example:
Input:
{([])}
Output:
0
Input:
{[[]])}
Output:
6
Sample Testcases:
Input Result
{[[]])} 6
Answer:(penalty regime: 10, 20, ... %)
import java.util.Scanner;