<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged dynamic-programming</title>
<link>https://coderlegion.com/tag/dynamic-programming</link>
<description>Powered by Question2Answer</description>
<item>
<title>Write a program to find minimum elements in array which has sum equals to given value</title>
<link>https://coderlegion.com/18568/write-a-program-to-find-minimum-elements-in-array-which-has-sum-equals-to-given-value</link>
<description>&lt;div&gt;&lt;p&gt;
Solving problems related to data structures and algorithm has become the norm of many technical interviews in recent times. So whether you are a beginner or seasoned developer, &lt;span style=&quot;color: #FF04FC;&quot;&gt;it is important you keep your problem solving skills active&lt;/span&gt;. In this article we are going to look at how to solve a common coding interview question with a clear explanation of the algorithm. So grab your coffee and let's dive in!
&lt;/p&gt;&lt;/div&gt;&lt;hr&gt;
&lt;div id=&quot;toc-div&quot;&gt;
&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;&lt;strong&gt; Table of Contents:&lt;a id=&quot;toc-toogle&quot; class=&quot;open&quot;&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div id=&quot;toc-list&quot;&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#problem-definition&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Problem Definition&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#solution-to-problem&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Solution to problem&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#understanding-the-concept-of-dynamic-programming&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Understanding the concept of dynamic programming&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#conclusion&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Conclusion&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;#references&quot;&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;References&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div&gt;&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Problem Definition&lt;/span&gt;&lt;a id=&quot;problem-definition&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Given an array &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;arr[]&lt;/code&gt;&lt;/span&gt; and a target integer &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;S&lt;/code&gt;&lt;/span&gt;, the objective is to determine the least amount of elements from the array &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;arr[]&lt;/code&gt;&lt;/span&gt; whose total adds up to the integer &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;S&lt;/code&gt;&lt;/span&gt;, where any element from the array can only be selected once to achieve a sum of &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;S&lt;/code&gt;&lt;/span&gt;.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Test case 1&lt;/b&gt;&lt;br&gt;&lt;b&gt;Inputs&lt;/b&gt;&lt;br&gt;6&lt;br&gt;2, 1, 3, 4, 7&lt;br&gt;&lt;b&gt;Output&lt;/b&gt;: 2&lt;br&gt;&lt;b&gt;Explanation&lt;/b&gt;&lt;br&gt;The first line of the input indicates the target integer S while the second with comma separately integers is the content of the array.&lt;br&gt;Possible solutions:&lt;br&gt;2 + 1 + 3 = 6&lt;br&gt;2 + 4 = 6&lt;br&gt;Therefore the minimum number of elements is 2.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Test case 2&lt;/b&gt;&lt;br&gt;&lt;b&gt;Inputs&lt;/b&gt;&lt;br&gt;4&lt;br&gt;1, 2, 5, 6, 7&lt;br&gt;&lt;b&gt;Output&lt;/b&gt;: -1&lt;br&gt;&lt;b&gt;Explanation&lt;/b&gt;&lt;br&gt;There are not possible solutions since no elements of the array can sum up to give 4 therefore the result is -1&lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Solution to problem&lt;/span&gt;&lt;a id=&quot;solution-to-problem&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Here is an example program in Python that finds the minimum number of elements in an array that add up to a given value:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;
def min_elements_with_sum(arr, target_sum):
    n = len(arr)
    dp = [float('inf')] * (target_sum + 1)
    dp[0] = 0

    for i in range(n):
        for j in range(target_sum, arr[i]-1, -1):
            dp[j] = min(dp[j], dp[j-arr[i]] + 1)

    if dp[target_sum] == float('inf'):
        return -1
    return dp[target_sum]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The program works by using a dynamic programming approach to find the minimum number of elements that add up to a given target sum.&lt;/p&gt;&lt;p&gt;It initializes a list &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;dp&lt;/code&gt;&lt;/span&gt; of size &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;target_sum + 1&lt;/code&gt;&lt;/span&gt; with all elements set to positive infinity.&lt;br&gt;The base case is when the target sum is &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;0&lt;/code&gt;&lt;/span&gt;, in which case the minimum number of elements is &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;0&lt;/code&gt;&lt;/span&gt;.&lt;br&gt;For each element in the input array,&lt;/p&gt;&lt;p&gt;it iterates through the &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;dp&lt;/code&gt;&lt;/span&gt; list in reverse order, starting from the &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;target_sum&lt;/code&gt;&lt;/span&gt; and ending at &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;arr[i]-1&lt;/code&gt;&lt;/span&gt;&lt;br&gt;It updates the value of &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;dp[j]&lt;/code&gt;&lt;/span&gt; with the minimum of the current value and &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;dp[j-arr[i]] + 1&lt;/code&gt;&lt;/span&gt;.&lt;br&gt;If &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;dp[target_sum]&lt;/code&gt;&lt;/span&gt; is still infinity, it means that the target sum cannot be achieved with the given array.&lt;/p&gt;&lt;p&gt;Else, it returns &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;dp[target_sum]&lt;/code&gt;&lt;/span&gt; as the minimum number of elements required to achieve the target sum.&lt;/p&gt;&lt;p&gt;The Time complexity of this algorithm is &lt;span style=&quot;color: #FF04FC;&quot;&gt;O(n*S)&lt;/span&gt; where &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;n&lt;/code&gt;&lt;/span&gt; is the size of array and &lt;span style=&quot;color: #FF04FC;&quot;&gt;&lt;code&gt;S &lt;/code&gt;&lt;/span&gt;is the target sum. Space complexity is &lt;span style=&quot;color: #FF04FC;&quot;&gt;O(S)&lt;/span&gt;.&lt;/p&gt;&lt;p&gt;This algorithm is based on the &lt;span style=&quot;color: #FF04FC;&quot;&gt;Unbounded Knapsack problem&lt;/span&gt; which is a classic problem in dynamic programming where we need to select items from a set such that we maximize the total value of the items while still staying within the given weight (or sum in this case) constraint.&lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Understanding the concept of dynamic programming&lt;/span&gt;&lt;a id=&quot;understanding-the-concept-of-dynamic-programming&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;&lt;span style=&quot;color: #FF04FC;&quot;&gt;Dynamic programming&lt;/span&gt; is a powerful technique for solving complex problems by breaking them down into simpler, overlapping subproblems. It is a method for solving problems by breaking them down into smaller, overlapping subproblems and storing the solutions to these subproblems in a table, called a &quot;&lt;span style=&quot;color: #FF04FC;&quot;&gt;dp table&lt;/span&gt;&quot;, to avoid redundant computation.&lt;/p&gt;&lt;p&gt;Dynamic programming can be applied to both optimization and counting problems. It is particularly useful for solving problems that have an &quot;&lt;span style=&quot;color: #FF04FC;&quot;&gt;optimal substructure&lt;/span&gt;&quot; property, meaning that an optimal solution to the problem can be constructed from optimal solutions to smaller subproblems.&lt;/p&gt;&lt;p&gt;There are two main approaches to dynamic programming: top-down (also known as memoization) and bottom-up (also known as tabulation).&lt;/p&gt;&lt;p&gt;Top-down dynamic programming, also known as &lt;span style=&quot;color: #FF04FC;&quot;&gt;memoization&lt;/span&gt;, involves recursively solving the problem and storing the solutions to subproblems in a table as they are calculated. This approach avoids redundant computation by checking the table for a stored solution before solving a subproblem.&lt;/p&gt;&lt;p&gt;Here is an example of a top-down dynamic programming solution for the problem of finding the nth Fibonacci number:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;
def fibonacci(n, dp):
    if n == 0 or n == 1:
        return n
    if dp[n] is not None:
        return dp[n]
    dp[n] = fibonacci(n-1, dp) + fibonacci(n-2, dp)
    return dp[n]

n = 10
dp = [None] * (n + 1)
print(fibonacci(n, dp))
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Bottom-up dynamic programming, also known as &lt;span style=&quot;color: #FF04FC;&quot;&gt;tabulation&lt;/span&gt;, involves solving the problem by starting with the smallest subproblems and building up to the final solution. This approach avoids the need for recursion and can be more efficient than the top-down approach when the subproblems are not recursive in nature.&lt;/p&gt;&lt;p&gt;Here is an example of a bottom-up dynamic programming solution for the problem of finding the nth Fibonacci number:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;
def fibonacci(n):
    dp = [0, 1] + [0] * (n - 1)
    for i in range(2, n + 1):
        dp[i] = dp[i - 1] + dp[i - 2]
    return dp[n]

print(fibonacci(10))
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dynamic programming can be applied to a wide range of problems, including but not limited to:&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Combinatorial optimization problems, such as the Knapsack problem, the Traveling Salesman problem, and the Longest Common Subsequence problem.&lt;/li&gt;&lt;li&gt;Graph algorithms, such as shortest path algorithms and minimum spanning tree algorithms&lt;/li&gt;&lt;li&gt;String algorithms, such as computing the Levenshtein distance&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Dynamic programming is a powerful technique that can be used to solve complex problems efficiently. However, it can be difficult to design an efficient dynamic programming solution for a problem, and it is not always the best approach for solving a problem.&lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;Conclusion&lt;/span&gt;&lt;a id=&quot;conclusion&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;&lt;p&gt;In this article, we have looked at how to solve a common coding interview question and also explain the concept of dynamic programming. Having this knowledge will help you to tackle similar problems and also to broaden your approach of tackling problems.&lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;h3&gt;&lt;span style=&quot;font-family:arial,helvetica,sans-serif;&quot;&gt;References&lt;/span&gt;&lt;a id=&quot;references&quot; class=&quot;custom_anchor&quot;&gt;&lt;/a&gt;&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Dynamic Programming - Algorithms and Data Structures - https://www.ics.uci.edu/~eppstein/161/960229.html&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/18568/write-a-program-to-find-minimum-elements-in-array-which-has-sum-equals-to-given-value</guid>
<pubDate>Thu, 21 May 2026 12:28:37 +0000</pubDate>
</item>
</channel>
</rss>