Advertisement
Kali_prasad

A20250603_Amazon_Hackon

Jun 4th, 2025
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. /*
  2. A20250603_Amazon_Hackon
  3. #math
  4.  
  5. you are taking a loan for m amount ,which you need to pay the intrest
  6. but the thing is
  7. you can take them as portions
  8. so for each portions you will have seperate interest
  9. the desired amount for you is >=2 and also you cannot have a portion which is <2
  10. now lets say you have divided them into portions
  11. for the current portion p you can pay (p/k) interest
  12. where k is smallest factor of p which is other than 1
  13. summing the interest of all these portions will give you the final interest i
  14. you need to try to minimise this i
  15.  
  16. -----------------------------------------------------------------------------------------------------
  17.  
  18. important thing to be noted here is -
  19.  
  20.  
  21. lets say you are taking loan of
  22. 7
  23. this can be divided into various ways
  24. like
  25. 2 2 3
  26. 4 3
  27. 2 5
  28. 7
  29. their interests would be
  30. 1 1 1
  31. 2 1
  32. 1 1
  33. 1
  34.  
  35. if you observe one thing ,interests of prime portions are always 1
  36. because the lowest factor other than 1 is always the number itself for a prime
  37. so you try to make your required amount as sum of primes may be like below
  38. 2 2 3
  39. 2 5
  40. 7
  41. now out of all these prime sums we need to know which is the best way ,that requires less number of possible primes
  42. may be you can think greedily like attacking the number and shrinking to smallest possible number
  43.  
  44. but that may not gaurantee you it is the best way  [D] [R] [v]
  45.  
  46. now to solve this we need goldbach's conjecture theorem
  47. he says
  48. for even's (other than 2) can be represented as just sum of 2 primes [BOOOOM] //strong conjecture (still unproven)
  49. say 42 this will be broken down as 23 and 19
  50. like that it is applied for every even number
  51.  
  52. for odd numbers it can be either shown as sum of 1 prime num,2 prime nums,3 prime nums at max
  53. hmm
  54. 1 prime is the odd num is prime itself
  55. 2 prime is (curr odd) = odd + even //even + even can never be odd number and odd + odd can never be odd
  56. now how do we will know which odd + even is the sum that makes curr odd
  57. hmm note that we have constraint that the portions should be prime
  58. that means the even prime is only 2 then you just need to check wether curr odd-2 is prime or not
  59. if the above conditions got failed ,inevitably the answer is 3 ;)
  60.  
  61.  
  62.  
  63.  
  64. -----------------------------------------------------------------------------------------------------
  65.  
  66. input -
  67. 8
  68.  
  69.  
  70. output -
  71. 2
  72.  
  73. */
  74.  
  75. import java.util.*;
  76.  
  77.  
  78.  
  79.  
  80.  
  81. @SuppressWarnings({"unused","unchecked"})
  82. public class A20250603_Amazon_Hackon{
  83.  
  84.     static Scanner sc = new Scanner(System.in);
  85.  
  86.     private static int[] getArray() {
  87.         String[] sArr = sc.nextLine().split(" ");
  88.         int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
  89.         return arr;
  90.     }
  91.  
  92.     private static char[] getCharArray() {
  93.         String[] sArr = sc.nextLine().split(" ");
  94.         char[] cArr = new char[sArr.length];
  95.         for (int i = 0; i < sArr.length; i++) {
  96.             cArr[i] = sArr[i].charAt(0); // Take the first character of each string
  97.         }
  98.         return cArr;
  99.     }
  100.  
  101.     private static int getMax(int[] arr) {
  102.         int currMax = Integer.MIN_VALUE;
  103.         for (int curr : arr) {
  104.             currMax = Math.max(currMax, curr);
  105.         }
  106.         return currMax;
  107.     }
  108.     public static boolean isPrime(int curr){
  109.         for(int i = 2;i*i<=curr;i+=1){
  110.             if(curr%i==0){
  111.                 // System.out.println("i is "+i);
  112.                 return false;
  113.             }
  114.         }
  115.         return true;
  116.     }
  117.  
  118.     public static void main(String args[]) {
  119.         // prepare the inputs
  120.  
  121.  
  122.        int loanAmount = getArray()[0];
  123.        int ans = -1;
  124.        if(loanAmount % 2 == 0){//even ,strong conjecture
  125.          if(loanAmount == 2){
  126.             ans = 1;
  127.          }else{
  128.             ans = 2;
  129.          }
  130.        }else{
  131.          if(isPrime(loanAmount)){
  132.             ans = 1;
  133.          }else if(isPrime(loanAmount-2)){
  134.             ans = 2;
  135.          }else{
  136.             ans = 3;
  137.          }
  138.        }
  139.        System.out.println("ans is "+ans);
  140.  
  141.     }
  142. }
  143.  
  144. class Pair{
  145.     int row;
  146.     int col;
  147.     public Pair(int i,int j){
  148.         this.row = i;
  149.         this.col = j;
  150.        
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement