Litcode Labs
Litcode Labs
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int a = input.nextInt();
int b = input.nextInt();
System.out.println(ret);
return x*y;
}
CS_JAVA_Lab 1_0924
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;
int n = input.nextInt();
arr[i] = input.nextInt();
doSomething(arr);
if (num > 0) {
positiveCount++;
negativeCount++;
} else {
zeroCount++;
}
CS_JAVA_Lab 2_0924
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.io.*;
import java.util.*;
if (availableHours < 4) {
System.out.println("Invalid Input");
return;
// Each device takes 4 hours to be tested, so calculate how many devices can be tested
// Ensure remaining devices can't be negative (if all devices are tested)
if (remainingDevices < 0) {
remainingDevices = 0;
// If the number of tested devices exceeds the total devices, adjust the output
System.out.println(devicesTested);
System.out.println(remainingDevices);
}
CS_JAVA_Lab 3_0924
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
return;
// Initialize array and check if all elements are integers between -10 and 10
try {
arr[i] = Integer.parseInt(tokens[i]);
return;
} catch (NumberFormatException e) {
// Call the function to check for zero-sum subarray and print results
System.out.println(arr.length);
// Function to check for zero-sum subarray using prefix sum and HashSet
int prefixSum = 0;
// Add the initial prefix sum (zero) to handle the case where a subarray starting from index 0 has sum zero
prefixSums.add(0);
prefixSum += num;
// If the prefix sum has been seen before, there is a zero-sum subarray
if (prefixSums.contains(prefixSum)) {
return true;
prefixSums.add(prefixSum);
return false;
}
CS_JAVA_Lab 4_0924
import java.io.*;
import java.util.*;
// Reading the input as a string to validate length and check if it's an integer
if (inputString.length() < 4) {
return;
return;
try {
if (number < 0) {
return;
}
System.out.println(encryptedNumber);
} catch (NumberFormatException e) {
// Swap the first digit with the third, and the second with the fourth
digits[0] = digits[2];
digits[2] = temp;
temp = digits[1];
digits[1] = digits[3];
digits[3] = temp;
// Reconstruct the number
int encryptedNumber = 0;
return encryptedNumber;
CS_JAVA_Lab 5_0924
import java.io.*;
import java.util.*;
// Convert cumulative sum to alphabet if it's odd, otherwise use the number itself
if (cumulativeSum % 2 != 0) {
} else {
System.out.println(pin.toString());
int sum = 0;
num /= 10;
return (char) ('a' + num - 1); // 1 -> 'a', 3 -> 'c', 5 -> 'e', etc.
}
CS_JAVA_Lab 6_0924
import java.io.*;
import java.util.*;
// Find and print the largest special prime less than num
System.out.println(largestSpecialPrime);
// Function to find the largest special prime less than a given number
return i;
if (num <= 1) {
return false;
return false;
return true;
if (!isPrime(subNum)) {
}
CS_JAVA_Lab 7_0924
import java.io.*;
import java.util.*;
// Input: divisor
int k = input.nextInt();
int n = input.nextInt();
arr[i] = input.nextInt();
System.out.println(result);
int count = 0;
count++;
return count;
CS_JAVA_Lab 8_0924
import java.io.*;
import java.util.*;
arr[i] = Integer.parseInt(sightings[i]);
System.out.println(result);
int maxFreq = 0;
int mostCommonPlant = Integer.MAX_VALUE; // Use MAX_VALUE to find the smallest ID in case of ties
maxFreq = currentFreq;
mostCommonPlant = plantID;
return mostCommonPlant;
}
CS_JAVA_Lab 9_0924
import java.util.*;
charlie[i] = Integer.parseInt(charlieInput[i]);
dave[i] = Integer.parseInt(daveInput[i]);
int charliePoints = 0;
int davePoints = 0;
// Compare each corresponding score
charliePoints++;
davePoints++;
CS_JAVA_Lab 10_0924
import java.util.*;
int n = input.nextInt();
int q = input.nextInt();
// Initialize the array with 0's, note the array is 1-indexed, but we use 0-indexed internally.
arr[start] += value;
if (end + 1 < n) {
arr[end + 1] -= value;
int currentSum = 0;
currentSum += arr[i];
maxValue = currentSum;
System.out.println(maxValue);
}
CS_JAVA_Lab 11_0924
import java.util.*;
arr1[i] = input.nextInt();
arr2[i] = input.nextInt();
arr3[i] = input.nextInt();
}
public static void findCommonElements(int[] arr1, int[] arr2, int[] arr3) {
int i = 0, j = 0, k = 0;
found = true;
i++;
j++;
k++;
i++;
j++;
} else {
k++;
if (!found) {
System.out.println("No Elements");
}
CS_JAVA_Lab 12_0924
import java.util.*;
class CustomStack {
public CustomStack() {
text.append(value);
} else {
if (!history.isEmpty()) {
switch (cmdType) {
editor.insert(parts[1]);
break;
editor.delete(Integer.parseInt(parts[1]));
break;
break;
editor.undo();
break;
CS_JAVA_Lab 13_0924
import java.util.*;
class CustomQueue {
public CustomQueue() {
// Enqueue operation
stack1.push(x);
// Dequeue operation
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
if (!stack2.isEmpty()) {
stack2.pop();
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
queue.enqueue(Integer.parseInt(parts[1]));
break;
queue.dequeue();
break;
if (front != -1) {
System.out.println(front);
break;
CS_JAVA_Lab 14_0924
import java.util.*;
System.out.println(result);
if (start.length() != end.length()) {
return false;
countStart[getIndex(c)]++;
countEnd[getIndex(c)]++;
switch (c) {
import java.io.*;
import java.util.*;
int n = input.nextInt();
int m = input.nextInt();
System.out.println(ret);
if (m == 0) return 0;
if (m == 1) return 1;
// Find the number of unstable wall configurations for a wall of height N of varying widths
unstable[0] = 0;
unstable[1] = 0;
long result = 0;
unstable[i] = result;
long result = 1;
while (b > 0) {
if ((b & 1) == 1) {
a = (a * a) % mod;
b >>= 1;
return result;
CS_JAVA_Lab 16_0924
import java.util.*;
candies.offer(Integer.parseInt(candy));
}
int steps = calculateStepsToReachTarget(candies, targetSweetness);
System.out.println(steps);
int steps = 0;
// If we exit the loop, check if the remaining candy meets the target