I do not have much knowledge abt java.
I have code which is a recursive method. But when I am trying to convert it into Iterator method I am getting runtime error
package com.algo;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;
class inputIter
{
private static HashMap<integer,>> cachedSolutions;
public static void main(String[] args)
{
cachedSolutions = new HashMap<integer,>>();
for (int j = 12; j < 13; ++j)
{
ArrayList<integer> answer = minLen(j);
Collections.sort(answer);
Collections.reverse(answer);
for (int i = 0; i < answer.size(); ++i)
{
if (i != 0)
System.out.printf("+");
System.out.printf("%d^2", answer.get(i));
}
System.out.println();
}
}
public static ArrayList<integer> minLen(int n)
{
ArrayList<integer> guess = new ArrayList<integer>();
while(n >0){
if (cachedSolutions.containsKey(n))
{
ArrayList<integer> ans = cachedSolutions.get(n);
ArrayList<integer> copy = new ArrayList<integer>();
for (int i: ans) copy.add(i);
return copy;
}
ArrayList<integer> best = null;
int bestInt = -1;
for (int i = (int)Math.sqrt(n); i >= 1; --i)
{
n = n - (i*i);
guess = new ArrayList<integer>();
best.size())
if (best == null || guess.size() < best.size())
{
best = guess;
bestInt = i;
}
}
best.add(bestInt);
int sum = 0;
for (int i = 0; i < best.size(); ++i)
{
sum += best.get(i) * best.get(i);
}
if (sum != n)
{
throw new RuntimeException(String.format("n = %d, sum=%d, arr=%s\n", n,
sum, best));
}
cachedSolutions.put(n, best);
previous solutions
ArrayList<integer> copy = new ArrayList<integer>();
for (int i: best) copy.add(i);
return copy;
}
return guess;}
}
____________
Output:
Exception in thread "main" java.lang.RuntimeException: n = -2, sum=9, arr=[3]
at com.algo.inputIter.minLen(inputIter.java:77)
at com.algo.inputIter.main(inputIter.java:15)
___________________________________
package com.algo;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.*;
class GetInputFromUser
{
private static HashMap<integer,>> cachedSolutions;
public static void main(String[] args)
{
cachedSolutions = new HashMap<integer,>>();
for (int j = 12; j < 13; ++j)
{
ArrayList<integer> answer = minLen(j);
Collections.sort(answer);
Collections.reverse(answer);
for (int i = 0; i < answer.size(); ++i)
{
if (i != 0)
System.out.printf("+");
System.out.printf("%d^2", answer.get(i));
}
System.out.println();
}
}
public static ArrayList<integer> minLen(int n)
{
if (n == 0)
return new ArrayList<integer>();
if (cachedSolutions.containsKey(n))
{
ArrayList<integer> ans = cachedSolutions.get(n);
ArrayList<integer> copy = new ArrayList<integer>();
for (int i: ans) copy.add(i);
return copy;
}
ArrayList<integer> best = null;
int bestInt = -1;
for (int i = (int)Math.sqrt(n); i >= 1; --i)
{
ArrayList<integer> guess = minLen(n - i*i);
if (best == null || guess.size() < best.size())
{
best = guess;
bestInt = i;
}
}
best.add(bestInt);
int sum = 0;
for (int i = 0; i < best.size(); ++i)
{
sum += best.get(i) * best.get(i);
}
if (sum != n)
{
throw new RuntimeException(String.format("n = %d, sum=%d, arr=%s\n", n, sum, best));
}
cachedSolutions.put(n, best);
ArrayList<integer> copy = new ArrayList<integer>();
for (int i: best) copy.add(i);
return copy;
}
}
_______________
OUTPUT
2^2+2^2+2^2