Created
June 8, 2021 14:07
-
-
Save BT-ICD/e1c29cc09b67bdb05540d2ade5795c1a to your computer and use it in GitHub Desktop.
Example: 2D Successor Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package APTest2017Demo; | |
public class APArrayDemo1 { | |
public static void main(String[] args) { | |
int[][] arr = {{15,5,9,10},{12,16,11,6},{14,8,13,7}}; | |
int num =80; | |
Position findPos = Successors.findPosition(num,arr); | |
if(findPos!=null){ | |
System.out.println("Found at position : " + findPos); | |
} | |
else | |
{ | |
System.out.println(num +" does not appear in arr "); | |
} | |
Position[][] successorArray = Successors.getSuccessorArray(arr); | |
for (int i = 0; i < successorArray.length; i++) { | |
for (int j = 0; j < successorArray[i].length; j++) { | |
System.out.print(successorArray[i][j] +"\t"); | |
} | |
System.out.println(""); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package APTest2017Demo; | |
public class Position { | |
int r,c; | |
public Position(int r, int c) { | |
this.r = r; | |
this.c = c; | |
} | |
@Override | |
public String toString() { | |
return "(" + "r=" + r + ", c=" + c +')'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package APTest2017Demo; | |
/* | |
* Method: To find position of a particular number (value) from two dimension integer array | |
* Returns the position of number in intArr; | |
* Returns null if no such element exists in intArr | |
* Precondition: intArr contains at least one row | |
* */ | |
public class Successors { | |
static Position findPosition(int num, int[][] intArr ){ | |
int i,j; | |
for(i = 0;i<intArr.length;i++){ | |
for(j=0;j<intArr[i].length;j++){ | |
if(num==intArr[i][j]) | |
{ | |
return new Position(i,j); | |
} | |
} | |
} | |
return null; | |
} | |
/* | |
* Method to find successor array | |
* The successor of an integer value is the integer that is one greater than that value. | |
* A successor of 8 is 9 | |
* A 2D successor array shows the position of the successor of each element in a give 2D integer array. | |
* The 2D successor array has the same dimensions as the given 2D integer array. | |
* Each element in the 2D successor array is the position (row, column) of the corresponding 2D integer array element's successor. | |
* The largest element in the 2D integer array does not have a successor in the 2D integer array, so its corresponding position in the 2D successor array is null. | |
* */ | |
static Position[][] getSuccessorArray(int[][] intArr){ | |
int min = findMinValue(intArr); | |
Position[][] positionArr = new Position[intArr.length][intArr[0].length]; | |
for (int i = 0; i < intArr.length; i++) { | |
for (int j = 0; j < intArr[i].length ; j++) { | |
int result = findMinValue(intArr,min); | |
if(result!=min){ | |
Position pos = findPosition(result,intArr); | |
Position posToChange = findPosition(min, intArr); | |
positionArr[posToChange.r][posToChange.c]= new Position(pos.r,pos.c); | |
min = result; | |
} | |
} | |
} | |
return positionArr; | |
} | |
/* | |
* To find minimum value from 2D integer array | |
* */ | |
static int findMinValue(int[][] intArr){ | |
int i,j; | |
int min = intArr[0][0]; | |
for(i=0;i<intArr.length;i++){ | |
for(j=0;j<intArr[i].length;j++){ | |
if(min>intArr[i][j]){ | |
min=intArr[i][j]; | |
} | |
} | |
} | |
return min; | |
} | |
/* | |
* To find maximum value from 2D integer array | |
* */ | |
static int findMaxValue(int[][] intArr){ | |
int i,j; | |
int max = intArr[0][0]; | |
for(i=0;i<intArr.length;i++){ | |
for(j=0;j<intArr[i].length;j++){ | |
if(max<intArr[i][j]){ | |
max=intArr[i][j]; | |
} | |
} | |
} | |
return max; | |
} | |
/* | |
* Method to find minimum value from 2D array of integers, which is greater than particular value. | |
* */ | |
static int findMinValue(int[][] intArr, int minValue){ | |
int i,j; | |
int min = minValue; | |
int result =findMaxValue(intArr); | |
for(i=0;i<intArr.length;i++){ | |
for(j=0;j<intArr[i].length;j++){ | |
if(intArr[i][j]>min){ | |
if(result>intArr[i][j]){ | |
result=intArr[i][j]; | |
} | |
} | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output
80 does not appear in arr
(r=1, c=1) (r=1, c=3) (r=0, c=3) (r=1, c=2)
(r=2, c=2) null (r=1, c=0) (r=2, c=3)
(r=0, c=0) (r=0, c=2) (r=2, c=0) (r=2, c=1)