A number can always be represented as a sum of squares of other numbers. Note that 1 is a square, and we can always break a number as (1*1 + 1*1 + 1*1 + …). Given a number N, the task is to represent N as the sum of minimum square numbers.
Input : 10
Output : 1 + 9
These are all possible ways
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 4
1 + 1 + 4 + 4
1 + 9
Choose one with minimum numbers
Input : 25
Output : 25
Prerequisites: Minimum number of squares whose sum equals to given number N
Approach: This is a typical application of dynamic programming. When we start from N = 6, we can reach 2 by subtracting the square of one i.e. one, 4 times, and by subtracting the square of two i.e. four, 1 time. So the subproblem for 2 is called twice.
Since the same subproblems are called again, this problem has Overlapping Subproblems property. So-min square sum problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputation of the same subproblems can be avoided by constructing a temporary array table in a bottom-up manner.
Below is the implementation of the above approach:
1 + 9
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum number of squares whose sum equals to a given number N | Set-3
- Minimum number of squares whose sum equals to given number n
- Minimum number of cubes whose sum equals to given number N
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Array containing power of 2 whose XOR and Sum of elements equals X
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N)
- Largest sub-array whose all elements are perfect squares
- Count of pairs of integers whose difference of squares is equal to N
- Paper Cut into Minimum Number of Squares | Set 2
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Find K numbers with sum equal to N and sum of their squares maximized
- Sum of squares of all Subsets of given Array
- Count of primes in a given range that can be expressed as sum of perfect squares
- Maximum possible sum of squares of stack elements satisfying the given properties
- Paper Cut into Minimum Number of Squares
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Smallest number greater or equals to N such that it has no odd positioned bit set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.