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.
Examples:
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 numbersInput : 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 the 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: 
C++
| // C++ program to represent N as the// sum of minimum square numbers.#include <bits/stdc++.h>usingnamespacestd;// Function for finding// minimum square numbersvector<int> minSqrNum(intn){  // A[i] of array arr store  // minimum count of  // square number to get i  intarr[n + 1], k;  // sqrNum[i] store last  // square number to get i  intsqrNum[n + 1];  vector<int> v;  // Initialize  arr[0] = 0;  sqrNum[0] = 0;  // Find minimum count of  // square number for  // all value 1 to n  for(inti = 1; i <= n; i++)   {    // In worst case it will    // be arr[i-1]+1 we use all    // combination of a[i-1] and add 1    arr[i] = arr[i - 1] + 1;    sqrNum[i] = 1;    k = 1;    // Check for all square    // number less or equal to i    while(k * k <= i)     {      // if it gives less      // count then update it      if(arr[i] > arr[i - k * k] + 1)       {        arr[i] = arr[i - k * k] + 1;        sqrNum[i] = k * k;      }      k++;    }  }  // Vector v stores optimum  // square number whose sum give N  while(n > 0)   {    v.push_back(sqrNum[n]);    n -= sqrNum[n];  }  returnv;}// Driver codeintmain(){  intn = 10;  vector<int> v;  // Calling function  v = minSqrNum(n);  // Printing vector  for(autoi = v.begin();             i != v.end(); i++)   {    cout << *i;    if(i + 1 != v.end())      cout << " + ";  }  return0;} | 
Java
| // Java program to represent // N as the sum of minimum // square numbers.importjava.util.*;classGFG{// Function for finding// minimum square numbersstaticVector<Integer> minSqrNum(intn){  // A[i] of array arr store  // minimum count of  // square number to get i  int[]arr = newint[n + 1];  intk = 0;  // sqrNum[i] store last  // square number to get i  int[]sqrNum = newint[n + 1];  Vector<Integer> v = newVector<>();  // Initialize  arr[0] = 0;  sqrNum[0] = 0;  // Find minimum count of  // square number for  // all value 1 to n  for(inti = 1; i <= n; i++)   {    // In worst case it will    // be arr[i-1]+1 we use all    // combination of a[i-1] and add 1    arr[i] = arr[i - 1] + 1;    sqrNum[i] = 1;    k = 1;    // Check for all square    // number less or equal to i    while(k * k <= i)     {      // if it gives less      // count then update it      if(arr[i] > arr[i - k * k] + 1)       {        arr[i] = arr[i - k * k] + 1;        sqrNum[i] = k * k;      }      k++;    }  }  // Vector v stores optimum  // square number whose sum give N  while(n > 0)   {    v.add(sqrNum[n]);    n -= sqrNum[n];  }  returnv;}// Driver codepublicstaticvoidmain(String[] args){  intn = 10;  Vector<Integer> v;  // Calling function  v = minSqrNum(n);  // Printing vector  for(inti = 0; i <v.size(); i++)   {    System.out.print(v.elementAt(i));    if(i+1!= v.size())      System.out.print(" + ");  }}}// This code is contributed by gauravrajput1 | 
Python3
| # Python3 program to represent N as the# sum of minimum square numbers.# Function for finding# minimum square numbersdefminSqrNum(n):    # arr[i] of array arr store    # minimum count of    # square number to get i    arr =[0] *(n +1)        # sqrNum[i] store last    # square number to get i    sqrNum =[0] *(n +1)    v =[]    # Find minimum count of    # square number for    # all value 1 to n    fori inrange(n +1):                # In worst case it will        # be arr[i-1]+1 we use all        # combination of a[i-1] and add 1        arr[i] =arr[i -1] +1        sqrNum[i] =1        k =1;                # Check for all square        # number less or equal to i        while(k *k <=i):                        # If it gives less            # count then update it            if(arr[i] > arr[i -k *k] +1):                arr[i] =arr[i -k *k] +1                sqrNum[i] =k *k            k +=1    # v stores optimum    # square number whose sum give N    while(n > 0):        v.append(sqrNum[n])        n -=sqrNum[n];            returnv# Driver coden =10# Calling functionv =minSqrNum(n)# Printing vectorfori inrange(len(v)):    print(v[i], end ="")        if(i < len(v) -1):        print(" + ", end ="")        # This article is contributed by Apurvaraj | 
C#
| // C# program to represent // N as the sum of minimum // square numbers.usingSystem;usingSystem.Collections.Generic;classGFG{// Function for finding// minimum square numbersstaticList<int> minSqrNum(intn){  // A[i] of array arr store  // minimum count of  // square number to get i  int[]arr = newint[n + 1];  intk = 0;  // sqrNum[i] store last  // square number to get i  int[]sqrNum = newint[n + 1];  List<int> v = newList<int>();  // Initialize  arr[0] = 0;  sqrNum[0] = 0;  // Find minimum count of  // square number for  // all value 1 to n  for(inti = 1; i <= n; i++)   {    // In worst case it will    // be arr[i-1]+1 we use all    // combination of a[i-1] and add 1    arr[i] = arr[i - 1] + 1;    sqrNum[i] = 1;    k = 1;    // Check for all square    // number less or equal to i    while(k * k <= i)     {      // if it gives less      // count then update it      if(arr[i] > arr[i - k * k] + 1)       {        arr[i] = arr[i - k * k] + 1;        sqrNum[i] = k * k;      }      k++;    }  }  // List v stores optimum  // square number whose sum give N  while(n > 0)   {    v.Add(sqrNum[n]);    n -= sqrNum[n];  }  returnv;}// Driver codepublicstaticvoidMain(String[] args){  intn = 10;  List<int> v;  // Calling function  v = minSqrNum(n);  // Printing vector  for(inti = 0; i <v.Count; i++)   {    Console.Write(v[i]);    if(i+1 != v.Count)      Console.Write(" + ");  }}}// This code is contributed by gauravrajput1 | 
Javascript
| <script>// Javascript program to represent N as the// sum of minimum square numbers.// Function for finding// minimum square numbersfunctionminSqrNum(n){  // A[i] of array arr store  // minimum count of  // square number to get i  vararr = Array(n+1), k;  // sqrNum[i] store last  // square number to get i  varsqrNum = Array(n+1);  varv = [];  // Initialize  arr[0] = 0;  sqrNum[0] = 0;  // Find minimum count of  // square number for  // all value 1 to n  for(vari = 1; i <= n; i++)   {    // In worst case it will    // be arr[i-1]+1 we use all    // combination of a[i-1] and add 1    arr[i] = arr[i - 1] + 1;    sqrNum[i] = 1;    k = 1;    // Check for all square    // number less or equal to i    while(k * k <= i)     {      // if it gives less      // count then update it      if(arr[i] > arr[i - k * k] + 1)       {        arr[i] = arr[i - k * k] + 1;        sqrNum[i] = k * k;      }      k++;    }  }  // Vector v stores optimum  // square number whose sum give N  while(n > 0)   {    v.push(sqrNum[n]);    n -= sqrNum[n];  }  returnv;}// Driver codevarn = 10;varv = [];// Calling functionv = minSqrNum(n);// Printing vectorfor(vari = 0; i<v.length; i++){    document.write(v[i]);  if(i + 1 != v.length)    document.write( " + ");}</script> | 
1 + 9
Time Complexity: O(n3/2)
Auxiliary Space: O(n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

 
                                    







