Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Solving Algorithm Problems in a Lantern Festival Themed Contest

Tech May 9 3

A. Festival Greeting

A simple greetign problem to start the contest.

public class Greeting {
    public static void main(String[] args) {
        System.out.println("Happy Lantern Festival!");
    }
}

B. Lantern Riddles

Simulate solving lantern riddles in a circular arrangement.

import java.util.*;

public class RiddleSolver {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int size = input.nextInt();
        int[] nums = new int[size];
        for (int i = 0; i < size; i++) {
            nums[i] = input.nextInt();
        }
        
        List<Integer> results = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            int left = (i - 1 + size) % size;
            int right = (i + 1) % size;
            results.add(nums[left] + nums[right]);
        }
        System.out.println(String.join(" ", 
            results.stream().map(String::valueOf).toArray(String[]::new)));
    }
}

C. Math Genius

Calculate the sum of absolute values of array elements.

import java.util.Scanner;

public class AbsoluteSum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        long total = 0;
        
        for (int i = 0; i < count; i++) {
            total += Math.abs(sc.nextLong());
        }
        System.out.println(total);
    }
}

D. Equation Solver

Solve a specific quadratic equation problem.

import java.util.Scanner;

public class EquationChecker {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int cases = input.nextInt();
        
        while (cases-- > 0) {
            long value = input.nextLong();
            if (value % 4 != 0) {
                System.out.println("No");
            } else {
                long solution = value / 4 - 1;
                if (solution <= 0 || solution % 2 == 0) {
                    System.out.println("No");
                } else {
                    System.out.println("Yes");
                    System.out.println(solution + " " + (solution + 2));
                }
            }
        }
    }
}

E. Isosceles Triangle

Find maximum number of valid isosceles triangle pairs.

import java.util.Arrays;
import java.util.Scanner;

public class TriangleFinder {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] sides = new int[n];
        int[] bases = new int[n];
        
        for (int i = 0; i < n; i++) sides[i] = sc.nextInt();
        for (int i = 0; i < n; i++) bases[i] = sc.nextInt();
        
        Arrays.sort(sides);
        Arrays.sort(bases);
        
        int count = 0, index = 0;
        for (int base : bases) {
            while (index < n && sides[index] * 2 <= base) {
                index++;
            }
            if (index < n) {
                index++;
                count++;
            }
        }
        System.out.println(count);
    }
}

F. Equation Calculation

Use binary seearch to solve a logarithmic equation.

import java.util.Scanner;

public class EquationBinarySearch {
    static boolean isValid(int x, int k, int m) {
        double root = Math.sqrt(x);
        double log = Math.log(x) / Math.log(k);
        return root + log > m;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int tests = sc.nextInt();
        
        while (tests-- > 0) {
            int k = sc.nextInt(), m = sc.nextInt();
            int left = 1, right = m * m;
            while (left <= right) {
                int mid = left + (right - left) / 2;
                if (isValid(mid, k, m)) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            System.out.println(left);
        }
    }
}
Tags: algorithm

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.