Find any possible two coordinates of Rectangle whose two coordinates are given | Java program for above approach ; Function to find the remaining two rectangle coordinates ; Pairs to store the position of given two coordinates of the rectangle . ; Pairs to store the remaining two coordinates of the rectangle . ; Traverse through matrix and find pairs p1 and p2 ; First Case ; Second Case ; Third Case ; Print the matrix ; Driver Code ; Given Input ; Function Call
import java . awt . * ; import java . util . * ; class GFG { static class pair < T , V > { T first ; V second ; } static void Create_Rectangle ( ArrayList < String > arr , int n ) { pair < Integer , Integer > p1 = new pair < > ( ) ; p1 . first = - 1 ; p1 . second = - 1 ; pair < Integer , Integer > p2 = new pair < > ( ) ; p2 . first = - 1 ; p2 . second = - 1 ; pair < Integer , Integer > p3 = new pair < > ( ) ; pair < Integer , Integer > p4 = new pair < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr . get ( i ) . charAt ( j ) == '1' ) if ( p1 . first == - 1 ) { p1 . first = i ; p1 . second = j ; } else { p2 . first = i ; p2 . second = j ; } } } p3 = p1 ; p4 = p2 ; if ( p1 . first . intValue ( ) == ( p2 . first ) . intValue ( ) ) { p3 . first = ( p1 . first + 1 ) % n ; p4 . first = ( p2 . first + 1 ) % n ; } else if ( p1 . second . intValue ( ) == ( p2 . second ) . intValue ( ) ) { p3 . second = ( p1 . second + 1 ) % n ; p4 . second = ( p2 . second + 1 ) % n ; } else { int temp = p3 . first ; p3 . first = p4 . first ; p4 . first = temp ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i == p3 . first ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == p3 . second ) System . out . print ( '1' ) ; else System . out . print ( arr . get ( i ) . charAt ( j ) ) ; } } else if ( i == p4 . first ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == p4 . second ) System . out . print ( '1' ) ; else System . out . print ( arr . get ( i ) . charAt ( j ) ) ; } } else System . out . print ( arr . get ( i ) ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 4 ; ArrayList < String > arr = new ArrayList < > ( ) ; arr . add ( "0010" ) ; arr . add ( "0000" ) ; arr . add ( "1000" ) ; arr . add ( "0000" ) ; Create_Rectangle ( arr , n ) ; } }