1+ .. include :: ./refsub.rst
2+
13Example Gallery
24===============
35
4- .. currentmodule :: manim_data_structures.m_array
5-
66Find Pair Sum In Sorted MArray
77------------------------------
88
99The code snippet below uses the famous two pointer technique to find the pair sum ``17 `` in the sorted array ``[2, 3, 5, 8, 9, 10, 11] ``.
1010
11- .. manim :: MainScene
11+ .. manim :: PairSumExample
1212 :quality: low
1313
1414 from manim_data_structures import *
1515
16- class MainScene (Scene):
16+ class PairSumExample (Scene):
1717 def isPairSumAnim(self, arr, n, val):
1818 p_i = MArrayPointer(self, arr, 0, 'i', mob_arrow_args={'color': GREEN}, mob_label_args={'color': GREEN})
1919 p_j = MArrayPointer(self, arr, n - 1, 'j', mob_arrow_args={'color': YELLOW}, mob_label_args={'color': YELLOW})
@@ -27,14 +27,14 @@ The code snippet below uses the famous two pointer technique to find the pair su
2727 pair_sum.update_value(arr.fetch_arr()[p_i.fetch_index()] + arr.fetch_arr()[p_j.fetch_index()])
2828
2929 if (pair_sum.fetch_value() == val):
30- pair_sum.fetch_mob_square().set(fill_color=GREEN)
30+ self.play( pair_sum.fetch_mob_square().animate. set(fill_color=GREEN) )
3131 return True
3232 elif(pair_sum.fetch_value() < val):
3333 p_i.shift_to_elem(p_i.fetch_index() + 1)
3434 else:
3535 p_j.shift_to_elem(p_j.fetch_index() - 1)
3636
37- pair_sum.fetch_mob_square().set(fill_color=RED)
37+ self.play( pair_sum.fetch_mob_square().aniamte. set(fill_color=RED) )
3838 return False
3939
4040 def construct(self):
@@ -43,3 +43,44 @@ The code snippet below uses the famous two pointer technique to find the pair su
4343 self.add(arr)
4444 self.isPairSumAnim(arr, 7, 17)
4545 self.wait(1)
46+
47+ Maxmimum Sum of K Consecutive Integers in MArray
48+ ------------------------------------------------
49+
50+ The code snippet below uses the sliding window technique to find the maximum sum of ``k = 4 `` consecutive elements in the array ``[1, 4, 2, 10, 2, 3, 1, 0, 20] ``
51+
52+ .. manim :: PairSumExample
53+ :quality: low
54+
55+ from manim_data_structures import *
56+
57+ class PairSumExample(Scene):
58+ def maxSumAnim(self, marr: MArray, k):
59+ arr = marr.fetch_arr()
60+ n = len(arr)
61+
62+ window = MArraySlidingWindow(self, marr, 0, k, 'Window')
63+ var_window_sum = MVariable(self, sum(arr[:k]), label='Window Sum')
64+ var_max_sum = MVariable(self, var_window_sum.fetch_value(), label='Max Sum')
65+ var_window_sum.shift(DOWN)
66+ var_max_sum.shift(DOWN * 2.5)
67+
68+ self.play(Create(window))
69+ self.play(Create(var_window_sum), Create(var_max_sum))
70+
71+ for i in range(n - k):
72+ window.shift_to_elem(i + 1)
73+ var_window_sum.update_value(var_window_sum.fetch_value() - arr[i] + arr[i + k])
74+ if var_window_sum.fetch_value() > var_max_sum.fetch_value():
75+ var_max_sum.update_value(var_window_sum.fetch_value())
76+
77+
78+ self.play(var_max_sum.fetch_mob_square().animate.set(fill_color=GREEN))
79+ return var_max_sum.fetch_value()
80+
81+ def construct(self):
82+ arr = MArray(self, [1, 4, 2, 10, 2, 3, 1, 0, 20], label='Array')
83+ arr.shift(UP * 1.5 + LEFT * 4)
84+ self.add(arr)
85+ self.maxSumAnim(arr, 4)
86+ self.wait(1)
0 commit comments