Variable Size Sliding Window | Largest Subarray of sum K | Part1 and Part 2
Added 2020-11-29 13:48:44 +0000 UTCComments
Samarpan can you forward the code ?
shishir chaurasiya
2023-01-13 04:53:23 +0000 UTCUse Hashmap for negative integers and one loop
Samarpan Biswas
2021-08-13 16:24:39 +0000 UTCi dont think this code is working for negative integers
Srikar reddy
2021-06-28 01:44:29 +0000 UTCWill you please explain the case where the whole array is iterated once but still we arent checking all the different possible windows?
Manali Sathe
2021-01-13 05:36:47 +0000 UTCYes the code fails for few cases I've observed that too. It fails when you are removing the elements in while loop and the sun becomes exactly 5. In the next iteration you'll increment j and you'll loose that case
Krishna Prem
2021-01-04 19:28:42 +0000 UTCAditya!! The code is failing for arr=[1,1,5] k=5. It is returning '0', should return '1'
Krishna Prem
2021-01-04 11:22:56 +0000 UTCpublic class MaximumAllsubarrays { public static void main(String[] args) { //i j int[] i = new int[] {4,1,1,1,2,3,5}; int k = 5; int max = maxOfAllSubArray(i,k); System.out.println(max); } private static int maxOfAllSubArray(int[] arr, int k) { int sum = 0; int i =0; int j =0; int mx = 0; while(j < arr.length) { sum = sum + arr[j]; if(sum < k) { j++; } else if(sum == k) { System.out.println("Condition Met"); mx=Math.max(mx,j-i+1); j++; } else if(sum>k) { while(sum>k) { sum=sum-arr[i]; i++; } j++; } } return mx; } }
maanasa subrahmanyam
2020-12-29 15:59:29 +0000 UTCPlease see below. I have this code. The condition should be met 4 times at least, but it only meets two times. I have tried to debug.
maanasa subrahmanyam
2020-12-29 15:59:23 +0000 UTC