Number of lines from given N points not parallel to X or Y axis | C # program to find the number of lines which are formed from given N points and not parallel to X or Y axis ; Function to find the number of lines which are formed from given N points and not parallel to X or Y axis ; This will store the number of points has same x or y coordinates using the map as the value of coordinate can be very large ; Counting frequency of each x and y coordinates ; Total number of pairs can be formed ; We can not choose pairs from these as they have same x coordinatethus they will result line segment parallel to y axis ; we can not choose pairs from these as they have same y coordinate thus they will result line segment parallel to x - axis ; Return the required answer ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int NotParallel ( int [ , ] p , int n ) { Dictionary < int , int > x_axis = new Dictionary < int , int > ( ) ; Dictionary < int , int > y_axis = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( x_axis . ContainsKey ( p [ i , 0 ] ) ) x_axis [ p [ i , 0 ] ] = x_axis [ p [ i , 0 ] ] + 1 ; else x_axis . Add ( p [ i , 0 ] , 1 ) ; if ( y_axis . ContainsKey ( p [ i , 1 ] ) ) y_axis [ p [ i , 1 ] ] = y_axis [ p [ i , 1 ] ] + 1 ; else y_axis . Add ( p [ i , 1 ] , 1 ) ; } int total = ( n * ( n - 1 ) ) / 2 ; foreach ( KeyValuePair < int , int > i in x_axis ) { int c = i . Value ; total -= ( c * ( c - 1 ) ) / 2 ; } foreach ( KeyValuePair < int , int > i in y_axis ) { int c = i . Value ; total -= ( c * ( c - 1 ) ) / 2 ; } return total ; } public static void Main ( String [ ] args ) { int [ , ] p = { { 1 , 2 } , { 1 , 5 } , { 1 , 15 } , { 2 , 10 } } ; int n = p . GetLength ( 0 ) ; Console . Write ( NotParallel ( p , n ) ) ; } }