Java Array Min Value minCeilDiv(int c, int... vals)

Here you can find the source of minCeilDiv(int c, int... vals)

Description

min Ceil Div

License

Open Source License

Parameter

Parameter Description
c != 0
vals different from 0

Return

min(ceil(c/v)) with v in vals

Declaration

public static int minCeilDiv(int c, int... vals) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * OscaR is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.//from   w ww  .ja  v  a 2 s .  com
 *   
 * OscaR is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License  for more details.
 *   
 * You should have received a copy of the GNU Lesser General Public License along with OscaR.
 * If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
 ******************************************************************************/

public class Main {
    /**
     * @param c != 0
     * @param vals different from 0
     * @return min(ceil(c/v)) with v in vals
     */
    public static int minCeilDiv(int c, int... vals) {
        assert (vals.length > 0);
        assert (vals[0] != 0);
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < vals.length; i++) {
            assert (vals[i] != 0);
            int tmp = ceilDiv(c, vals[i]);
            if (tmp < res)
                res = tmp;
        }
        return res;
    }

    /**
     * @param v1
     * @param v2 != 0
     * @return ceil(v1/v2)
     */
    public static int ceilDiv(int v1, int v2) {
        return v1 / v2
                + (((v1 % v2 != 0) && positiveProduct(v1, v2)) ? 1 : 0);
    }

    public static boolean positiveProduct(int v1, int v2) {
        return (v2 > 0 && v1 > 0) || (v1 < 0 && v2 < 0);
    }
}

Related

  1. min_max_mean_stddev(long[] counts)
  2. min_of_ints(int[] data)
  3. minAndMaxOfArray(float[] array)
  4. minarr(double[] a)
  5. minArray(int[] arr)
  6. minDiff(int... offs)
  7. minDist(double[] q, double[] c, double[][] distMatrix, int orig_n)
  8. minDistance(int[][] colors, int len, int[] color)
  9. minDouble(double a, double... others)