Package Util
Class Data
java.lang.Object
Util.Data
Provides utility methods for data manipulation, including finding minimum and maximum values,
calculating prefix and suffix sums, and performing binary search operations on arrays and lists.
Supports various data types including integers, longs, doubles, and generics with Comparable.
- Since:
- 1.0
- Version:
- 1.0
- Author:
- Sahasrad Chippa
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Boolean[]
box
(boolean[] arr) Converts an array of primitive booleans to an array of Boolean objects.static Character[]
box
(char[] arr) Converts an array of primitive chars to an array of Character objects.static Double[]
box
(double[] arr) Converts an array of primitive doubles to an array of Double objects.static Integer[]
box
(int[] arr) Converts an array of primitive ints to an array of Integer objects.static Long[]
box
(long[] arr) Converts an array of primitive longs to an array of Long objects.static double
getMax
(double[] arr) Returns the maximum value from a double array.static int
getMax
(int[] arr) Returns the maximum value from an int array.static long
getMax
(long[] arr) Returns the maximum value from a long array.static <U extends Comparable<U>>
UReturns the maximum value from a list of comparable elements.static <U extends Comparable<U>>
UgetMax
(U[] arr) Returns the maximum value from an array of comparable elements.static double
getMin
(double[] arr) Returns the minimum value from a double array.static int
getMin
(int[] arr) Returns the minimum value from an int array.static long
getMin
(long[] arr) Returns the minimum value from a long array.static <U extends Comparable<U>>
UReturns the minimum value from a list of comparable elements.static <U extends Comparable<U>>
UgetMin
(U[] arr) Returns the minimum value from an array of comparable elements.static int
lowerBound
(char[] arr, char target) Finds the index of the first element in a sorted array of characters that is not less than the target character.static int
lowerBound
(double[] arr, double target) Finds the index of the first element in a sorted array of doubles that is not less than the target double.static int
lowerBound
(int[] arr, int target) Finds the index of the first element in a sorted array of integers that is not less than the target integer.static int
lowerBound
(long[] arr, long target) Finds the index of the first element in a sorted array of longs that is not less than the target long.static <U extends Comparable<U>>
intlowerBound
(List<U> list, U target) Finds the index of the first element in a sorted list of comparable elements that is not less than the target value using a lower bound binary search algorithm.static <U extends Comparable<U>>
intlowerBound
(U[] arr, U target) Finds the index of the first element in a sorted array that is not less than the target value using a lower bound binary search algorithm.static double[]
prefSum
(double[] arr) Calculates the prefix sum array for an array of doubles.static int[]
prefSum
(int[] arr) Calculates the prefix sum array for an array of integers.static long[]
prefSum
(long[] arr) Calculates the prefix sum array for an array of longs.static BigInteger[]
prefSum
(BigInteger[] arr) Calculates the prefix sum array for an array of BigIntegers.static double[]
suffixSum
(double[] arr) Calculates the suffix sum for an array of doubles.static int[]
suffixSum
(int[] arr) Calculates the suffix sum for an array of integers.static long[]
suffixSum
(long[] arr) Calculates the suffix sum for an array of longs.static BigInteger[]
suffixSum
(BigInteger[] arr) Calculates the suffix sum for an array of BigIntegers.static int
upperBound
(char[] arr, char target) Finds the index of the first element in the sorted array of characters that is greater than the target character.static int
upperBound
(double[] arr, double target) Finds the index of the first element in the sorted array of doubles that is greater than the target double.static int
upperBound
(int[] arr, int target) Finds the index of the first element in the sorted array of integers that is greater than the target integer.static int
upperBound
(long[] arr, long target) Finds the index of the first element in the sorted array of longs that is greater than the target long.static <U extends Comparable<U>>
intupperBound
(List<U> list, U target) Finds the index of the first element in a sorted list that is greater than the target using an upper bound binary search algorithm.static <U extends Comparable<U>>
intupperBound
(U[] arr, U target) Finds the index of the first element in a sorted array that is greater than the target using an upper bound binary search algorithm.
-
Constructor Details
-
Data
public Data()
-
-
Method Details
-
getMin
public static int getMin(int[] arr) Returns the minimum value from an int array. Runs inO(n)
time.- Parameters:
arr
- The array of integers.- Returns:
- The minimum integer in the array.
-
getMin
public static long getMin(long[] arr) Returns the minimum value from a long array. Runs inO(n)
time.- Parameters:
arr
- The array of longs.- Returns:
- The minimum long in the array.
-
getMin
public static double getMin(double[] arr) Returns the minimum value from a double array. Runs inO(n)
time.- Parameters:
arr
- The array of doubles.- Returns:
- The minimum double in the array.
-
getMin
Returns the minimum value from an array of comparable elements. Runs inO(n)
time assuming the compare operation runs inO(1)
.- Type Parameters:
U
- The type of elements in the array, which extends Comparable.- Parameters:
arr
- The array of Comparable type elements.- Returns:
- The minimum element in the array, or null if the array is empty or contains only null.
-
getMin
Returns the minimum value from a list of comparable elements. Runs inO(n)
time assuming the compare operation runs inO(1)
.- Type Parameters:
U
- The type of elements in the list, which extends Comparable.- Parameters:
list
- The list of Comparable elements.- Returns:
- The minimum element in the list, or null if the list is empty or contains only null.
-
getMax
public static int getMax(int[] arr) Returns the maximum value from an int array. Runs inO(n)
time.- Parameters:
arr
- The array of integers.- Returns:
- The maximum integer in the array.
-
getMax
public static long getMax(long[] arr) Returns the maximum value from a long array. Runs inO(n)
time.- Parameters:
arr
- The array of longs.- Returns:
- The maximum long in the array.
-
getMax
public static double getMax(double[] arr) Returns the maximum value from a double array. Runs inO(n)
time.- Parameters:
arr
- The array of doubles.- Returns:
- The maximum double in the array.
-
getMax
Returns the maximum value from a list of comparable elements. Runs inO(n)
time assuming the compare operation runs inO(1)
.- Type Parameters:
U
- The type of elements in the list, which extends Comparable.- Parameters:
list
- The list of Comparable elements.- Returns:
- The maximum element in the list, or null if the list is empty or contains only null.
-
getMax
Returns the maximum value from an array of comparable elements. Runs inO(n)
time assuming the compare operation runs inO(1)
.- Type Parameters:
U
- The type of elements in the array, which extends Comparable.- Parameters:
arr
- The array of Comparable type elements.- Returns:
- The maximum element in the array, or null if the array is empty or contains only null.
-
prefSum
public static int[] prefSum(int[] arr) Calculates the prefix sum array for an array of integers. Runs inO(n)
time.- Parameters:
arr
- The array of integers.- Returns:
- An array representing the prefix sums.
-
prefSum
public static long[] prefSum(long[] arr) Calculates the prefix sum array for an array of longs. Runs inO(n)
time.- Parameters:
arr
- The array of longs.- Returns:
- An array representing the prefix sums.
-
prefSum
public static double[] prefSum(double[] arr) Calculates the prefix sum array for an array of doubles. Runs inO(n)
time.- Parameters:
arr
- The array of doubles.- Returns:
- An array representing the prefix sums.
-
prefSum
Calculates the prefix sum array for an array of BigIntegers. Runs inO(n)
time.- Parameters:
arr
- The array of BigIntegers.- Returns:
- An array representing the prefix sums.
-
suffixSum
public static int[] suffixSum(int[] arr) Calculates the suffix sum for an array of integers. Runs inO(n)
time.- Parameters:
arr
- The array of integers.- Returns:
- An array representing the suffix sums.
-
suffixSum
public static long[] suffixSum(long[] arr) Calculates the suffix sum for an array of longs. Runs inO(n)
time.- Parameters:
arr
- The array of longs.- Returns:
- An array representing the suffix sums.
-
suffixSum
public static double[] suffixSum(double[] arr) Calculates the suffix sum for an array of doubles. Runs inO(n)
time.- Parameters:
arr
- The array of doubles.- Returns:
- An array representing the suffix sums.
-
suffixSum
Calculates the suffix sum for an array of BigIntegers. Runs inO(n)
time.- Parameters:
arr
- The array of BigIntegers.- Returns:
- An array representing the suffix sums.
-
box
Converts an array of primitive ints to an array of Integer objects. Runs inO(n)
time.- Parameters:
arr
- The primitive int array.- Returns:
- An array of Integer objects.
-
box
Converts an array of primitive longs to an array of Long objects. Runs inO(n)
time.- Parameters:
arr
- The primitive long array.- Returns:
- An array of Long objects.
-
box
Converts an array of primitive doubles to an array of Double objects. Runs inO(n)
time.- Parameters:
arr
- The primitive double array.- Returns:
- An array of Double objects.
-
box
Converts an array of primitive chars to an array of Character objects. Runs inO(n)
time.- Parameters:
arr
- The primitive char array.- Returns:
- An array of Character objects.
-
box
Converts an array of primitive booleans to an array of Boolean objects. Runs inO(n)
time.- Parameters:
arr
- The primitive boolean array.- Returns:
- An array of Boolean objects.
-
lowerBound
Finds the index of the first element in a sorted array that is not less than the target value using a lower bound binary search algorithm. Runs inO(log(n))
time assuming the compare operation runs inO(1)
.- Type Parameters:
U
- The type of elements in the array, which extends Comparable.- Parameters:
arr
- The sorted array of comparable elements.target
- The target value to compare against.- Returns:
- The index of the first element in the array that is not less than the target value.
-
lowerBound
public static int lowerBound(int[] arr, int target) Finds the index of the first element in a sorted array of integers that is not less than the target integer. Runs inO(log(n))
time.- Parameters:
arr
- The sorted array of integers.target
- The target integer to compare against.- Returns:
- The index of the first element in the array that is not less than the target integer.
-
lowerBound
public static int lowerBound(long[] arr, long target) Finds the index of the first element in a sorted array of longs that is not less than the target long. Runs inO(log(n))
time.- Parameters:
arr
- The sorted array of longs.target
- The target long to compare against.- Returns:
- The index of the first element in the array that is not less than the target long.
-
lowerBound
public static int lowerBound(double[] arr, double target) Finds the index of the first element in a sorted array of doubles that is not less than the target double. Runs inO(log(n))
time.- Parameters:
arr
- The sorted array of doubles.target
- The target double to compare against.- Returns:
- The index of the first element in the array that is not less than the target double.
-
lowerBound
public static int lowerBound(char[] arr, char target) Finds the index of the first element in a sorted array of characters that is not less than the target character. Runs inO(log(n))
time.- Parameters:
arr
- The sorted array of characters.target
- The target character to compare against.- Returns:
- The index of the first element in the array that is not less than the target character.
-
lowerBound
Finds the index of the first element in a sorted list of comparable elements that is not less than the target value using a lower bound binary search algorithm. Runs inO(log(n))
time assuming the compare operation runs inO(1)
.- Type Parameters:
U
- The type of elements in the list, which extends Comparable.- Parameters:
list
- The sorted list of comparable elements.target
- The target value to compare against.- Returns:
- The index of the first element in the list that is not less than the target value.
-
upperBound
Finds the index of the first element in a sorted array that is greater than the target using an upper bound binary search algorithm. Runs inO(log(n))
time assuming the compare operation runs inO(1)
.- Type Parameters:
U
- The type of elements in the array, which extends Comparable.- Parameters:
arr
- The sorted array of comparable elements.target
- The target value to compare against.- Returns:
- The index of the first element in the array that is greater than the target value.
-
upperBound
public static int upperBound(int[] arr, int target) Finds the index of the first element in the sorted array of integers that is greater than the target integer. Runs inO(log(n))
time.- Parameters:
arr
- The sorted array of integers.target
- The target integer to compare against.- Returns:
- The index of the first element in the array that is greater than the target integer.
-
upperBound
public static int upperBound(long[] arr, long target) Finds the index of the first element in the sorted array of longs that is greater than the target long. Runs inO(log(n))
time.- Parameters:
arr
- The sorted array of longs.target
- The target long to compare against.- Returns:
- The index of the first element in the array that is greater than the target long.
-
upperBound
public static int upperBound(double[] arr, double target) Finds the index of the first element in the sorted array of doubles that is greater than the target double. Runs inO(log(n))
time.- Parameters:
arr
- The sorted array of doubles.target
- The target double to compare against.- Returns:
- The index of the first element in the array that is greater than the target double.
-
upperBound
public static int upperBound(char[] arr, char target) Finds the index of the first element in the sorted array of characters that is greater than the target character. Runs inO(log(n))
time.- Parameters:
arr
- The sorted array of characters.target
- The target character to compare against.- Returns:
- The index of the first element in the array that is greater than the target character.
-
upperBound
Finds the index of the first element in a sorted list that is greater than the target using an upper bound binary search algorithm. Runs inO(log(n))
time assuming the compare operation runs inO(1)
.- Type Parameters:
U
- The type of elements in the list, which extends Comparable.- Parameters:
list
- The sorted list of comparable elements.target
- The target value to compare against.- Returns:
- The index of the first element in the list that is greater than the target value.
-