Advertisement
Kali_prasad

pick a range and replace from 2nd array to get max sum subarr in 1st

Jan 27th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4.  
  5. given 2 array of N size integers task is to find the max sum of arrA, twist is you can pick a range of elements from 2nd arr
  6. and replace the same index elements in 1st array to get max sum .
  7.  
  8.  
  9. */
  10. /**
  11. important point to understand here -
  12.  
  13.  
  14.  
  15. lets assume you are given 2 arrays
  16.  
  17. -100 -20 10 -100 -1000//arrA
  18. -100 -200 -100 100 -1000//arrB
  19.  
  20.  
  21.  
  22. lets solve this using dp
  23. at any point of time
  24. your arr[i] can be part of replacement or need not to be , so you need to consider both the situations
  25.  
  26. when arr[i] is a part of replacement 2 possibilities
  27. this replacement is started long back and still continuing
  28. yyxxx
  29. or
  30. this replacement is just started now
  31. yyyyx
  32.  
  33. when arr[i] is not a part of replacement 3 possibilities
  34. this replacement is never happened
  35. yyyyy
  36. or
  37. this replacement is ended just now
  38. yyxxy
  39. or
  40. this replacement is ended long time back
  41. xxyyy
  42.  
  43. */
  44. /*
  45.  
  46. //inputs for 2 arrays
  47. -100 -20 10 -100 -1000
  48. -100 -200 -100 100 -1000
  49.  
  50. expected output -
  51. The result is 110
  52.  
  53.  
  54. */
  55.  
  56.  
  57. @SuppressWarnings("unused")
  58. public class A20250123_GoogleOA {
  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[] arrA = getArray();
  89. int[] arrB = getArray();
  90. int arrLen = arrA.length;
  91.  
  92. int lastIndex = arrLen - 1;
  93. int[] kadane = new int[arrLen];//this helps to give best possible sum , without any replacement
  94. int sum =0;
  95. //fill the kadane
  96. for(int i =0;i<arrLen;i+=1){
  97. sum = getMax(new int[]{
  98. 0,//make 0 if sum is becoming negative when involving curr element
  99. arrA[i],//considering the case if the sum be starting with current element
  100. arrA[i]+sum//considering the incoming best sum
  101. });
  102.  
  103. kadane[i] = sum;
  104. }
  105.  
  106. //initialize the dp
  107. int[][] dp = new int[arrLen][2];//for every case we need to see wether if current involved in replacement or if not involved in replacement
  108. //0 for not involved , 1 for involved
  109. //atleast 0 index should be filled manually
  110.  
  111. dp[0][0] = getMax(new int[]{0,arrA[0]});
  112. dp[0][1] = getMax(new int[]{0,arrB[0]});
  113. int maxi = Integer.MIN_VALUE;
  114.  
  115. for(int i =1;i<arrLen;i+=1){
  116. //if not involved
  117. dp[i][0] = getMax(new int[]{
  118. 0,arrA[i],
  119. arrA[i]+dp[i-1][0],//replacement previously long back ended
  120. arrA[i]+dp[i-1][1],//replacement just now ended
  121. arrA[i]+kadane[i-1]//replacement never happened ,[imp] case
  122. });
  123.  
  124. //if involved
  125. dp[i][1] = getMax(new int[]{
  126. 0,arrB[i],
  127. arrB[i]+dp[i-1][1],//replacement started from long back
  128. arrB[i]+kadane[i-1]//replacement just now started
  129. });
  130.  
  131. maxi = getMax(new int[]{maxi , dp[i][0], dp[i][1]});
  132.  
  133. }
  134.  
  135.  
  136.  
  137. int ans = maxi;
  138.  
  139.  
  140.  
  141. int res = ans;
  142.  
  143. System.out.println("The result is " + res);
  144.  
  145. }
  146. }
  147.  
  148. class Pair{
  149. int row;
  150. int col;
  151. public Pair(int i,int j){
  152. this.row = i;
  153. this.col = j;
  154.  
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement