Java Array Intersect intersectRanges(int[] range1, int[] range2)

Here you can find the source of intersectRanges(int[] range1, int[] range2)

Description

Each array represents a set of closed or open Range, like so: [0,10,50,60] - Ranges are {0-9}, {50-59} [20] - Ranges are {20-} [30,40,100] - Ranges are {30-39}, {100-} All Ranges in the array have a closed lower bound.

License

Apache License

Parameter

Parameter Description
range1 a parameter
range2 a parameter

Declaration

public static int[] intersectRanges(int[] range1, int[] range2) 

Method Source Code

//package com.java2s;
/**//  w ww  . j  a  v a 2s . c om
 * Copyright 2010-present Facebook.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * Each array represents a set of closed or open Range, like so:
     * [0,10,50,60] - Ranges are {0-9}, {50-59}
     * [20] - Ranges are {20-}
     * [30,40,100] - Ranges are {30-39}, {100-}
     *
     * All Ranges in the array have a closed lower bound. Only the last Range in each array may be open.
     * It is assumed that the passed in arrays are sorted with ascending order.
     * It is assumed that no two elements in a given are equal (i.e. no 0-length ranges)
     *
     * The method returns an intersect of the two passed in Range-sets
     * @param range1
     * @param range2
     * @return
     */
    public static int[] intersectRanges(int[] range1, int[] range2) {
        if (range1 == null) {
            return range2;
        } else if (range2 == null) {
            return range1;
        }

        int[] outputRange = new int[range1.length + range2.length];
        int outputIndex = 0;
        int index1 = 0, lower1, upper1;
        int index2 = 0, lower2, upper2;
        while (index1 < range1.length && index2 < range2.length) {
            int newRangeLower = Integer.MIN_VALUE, newRangeUpper = Integer.MAX_VALUE;
            lower1 = range1[index1];
            upper1 = Integer.MAX_VALUE;

            lower2 = range2[index2];
            upper2 = Integer.MAX_VALUE;

            if (index1 < range1.length - 1) {
                upper1 = range1[index1 + 1];
            }
            if (index2 < range2.length - 1) {
                upper2 = range2[index2 + 1];
            }

            if (lower1 < lower2) {
                if (upper1 > lower2) {
                    newRangeLower = lower2;
                    if (upper1 > upper2) {
                        newRangeUpper = upper2;
                        index2 += 2;
                    } else {
                        newRangeUpper = upper1;
                        index1 += 2;
                    }
                } else {
                    index1 += 2;
                }
            } else {
                if (upper2 > lower1) {
                    newRangeLower = lower1;
                    if (upper2 > upper1) {
                        newRangeUpper = upper1;
                        index1 += 2;
                    } else {
                        newRangeUpper = upper2;
                        index2 += 2;
                    }
                } else {
                    index2 += 2;
                }
            }

            if (newRangeLower != Integer.MIN_VALUE) {
                outputRange[outputIndex++] = newRangeLower;
                if (newRangeUpper != Integer.MAX_VALUE) {
                    outputRange[outputIndex++] = newRangeUpper;
                } else {
                    // If we reach an unbounded/open range, then we know we're done.
                    break;
                }
            }
        }

        return Arrays.copyOf(outputRange, outputIndex);
    }
}

Related

  1. intersectArrays(int[] targetArray, int[] selfArray)
  2. intersection(final long[] array1, final long[] array2)
  3. intersection(int[] array1, int[] array2)
  4. intersection(String[] ary1, String[] ary2)
  5. intersection(String[] ss1, String[] ss2)
  6. isArrayIntersect(Object[] objArr1, Object[] objArr2)