Leaqio.com
Aditya Verma from patreon
Aditya Verma patreon

Variable Size Sliding Window | Largest Subarray of sum K | Part1 and Part 2

🕑 Added 2020-11-29 13:48:44 +0000 UTC

Comments

shishir chaurasiya

Samarpan can you forward the code ?

Samarpan Biswas

Use Hashmap for negative integers and one loop

Srikar reddy

i dont think this code is working for negative integers

Manali Sathe

Will you please explain the case where the whole array is iterated once but still we arent checking all the different possible windows?

Krishna Prem

Yes 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

Aditya!! The code is failing for arr=[1,1,5] k=5. It is returning '0', should return '1'

maanasa subrahmanyam

public 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

Please 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.


More Creators