Loading...
More Java Posts
class main {public static boolean findDuplicates(int[] nums) {for(int i = 0; i < nums.length; i++) {for(int j = 0; j < i; j++) {if(nums[i] == nums[j]) {return true;}}}return false;}public static void main(String[] args) {int[] myArray = {1,3};System.out.println(findDuplicates(myArray));}}
import java.util.Arrays;public class Main {public static int findMinNonCreatableSum(int[] coins) {// Sort the arrayArrays.sort(coins);// Initialize max creatable sumint maxCreatable = 0;// Iterate through coinsfor (int coin : coins) {// If current coin is greater than maxCreatable + 1,// we found our answerif (coin > maxCreatable + 1) {return maxCreatable + 1;}// Add current coin to maxCreatablemaxCreatable += coin;}// If we can create all sums up to maxCreatable,// the answer is maxCreatable + 1return maxCreatable + 1;}public static void main(String[] args) {int[] coins = {2, 9, 1, 2, 7};int result = findMinNonCreatableSum(coins);System.out.println("Smallest sum that cannot be created: " + result);}}
import java.util.Arrays;public class LongestIncreasingSubsequence {public int lengthOfLIS(int[] nums) {if (nums.length == 0) {return 0;}int[] dp = new int[nums.length];Arrays.fill(dp, 1); // Initialize all elements in dp array to 1, as each element itself is a valid subsequence of length 1.int maxLength = 1; // Initialize the maximum length to 1, considering a single element as the sequence.for (int i = 1; i < nums.length; i++) {for (int j = 0; j < i; j++) {if (nums[i] > nums[j]) {dp[i] = Math.max(dp[i], dp[j] + 1); // If nums[i] is greater than nums[j], update the longest subsequence length ending at i.}}maxLength = Math.max(maxLength, dp[i]); // Update the maximum length if needed.}return maxLength;}public static void main(String[] args) {LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();int[] nums = {10, 9, 2, 5, 3, 7, 101, 18};int result = lis.lengthOfLIS(nums);System.out.println("Length of the Longest Increasing Subsequence: " + result); // Output: 4}}
public class Factorial{public static void main(String[] args){ final int NUM_FACTS = 100;for(int i = 0; i < NUM_FACTS; i++)System.out.println( i + "! is " + factorial(i));}public static int factorial(int n){ int result = 1;for(int i = 2; i <= n; i++)result *= i;return result;}}
public class UserService {private UserRepository userRepository;public UserService(UserRepository userRepository) {this.userRepository = userRepository;}public User getUserById(long id) {try {return userRepository.getUserById(id);} catch (Exception e) {// Log the exception hereSystem.out.println("An error occurred while fetching the user: " + e.getMessage());return null;}}}public class UserRepository {public User getUserById(long id) throws Exception {// Database query to fetch the userif (id < 0) {throw new Exception("Invalid user id");}// Return the user objectreturn new User(id, "John Doe");}}public class User {private long id;private String name;public User(long id, String name) {this.id = id;this.name = name;}public long getId() {return id;}public String getName() {return name;}}