Java program:
import java . io . * ; import java . util . * ; import java . util . stream . Collectors ; class GFG { private static int dis ( int [ ] ans , int [ ] attacker ) { return Math . abs ( ans [ 0 ] - attacker [ 0 ] ) + Math . abs ( ans [ 1 ] - attacker [ 1 ] ) ; } private static List < List < Integer > > findQueens ( int [ ] [ ] queens , int [ ] king ) { List < List < Integer > > sol = new ArrayList < List < Integer > > ( ) ; int [ ] [ ] attackers = new int [ 8 ] [ 2 ] ; for ( int i = 0 ; i < 8 ; i ++ ) { Arrays . fill ( attackers [ i ] , - 1 ) ; } for ( int i = 0 ; i < queens . length ; i ++ ) { if ( king [ 0 ] == queens [ i ] [ 0 ] && king [ 1 ] > queens [ i ] [ 1 ] ) { if ( ( attackers [ 3 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 3 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 3 ] = queens [ i ] ; } if ( king [ 0 ] == queens [ i ] [ 0 ] && king [ 1 ] < queens [ i ] [ 1 ] ) { if ( ( attackers [ 4 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 4 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 4 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == king [ 1 ] - queens [ i ] [ 1 ] && king [ 0 ] > queens [ i ] [ 0 ] ) { if ( ( attackers [ 0 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 0 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 0 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == king [ 1 ] - queens [ i ] [ 1 ] && king [ 0 ] < queens [ i ] [ 0 ] ) { if ( ( attackers [ 7 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 7 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 7 ] = queens [ i ] ; } if ( king [ 1 ] - queens [ i ] [ 1 ] == 0 && king [ 0 ] > queens [ i ] [ 0 ] ) { if ( ( attackers [ 1 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 1 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 1 ] = queens [ i ] ; } if ( king [ 1 ] - queens [ i ] [ 1 ] == 0 && king [ 0 ] < queens [ i ] [ 0 ] ) { if ( ( attackers [ 6 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 6 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 6 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == - ( king [ 1 ] - queens [ i ] [ 1 ] ) && king [ 0 ] > queens [ i ] [ 0 ] ) { if ( ( attackers [ 2 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 2 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 2 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == - ( king [ 1 ] - queens [ i ] [ 1 ] ) && king [ 0 ] < queens [ i ] [ 0 ] ) { if ( ( attackers [ 5 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 5 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 5 ] = queens [ i ] ; } } for ( int i = 0 ; i < 8 ; i ++ ) if ( attackers [ i ] [ 0 ] != - 1 ) sol . add ( Arrays . stream ( attackers [ i ] ) . boxed ( ) . collect ( Collectors . toList ( ) ) ) ; return sol ; } private static void print ( List < List < Integer > > ans ) { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) System . out . print ( ans . get ( i ) . get ( j ) + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] king = { 2 , 3 } ; int [ ] [ ] queens = { { 0 , 1 } , { 1 , 0 } , { 4 , 0 } , { 0 , 4 } , { 3 , 3 } , { 2 , 4 } } ; List < List < Integer > > ans = findQueens ( queens , king ) ; print ( ans ) ; } }

Index/key variables: i
Accumulator variables: i, j
Iterator variables: i, j
Class/struct names: GFG