Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, I get little bit bored so play around with Java to have some experience, can be poorly coded and without tuning, as I am trying to increase my coding skill, comments are welcome. My computer able to answer me for small arrays:)</p> <p>Output looks like:</p> <pre><code>Largest rectangle range is ; 48 ------------------------------------------------- input values; [ 4,2,4,4,6,8,9 ] ------------------------------------------------- Array details of the rectangle: A1: [ 6 ] B1: [ 8 ] A2: [ 2,4 ] B2: [ 4,4 ] </code></pre> <p>combination.class using Kenneth algorithm;</p> <p>import java.math.BigInteger;</p> <pre><code>public class Combination { /** * Burak */ private int[] a; private int n; private int r; private BigInteger numLeft; private BigInteger total; public Combination (int n, int r) { if (r &gt; n) { throw new IllegalArgumentException (); } if (n &lt; 1) { throw new IllegalArgumentException (); } this.n = n; this.r = r; a = new int[r]; BigInteger nFact = getFactorial (n); BigInteger rFact = getFactorial (r); BigInteger nminusrFact = getFactorial (n - r); total = nFact.divide (rFact.multiply (nminusrFact)); reset (); } //------ // Reset //------ public void reset () { for (int i = 0; i &lt; a.length; i++) { a[i] = i; } numLeft = new BigInteger (total.toString ()); } //------------------------------------------------ // Return number of combinations not yet generated //------------------------------------------------ public BigInteger getNumLeft () { return numLeft; } //----------------------------- // Are there more combinations? //----------------------------- public boolean hasMore () { return numLeft.compareTo (BigInteger.ZERO) == 1; } //------------------------------------ // Return total number of combinations //------------------------------------ public BigInteger getTotal () { return total; } //------------------ // Compute factorial //------------------ private static BigInteger getFactorial (int n) { BigInteger fact = BigInteger.ONE; for (int i = n; i &gt; 1; i--) { fact = fact.multiply (new BigInteger (Integer.toString (i))); } return fact; } //-------------------------------------------------------- // Generate next combination (algorithm from Rosen p. 286) //-------------------------------------------------------- public int[] getNext () { if (numLeft.equals (total)) { numLeft = numLeft.subtract (BigInteger.ONE); return a; } int i = r - 1; while (a[i] == n - r + i) { i--; } a[i] = a[i] + 1; for (int j = i + 1; j &lt; r; j++) { a[j] = a[i] + j - i; } numLeft = numLeft.subtract (BigInteger.ONE); return a; } } </code></pre> <p>And main Combinator.class;</p> <p>import java.util.*;</p> <p>public class Combinator {</p> <pre><code>/** * @param args */ private static int[] ad; private static int[] bd; private static String a1; private static String a2; private static String b1; private static String b2; private static int bestTotal =0; public static void main(String[] args) { int[] array={4,2,4,4,6,8,9}; getBestCombination(array, 1); if(bestTotal &lt;= 0){ System.out.println("System couldnt create any rectangle."); }else{ System.out.println("Largest rectangle range is ; " + bestTotal); System.out.println("-------------------------------------------------"); System.out.println("input values; " + parseArrayToString(array)); System.out.println("-------------------------------------------------"); System.out.println("Array details of the rectangle:"); System.out.println("A1: " + a1); System.out.println("B1: " + b1); System.out.println("A2: " + a2); System.out.println("B2: " + b2); } } private static void getBestCombination(int[] array, int level){ int[] a; int[] b; int bestPerimeter = getTotal(array,true); Vector&lt;Vector&lt;Integer&gt;&gt; results = null; for(int o=array.length-1;o&gt;=1;o--){ for(int u=bestPerimeter;u&gt;=1;u--){ results = Combinator.compute (array, o, u); if(results.size() &gt; 0){ for(int i=0;i&lt;results.size();i++){ a = new int[results.elementAt(i).size()]; for(int j = 0;j&lt;results.elementAt(i).size();j++){ a[j] = results.elementAt(i).elementAt(j); } b = removeItems(array, results.elementAt(i)); if(level == 1){ getBestCombination(a,2); getBestCombination(b,3); }else if(level == 2){ ad = a; bd = b; }else{ getBestCombination(a,4); getBestCombination(b,4); if(getTotal(ad, false) == getTotal(a, false) &amp;&amp; getTotal(bd, false) == getTotal(b, false)){ if(bestTotal&lt;(getTotal(ad, false)*getTotal(bd, false))){ bestTotal = getTotal(ad, false)*getTotal(bd, false); a1 = parseArrayToString(ad); a2 = parseArrayToString(a); b1 = parseArrayToString(bd); b2 = parseArrayToString(b); } }else if(getTotal(ad, false) == getTotal(b, false) &amp;&amp; getTotal(bd, false) == getTotal(a, false)){ if(bestTotal&lt;(getTotal(ad, false)*getTotal(bd, false))){ bestTotal = getTotal(ad, false)*getTotal(bd, false); a1 = parseArrayToString(ad); a2 = parseArrayToString(b); b1 = parseArrayToString(bd); b2 = parseArrayToString(a); } } } } } } } } private static String parseArrayToString(int[] items){ String s = "[ "; for(int i=0;i&lt;items.length;i++){ if(i!=items.length-1){ s = s + items[i] + ","; }else{ s = s + items[i]; } } s = s + " ]"; return s; } @SuppressWarnings("rawtypes") private static int[] removeItems(int[] array, Vector items){ ArrayList&lt;Integer&gt; res = new ArrayList&lt;Integer&gt;(); for(int i=0;i&lt;array.length;i++){ res.add(array[i]); } for(int u = 0;u&lt;items.size();u++){ res.remove(items.elementAt(u)); } int[] results = new int[res.size()]; for(int o=0;o&lt;res.size();o++){ results[o] = res.get(o); } return results; } private static int getTotal(int[] array,boolean bestPerimeter){ int sum = 0; for (int i = 0; i &lt; array.length; i++) { sum += array[i]; } if(bestPerimeter == true){ if(sum%2!=0){ sum = sum -1; } sum = sum/2; } //System.out.println(sum); return sum; } @SuppressWarnings("rawtypes") private static int getSum (Vector v) { int sum = 0; Integer n; for (int i = 0; i &lt; v.size (); i++) { n = (Integer) v.elementAt(i); sum += n.intValue (); } return sum; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static Vector&lt;Vector&lt;Integer&gt;&gt; compute (int[] array, int atATime, int desiredTotal) { int[] indices; Combination gen = new Combination (array.length, atATime); Vector results = new Vector (); Vector combination; int sum; Integer intObj; while (gen.hasMore ()) { combination = new Vector (); indices = gen.getNext (); for (int i = 0; i &lt; indices.length; i++) { intObj = new Integer (array[indices[i]]); combination.addElement (intObj); } sum = getSum (combination); if (sum == desiredTotal) { Collections.sort (combination); if (!results.contains (combination)) { results.addElement (combination); } } } return results; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload