Question Index

In chess, assuming that the columns are numbered A to H and the rows are numbered 1 to 8. Write a program to find all the positions to which a black camel can move to without getting killed by white elephant. Assume that there is nothing else on the chess board apart from these two.
NOTE: The list returned should be first sorted by columns and then by rows, so A4 comes before B1 and C7 comes before D5 etc.,

Input (Black Camel Position, White Elephant Position) Output (Sorted List of Positions)
C2, D5 [A4, B1, B3, E4, G6, H7]
D4, C7 [A1, B2, B6, E3, E5, F2, F6, G1, H8]
F8, H7 [A3, B4, C5, D6]
H1, A7 [C6, D5, E4, F3, G2]

class FindBlackCamelPositionsWithWhiteElephant

{    public static void main(String s[])
        ChessPosition camel = new ChessPosition('C', 2);
        ChessPosition elephant = new ChessPosition('D', 5);
        System.out.println("The positions which the camel can move to with out getting killed : " + getPossiblePositions(camel, elephant));


public static List<ChessPosition> getPossiblePositions(ChessPosition camel, ChessPosition elephant) {
//Write code here to get the positions in which a black camel can move to without getting killed by white elephant
class ChessPosition {

char column;
int row;

public ChessPosition(char column, int row) {
this.column = column;
this.row = row;

public String toString() {
return column + "" + row;



Topic: Collection Framework In Java

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App