Class ArrayProblems

java.lang.Object
  extended by ArrayProblems

public class ArrayProblems
extends Object

ArrayProblems.java
CS 307

VERY IMPORTANT: For this assignment only, the only methods and classes from the Java standard library you may use in your final solutions are:
1. All methods from the System class
2. All methods from the Math class
3. native arrays, including the arr.length field and creating and using new native arrays


Constructor Summary
ArrayProblems()
           
 
Method Summary
static int hammingDistance(int[] aList, int[] bList)
          Determine the Hamming distance between two arrays of ints.
static boolean isPermutation(int[] listA, int[] listB)
          Determine if one array of ints is a permutation of another.
static void main(String[] args)
           
static String maxSum(int[][] mat)
          Determine which row or column in a matrix has the largest sum.
static int[] minDifference(int[] nums)
          Find the two values in an array of ints that have the smallest absolute difference.
static int mostValuablePlot(int[][] city)
          Given a 2D array of ints return the value of the most valuable contigous sub rectangle in the 2D array.
static boolean queensAreSafe(char[][] board)
          Determine if the chess board represented by board is a safe set up.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayProblems

public ArrayProblems()
Method Detail

hammingDistance

public static int hammingDistance(int[] aList,
                                  int[] bList)
Determine the Hamming distance between two arrays of ints. Neither the parameter aList or bList are altered as a result of this method.

Parameters:
aList - != null, aList.length == bList.length
bList - != null, bList.length == aList.length
Returns:
the Hamming Distance between the two arrays of ints.

isPermutation

public static boolean isPermutation(int[] listA,
                                    int[] listB)
Determine if one array of ints is a permutation of another. Neither the parameter listA or the parameter listB are altered as a result of this method.

Parameters:
listA - != null
listB - != null
Returns:
true if listB is a permutation of listA, false otherwise

minDifference

public static int[] minDifference(int[] nums)
Find the two values in an array of ints that have the smallest absolute difference. The parameter nums is not altered as a result of this method.

Parameters:
nums - The array being searched. nums != null, nums.length >= 2
Returns:
Returns an array of length 2. The elements of the result are the indices of ints in nums that have the smallest absolute difference of any pair of ints in nums.
If there is more than one pair of ints that meet this criteria return the indices of the pair with the index closest to zero.
If there is more than one pair of ints with the index closest to 0, return the indices of the pair with the second index closest to 0.
The first element of the result is the index closest to 0. For example given the array {5, 3, 21, 10, 12, 7} the method would return {0, 1}.

maxSum

public static String maxSum(int[][] mat)
Determine which row or column in a matrix has the largest sum. The parameter mat is not altered as a result of this method call.

pre: mat != null, mat.length > 0, mat is a rectangular matrix, mat[0].length > 0

post: determine which row or column of ints has the maximum sum in max. If a row contains the maximum sum, return a string starting with "R" and then the number of the row with no spaces. For example "R0" or "R12". If a column contains the maximum sum, return a string starting with "C" and then the number of the column with no spaces. For example "C0" or "C12". If there is more than one row or column with the maximum sum return rows over columns first, then smaller indices over larger indices.
Thus if rows 3, 5, and 12, and columns 0 and 2 all contained the same maximum sum the method would return "R3".


mostValuablePlot

public static int mostValuablePlot(int[][] city)
Given a 2D array of ints return the value of the most valuable contigous sub rectangle in the 2D array. The sub rectnagle must be at lest 1 by 1.

pre: mat != null, mat.length > 0, mat[0].length > 0, mat is a rectangular matrix.

post: return the value of the most valuable contigous sub rectangle in city.

Parameters:
city - The 2D array of ints representing the value of each block in a portion of a city.
Returns:
return the value of the most valuable contigous sub rectangle in city.

queensAreSafe

public static boolean queensAreSafe(char[][] board)
Determine if the chess board represented by board is a safe set up.

pre: board != null, board.length > 0, board is a square matrix. (In other words all rows in board have board.length columns.), all elements of board == 'q' or '.'. 'q's represent queens, '.'s represent open spaces.

post: return true if the configuration of board is safe, that is no queen can attack any other queen on the board. false otherwise. the parameter board is not altered as a result of this method.

Parameters:
board - the chessboard
Returns:
true if the configuration of board is safe, that is no queen can attack any other queen on the board. false otherwise.

main

public static void main(String[] args)