
Now, we will compare this window sum with the maximum_sum. Hence, we will get our new window sum by subtracting 5 and then adding 0 to it. Therefore, now it discards 5 from the window and adds 0 to the window.
Now, we slide our window by a unit index. Now, we set the maximum_sum as current_window i.e 6. This is the initial phase where we have calculated the initial window sum starting from index 0. The below representation will make it clear how the window slides over the array.Ĭonsider an array arr = and value of k = 3 and n = 5
To get the current sum of a block of k elements just subtract the first element from the previous block and add the last element of the current block. Then we will graze linearly over the array till it reaches the end and simultaneously keep track of the maximum sum. We compute the sum of the first k elements out of n terms using a linear loop and store the sum in variable window_sum. The pane will cover the next k consecutive elements. Now, if we apply force on the window, it moves a unit distance ahead. Now, co-relate the window with array arr of size n and pane with current_sum of size k elements. Consider, initially the pane is at extreme left i.e., at 0 units from the left. Sliding Window Technique: The technique can be best understood with the window pane in the bus, considering a window of length n and the pane which is fixed in it of length k. ISRO CS Syllabus for Scientist/Engineer Exam. ISRO CS Original Papers and Official Keys. GATE CS Original Papers and Official Keys. Cellular automaton circular patterns full#
Full Stack Development with React & Node JS(Live).Android App Development with Kotlin(Live).
Java Programming - Beginner to Advanced. Data Structure & Algorithm-Self Paced(C++/JAVA). Data Structure & Algorithm Classes (Live).