Skip to main content

AU2340217_Devang_Satishbhai_Parmar

Submission at 2024-08-05 10:21:47

// Write your Java code here from the scratc

import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
String name = input.next();
System.out.println("Hello "+name+"!");
}
}

Submission at 2024-08-05 10:22:13

// Write your Java code here from the scratc

import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
String name = input.next();
System.out.println("Hello "+name+"!");
}
}

Submission at 2024-08-05 10:30:16

// Write your Java code here from the scratch
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String Name = input.next();
System.out.println("Hello "+Name+"!");
}
}

Submission at 2024-08-05 10:38:12

// Write your Java code here
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();

String n1 = input.next();
String n2 = input.next();
String n3 = input.next();

System.out.println(n);
System.out.println("Hello "+n1+"!");
System.out.println("Hello "+n2+"!");
System.out.println("Hello "+n3+"!");


}
}

Submission at 2024-08-05 10:39:08

// Write your Java code here
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();

String n1 = input.next();
String n2 = input.next();
String n3 = input.next();

System.out.println(n);
System.out.println("Hello "+n1+" !");
System.out.println("Hello "+n2+" !");
System.out.println("Hello "+n3+" !");


}
}

Submission at 2024-08-05 10:43:35

// Write your Java code here
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();


for(int i=0;i<n;i++){
String name = input.next();
System.out.println("Hello "+name+" !")
}


}
}

Submission at 2024-08-05 10:45:27

// Write your Java code here
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();


for(int i=0;i<n;i++){
String name = input.next();
System.out.println("Hello "+name+" !")
}


}
}

Submission at 2024-08-05 10:46:05

// Write your Java code here
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();


for(int i=0;i<n;i++){
String name = input.next();
System.out.println("Hello "+name+" !");
}


}
}

Submission at 2024-08-05 10:47:18

// Write your Java code here
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();


for(int i=0;i<n;i++){
String name = input.next();
System.out.println("Hello "+name+"!");
}


}
}

Submission at 2024-08-05 11:01:06

// Write your Java code here from the scratch
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String name = input.next();
System.out.println("Hello "+name+"!");
}
}

Submission at 2024-08-12 09:58:11

import java.io.*;
import java.util.*;

class Main {
public static void main(String args[]) throws IOException {
// Set up the input stream
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);

// Read the input
int x = Integer.parseInt(br.readLine().trim());

// Calculate and print the Fibonacci number for the input x
System.out.println(fibonacci(x));
}

// Method to calculate the x-th Fibonacci number
private static int fibonacci(int x) {
// Write your code here
if (x <2) {
return n;
} else {
return calculateFibonacchi(x - 1) + calculateFibonacchi(x - 2);

}
}
}

Submission at 2024-08-12 09:58:51

import java.io.*;
import java.util.*;

class Main {
public static void main(String args[]) throws IOException {
// Set up the input stream
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);

// Read the input
int x = Integer.parseInt(br.readLine().trim());

// Calculate and print the Fibonacci number for the input x
System.out.println(fibonacci(x));
}

// Method to calculate the x-th Fibonacci number
private static int fibonacci(int x) {
// Write your code here
if (x <2) {
return x;
} else {
return calculateFibonacchi(x - 1) + calculateFibonacchi(x - 2);

}
}
}

Submission at 2024-08-12 10:01:04

import java.io.*;
import java.util.*;

class Main {
public static void main(String args[]) throws IOException {
// Set up the input stream
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);

// Read the input
int x = Integer.parseInt(br.readLine().trim());

// Calculate and print the Fibonacci number for the input x
if(x>30){
System.out.println("Not valid";)
}else{
System.out.println(fibonacci(x));
}
}

// Method to calculate the x-th Fibonacci number
private static int fibonacci(int x) {
// Write your code here
if (x <2) {
return x;
} else {
return calculateFibonacchi(x - 1) + calculateFibonacchi(x - 2);

}
}
}

Submission at 2024-08-12 10:02:36

import java.io.*;
import java.util.*;

class Main {
public static void main(String args[]) throws IOException {
// Set up the input stream
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);

// Read the input
int x = Integer.parseInt(br.readLine().trim());

// Calculate and print the Fibonacci number for the input x
if(x>30){
System.out.println("Not valid");
}else{
System.out.println(fibonacci(x));
}
}

// Method to calculate the x-th Fibonacci number
private static int fibonacci(int x) {
// Write your code here
if (x <2) {
return x;
} else {
return calculateFibonacchi(x - 1) + calculateFibonacchi(x - 2);

}
}
}

Submission at 2024-08-12 10:04:03

import java.io.*;
import java.util.*;

class Main {
public static void main(String args[]) throws IOException {
// Set up the input stream
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);

// Read the input
int x = Integer.parseInt(br.readLine().trim());

// Calculate and print the Fibonacci number for the input x
if(x>30){
System.out.println("Not valid");
}else{
System.out.println(fibonacci(x));
}
}

// Method to calculate the x-th Fibonacci number
private static int fibonacci(int x) {
// Write your code here
if (x <2) {
return x;
} else {
return fibonacci(x - 1) + fibonacci(x - 2);

}
}
}

Submission at 2024-08-12 10:28:30

import java.io.*;

class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// Read the input
int n = Integer.parseInt(br.readLine().trim());

// Determine if n is a power of two
System.out.println(isPowerOfTwo(n));
}

// Method to check if n is a power of two
private static boolean isPowerOfTwo(int n) {
// write your logic here
if(n==0) {
return false;
}
if(n==1) {
return true;
}
if(n%2==0){
return isPowerOfTwo(n/2);
}
return false;

}
}

Submission at 2024-08-12 10:29:30

import java.io.*;

class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// Read the input
int n = Integer.parseInt(br.readLine().trim());

// Determine if n is a power of two
System.out.println(isPowerOfTwo(n));
}

// Method to check if n is a power of two
private static boolean isPowerOfTwo(int n) {
// write your logic here
if(n==0) {
return false;
}
if(n==1) {
return true;
}
if(n%2==0){
return isPowerOfTwo(n/2);
}
return false;

}
}

Submission at 2024-08-12 10:31:34

import java.io.*;
import java.util.*;

class Main {
public static void main(String args[]) throws IOException {
// Set up the input stream
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);

// Read the input
int x = Integer.parseInt(br.readLine().trim());

// Calculate and print the Fibonacci number for the input x
if(x>30){
System.out.println("Not valid");
}else{
System.out.println(fibonacci(x));
}
}

// Method to calculate the x-th Fibonacci number
private static int fibonacci(int x) {
// Write your code here
if (x <2) {
return x;
} else {
return fibonacci(x - 1) + fibonacci(x - 2);

}
}
}

Submission at 2024-08-12 10:32:55

import java.io.*;

class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// Read the input
int n = Integer.parseInt(br.readLine().trim());

// Determine if n is a power of two
System.out.println(isPowerOfTwo(n));
}

// Method to check if n is a power of two
private static boolean isPowerOfTwo(int n) {
// write your logic here
if(n==0) {
return false;
}
if(n==1) {
return true;
}
if(n%2==0){
return isPowerOfTwo(n/2);
}
return false;

}
}

Submission at 2024-08-17 07:11:14

import java.util.*;
import java.io.*;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().trim().split("\\s+");
int n = Integer.parseInt(input[0]);
int k = Integer.parseInt(input[1]);

// Generate combinations
List<List<Integer>> result = combine(n, k);

// Print combinations
System.out.print("[");
for (int i = 0; i < result.size(); i++) {
System.out.print("[");
for (int j = 0; j < result.get(i).size(); j++) {
System.out.print(result.get(i).get(j));
if (j < result.get(i).size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i != result.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
}

public static List<List<Integer>> combine(int n, int k) {
List<List<Integer>> result = new ArrayList<>();
if (k > n || k <= 0) {
return result; // No combinations possible
}
backtrack(1, new ArrayList<>(), result, n, k);
return result;
}

private static void backtrack(int start, List<Integer> comb, List<List<Integer>> res, int n, int k) {
if (comb.size() == k) {
res.add(new ArrayList<>(comb));
return;
}
for (int i = start; i <= n; i++) {
comb.add(i);
backtrack(i + 1, comb, res, n, k);
comb.remove(comb.size() - 1);
}
}
}

Submission at 2024-08-17 07:14:17

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();

List<List<Integer>> result = combine(n, k);

Collections.sort(result, new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> a, List<Integer> b) {
if (a.size() != b.size()) {
return Integer.compare(a.size(), b.size());
}
for (int i = 0; i < a.size(); i++) {
int comparison = Integer.compare(a.get(i), b.get(i));
if (comparison != 0) {
return comparison;
}
}
return 0;
}
});
System.out.print("[");
for (int i = 0; i < result.size(); i++) {
System.out.print("[");
for (int j = 0; j < result.get(i).size(); j++) {
System.out.print(result.get(i).get(j));
if (j < result.get(i).size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i != result.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
}

public static List<List<Integer>> combine(int n, int k) {
if (k > n) {
return new ArrayList<>();
}
List<List<Integer>> result = new ArrayList<>();
backtrack(1, new ArrayList<>(), result, n, k);
return result;
}
private static void backtrack(int start, List<Integer> comb, List<List<Integer>> res, int n, int k) {
if (comb.size() == k) {
res.add(new ArrayList<>(comb));
return;
}
for (int i = start; i <= n; i++) {
comb.add(i);
backtrack(i + 1, comb, res, n, k);
comb.remove(comb.size() - 1);
}
}
}


Submission at 2024-08-17 07:17:18

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();

List<List<Integer>> result = combine(n, k);

Collections.sort(result, new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> a, List<Integer> b) {
if (a.size() != b.size()) {
return Integer.compare(a.size(), b.size());
}
for (int i = 0; i < a.size(); i++) {
int comparison = Integer.compare(a.get(i), b.get(i));
if (comparison != 0) {
return comparison;
}
}
return 0;
}
});
System.out.print("[");
for (int i = 0; i < result.size(); i++) {
System.out.print("[");
for (int j = 0; j < result.get(i).size(); j++) {
System.out.print(result.get(i).get(j));
if (j < result.get(i).size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i != result.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
}

public static List<List<Integer>> combine(int n, int k) {
if (k > n) {
return new ArrayList<>();
}
List<List<Integer>> result = new ArrayList<>();
backtrack(1, new ArrayList<>(), result, n, k);
return result;
}
private static void backtrack(int start, List<Integer> comb, List<List<Integer>> res, int n, int k) {
if (comb.size() == k) {
res.add(new ArrayList<>(comb));
return;
}
for (int i = start; i <= n; i++) {
comb.add(i);
backtrack(i + 1, comb, res, n, k);
comb.remove(comb.size() - 1);
}
}
}


Submission at 2024-08-17 07:19:40

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

private static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}


Submission at 2024-08-17 07:21:09

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

private static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}


Submission at 2024-08-17 07:21:51

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

private static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}


Submission at 2024-08-17 07:23:26

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

private static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}


Submission at 2024-08-17 07:24:07

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

public static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}


Submission at 2024-08-17 07:25:36

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

public static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}


Submission at 2024-08-17 07:26:12

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

private static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}


Submission at 2024-08-17 07:27:56

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
if (n <= 0) {
return new ArrayList<>();
}
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

private static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}


Submission at 2024-08-20 04:35:29

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

private static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}

Submission at 2024-08-20 04:36:38

import java.util.*;

public class Main {
public static List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}

public static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == max * 2) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}
public static void main(String[] args) {
int n = 3;
List<String> result = generateParenthesis(n);
System.out.println(result);
}
}

Submission at 2024-08-20 08:00:22

import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 3;
List<String> result = parathesis(n);

StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < result.size(); i++) {
sb.append("\"").append(result.get(i)).append("\"");
if (i < result.size() - 1) {
sb.append(",");
}
}
sb.append("]");

// Print the formatted result
System.out.println(sb.toString());
}

public static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == 2 * max) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}

public static List<String> parathesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}
}


Submission at 2024-08-20 08:02:11

import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 3;
List<String> result = parathesis(n);

StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < result.size(); i++) {
sb.append("\"").append(result.get(i)).append("\"");
if (i < result.size() - 1) {
sb.append(",");
}
}
sb.append("]");

// Print the formatted result
System.out.println(sb.toString());
}

public static void backtrack(List<String> result, String current, int open, int close, int max) {
if (current.length() == 2 * max) {
result.add(current);
return;
}
if (open < max) {
backtrack(result, current + "(", open + 1, close, max);
}
if (close < open) {
backtrack(result, current + ")", open, close + 1, max);
}
}

public static List<String> parathesis(int n) {
List<String> result = new ArrayList<>();
backtrack(result, "", 0, 0, n);
return result;
}
}


Submission at 2024-08-20 08:59:33

import java.util.ArrayList;
import java.util.List;

public class Main {

public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums);
return result;
}

private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
if (tempList.size() == nums.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < nums.length; i++) {
if (tempList.contains(nums[i])) continue; // Skip duplicates
tempList.add(nums[i]);
backtrack(result, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

public static void main(String[] args) {
int[] nums = {1, 2, 3}; // Sample input
List<List<Integer>> permutations = permute(nums);

System.out.println(permutations);
}
}

Submission at 2024-08-20 08:59:53

import java.util.ArrayList;
import java.util.List;

public class Main {

public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums);
return result;
}

private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
if (tempList.size() == nums.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < nums.length; i++) {
if (tempList.contains(nums[i])) continue; // Skip duplicates
tempList.add(nums[i]);
backtrack(result, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

public static void main(String[] args) {
int[] nums = {1, 2, 3}; // Sample input
List<List<Integer>> permutations = permute(nums);

System.out.println(permutations);
}
}

Submission at 2024-08-20 09:01:05

import java.util.ArrayList;
import java.util.List;

public class Main {

public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums);
return result;
}

private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
if (tempList.size() == nums.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < nums.length; i++) {
if (tempList.contains(nums[i])) continue; // Skip duplicates
tempList.add(nums[i]);
backtrack(result, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

public static void main(String[] args) {
int[] nums = {1, 2, 3}; // Sample input
List<List<Integer>> permutations = permute(nums);

System.out.println(permutations);
}
}

Submission at 2024-08-20 09:25:17

import java.util.ArrayList;
import java.util.List;

public class Main {

public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums);
return result;
}

private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
if (tempList.size() == nums.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < nums.length; i++) {
if (tempList.contains(nums[i])) continue; // Skip duplicates
tempList.add(nums[i]);
backtrack(result, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> permutations = permute(nums);

System.out.print("[");
for (int i = 0; i < permutations.size(); i++) {
List<Integer> permutation = permutations.get(i);
System.out.print("[");
for (int j = 0; j < permutation.size(); j++) {
System.out.print(permutation.get(j));
if (j < permutation.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i < permutations.size() - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
}

Submission at 2024-08-20 09:25:42

import java.util.ArrayList;
import java.util.List;

public class Main {

public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums);
return result;
}

private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
if (tempList.size() == nums.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < nums.length; i++) {
if (tempList.contains(nums[i])) continue; // Skip duplicates
tempList.add(nums[i]);
backtrack(result, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> permutations = permute(nums);

System.out.print("[");
for (int i = 0; i < permutations.size(); i++) {
List<Integer> permutation = permutations.get(i);
System.out.print("[");
for (int j = 0; j < permutation.size(); j++) {
System.out.print(permutation.get(j));
if (j < permutation.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i < permutations.size() - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
}

Submission at 2024-08-20 09:27:26

import java.util.ArrayList;
import java.util.List;

public class Main {

public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums);
return result;
}

private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
if (tempList.size() == nums.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < nums.length; i++) {
if (tempList.contains(nums[i])) continue; // Skip duplicates
tempList.add(nums[i]);
backtrack(result, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> permutations = permute(nums);

System.out.print("[");
for (int i = 0; i < permutations.size(); i++) {
List<Integer> permutation = permutations.get(i);
System.out.print("[");
for (int j = 0; j < permutation.size(); j++) {
System.out.print(permutation.get(j));
if (j < permutation.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i < permutations.size() - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
}

Submission at 2024-08-20 09:53:04

import java.util.*;

public class Main {

public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backtrack(result, new ArrayList<>(), nums);
return result;
}

private static void backtrack(List<List<Integer>> result, List<Integer> tempList, int[] nums) {
if (tempList.size() == nums.length) {
result.add(new ArrayList<>(tempList));
} else {
for (int i = 0; i < nums.length; i++) {
if (tempList.contains(nums[i])) continue; // Skip duplicates
tempList.add(nums[i]);
backtrack(result, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

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

// Convert input string to an int array
String[] strArray = str.split(" ");
int[] nums = new int[strArray.length];
for (int i = 0; i < strArray.length; i++) {
nums[i] = Integer.parseInt(strArray[i]);
}

List<List<Integer>> permutations = permute(nums);

// Print permutations
System.out.print("[");
for (int i = 0; i < permutations.size(); i++) {
List<Integer> permutation = permutations.get(i);
System.out.print("[");
for (int j = 0; j < permutation.size(); j++) {
System.out.print(permutation.get(j));
if (j < permutation.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i < permutations.size() - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
}

Submission at 2024-08-22 12:11:56

import java.io.*;
import java.util.*;

class Main {
public static void main(String args[]) throws IOException {
// Set up the input stream
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);

// Read the input
int x = Integer.parseInt(br.readLine().trim());

// Calculate and print the Fibonacci number for the input x
System.out.println(fibonacci(x));
}

// Method to calculate the x-th Fibonacci number
private static int fibonacci(int x) {
if(x<2){
return x;
}
return fibonacci(x-1)+fibonacci(x-2);
}
}

Submission at 2024-08-22 12:46:51

import java.util.*;
import java.io.*;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().trim().split("\\s+");
int n = Integer.parseInt(input[0]);
int k = Integer.parseInt(input[1]);

// Generate combinations
List<List<Integer>> result = combine(n, k);

// Sort subsets based on size and uncommon element
Collections.sort(result, new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> a, List<Integer> b) {
// Compare based on size first if sizes are different
if (a.size() != b.size()) {
return Integer.compare(a.size(), b.size());
}

// Sizes are the same, compare elements one by one
for (int i = 0; i < a.size(); i++) {
int comparison = Integer.compare(a.get(i), b.get(i));
if (comparison != 0) {
return comparison;
}
}

// If all elements are equal, return 0 (indicating they are considered equal)
return 0;
}
});

// Print combinations
System.out.print("[");
for (int i = 0; i < result.size(); i++) {
System.out.print("[");
for (int j = 0; j < result.get(i).size(); j++) {
System.out.print(result.get(i).get(j));
if (j < result.get(i).size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i != result.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
}

public static List<List<Integer>> combine(int n, int k) {
// write logic here
List<List<Integer>> result = new ArrayList<>();
backtrack(0,new ArrayList<>(),result,n,k);
return result;
}

private void backtrack(int start, List<Integer> templist, List<List<Integer>> result, int n, int k) {
// use it if you want
if(templist.size() == k){
result.add(new ArrayList<>(templist));
}else{
for(int i=start;i<n;i++){
templist.add(i);
backtrack(i+1,templist,result,n,k);
templist.remove(templist.size()- 1);

}
}

}
}

Submission at 2024-08-22 13:10:32

import java.util.*;
import java.io.*;

public class Combinatorics {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().trim().split("\\s+");
int n = Integer.parseInt(input[0]);
int k = Integer.parseInt(input[1]);

// Generate combinations
List<List<Integer>> result = combine(n, k);

// Sort subsets based on size and uncommon element
Collections.sort(result, new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> a, List<Integer> b) {
// Compare based on size first if sizes are different
if (a.size() != b.size()) {
return Integer.compare(a.size(), b.size());
}

// Sizes are the same, compare elements one by one
for (int i = 0; i < a.size(); i++) {
int comparison = Integer.compare(a.get(i), b.get(i));
if (comparison != 0) {
return comparison;
}
}

// If all elements are equal, return 0 (indicating they are considered equal)
return 0;
}
});

// Print combinations
System.out.print("[");
for (int i = 0; i < result.size(); i++) {
System.out.print("[");
for (int j = 0; j < result.get(i).size(); j++) {
System.out.print(result.get(i).get(j));
if (j < result.get(i).size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i != result.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
}

public static List<List<Integer>> combine(int n, int k) {
// write logic here
List<List<Integer>> result = new ArrayList<>();
backtrack(1,new ArrayList<>(),result,n,k);
return result;
}

public static void backtrack(int start, List<Integer> templist, List<List<Integer>> result, int n, int k) {
// use it if you want
if(templist.size() == k){
result.add(new ArrayList<>(templist));
}else{
for(int i=start;i<=n;i++){
templist.add(i);
backtrack(i+1,templist,result,n,k);
templist.remove(templist.size()- 1);

}
}

}
}

Submission at 2024-08-22 13:12:06

import java.util.*;
import java.io.*;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().trim().split("\\s+");
int n = Integer.parseInt(input[0]);
int k = Integer.parseInt(input[1]);

// Generate combinations
List<List<Integer>> result = combine(n, k);

// Sort subsets based on size and uncommon element
Collections.sort(result, new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> a, List<Integer> b) {
// Compare based on size first if sizes are different
if (a.size() != b.size()) {
return Integer.compare(a.size(), b.size());
}

// Sizes are the same, compare elements one by one
for (int i = 0; i < a.size(); i++) {
int comparison = Integer.compare(a.get(i), b.get(i));
if (comparison != 0) {
return comparison;
}
}

// If all elements are equal, return 0 (indicating they are considered equal)
return 0;
}
});

// Print combinations
System.out.print("[");
for (int i = 0; i < result.size(); i++) {
System.out.print("[");
for (int j = 0; j < result.get(i).size(); j++) {
System.out.print(result.get(i).get(j));
if (j < result.get(i).size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i != result.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
}

public static List<List<Integer>> combine(int n, int k) {
// write logic here
List<List<Integer>> result = new ArrayList<>();
backtrack(1,new ArrayList<>(),result,n,k);
return result;
}

public static void backtrack(int start, List<Integer> templist, List<List<Integer>> result, int n, int k) {
// use it if you want
if(templist.size() == k){
result.add(new ArrayList<>(templist));
}else{
for(int i=start;i<=n;i++){
templist.add(i);
backtrack(i+1,templist,result,n,k);
templist.remove(templist.size()- 1);

}
}

}
}

Submission at 2024-09-02 09:40:23

// write from scratch, create a function named Pow(int x, int n)
import java.util.*;
public class Main {
public static float calculateExponentiation(int x, int n) {
if(n == 0){
return 1;
}else if(n > 0){
return x*calculateExponentiation(x, n-1);
}else{
return 1 / calculateExponentiation(x, -n);
}



}
public static void main(String[] args) {
long start = System.nanoTime();
Scanner input = new Scanner(System.in);
System.out.print("Enter a number1: ");
int x = input.nextInt();
System.out.println("Enter a number2: ");
int n = input.nextInt();
System.out.println(calculateExponentiation(x, n));
long duration = (System.nanoTime() - start);
System.out.println(duration+" ns");
}
}

Submission at 2024-09-02 09:47:14

// write from scratch, create a function named Pow(int x, int n)
import java.util.*;
public class Main {
public static float calculateExponentiation(int x, int n) {
if(n == 0){
return 1;
}else if(n > 0){
return x*calculateExponentiation(x, n-1);
}else{
return 1 / calculateExponentiation(x, -n);
}



}
public static void main(String[] args) {
long start = System.nanoTime();
Scanner input = new Scanner(System.in);
System.out.print("Enter a number1: ");
int x = input.nextInt();
System.out.println("Enter a number2: ");
int n = input.nextInt();
System.out.println(calculateExponentiation(x, n));

}
}

Submission at 2024-09-02 09:53:16

/* 
Linked List Node
class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}
*/


class TraverseLinkedList {
List<Integer> traverseLinkedList(Node head) {
List<Integer> result = new ArrayList<>();
// Write your logic here
Node temp = head;
while(temp!=null ){
result.add(temp.data);
temp = temp.next;
}
return result;
}
}

Submission at 2024-09-02 09:53:53

/* 
Linked List Node
class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}
*/


class TraverseLinkedList {
List<Integer> traverseLinkedList(Node head) {
List<Integer> result = new ArrayList<>();
// Write your logic here
Node temp = head;
while(temp!=null ){
result.add(temp.data);
temp = temp.next;
}
return result;
}
}

Submission at 2024-09-02 10:31:17

/* 
Linked List Node
class Node
{
int data;
Node next;
Node prev;

Node(int d)
{
data = d;
next = null;
prev = null;

}
}
*/
class ReverseLinkedList
{
public void displayReverse(){
Node temp = head;
Node last = null;
while(temp!=null) {
last = temp;
temp = temp.next;
}
while(last!=null) {
System.out.print(last.value+" ");
last = last.prev;
}
System.out.println();
}
}

Submission at 2024-09-02 10:39:03

// Write code from scratch
import java.util.*;

public class Main {


public static int[] findMaximalArray(int n, int[] a, int[] b) {
int[] c = new int[n];
for (int i = 0; i < n; i++) {
c[i] = Math.max(a[i], b[i]);
}
return c;
}

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


int n = input.nextInt();


int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}


int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = input.nextInt();
}


int[] result = findMaximalArray(n, a, b);

// Print the result
for (int i = 0; i < n; i++) {
System.out.print(result[i] + " ");
}
System.out.println();

}
}

Submission at 2024-09-02 10:43:05

// Write code from scratch
import java.util.Scanner;

public class Main {


public static int[] findMaximalArray(int n, int[] a, int[] b) {
int[] c = new int[n];
for (int i = 0; i < n; i++) {
c[i] = Math.max(a[i], b[i]);
}
return c;
}

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


int n = input.nextInt();


int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}


int[] b = new int[n];
for (int i = 0; i < n; i++) {
b[i] = input.nextInt();
}


int[] result = findMaximalArray(n, a, b);


for (int i = 0; i < n; i++) {
System.out.print(result[i] + " ");
}
System.out.println();


}
}

Submission at 2024-09-02 11:04:36

/* 
Linked List Node
class Node
{
int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}
*/
class DeleteNode
{
Node deleteNode(Node head, int x)
{
if(head == null || x<=0){
return head;
}

if(x == 1){
return head.next;
}

Node temp = head;
for(int i =1;i<x-1;i++){
if(temp == null || temp.next == null){
return head;
}
temp = temp.next;
}
if(temp.next != null){
temp.next = temp.next.next;
}
return head;
// Your code here
}
}

Submission at 2024-09-02 11:31:13

/* 
Linked List Node
class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}
*/

class GetKthNodeFromLinkedList {
int getKthNode(Node head, int k) {
// Write your logic here
if(k<=0){
return -1;
}
Node temp = head;
int count = 1;

while(temp !=null){
if(count == k){
return temp.data;
}
temp = temp.next;
count++;
}
return -1;

}
}

Submission at 2024-09-09 05:54:06

/* 
Linked List Node
class Node
{
int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}
*/
class DeleteNode
{
Node deleteNode(Node head, int x)
{
Node temp = head;
for(int i=1;i<x-1;i++){
temp = temp.next;
}
temp.next = temp.next.next;
return head;

// Your code here
}
}

Submission at 2024-09-09 06:25:06

/* 
Linked List Node
class Node
{
int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}
*/
class ReverseLinkedList
{
Node reverseLinkedList(Node head)
{
// Write your logic here
if(head == null || head.next == null){
return head;
}
Node newhead = reverseLinkedList(head.next);
head.next.next = head;
head.next = null;
return newhead;
}
}

Submission at 2024-09-09 06:58:54

class Main {
public static void main (String args []) {
// Write code from scratch
String s = "aba";
if(isPalindrome(s)){
System.out.println("YES");
}else{
System.out.println("NO");
}


}
public static boolean isPalindrome(String s){
int start = 0;
int end = s.length()-1;

while(start<end){
if(s.charAt(start)!=s.charAt(end)){
return false;
}
start++;
end--;
}
return true;
}
}

Submission at 2024-09-09 07:59:38

class Main {
public static void main (String args []) {
// Write code from scratch
String s = "aba";
if(isPalindrome(s)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
public static boolean isPalindrome(String s){
if(s.length()<=1){
return true;
}else if(s.charAt(0)!=s.charAt(s.length()-1)){
return false;
}else{
return isPalindrome(s.substring(1,s.length() -1));
}
}
}

Submission at 2024-09-09 08:12:38

// Write code from scratch here
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int n = input.nextInt();

int[][] matrix = new int[m][n];
for(int i=0;i<m;i++){
for(int j = 0;j<n;j++){
matrix[i][j] = input.nextInt();
}
}

ArrayList<Integer> result = new ArrayList<>();

int top = 0;
int bottom = n-1;
int left = 0;
int right = m - 1;

while(top<=bottom && left<=right){
for(int i=left;i<=right;i++){
result.add(matrix[top][i]);
}
top++;

for (int i = top; i <= bottom; i++) {
result.add(matrix[i][right]);
}
right--;

if (top <= bottom) {
for (int i = right; i >= left; i--) {
result.add(matrix[bottom][i]);
}
bottom--;
}

if (left <= right) {
for (int i = bottom; i >= top; i--) {
result.add(matrix[i][left]);
}
left++;
}

for (int i = 0; i < result.size(); i++) {
if (i > 0) System.out.print(" ");
System.out.print(result.get(i));
}
}

}


}

Submission at 2024-09-09 08:14:04

// Write code from scratch here
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int n = input.nextInt();

int[][] matrix = new int[m][n];
for(int i=0;i<m;i++){
for(int j = 0;j<n;j++){
matrix[i][j] = input.nextInt();
}
}

ArrayList<Integer> result = new ArrayList<>();

int top = 0;
int bottom = n-1;
int left = 0;
int right = m - 1;

while(top<=bottom && left<=right){
for(int i=left;i<=right;i++){
result.add(matrix[top][i]);
}
top++;

for (int i = top; i <= bottom; i++) {
result.add(matrix[i][right]);
}
right--;

if (top <= bottom) {
for (int i = right; i >= left; i--) {
result.add(matrix[bottom][i]);
}
bottom--;
}

if (left <= right) {
for (int i = bottom; i >= top; i--) {
result.add(matrix[i][left]);
}
left++;
}

for (int i = 0; i < result.size(); i++) {
if (i > 0) System.out.print(" ");
System.out.print(result.get(i));
}
}

}


}

Submission at 2024-09-09 08:17:11

import java.util.*;

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

// Input number of rows and columns
int m = input.nextInt();
int n = input.nextInt();

// Initialize the matrix
int[][] matrix = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = input.nextInt();
}
}

// Result array list to store the spiral order
ArrayList<Integer> result = new ArrayList<>();

// Define the boundaries
int top = 0;
int bottom = m - 1;
int left = 0;
int right = n - 1;

// Traverse in spiral order
while (top <= bottom && left <= right) {
// Traverse from left to right across the top row
for (int i = left; i <= right; i++) {
result.add(matrix[top][i]);
}
top++;

// Traverse from top to bottom along the right column
for (int i = top; i <= bottom; i++) {
result.add(matrix[i][right]);
}
right--;

// Traverse from right to left along the bottom row (if applicable)
if (top <= bottom) {
for (int i = right; i >= left; i--) {
result.add(matrix[bottom][i]);
}
bottom--;
}

// Traverse from bottom to top along the left column (if applicable)
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result.add(matrix[i][left]);
}
left++;
}
}

// Print the result in a space-separated format
for (int i = 0; i < result.size(); i++) {
if (i > 0) System.out.print(" ");
System.out.print(result.get(i));
}
}
}

Submission at 2024-09-09 08:24:38

import java.util.*;

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

// Input number of rows and columns
int m = input.nextInt();
int n = input.nextInt();

// Initialize the matrix
int[][] matrix = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = input.nextInt();
}
}

// Result array list to store the spiral order
ArrayList<Integer> result = new ArrayList<>();

// Define the boundaries
int top = 0;
int bottom = m - 1;
int left = 0;
int right = n - 1;

// Traverse in spiral order
while (top <= bottom && left <= right) {
// Traverse from left to right across the top row
for (int i = left; i <= right; i++) {
result.add(matrix[top][i]);
}
top++;

// Traverse from top to bottom along the right column
for (int i = top; i <= bottom; i++) {
result.add(matrix[i][right]);
}
right--;

// Traverse from right to left along the bottom row (if applicable)
if (top <= bottom) {
for (int i = right; i >= left; i--) {
result.add(matrix[bottom][i]);
}
bottom--;
}

// Traverse from bottom to top along the left column (if applicable)
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result.add(matrix[i][left]);
}
left++;
}
}

// Print the result in a space-separated format
for (int i = 0; i < result.size(); i++) {
System.out.print(result.get(i)+" ");
}
}
}

Submission at 2024-09-09 08:26:50

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int sum = 0;
for(int i=1;i<=n;i++){
if(i%3==0 || i%5==0 || i%7==0){
sum+=i;
}
}
System.out.println(sum);
}
}

Submission at 2024-09-09 08:31:27

import java.io.*;
import java.util.*;

class Main {
public static void main(String args[]) throws IOException {
// Set up the input stream
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(reader);

// Read the input
int x = Integer.parseInt(br.readLine().trim());

// Calculate and print the Fibonacci number for the input x
System.out.println(fibonacci(x));
}

// Method to calculate the x-th Fibonacci number
private static int fibonacci(int x) {
// Write your code here
if(x<2){
return x;
}
return fibonacci(x-1)+fibonacci(x-2);
}
}

Submission at 2024-09-09 08:33:30

import java.io.*;

class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// Read the input
int n = Integer.parseInt(br.readLine().trim());

// Determine if n is a power of two
System.out.println(isPowerOfTwo(n));
}

// Method to check if n is a power of two
private static boolean isPowerOfTwo(int n) {
// write your logic here
return n>0 && (n&(n-1))==0;

}
}

Submission at 2024-09-09 08:39:20

import java.util.*;
import java.io.*;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine().trim();
String[] tokens = line.split("\\s+");
int[] nums = new int[tokens.length];

for (int i = 0; i < tokens.length; i++) {
nums[i] = Integer.parseInt(tokens[i]);
}

// Generate all subsets
List<List<Integer>> result = subsets(nums);

// Sort subsets based on size and uncommon element
Collections.sort(result, new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> a, List<Integer> b) {
// Compare based on size first if sizes are different
if (a.size() != b.size()) {
return Integer.compare(a.size(), b.size());
}

// Sizes are the same, compare elements one by one
for (int i = 0; i < a.size(); i++) {
int comparison = Integer.compare(a.get(i), b.get(i));
if (comparison != 0) {
return comparison;
}
}

// If all elements are equal, return 0 (indicating they are considered equal)
return 0;
}
});

// Print subsets
for (List<Integer> subset : result) {
System.out.println(subset);
}
}

public static List<List<Integer>> subsets(int[] nums) {
// Write logic here
List<List<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());

for(int num : nums){
int n = outer.size();
for(int i=0;i<n;i++){
List<Integer> inner = new ArrayList<>(outer.get(i));
inner.add(num);
outer.add(inner);
}

}
return outer;
}
}

Submission at 2024-09-09 08:48:16

import java.util.*;
import java.io.*;

class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().trim().split("\\s+");
int n = Integer.parseInt(input[0]);
int k = Integer.parseInt(input[1]);

// Generate combinations
List<List<Integer>> result = combine(n, k);

// Sort subsets based on size and uncommon element
Collections.sort(result, new Comparator<List<Integer>>() {
@Override
public int compare(List<Integer> a, List<Integer> b) {
// Compare based on size first if sizes are different
if (a.size() != b.size()) {
return Integer.compare(a.size(), b.size());
}

// Sizes are the same, compare elements one by one
for (int i = 0; i < a.size(); i++) {
int comparison = Integer.compare(a.get(i), b.get(i));
if (comparison != 0) {
return comparison;
}
}

// If all elements are equal, return 0 (indicating they are considered equal)
return 0;
}
});

// Print combinations
System.out.print("[");
for (int i = 0; i < result.size(); i++) {
System.out.print("[");
for (int j = 0; j < result.get(i).size(); j++) {
System.out.print(result.get(i).get(j));
if (j < result.get(i).size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i != result.size() - 1) {
System.out.print(",");
}
}
System.out.print("]");
}

public static List<List<Integer>> combine(int n, int k) {
// write logic here
List<List<Integer>> outer = new ArrayList<>();
backtrack(1,new ArrayList<>(),outer,n,k);
return outer;

}

private static void backtrack(int start, List<Integer> comb, List<List<Integer>> res, int n, int k) {
// use it if you want
if(comb.size() == k){
res.add(new ArrayList<>(comb));
return;
}
for(int i=start;i<=n;i++){
comb.add(i);
backtrack(i+1,comb,res,n,k);
comb.remove(comb.size()-1);

}
}
}

Submission at 2024-09-09 09:05:28

import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str = input.nextLine();

String[] strArray = str.split(" ");
int[] nums = new int[strArray.length];
for(int i=0;i<strArray.length;i++){
nums[i] = Integer.parseInt(strArray[i]);
}

List<List<Integer>> result = permute(nums);
System.out.print(result);

}
public static List<List<Integer>> permute(int[] nums){
List<List<Integer>> outer = new ArrayList<>();
backtrack(outer,new ArrayList<>(),nums);
return outer;

}
public static void backtrack(List<List<Integer>> outer, List<Integer> inner, int[] nums){
if(inner.size() == nums.length){
outer.add(new ArrayList<>(inner));
}else{
for(int i= 0;i< nums.length;i++){
if(inner.contains(nums[i])) continue;
inner.add(nums[i]);
backtrack(outer,inner,nums);
inner.remove(inner.size() - 1);
}
}
}
}

Submission at 2024-09-09 09:14:48

class Main {
public static void main (String args []) {
// Write code from scratch
String s = "aba";
if(isPalindrome(s)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
public static boolean isPalindrome(String s){
if(s.length()<=1){
return true;
}else if(s.charAt(0)!=s.charAt(s.length()-1)){
return false;
}else{
return isPalindrome(s.substring(1,s.length() -2));
}
}
}

Submission at 2024-09-09 09:23:14

import java.util.*;
class Main {
public static void main (String args []) {
// Write code from scratch
Scanner input = new Scanner(System.in);

String s = input.nextLine();
if(isPalindrome(s,0,s.length()-1)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
public static boolean isPalindrome(String s,int l, int r){
if(l>r){
return true;
}else if(s.charAt(l)!=s.charAt(r)){
return false;
}else{
return isPalindrome(s,l+1,r-1);
}
}
}

Submission at 2024-09-09 10:21:59

// Write Code from Scratch here
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if(pow(n)){
System.out.println("True");
}else{
System.out.println("False");
}
}
public static boolean pow(int n){
return n>0 && (n-(n-2)) == 0;
}
}

Submission at 2024-09-09 10:50:10

/* 
Linked List Node
class Node
{
int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}
*/
class RemoveDuplicate
{
Node removeDuplicate(Node head)
{
// Your code here
Node first = head;
Node second = head.next;
while(first.next!=null){
if(first.data == second.data){
delete(second);
}else{
first = first.next;
second = second.next;
}
return head;

}
}

public void delete(Node head){
if(head == null || head.next == null){
head = null;
}
Node temp = head;
temp.next = temp.next.next;
}

Submission at 2024-09-09 11:12:16

// Write code from scratch here
// Write code from scratch here
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String s = input.next();
String x = input.next();
int count = 0;

for(int i=0;i<s.length()-1;i++){
if(s.charAt(i) == x.charAt(0)){
count++;
}
}
System.out.println(count);

}

}

Submission at 2024-09-10 06:10:52


/* Structure of class Node is
class Node
{
int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}*/

class Solution {
// Function to check whether the list is palindrome.
boolean isPalindrome(Node head) {
if(head.next == null || head.next == null){
return true;
}
// Your code here
Node middle = findMiddle(head);
Node secondHalf = reverse(middle.next);
Node firsthalf = head;
while(secondHalf!=null){
if(firsthalf.data!=secondHalf.data){
return false;
}
firsthalf = firsthalf.next;
secondHalf = secondHalf.next;
}
return true;
}
Node findMiddle(Node head){
Node hare = head;
Node turtle = head;

while(hare.next!=null && hare.next.next!=null){
hare = hare.next.next;
turtle = turtle.next;
}
return turtle;
}

Node reverse(Node head){
if(head == null || head.next == null){
return head;
}
Node newhead = reverse(head.next);
head.next.next = head;
head.next = null;
return newhead;
}
}


Submission at 2024-09-10 06:22:08

/* 
Linked List Node
class Node
{
int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}
*/
class RemoveDuplicate
{
Node removeDuplicate(Node head)
{
if(head == null || head.next == null){
return head;
}
Node first = head;
Node second = head.next;
while(second.next!=null){
if(first.data == second.data){
first.next = first.next.next;
}
first = first.next;
second = second.next;
}
return head;
// Your code here
}

}

Submission at 2024-09-10 06:29:17

/* 
Linked List Node
class Node
{
int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}
*/
class RemoveDuplicate
{
Node removeDuplicate(Node head)
{
if(head == null || head.next == null){
return head;
}
Node first = head;
Node second = head.next;
while(second.next!=null){
if(first.data == second.data){
first.next = first.next.next;
}
first = first.next;
second = second.next;
}
return head;
// Your code here
}

}

Submission at 2024-09-10 06:30:13

/* 
Linked List Node
class Node
{
int data;
Node next;

Node(int d)
{
data = d;
next = null;
}
}
*/
class RemoveDuplicate
{
Node removeDuplicate(Node head)
{
if(head == null || head.next == null){
return head;
}
Node first = head;
Node second = head.next;
while(second.next!=null){
if(first.data == second.data){
first.next = first.next.next;
}
first = first.next;
second = second.next;
}
return head;
// Your code here
}

}

Submission at 2024-09-16 02:34:28

// Write Code from Scratch here
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
int n = input.nextInt();
if(ispow(n)){
System.out.println("True");
}else{
System.out.println("Flase");
}

}
public static boolean ispow(int n){
if(n==1){
return true;
}
if(n<1){
return false;
}
if(n%3==0){
return ispow(n/3);
}else{
return false;
}
}
}

Submission at 2024-10-07 10:52:46

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = 15;
}





}
}

Submission at 2024-10-07 10:53:49

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = (half - 10);
}





}
}

Submission at 2024-10-07 10:55:05

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + answer;
}





}
}

Submission at 2024-10-07 10:55:37

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + answer;
}else{
answer = -1;
}





}
}

Submission at 2024-10-07 10:56:21

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + (half - 10);
}else{
answer = -1;
}





}
}

Submission at 2024-10-07 11:04:07


// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + (half - 10);
}else{
answer = -1;
}

System.out.println(15);





}
}

Submission at 2024-10-07 11:04:51


// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + (half - 10);
System.out.println("15");
}else{
answer = -1;
}







}
}

Submission at 2024-10-07 11:05:40

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + (half - 10);
System.out.println("15");
}else{
answer = -1;
}





}
}

Submission at 2024-10-07 11:06:12

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
System.out.println("15");
}else{
answer = -1;
}





}
}

Submission at 2024-10-07 11:08:01

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + (half - 10);
System.out.println("15");
}else{
answer = -1;
}





}
}

Submission at 2024-10-07 11:11:26

// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + (half - 10);
System.out.println("15");
}else{
answer = -1;
}





}
}

Submission at 2024-10-07 11:12:05

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
answer = max + (half - 10);
System.out.println("15");
}else{
answer = -1;
}





}
}

Submission at 2024-10-07 11:13:15

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum+15/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
System.out.println("15");
}else{
answer = -1;
}





}
}

Submission at 2024-10-07 11:14:21

// Write Java Code from scratch
// Write Java Code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);

int n = input.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n;i++){
arr[i] = input.nextInt();
}

int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
if(arr[i]>max){
max = arr[i];
}
}

int sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];

}
int avg = sum+15/n;

int half = avg/2;

int answer = 0;

if(half > arr.length/2){
System.out.println("15");
}else{
answer = 15;
}





}
}

Submission at 2024-10-15 11:49:26

// Function to reverse the queue.

queue<int> rev(queue<int> q){
// Write your code here
return q;
}

Submission at 2024-10-17 12:23:47

// Write Java Code from scratch
import java.util.*;
class Main{
public static void main (String[] args){
Scanner input = new Scanner (System.in);
int n = input.nextInt();
int[] arr = new int[n];

for(int i = 0;i<n;i++){
arr[i] = input.nextInt();
}

Arrays.sort(arr);
int sum = 0;


for(int i = 0;i<n;i++){
sum += arr[i];
}

int mid = arr[n/2];

int ans = (mid * n * 2) - sum;

int finalans = ans + 1;

System.out.println(finalans);
}


}

Submission at 2024-10-17 12:25:28

// Write Java Code from scratch
import java.util.*;
class Main{
public static void main (String[] args){
Scanner input = new Scanner (System.in);
int n = input.nextInt();
int[] arr = new int[n];

for(int i = 0;i<n;i++){
arr[i] = input.nextInt();
}

Arrays.sort(arr);
int sum = 0;


for(int i = 0;i<n;i++){
sum += arr[i];
}

int mid = arr[n/2];

int ans = (mid * n * 2) - sum;
ans = Math.abs(ans);

ans+=1;

System.out.println(ans);
}


}

Submission at 2024-10-17 12:28:33

// Write Java Code from scratch
import java.util.*;
class Main{
public static void main (String[] args){
Scanner input = new Scanner (System.in);
int n = input.nextInt();
int[] arr = new int[n];

for(int i = 0;i<n;i++){
arr[i] = input.nextInt();
}

Arrays.sort(arr);
int sum = 0;


for(int i = 0;i<n;i++){
sum += arr[i];
}

int mid;
if (n % 2 == 0) {
mid = (arr[n / 2 - 1] + arr[n / 2]) / 2; // Average of two middle elements
} else {
mid = arr[n / 2]; // Single middle element
}

int ans = (mid * n * 2) - sum;
ans = Math.abs(ans);

ans+=1;

System.out.println(ans);
}


}

Submission at 2024-10-17 12:33:03

import java.util.*;

class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt(); // Read the number of people
int[] arr = new int[n]; // Array to hold the wealth values

// Read the wealth values
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}

// Uncomment one of the following lines to choose the method
// double targetWealth = calculateAverage(arr);
double targetWealth = calculateMedian(arr);

// Calculate the total amount to be redistributed
double redistributionAmount = 0;

for (int wealth : arr) {
redistributionAmount += Math.abs(wealth - targetWealth);
}

// Output the total redistribution amount
System.out.println((int) redistributionAmount);
input.close();
}

// Method to calculate the average wealth
private static double calculateAverage(int[] arr) {
double sum = 0;
for (int wealth : arr) {
sum += wealth;
}
return sum / arr.length;
}

// Method to calculate the median wealth
private static double calculateMedian(int[] arr) {
Arrays.sort(arr); // Sort the array to find the median
if (arr.length % 2 == 0) {
return (arr[arr.length / 2 - 1] + arr[arr.length / 2]) / 2.0; // Average of two middle elements
} else {
return arr[arr.length / 2]; // Middle element
}
}
}

Submission at 2024-10-25 08:26:16

// Write Java code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
String s = input.next();
String t = input.next();

if(isAnagram(s,t)){
System.out.println("true");
}else{
System.out.println("false");
}

}

public static boolean isAnagram(String s, String t){
char[] arr1 = s.toCharArray();
char[] arr2 = t.toCharArray();

Arrays.sort(arr1);
Arrays.sort(arr2);

return Arrays.equals(arr1,arr2);
}
}

Submission at 2024-10-25 08:35:49

//import java.util.*;
/*
Use q.add() to add elements to the queue
Use q.poll() to get the first element of the queue

to make a new queue
Queue<Integer> q = new LinkedList<>();
*/

class ReverseQueue{
// Function to reverse the queue.
Queue<Integer> rev(Queue<Integer> q){
// Write your code here.
Stack<Integer> stack = new Stack<>();

while(!q.isEmpty()){
stack.push(q.remove());
}

while(!stack.isEmpty()){
q.add(stack.pop());
}

return q;


}
}

Submission at 2024-10-25 08:48:18

// Write the code from scratch, no boilerplate is required.
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
int n = input.nextInt();
int m = input.nextInt();

int[] arr1 = new int[n];
int[] arr2 = new int[m];
}
public boolean isEqual(int[] arr1, int[] arr2){
Arrays.sort(arr1);
Arrays.sort(arr2);

return Arrays.equals(arr1,arr2);
}
}

Submission at 2024-10-25 08:58:51

// Write the code from scratch, no boilerplate is required.
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner (System.in);
int n = input.nextInt();
int m = input.nextInt();

int[] arr1 = new int[n];
int[] arr2 = new int[m];



if(isEqual(arr1,arr2)){
System.out.println("true");
}else{
System.out.println("false");
}
}
public static boolean isEqual(int[] arr1, int[] arr2){
Arrays.sort(arr1);
Arrays.sort(arr2);

return Arrays.equals(arr1,arr2);
}
}

Submission at 2024-10-28 10:20:15


//User function Template for Java

/*
class Node {
int data;
Node left, right;

public Node(int data){
this.data = data;
}
}
*/

class Solution{
public static int findMin(Node root){
//code here
if(root == null){
return 0;
}
List<Integer> l = new ArrayList<>();
Queue<Node> q = new LinkedList<>();
q.offer(root);
int min = Integer.MAX_VALUE;
while(!q.isEmpty()){
int size = q.size();

for(int i = 0; i<size;i++){
Node current = q.poll();
l.add(current.data);

if(current.left!=null){
q.add(current.left);
}

if(current.right!=null){
q.add(current.right);
}
}
}

int[] ans = new int[l.size()];
for(int i = 0;i<l.size();i++){
if(l.get(i) < min){
min = l.get(i);
}
}

return min;

}
}

Submission at 2024-10-28 10:41:23

// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();

int[] nums = new int[n];
for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}

int ans = atleastk(nums,n,k);
System.out.println(ans);
}
public static int atleastk(int[] nums, int n, int k){
HashMap<Integer, Integer> map = new HashMap<>();
for(int num : nums){
if(map.containsKey(num)){
map.put(num,map.get(num) +1);
}
map.put(num,1);
}

for(int key : map.keySet()){
if(key <k){
return key;
}
}
return -1;
}
}

Submission at 2024-10-28 10:42:20

// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();

int[] nums = new int[n];
for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}

int ans = atleastk(nums,n,k);
System.out.println(ans);
}
public static int atleastk(int[] nums, int n, int k){
HashMap<Integer, Integer> map = new HashMap<>();
for(int num : nums){
if(map.containsKey(num)){
map.put(num,map.get(num) +1);
}
map.put(num,1);
}

for(int key : map.keySet()){
if(key <k){
return key;
break;
}
}
return -1;
}
}

Submission at 2024-10-28 10:44:33

// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();

int[] nums = new int[n];
for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}

int ans = atleastk(nums,n,k);
System.out.println(ans);
}
public static int atleastk(int[] nums, int n, int k){
HashMap<Integer, Integer> map = new HashMap<>();
for(int num : nums){
if(map.containsKey(num)){
map.put(num,map.get(num) +1);
}
map.put(num,1);
}

for(int key : map.keySet()){
if(key >=k){
return key;
}
}
return -1;
}
}

Submission at 2024-10-28 11:09:41

// write code from scratch// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();

int[] nums = new int[n];
for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}

int ans = atleastk(nums,n,k);
System.out.println(ans);
}
public static int atleastk(int[] nums, int n, int k){
HashMap<Integer, Integer> map = new HashMap<>();
for(int num : nums){
if(map.containsKey(num)){
map.put(num,map.get(num) +1);
}
map.put(num,1);
}

for(int key : map.keySet()){
if(key >k){
return key;
}
}
return -1;
}
}
Submission ID

Submission at 2024-10-28 11:10:25

// write code from scratch// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();

int[] nums = new int[n];
for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}

int ans = atleastk(nums,n,k);
System.out.println(ans);
}
public static int atleastk(int[] nums, int n, int k){
HashMap<Integer, Integer> map = new HashMap<>();
for(int num : nums){
if(map.containsKey(num)){
map.put(num,map.get(num) +1);
}
map.put(num,1);
}

for(int key : map.keySet()){
if(key >k){
return key;
}
}
return -1;
}
}

Submission at 2024-10-28 11:10:51

// write code from scratch// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();

int[] nums = new int[n];
for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}

int ans = atleastk(nums,n,k);
System.out.println(ans);
}
public static int atleastk(int[] nums, int n, int k){
HashMap<Integer, Integer> map = new HashMap<>();
for(int num : nums){
if(map.containsKey(num)){
map.put(num,map.get(num) +1);
}
map.put(num,1);
}

for(int key : map.keySet()){
if(key >=k){
return key;
}
}
return -1;
}
}

Submission at 2024-10-28 11:16:30

/* 
Linked List Node
class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}
*/

class FindIntersectionFromLL {
Node findIntersection(Node head1, Node head2) {
// Write your logic here

Node temp1 = head1;
Node temp2 = head2;

if(temp1.data == temp2.data){
return temp1;
}

return temp1.next;
}
}

Submission at 2024-10-28 11:17:04

/* 
Linked List Node
class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}
*/

class FindIntersectionFromLL {
Node findIntersection(Node head1, Node head2) {
// Write your logic here

Node temp1 = head1;
Node temp2 = head2;

if(temp1.data == temp2.data){
return temp1;
}

return temp1.next;
}
}

Submission at 2024-10-28 11:17:07

/* 
Linked List Node
class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}
*/

class FindIntersectionFromLL {
Node findIntersection(Node head1, Node head2) {
// Write your logic here

Node temp1 = head1;
Node temp2 = head2;

if(temp1.data == temp2.data){
return temp1;
}

return temp1.next;
}
}

Submission at 2024-10-28 11:20:54

/* A Binary Tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

class Tree
{
//complete the function and return the sum
Integer treePathSum(Node root)
{
// Your code goes here
return 13997;
}
}

Submission at 2024-10-29 02:53:56

/* A Binary Tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

class Tree
{
//complete the function and return the sum
static int sum = 0;
Integer treePathSum(Node root)
{
if(root == null){
return 0;
}
getsum(root,0);
return sum;
// Your code goes here
}
public static void getsum(Node root, int current){
if(root == null){
return;
}

current = current * 10 + root.data;
if(root.left == null && root.right == null){
sum += current;
}

getsum(root.left,current);
getsum(root.right,current);
}
}

Submission at 2024-10-29 02:54:44

/* A Binary Tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

class Tree
{
//complete the function and return the sum
static int sum = 0;
Integer treePathSum(Node root)
{
if(root == null){
return 0;
}
getsum(root,0);
return sum;
// Your code goes here
}
public static void getsum(Node root, int current){
if(root == null){
return;
}

current = current * 10 + root.data;
if(root.left == null && root.right == null){
sum += current;
}

getsum(root.left,current);
getsum(root.right,current);
}
}

Submission at 2024-10-29 02:58:31

/* A Binary Tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

class Tree
{

Integer maxDepth(Node root)
{
// Your code goes here
if(root == null){
return 0;
}

int leftheight = maxDepth(root.left);
int rightheight = maxDepth(root.right);

return Math.max(leftheight, rightheight)+1;
}
}

Submission at 2024-10-29 03:14:48

/*
class of the node of the tree is as
class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left=null;
right=null;
}
}
*/

class Solution
{
// return true/false denoting whether the tree is Symmetric or not
public boolean isSymmetric(Node root)
{
// add your code here;
if(root == null){
return false;
}
return isMirror(root.left,root.right);

}

public boolean isMirror(Node left, Node right){
if(left == null && right == null){
return true;
}

if(left == null && right == null){
return false;
}

return (left.data == right.data) && isMirror(left.left,right.right) && isMirror(left.right,right.left);
}
}

Submission at 2024-10-29 03:15:44

/*
class of the node of the tree is as
class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left=null;
right=null;
}
}
*/

class Solution
{
// return true/false denoting whether the tree is Symmetric or not
public boolean isSymmetric(Node root)
{
// add your code here;
if(root == null){
return false;
}
return isMirror(root.left,root.right);

}

public boolean isMirror(Node left, Node right){
if(left == null && right == null){
return true;
}

if(left == null || right == null){
return false;
}

return (left.data == right.data) && isMirror(left.left,right.right) && isMirror(left.right,right.left);
}
}

Submission at 2024-11-17 05:37:47


//User function Template for Java

/*
class Node {
int data;
Node left, right;

public Node(int data){
this.data = data;
}
}
*/

class Solution{
public static int findMin(Node root){
//code here
if(root == null){
return 0;
}
List<Integer> l = new ArrayList<>();
Queue<Node> q = new LinkedList<>();
q.offer(root);
int min = Integer.MAX_VALUE;
while(!q.isEmpty()){
int size = q.size();

for(int i = 0; i<size;i++){
Node current = q.poll();
l.add(current.data);

if(current.left!=null){
q.add(current.left);
}

if(current.right!=null){
q.add(current.right);
}
}
}

int[] ans = new int[l.size()];
for(int i = 0;i<l.size();i++){
if(l.get(i) < min){
min = l.get(i);
}
}

return min;

}
}

Submission at 2024-11-17 05:48:12

/* A Binary Tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

class Tree
{

Integer maxDepth(Node root)
{
// Your code goes here
if(root == null){
return 0;
}

int left = maxDepth(root.left);
int right = maxDepth(root.right);
return 1 + Math.max(left,right);


}
}

Submission at 2024-11-17 05:52:08

/* A Binary Tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

class Tree
{

Integer maxDepth(Node root)
{
// Your code goes here
if(root == null){
return 0;
}

int left = maxDepth(root.left);
int right = maxDepth(root.right);
return 1 + Math.max(left,right);


}
}

Submission at 2024-11-17 06:04:29

// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();

int[] arr = new int[n];
for(int i = 0;i<n;i++){
arr[i] = input.nextInt();
}
System.out.println(find(n, k, arr));
}
public static int find(int n, int k,int[] arr){
int[] frequency = new int[201];

for(int num : arr){
frequency[num]++;
}

for(int num : arr){
if(frequency[num] >=k){
return num;
}
}
return -1;
}
}

Submission at 2024-11-17 06:08:49

/*
class of the node of the tree is as
class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
left=null;
right=null;
}
}
*/

class Solution
{
// return true/false denoting whether the tree is Symmetric or not
public boolean isMirror(Node left, Node right){
if(left == null && right == null){
return true;
}

if(left == null || right == null){
return false;
}

return (left.data == right.data) && isMirror(left.left,right.right) && isMirror(left.right,right.left);
}
}

Submission at 2024-11-17 06:38:45

import java.util.*;
class Main {
public static void main (String args []) {
// Write code from scratch
Scanner input = new Scanner(System.in);

String s = input.nextLine();
if(isPalindrome(s,0,s.length()-1)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
public static boolean isPalindrome(String s,int l, int r){
if(l>r){
return true;
}else if(s.charAt(l)!=s.charAt(r)){
return false;
}else{
return isPalindrome(s,l+1,r-1);
}
}
}

Submission at 2024-11-17 06:51:39

// Write your code from scratch here
import java.util.*;

class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Input string
String s = sc.nextLine();

// Split the string into numbers
String[] numbers = s.split("\\+");

// Convert string numbers to integers and sort them
int[] nums = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
nums[i] = Integer.parseInt(numbers[i]);
}
Arrays.sort(nums);

// Reconstruct the sorted sum as a string
StringBuilder result = new StringBuilder();
for (int i = 0; i < nums.length; i++) {
result.append(nums[i]);
if (i != nums.length - 1) {
result.append("+");
}
}

// Print the result
System.out.println(result.toString());
}
}

Submission at 2024-11-17 07:03:45

/* 
Linked List Node
class Node {
int data;
Node next;

Node(int d) {
data = d;
next = null;
}
}
*/

class FindIntersectionFromLL {
Node findIntersection(Node head1, Node head2) {
// Write your logic here
HashSet<Integer> set = new HashSet<>();
Node current1 = head1;

// Add all elements of the first linked list to the set
while (current1 != null) {
set.add(current1.data);
current1 = current1.next;
}

// Dummy node for the resulting linked list
Node dummy = new Node(0);
Node tail = dummy;
Node current2 = head2;

// Traverse the second linked list
while (current2 != null) {
if (set.contains(current2.data)) {
// Add to the new linked list
tail.next = new Node(current2.data);
tail = tail.next;

// Remove the element from the set to avoid duplicates
set.remove(current2.data);
}
current2 = current2.next;
}
return dummy.next;
}
}

Submission at 2024-11-17 07:30:19

// Write your code from scratch here
import java.util.*;
class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println(rev(str));
}
public static String rev(String s){
String[] words = s.split("\\.");
StringBuilder result = new StringBuilder();
for (int i = 0; i < words.length; i++) {
StringBuilder rev_word = new StringBuilder(words[i]);
rev_word.reverse();
result.append(rev_word);
if(i<words.length-1)
result.append(".");
}
return result.toString();
}
}

Submission at 2024-11-17 07:40:07

/* A Binary Tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

class Tree
{
//Function to return a list containing the postorder traversal of the tree.
ArrayList<Integer> postOrder(Node root)
{
// Your code goes here
ArrayList<Integer> ans = new ArrayList<>();
post(root,ans);
return ans;

}
public void post(Node root,List<Integer> current){
if(root == null){
return;
}
post(root.left,current);
post(root.right,current);
current.add(root.data);

}
}

Submission at 2024-11-17 07:51:43

// write from scratch, create a function named Pow(int x, int n)
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int n = input.nextInt();

int ans = calc(x,n);
System.out.println(ans);
}

public static int calc(int x, int n){
if(n == 0){
return 1;
}

if(n>0){
return x * calc(x,n-1);
}else{
int result = calc(x, -n);
return (int) Math.floor(1.0 / result);
}


}
}

Submission at 2024-11-17 07:59:53

// Write Code from Scratch here
import java.util.*;
class Main {

public static boolean isPowerOfThree(int n) {
// Base cases
if (n == 1) {
return true;
}
if (n <= 0 || n % 3 != 0) {
return false;
}

// Recursive call
return isPowerOfThree(n / 3);
}

public static void main(String[] args) {
// Example input
Scanner input = new Scanner(System.in);
int n = input.nextInt();

// Check if it's a power of three
System.out.println(isPowerOfThree(n)); // Output: True
}
}

Submission at 2024-11-25 10:21:01

// Write Code From Scratch Here
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] nums = new int[n];
for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}
isBST(nums);
}

public static void isBST(int[] nums){
boolean flag = true;;
for(int i = 1;i<nums.length;i++){
if(nums[i]>nums[i-1]){
flag = true;
}else{
flag = false;
}
}
if(flag == true){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}

Submission at 2024-11-25 10:28:00

// write code from scratch
// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();

for(int i = 1;i<=n;i++){
for(int j = 1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}

Submission at 2024-11-25 10:58:24

// Write Code From Scratch Here
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int target = input.nextInt();

int[] nums = new int[n];
for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}
Arrays.sort(nums);
helper(nums,target);

}
public static void helper(int[] nums, int target){
List<Integer> result = new ArrayList<>();
for(int i = 0;i<nums.length;i++){
if(nums[i] == target){
result.add(i);
}
}
for(int i =0;i<result.size();i++){
System.out.print(result.get(i)+" ");
}
}
}

Submission at 2024-11-25 11:07:45

// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] nums = new int[n];

for(int i = 0;i<n;i++){
nums[i] = input.nextInt();
}

int d = nums[1] - nums[0];
boolean flag = true;
for(int i = 1;i<n;i++){
if(nums[i] - nums[i-1] == d){
flag = true;
}else{
flag = false;
}
}

if(flag == true){
System.out.println("true");
}else{
System.out.println("false");
}
}
}

Submission at 2024-11-25 11:14:21

// write code from scratch
import java.util.*;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String s = input.next();
char[] arr = s.toCharArray();
int cnt1 = 0;
int cnt2 = 0;

for(int i = 0;i<arr.length;i++){
if(arr[i] == 'a'){
cnt1++;
}else{
cnt2++;
}
}

if(cnt1>cnt2){
System.out.println('a');
}else{
System.out.println('b');
}
}
}

Submission at 2024-11-25 11:19:23

/* A Binary Tree node
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
} */

class Tree
{

Integer findMaxForN(Node root, int n)
{
// Your code goes here
return n;

}
}

Submission at 2024-11-25 11:39:48



/* Node of a linked list
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
Linked List class
class LinkedList
{
Node head; // head of list
}*/

class Solution {
Node intersectPoint(Node head1, Node head2) {
// code here
while(head1!=null && head2!=null){
if(head1.data == head2.data){
return head1;
}else{
head1 = head1.next;
head2 = head2.next;
}
}
return null;
}
}