LPU Java Unit 2 Static Questions | LPUCOLAB

LPU Java Unit 2 Static Questions | LPUCOLAB

LPU_Java_Unit 2_Static Questions


1. Problem Statement

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:

  • Element 16 is not greater than the sum of elements to its right (28 + 74 + 19 + 25 + 11 = 157).
  • Element 28 is not greater than the sum of elements to its right (74 + 19 + 25 + 11 = 129).
  • Element 74 is greater than the sum of elements to its right (19 + 25 + 11 = 55).
  • Element 19 is not greater than the sum of elements to its right (25 + 11 = 36).
  • Element 25 is greater than the sum of elements to its right (11).
  • The last element, 11, is always a leader.

So, the output is {74, 25, 11}.

Input format:

  • The first line contains an integer N, representing the number of elements in the array.
  • The second line consists of 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] + " ");
        }
    }
}

2. Problem Statement

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:

  • The first line contains an integer N, representing the number of monthly profit/loss records.
  • The second line consists of 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;
    }
}

3. Problem Statement

Meteorologist Seetha uses method overloading to determine weather changes. She has a utility class, StringOps, to perform various operations on weather condition strings.

  • If one string is input, it is reversed.
  • If two strings are input, they are concatenated without spaces.
  • If more than two strings are input, the output is "Invalid input".

Input format:

  • One or two space-separated strings.
  • More than two strings will output "Invalid input".

Output format:

  • If one string: Reversed version of the string.
  • If two strings: Concatenation of both strings without spaces.
  • If more than two strings: "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";
        }
    }
}

4. Problem Statement

You are building a chat application that encodes and decodes messages for efficient storage and transmission.

Input format:

  • For encoding: A sequence of strings separated by a delimiter #.
  • For decoding: A single encoded string.

Output format:

  • For encoding: A single string representing the encoded data.
  • For decoding: A sequence of strings separated by the delimiter.

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("#");
    }
}

5. Problem Statement

Elisa, a language processing specialist, needs to convert strings to character arrays for analysis.

Input format:

  • A single string to be converted to a character array.

Output format:

  • The output prints a character array derived from the string.

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