Advertisement
Kali_prasad

range dp

Jan 27th, 2025 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4.  
  5. given an array of N integers task is to find the max sum , twist is you are given an integer x and pick a range of elements
  6. and multiply the elements to get max sum .
  7.  
  8.  
  9.  */
  10. /**
  11.  important point to understand here -
  12.  
  13. here x is an integer that means
  14. x can also be negative
  15.  
  16. lets assume you are given an array
  17.  0  1 2 3 4   5//indices
  18. -1 -2 3 4 5 -10
  19.  
  20. and an integer x as -10
  21.  
  22. lets solve this using dp
  23. at any point of time
  24. your arr[i] can be part of multiplcation or need not to be , so you need to consider both the situations
  25.  
  26. when arr[i] is a part of multiplcation 2 possibilities
  27. this multiplication is started long back and still continuing
  28. yyyxx
  29. or
  30. this multiplication is just started now
  31. yyyyx
  32.  
  33. when arr[i] is not a part of multiplcation 3 possibilities
  34. this multiplication is never happened
  35. yyyyy
  36. or
  37. this multiplication is ended just now
  38. yyxxy
  39. or
  40. this multiplication is ended long time back
  41. xxyyy
  42.  
  43.  */
  44.  /*
  45.  
  46. //inputs for array and given an x
  47. -1 -2 3 4 5 -10
  48. -10
  49.  
  50. expected output -
  51. The result is 112
  52.  
  53.  
  54.  */
  55.  
  56.  
  57. @SuppressWarnings("unused")
  58. public class A20250122_RubrikOA {
  59.  
  60.     static Scanner sc = new Scanner(System.in);
  61.  
  62.     private static int[] getArray() {
  63.         String[] sArr = sc.nextLine().split(" ");
  64.         int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
  65.         return arr;
  66.     }
  67.  
  68.     private static char[] getCharArray() {
  69.         String[] sArr = sc.nextLine().split(" ");
  70.         char[] cArr = new char[sArr.length];
  71.         for (int i = 0; i < sArr.length; i++) {
  72.             cArr[i] = sArr[i].charAt(0); // Take the first character of each string
  73.         }
  74.         return cArr;
  75.     }
  76.  
  77.     private static int getMax(int[] arr) {
  78.         int currMax = Integer.MIN_VALUE;
  79.         for (int curr : arr) {
  80.             currMax = Math.max(currMax, curr);
  81.         }
  82.         return currMax;
  83.     }
  84.  
  85.     public static void main(String args[]) {
  86.         // prepare the inputs
  87.  
  88.         int[] arr = getArray();
  89.         int arrLen = arr.length;
  90.         int lastIndex = arrLen - 1;
  91.         int x = getArray()[0];
  92.         int[] kadane = new int[arrLen];//this helps to give best possible sum , without any multiplication
  93.         int sum =0;
  94.         //fill the kadane
  95.         for(int i =0;i<arrLen;i+=1){
  96.             sum = getMax(new int[]{
  97.                 0,//make 0 if sum is becoming negative when involving curr element
  98.             arr[i],//considering the case if the sum be starting with current element
  99.             arr[i]+sum//considering the incoming best sum
  100.             });
  101.  
  102.             kadane[i] = sum;
  103.         }
  104.  
  105.         //initialize the dp
  106.         int[][] dp = new int[arrLen][2];//for every case we need to see wether if current involved in multiplication or if not involved in multiplication
  107.         //0 for not involved , 1 for involved
  108.         //atleast 0 index should be filled manually
  109.  
  110.         dp[0][0] = getMax(new int[]{0,arr[0]});
  111.         dp[0][1] = getMax(new int[]{0,arr[0]*x});
  112.         int maxi = Integer.MIN_VALUE;
  113.         for(int i =1;i<arrLen;i+=1){
  114.             //if not involved
  115.             dp[i][0] = getMax(new int[]{
  116.                 0,arr[i],
  117.                 arr[i]+dp[i-1][0],//multiplication previously long back ended
  118.                 arr[i]+dp[i-1][1],//multiplication just now ended
  119.                 arr[i]+kadane[i-1]//multiplication never happened ,[imp] case
  120.             });
  121.  
  122.             //if involved
  123.             dp[i][1] = getMax(new int[]{
  124.                 0,arr[i]*x,
  125.                 arr[i]*x+dp[i-1][1],//multiplication started from long back
  126.                 arr[i]*x+kadane[i-1]//multiplication just now started
  127.             });
  128.  
  129.             maxi = getMax(new int[]{maxi , dp[i][0], dp[i][1]});
  130.         }
  131.  
  132.  
  133.  
  134.         int ans = maxi;
  135.  
  136.        
  137.  
  138.         int res = ans;
  139.        
  140.         System.out.println("The result is " + res);
  141.  
  142.     }
  143. }
  144.  
  145. class Pair{
  146.     int row;
  147.     int col;
  148.     public Pair(int i,int j){
  149.         this.row = i;
  150.         this.col = j;
  151.        
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement