1) CHARACTER COUNT:
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String input1 =sc.nextLine();
int input2=sc.nextInt();
String input3= sc.next();
int result = countCharacter(input1,input2,input3);
System.out.println(result);
}
public static int countCharacter(String input1,int input2,String input3){
if(input2 > input1.length()){
input2=input1.length();
}
String subString = input1.substring(0,input2);
char targetChar = input3.charAt(0);
int count=0;
for(char ch:subString.toCharArray()){
if(ch == targetChar){
count++;
}
}
return count;
}
}
2)ARCTIC TEMPERATURE TRACKER:
public class ArcticTemperatureTracker {
public static int longestDropPeriod(int[] input1, int input2) {
int currentStreak = 1;
int longestStreak = 0;
for (int i = 1; i < input2; i++) {
if (input1[i] < input1[i - 1]) {
currentStreak++;
} else {
if (currentStreak >= 3) {
longestStreak = Math.max(longestStreak, currentStreak);
}
currentStreak = 1;
}
}
// Final check after the loop in case the longest streak is at the end
if (currentStreak >= 3) {
longestStreak = Math.max(longestStreak, currentStreak);
}
return longestStreak;
}
public static void main(String[] args) {
// Example inputs
int[] input1 = {10, 9, 8, 7, 8, 6, 5, 4, 3};
int input2 = input1.length;
// Output result
System.out.println(longestDropPeriod(input1, input2)); // Expected output: 4
}
}
3)VOWEL PERMUTATION:
import java.util.*;
public class VowelPermutation {
public static int vowelPermutation(String input1) {
int consonants = 0;
for (char ch : input1.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(ch) == -1 && Character.isLetter(ch)) {
consonants++;
}
}
if (consonants == 0) {
return 0;
}
return factorial(consonants);
}
private static int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
String input1 = sc.nextLine();
System.out.println(vowelPermutation(input1));
}
}
4)WHITE SPACE DIFFERENCE:
public class Main {
public static String difference(String input1, String input2) {
int count1 = countWhitespaces(input1);
int count2 = countWhitespaces(input2);
int difference = Math.abs(count1 - count2);
if (difference % 2 == 0) {
return "Even" + difference;
} else {
return "Odd" + difference;
}
}
private static int countWhitespaces(String str) {
int count = 0;
for (char ch : str.toCharArray()) {
if (ch == ' ') {
count++;
}
}
return count;
}
public static void main(String[] args) {
String input1 = "He ll o W or ld";
String input2 = "Hello World";
System.out.println(difference(input1, input2));
}
Working Example:
For input1 = "He ll o W or ld" and input2 = "Hello World":
o countWhitespaces(input1) will return 5 because there are 5 spaces.
o countWhitespaces(input2) will return 1 because there is only 1 space.
o The absolute difference is Math.abs(5 - 1) = 4, which is even.
o Therefore, the method will return "Even4".
5) REBOUND HEIGHT:
import java.util.Scanner;
public class Solution {
public static int rebound(int input1, int input2, int input3) {
double e = (double) input3 / input2;
double reboundHeight = input1 * Math.pow(e, 2);
return (int) reboundHeight;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input1 = scanner.nextInt();
int input2 = scanner.nextInt();
int input3 = scanner.nextInt();
int result = rebound(input1,input2,input3);
System.out.println(result);
scanner.close();
}
}
Output:
Enter initial height (H): 100
Enter initial velocity (V): 20
Enter final velocity (Vf): 10
The rebound height after N bounces is: 25
6)POET AND RHYMES:
import java.util.Scanner;
public class RhymingWords {
public static String rhymingWords(String input1, String[] input2, int input3) {
String res = "No Word";
int maxVal = 0;
for (String word : input2) {
if (!word.equals(input1)) {
String s = new StringBuilder(input1).reverse().toString();
String t = new StringBuilder(word).reverse().toString();
int curr = 0;
int i = 0;
while (i < Math.min(s.length(), t.length())) {
if (s.charAt(i) == t.charAt(i)) {
curr++;
i++;
} else {
break;
}
}
if (curr > maxVal) {
maxVal = curr;
res = word;
}
}
}
return res;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input1 = sc.nextLine(); // word given
int input3 = sc.nextInt(); // length of dictionary
sc.nextLine(); // consume the newline
String[] input2 = sc.nextLine().split(" "); // dictionary words
System.out.println(rhymingWords(input1, input2, input3));
sc.close();
}
}
Output:
stone
5
bone clone phone tone zone
Tone
7)ISLAND LIFE:
import java.util.Scanner;
public class IslandLife {
public static int minimumBoxes(int input1, int input2, int input3) {
// Calculate the number of days on which you can buy boxes (6 days per week)
int buyDays = (input3 / 7) * 6 + Math.min(input3 % 7, 6);
// Total sweets required to survive S days
int totalSweets = input2 * input3;
// Check if the number of sweets required is more than what can be bought
if (totalSweets > buyDays * input1) {
return -1;
}
int minBoxes = (int) Math.ceil((double) totalSweets / input1);
return minBoxes;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input1 = sc.nextInt();
int input2 = sc.nextInt();
int input3 = sc.nextInt();
int result = minimumBoxes(input1, input2, input3);
System.out.println(result);
}
}
Output:
10
2
7
2
8) NAME ENTRY:
import java.util.Scanner;
public class NameFormatter {
public static String formatName(String input1, String input2) {
// Convert first name to lowercase and last name to uppercase
String formattedFirstName = input1.toLowerCase();
String formattedLastName = input2.toUpperCase();
// Return the formatted name
return formattedFirstName + " " + formattedLastName;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first name: ");
String input1 = sc.nextLine();
System.out.print("Enter last name: ");
String input2 = sc.nextLine();
// Call the formatName function and print the result
String result = formatName(input1, input2);
System.out.println("Formatted Name: " + result);
}
}
Output:
Enter first name: VIRat
Enter last name: koHLi
Formatted Name: virat KOHLI
9) FIND DIVIDEND:
public class Main {
public static int findDividendIndex(int[] array, int divisor, int quotient, int
remainder, int length) {
int dividend = divisor * quotient + remainder;
return searchDividendIndex(array, dividend, length);
}
private static int searchDividendIndex(int[] array, int dividend, int length) {
for (int i = 0; i < length; i++) {
if (array[i] == dividend) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = {10, 15, 20, 25, 30};
int divisor = 5;
int quotient = 3;
int remainder = 0;
int length = array.length;
int index = findDividendIndex(array, divisor, quotient, remainder, length);
System.out.println("Index of dividend: " + index);
}
}
10) HALLOWEEN CANDLES
public class Main {
public static int maxCandies(int n, int[] prices, int money) {
int candiesCount = 0;
for (int i = 0; i < n; i++) {
if (prices[i] % 5 == 0) {
candiesCount++;
} else if (money >= prices[i]) {
money -= prices[i];
candiesCount++;
} else {
break;
}
}
return candiesCount;
}
public static void main(String[] args) {
int n = 3;
int[] prices = {5, 5, 105};
int money = 16;
int result = maxCandies(n, prices, money);
System.out.println("Maximum number of candies Bob can buy: " + result);
}
}
11) CLEAN STRING:
public class CleanString {
// Function to remove characters in B from string A
public static String getCleanString(String input1, String input2) {
// Convert B to a set of characters for quick lookup
boolean[] charsIninput2 = new boolean[26];
for (char ch : input2.toCharArray()) {
charsIninput2[ch - 'A'] = true; // Mark character presence in B
}
// Build the result with characters from A that aren't in B
StringBuilder result = new StringBuilder();
for (char ch : input1.toCharArray()) {
if (!charsIninput2[ch - 'A']) { // If character is not in B, add to result
result.append(ch);
}
}
return result.length() > 0 ? result.toString() : "Empty"; // Return empty string if
result is empty
}
public static void main(String[] args) {
// Test case 1
String input1 = "HELLO";
String input2 = "LO";
System.out.println( getCleanString(input1, input2)); // Expected: "HE"
}
}
Output:
Enter string A: ABCDEF
Enter string B: BDF
Result: ACE
Enter string A: HELLO
Enter string B: HELLO
Result: Empty
12) REVERSE ARRAY:
import java.util.Scanner;
public class ReverseArray {
public static int sumEvenPositions(int[] input1, int input2) {
int sum = 0;
for (int i = 0; i < input2 / 2; i++) {
int temp = input1[i];
input1[i] = input1[input2 - i - 1];
input1[input2 - i - 1] = temp;
}
for (int i = 0; i < input2; i += 2) {
sum += input1[i];
}
return sum;
}
public static void main(String[] args) {
int[] input1 = {10, 20, 30, 40, 50, 60};
int input2 = 6;
// Expected Output: Sum of elements at even positions in reversed array
int result = sumEvenPositions(input1, input2);
System.out.println(result);
}
}
OUTPUT : 120
13) MAXIMIZE PAIR PRODUCT:
import java.util.*;
public class Main {
public static void main(String[] args) {
// Example usage:
int input1 = 6;
int[] input2 = {10,8,12,6,9,9};
int[] result = findPairWithMaxProduct(input1,input2);
if (result != null) {
System.out.println("("+result[0]+","+result[1]+")");
} else {
System.out.println("No valid pair found.");
}
}
public static int[] findPairWithMaxProduct(int input1, int[] input2) {
int maxProduct = Integer.MIN_VALUE;
int[] bestPair = null;
for (int i = 0; i < input1; i++) {
for (int j = i + 1; j < input1; j++) {
int sum = input2[i] + input2[j];
int product = input2[i] * input2[j];
if (sum == 18 && input2[i] > input2[j] && product > maxProduct) {
maxProduct = product;
bestPair = new int[] {input2[i], input2[j]};
}
else{
bestPair = new int[] {input2[j], input2[i]};
}
}
}
return bestPair;
}
}
OUTPUT: (9,9)
14) CANOPY AREA:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int input1 = sc.nextInt();
int shadowArea = calculateCanopyShadowArea(input1);
System.out.println(shadowArea);
}
public static int calculateCanopyShadowArea(int input1) {
double pi = 3.141;
double area = pi * input1 * input1;
//return (int) Math.round(area);
return (int)area;
}
}
Output:
5
78