you give array positive integer bean integer represent number magic bean find particular magic bagremove number bean possibly bag number bean remain nonempty bag contain bean equal once bean remove bag allow return bagsreturn minimum number magic bean remove Example 1input bean 4165output 4explanation we remove 1 bean bag 1 bean this result remain bag 4065 then remove 2 bean bag 6 bean this result remain bag 4045 then remove 1 bean bag 5 bean this result remain bag 4044we remove total 1 2 1 4 bean remain nonempty bag equal number beansThere solution remove 4 bean fewerexample 2input bean 21032output 7explanation we remove 2 bean bag 2 bean this result remain bag 01032 then remove 2 bean bag 2 bean this result remain bag 01030 then remove 3 bean bag 3 bean this result remain bag 01000we remove total 2 2 3 7 bean remain nonempty bag equal number beansthere solution remove 7 bean few Constraints1 beanslength 1051 beansi 105