Skip to content

Instantly share code, notes, and snippets.

@robbielynch
Created July 23, 2012 13:38
Show Gist options
  • Save robbielynch/3163641 to your computer and use it in GitHub Desktop.
Save robbielynch/3163641 to your computer and use it in GitHub Desktop.
N Kueen - HackerRank Code Sprint - Interviewstreet.com
import java.util.ArrayList;
import java.util.Scanner;
public class NKueen {
private static ArrayList<Integer> kueens = new ArrayList<Integer>();
static int N = 0;
public static void main (String args[]){
Scanner in = new Scanner(System.in);
//input
N = in.nextInt();
//loop through
generatePositions();
//output positions
outputKueenPositions();
}
private static void outputKueenPositions() {
System.out.println(N);
for(int i = 0; i < kueens.size();i++){
if(i == kueens.size() -1){
System.out.print(kueens.get(i) + " ");
}else{
System.out.print(kueens.get(i) + " ");
}
}
}
private static void generatePositions() {
boolean col1Empty = true;
boolean col2Empty = true;
int c = 3; //column number - start from column 3
if(N <= 3){
c = 1;
kueens.add(c);
}else{
kueens.add(c);
for(int r = 1; r < N;r++){
if(c+3 <= N){
c = c+3;
kueens.add(c);
}else{
if(col1Empty){
c = 1;
col1Empty = false;
kueens.add(c);
}else if(col2Empty){
c = 2;
col2Empty = false;
kueens.add(c);
}else{
c = (c+3) - N;
kueens.add(c);
}
}
}
}
}
}
@christian-mann
Copy link

christian-mann commented Jul 24, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment