Skip to main content

quick sort

Nov 19, 2022CodeCatch
Loading...

More Java Posts

Contains Duplicate Java

May 21, 2025AustinLeath

0 likes • 2 views

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

Untitled

May 15, 2025AustinLeath

0 likes • 2 views

import java.util.Arrays;
public class Main {
public static int findMinNonCreatableSum(int[] coins) {
// Sort the array
Arrays.sort(coins);
// Initialize max creatable sum
int maxCreatable = 0;
// Iterate through coins
for (int coin : coins) {
// If current coin is greater than maxCreatable + 1,
// we found our answer
if (coin > maxCreatable + 1) {
return maxCreatable + 1;
}
// Add current coin to maxCreatable
maxCreatable += coin;
}
// If we can create all sums up to maxCreatable,
// the answer is maxCreatable + 1
return 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);
}
}

longest sequence

Oct 7, 2023AustinLeath

0 likes • 5 views

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
}
}

Factorial

Nov 19, 2022CodeCatch

0 likes • 0 views

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

Reverse String Java

May 21, 2025AustinLeath

0 likes • 0 views

try-catch modularity

Jan 31, 2023AustinLeath

0 likes • 0 views

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 here
System.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 user
if (id < 0) {
throw new Exception("Invalid user id");
}
// Return the user object
return 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;
}
}