Robin is a tech-savvy teenager diving into programming. He is working on a project to find special elements in an array called 'leaders.' Leaders are exceptional elements greater than the sum of all the elements to their right.
Example
Input:
6
16 28 74 19 25 11
Output:
74 25 11
Explanation:
So, the output is {74, 25, 11}
.
Input format:
N
, representing the number of elements in the array.N
space-separated integers, representing the elements of the array.Output format:
The output prints the special elements in the array that are greater than the sum of all elements to their right.
Solution:
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();
}
for (int i = 0; i < N; i++) {
int sumRight = 0;
// Calculate the sum of elements to the right of arr[i]
for (int j = i + 1; j < N; j++) {
sumRight += arr[j];
}
if (arr[i] > sumRight) {
System.out.print(arr[i] + " ");
}
}
}
Emily runs a bakery and tracks her monthly profits and losses. She wants to calculate her maximum net profit by removing one continuous subarray (one or more consecutive months) with the highest loss (i.e., the subarray with the smallest sum).
Example
Input:
6
1000 -200 300 -500 400 300
Output:
1800
Explanation:
The worst-performing month was with a loss of 500. After ignoring that month, the net profit is:
1000 - 200 + 300 + 400 + 300 = 1800
.
Input format:
N
, representing the number of monthly profit/loss records.N
space-separated integers, where negative values represent losses.Output format:
The output prints a single integer representing the net profit.
Solution:
class ExpenseAnalyzer {
public int calculateMaxRemainingBudget(int[] expenses) {
int n = expenses.length;
int totalSum = 0;
for (int i = 0; i < n; i++) {
totalSum += expenses[i];
}
int minSubarraySum = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
int currentSum = 0;
for (int j = i; j < n; j++) {
currentSum += expenses[j];
if (currentSum < minSubarraySum) {
minSubarraySum = currentSum;
}
}
}
if (minSubarraySum == totalSum) {
return totalSum;
}
return totalSum - minSubarraySum;
}
}
Meteorologist Seetha uses method overloading to determine weather changes. She has a utility class, StringOps
, to perform various operations on weather condition strings.
"Invalid input"
.Input format:
"Invalid input"
.Output format:
"Invalid input"
.Solution:
class StringOps {
public String manipulate(String[] input) {
if (input.length == 1) {
return new StringBuilder(input[0]).reverse().toString();
} else if (input.length == 2) {
return input[0] + input[1];
} else {
return "Invalid input";
}
}
}
You are building a chat application that encodes and decodes messages for efficient storage and transmission.
Input format:
#
.Output format:
Solution:
class EncoderDecoder {
public String encode(String[] messages) {
StringBuilder encoded = new StringBuilder();
for (int i = 0; i < messages.length; i++) {
encoded.append(messages[i]);
if (i < messages.length - 1) {
encoded.append("#");
}
}
return encoded.toString();
}
public String[] decode(String encoded) {
return encoded.split("#");
}
}
Elisa, a language processing specialist, needs to convert strings to character arrays for analysis.
Input format:
Output format:
Solution:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
char[] charArray = input.toCharArray();
System.out.println(Arrays.toString(charArray));
scanner.close();
}