0% found this document useful (0 votes)
33 views8 pages

CRT - Assessment

Uploaded by

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

CRT - Assessment

Uploaded by

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

Problem Statement

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

Answer:(penalty regime: 10, 20, ... %)

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

Answer:(penalty regime: 10, 20, ... %)


import java.util.Scanner;

public class Main {

public static long factors(long n) {


if (n <= 1) {
return 0;
}

long sumFactors = 0;
long sqrtN = (int) Math.sqrt(n);

for (int i = 1; i <= sqrtN; i++) {


if (n % i == 0) {
sumFactors += i + n / i;
}
}

if (sqrtN * sqrtN == n) {
sumFactors -= sqrtN;
}

sumFactors -= n;
return sumFactors;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

// Read input
int n = scanner.nextInt();
long[] listOfNumbers = new long[n];
for (int i = 0; i < n; i++) {
listOfNumbers[i] = scanner.nextLong();
}

boolean flag = false;


for (long number : listOfNumbers) {
long factorSum = factors(number);
for (long value : listOfNumbers) {
if (factorSum == value) {
flag = true;
System.out.print(number + " ");
break;
}
}
}
if (!flag) {
System.out.println("-1");
}

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

Answer:(penalty regime: 10, 20, ... %)


import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();

Set<Character> set = new LinkedHashSet<>();


for (char c : input.toCharArray()) {
set.add(c);
}

List<Character> list = new ArrayList<>(set);


Collections.reverse(list);
StringBuilder result = new StringBuilder();
for (char c : list) {
result.append(c);
}

System.out.println(result.toString());
}
}

Problem Statement: Bracket Matching


Given a string containing brackets {}, (), and [], determine whether all the
brackets are properly matched. If all the brackets are matched, return 0. Otherwise,
return the position of the first mismatched bracket.
Input:
● A string s containing brackets {}, (), and [].

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;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int n = input.length();
char[] stack = new char[n];
int top = -1;
int mismatchedPosition = 0;

for (int i = 0; i < n; i++) {


char current = input.charAt(i);
if (current == '{' || current == '[' || current == '(') {
top++;
stack[top] = current;
} else if (top >= 0 && ((stack[top] == '{' && current ==
'}') || (stack[top] == '[' && current == ']') || (stack[top] == '(' &&
current == ')'))) {
stack[top] = '\0'; // Clear the matched character
top--;
} else {
mismatchedPosition = i + 1;
break;
}
}
System.out.println(mismatchedPosition);
}
}

You might also like