Advertisement
Kali_prasad

maxMedianSum

Jan 7th, 2025
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4.  
  5. given an N positive integers , task is to  find max median sum , you can find median from any random group of 3 elements from the
  6. array , you cant reuse the same elements ,once you used in group , you can form any number of 3 element groups from array
  7.  
  8.  
  9.  
  10.  
  11.  */
  12. /**
  13.  important point to understand here -
  14.  
  15.  you can form max n/3 groups from array,lets use all of them as all are positive numbers and task is to maximise
  16. lets assume some nums exist in sorted order
  17. a b c d x y z
  18. z can never be median nor can be reused , so focus on y's in the group
  19. think how you can maximise the y's in the median group
  20.  
  21. in a sorted array as above
  22.  
  23. you can form a y z instead of x y z
  24. you can form b d x instead of b c d
  25.  
  26. so for left side elements you are taking min elements to cover left part, which helps you to be your medians as max as possible
  27.  
  28.  */
  29.  /*
  30.  
  31. //inputs for array , given you positive integer array
  32.  
  33. 7 8 9 4 5 6 10 1 2 3
  34.  
  35.  
  36. expected output -
  37. The result is 21
  38.  
  39.  
  40.  */
  41.  
  42.  
  43. @SuppressWarnings("unused")
  44. public class A20250107_maxMedianSum {
  45.  
  46.     static Scanner sc = new Scanner(System.in);
  47.  
  48.     private static int[] getArray() {
  49.         String[] sArr = sc.nextLine().split(" ");
  50.         int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
  51.         return arr;
  52.     }
  53.  
  54.     private static char[] getCharArray() {
  55.         String[] sArr = sc.nextLine().split(" ");
  56.         char[] cArr = new char[sArr.length];
  57.         for (int i = 0; i < sArr.length; i++) {
  58.             cArr[i] = sArr[i].charAt(0); // Take the first character of each string
  59.         }
  60.         return cArr;
  61.     }
  62.  
  63.     private static int getMax(int[] arr) {
  64.         int currMax = Integer.MIN_VALUE;
  65.         for (int curr : arr) {
  66.             currMax = Math.max(currMax, curr);
  67.         }
  68.         return currMax;
  69.     }
  70.  
  71.     public static void main(String args[]) {
  72.         // prepare the inputs
  73.  
  74.         int[] arr = getArray();
  75.         int arrLen = arr.length;
  76.         int lastIndex = arrLen - 1;
  77.         Arrays.sort(arr);
  78.  
  79.         int possibleGroupsCount = (int) Math.floor(arrLen/3);
  80.         int sum=0;
  81.         for(int j=lastIndex-1;j>=0;j-=2){
  82.             sum+=arr[j];
  83.             possibleGroupsCount--;
  84.             if(possibleGroupsCount<=0){
  85.                 break;
  86.             }
  87.         }
  88.  
  89.        
  90.  
  91.         int res = sum;
  92.        
  93.         System.out.println("The result is " + res);
  94.  
  95.     }
  96. }
  97.  
  98. class Pair{
  99.     int row;
  100.     int col;
  101.     public Pair(int i,int j){
  102.         this.row = i;
  103.         this.col = j;
  104.        
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement