codeparrot/xlcost-text-to-code huggingface dataset:
Count even paths in Binary Tree | C # program for the above approach ; A Tree node ; Utility function to create a new node ; Utility function to count the even path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is odd ; Increment count when encounter leaf node with all node value even ; Left recursive call , and save the value of count ; Right recursive call , and return value of count ; Function to count the even paths in a given Binary tree ; Function call with count = 0 ; Driver Code ; Tree ; Function call
using System ; class GFG { class Node { public int key ; public Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static int evenPaths ( Node node , int count ) { if ( node == null || ( node . key % 2 != 0 ) ) { return count ; } if ( node . left == null && node . right == null ) { count ++ ; } count = evenPaths ( node . left , count ) ; return evenPaths ( node . right , count ) ; } static int countEvenPaths ( Node node ) { return evenPaths ( node , 0 ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 12 ) ; root . left = newNode ( 13 ) ; root . right = newNode ( 12 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 16 ) ; root . right . left . left = newNode ( 21 ) ; root . right . left . right = newNode ( 22 ) ; root . right . right . left = newNode ( 22 ) ; root . right . right . right = newNode ( 24 ) ; root . right . right . right . left = newNode ( 8 ) ; Console . WriteLine ( countEvenPaths ( root ) ) ; } }
Sum of subsets of all the subsets of an array | O ( 3 ^ N ) | C # implementation of the approach ; To store the readonly ans ; Function to sum of all subsets of a given array ; Base case ; Recursively calling subsetSum ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < int > c = new List < int > ( ) ; static int ans = 0 ; static void subsetSum ( int i , int curr ) { if ( i == c . Count ) { ans += curr ; return ; } subsetSum ( i + 1 , curr + c [ i ] ) ; subsetSum ( i + 1 , curr ) ; } static void subsetGen ( int [ ] arr , int i , int n ) { if ( i == n ) { subsetSum ( 0 , 0 ) ; return ; } subsetGen ( arr , i + 1 , n ) ; c . Add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n ) ; c . RemoveAt ( c . Count - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; int n = arr . Length ; subsetGen ( arr , 0 , n ) ; Console . WriteLine ( ans ) ; } }
Print the DFS traversal step | C # program for the above approach ; Function to print the complete DFS - traversal ; Check if all th node is visited or not and count unvisited nodes ; If all the node is visited return ; ; Mark not visited node as visited ; Track the current edge ; Print the node ; Check for not visited node and proceed with it . ; call the DFs function if not visited ; Backtrack through the last visited nodes ; Function to call the DFS function which prints the DFS - traversal stepwise ; Create a array of visited node ; Vector to track last visited road ; Initialize all the node with false ; call the function ; Function to insert edges in Graph ; Driver Code ; number of nodes and edges in the graph ; Function call to create the graph ; Call the function to print
using System ; using System . Collections . Generic ; public class GFG { static int N = 1000 ; static List < List < int > > adj = new List < List < int > > ( ) ; static void dfsUtil ( int u , int node , bool [ ] visited , List < List < int > > road_used , int parent , int it ) { int c = 0 ; for ( int i = 0 ; i < node ; i ++ ) if ( visited [ i ] ) c ++ ; if ( c == node ) return ; visited [ u ] = true ; road_used . Add ( new List < int > ( ) { parent , u } ) ; Console . Write ( u + " ▁ " ) ; foreach ( int x in adj [ u ] ) { if ( ! visited [ x ] ) { dfsUtil ( x , node , visited , road_used , u , it + 1 ) ; } } for ( int y = 0 ; y < road_used . Count ; y ++ ) { if ( road_used [ y ] [ 1 ] == u ) { dfsUtil ( road_used [ y ] [ 0 ] , node , visited , road_used , u , it + 1 ) ; } } } static void dfs ( int node ) { bool [ ] visited = new bool [ node ] ; List < List < int > > road_used = new List < List < int > > ( ) ; for ( int i = 0 ; i < node ; i ++ ) { visited [ i ] = false ; } dfsUtil ( 0 , node , visited , road_used , - 1 , 0 ) ; } static void insertEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static public void Main ( ) { int node = 11 ; for ( int i = 0 ; i < N ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } insertEdge ( 0 , 1 ) ; insertEdge ( 0 , 2 ) ; insertEdge ( 1 , 5 ) ; insertEdge ( 1 , 6 ) ; insertEdge ( 2 , 4 ) ; insertEdge ( 2 , 9 ) ; insertEdge ( 6 , 7 ) ; insertEdge ( 6 , 8 ) ; insertEdge ( 7 , 8 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 3 , 9 ) ; insertEdge ( 3 , 10 ) ; insertEdge ( 9 , 10 ) ; dfs ( node ) ; } }
Find Maximum number possible by doing at | C # program to find maximum integer possible by doing at - most K swap operations on its digits . ; Function to find maximum integer possible by doing at - most K swap operations on its digits ; Return if no swaps left ; Consider every digit ; Compare it with all digits after it ; if digit at position i is less than digit at position j , swap it and check for maximum number so far and recurse for remaining swaps ; swap str [ i ] with str [ j ] ; If current num is more than maximum so far ; recurse of the other k - 1 swaps ; Backtrack ; Driver code
using System ; class GFG { static String max ; static void findMaximumNum ( char [ ] str , int k ) { if ( k == 0 ) return ; int n = str . Length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( str [ i ] < str [ j ] ) { char t = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = t ; if ( String . Join ( " " , str ) . CompareTo ( max ) > 0 ) max = String . Join ( " " , str ) ; findMaximumNum ( str , k - 1 ) ; char c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; } } } } public static void Main ( String [ ] args ) { String str = "129814999" ; int k = 4 ; max = str ; findMaximumNum ( str . ToCharArray ( ) , k ) ; Console . Write ( max + " STRNEWLINE " ) ; } }
Print all possible paths from top left to bottom right of a mXn matrix | C # program to Print all possible paths from top left to bottom right of a mXn matrix ; If we reach the bottom of maze , we can only move right ; path . append ( maze [ i ] [ k ] ) ; If we hit this block , it means one path is completed . Add it to paths list and print ; If we reach to the right most corner , we can only move down ; path . append ( maze [ j ] [ k ] ) If we hit this block , it means one path is completed . Add it to paths list and print ; Add current element to the path list path . append ( maze [ i ] [ j ] ) ; Move down in y direction and call findPathsUtil recursively ; Move down in y direction and call findPathsUtil recursively ;  ; Driver code
using System ; using System . Collections . Generic ; class GFG { static List < List < int > > allPaths = new List < List < int > > ( ) ; static void findPathsUtil ( List < List < int > > maze , int m , int n , int i , int j , List < int > path , int indx ) { if ( i == m - 1 ) { for ( int k = j ; k < n ; k ++ ) { path [ indx + k - j ] = maze [ i ] [ k ] ; } Console . Write ( " [ " + path [ 0 ] + " , ▁ " ) ; for ( int z = 1 ; z < path . Count - 1 ; z ++ ) { Console . Write ( path [ z ] + " , ▁ " ) ; } Console . WriteLine ( path [ path . Count - 1 ] + " ] " ) ; allPaths . Add ( path ) ; return ; } if ( j == n - 1 ) { for ( int k = i ; k < m ; k ++ ) { path [ indx + k - i ] = maze [ k ] [ j ] ; } Console . Write ( " [ " + path [ 0 ] + " , ▁ " ) ; for ( int z = 1 ; z < path . Count - 1 ; z ++ ) { Console . Write ( path [ z ] + " , ▁ " ) ; } Console . WriteLine ( path [ path . Count - 1 ] + " ] " ) ; allPaths . Add ( path ) ; return ; } path [ indx ] = maze [ i ] [ j ] ; findPathsUtil ( maze , m , n , i + 1 , j , path , indx + 1 ) ; findPathsUtil ( maze , m , n , i , j + 1 , path , indx + 1 ) ; } static void findPaths ( List < List < int > > maze , int m , int n ) { List < int > path = new List < int > ( ) ; for ( int i = 0 ; i < m + n - 1 ; i ++ ) { path . Add ( 0 ) ; } findPathsUtil ( maze , m , n , 0 , 0 , path , 0 ) ; } static void Main ( ) { List < List < int > > maze = new List < List < int > > ( ) ; maze . Add ( new List < int > { 1 , 2 , 3 } ) ; maze . Add ( new List < int > { 4 , 5 , 6 } ) ; maze . Add ( new List < int > { 7 , 8 , 9 } ) ; findPaths ( maze , 3 , 3 ) ; } }
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | C # program to check if there exists a pair in array whose sum results in x . ; Function to find and print pair ; Driver code
using System ; class GFG { static bool chkPair ( int [ ] A , int size , int x ) { for ( int i = 0 ; i < ( size - 1 ) ; i ++ ) { for ( int j = ( i + 1 ) ; j < size ; j ++ ) { if ( A [ i ] + A [ j ] == x ) { Console . WriteLine ( " Pair ▁ with ▁ a ▁ given ▁ sum ▁ " + x + " ▁ is ▁ ( " + A [ i ] + " , ▁ " + A [ j ] + " ) " ) ; return true ; } } } return false ; } public static void Main ( ) { int [ ] A = { 0 , - 1 , 2 , - 3 , 1 } ; int x = - 2 ; int size = A . Length ; if ( chkPair ( A , size , x ) ) { Console . WriteLine ( " Valid ▁ pair ▁ exists " ) ; } else { Console . WriteLine ( " No ▁ valid ▁ pair ▁ exists ▁ for ▁ " + x ) ; } } }
Reduce the array by deleting elements which are greater than all elements to its left | C # program for the above approach ; Function to implement merging of arr [ ] ; Function to delete all elements which satisfy the condition A [ i ] > A [ i - 1 ] ; Divide array into its subarray ; Getting back merged array with all its right element greater than left one . ; Driver Code ; Given array arr [ ]
using System ; using System . Collections . Generic ; class GFG { static List < int > merge ( List < int > x , List < int > y ) { foreach ( int i in y ) { if ( x [ x . Count - 1 ] > i ) x . Add ( i ) ; } return x ; } static List < int > mergeDel ( List < int > l ) { if ( l . Count == 1 ) return l ; int m = l . Count / 2 ; List < int > temp1 = l . GetRange ( 0 , m ) ; List < int > temp2 = l . GetRange ( m , l . Count - m ) ; return merge ( mergeDel ( temp1 ) , mergeDel ( temp2 ) ) ; } public static void Main ( string [ ] args ) { List < int > arr = new List < int > { 5 , 4 , 3 , 2 , 1 } ; List < int > ans = mergeDel ( arr ) ; Console . Write ( " [ ▁ " ) ; foreach ( int x in ans ) Console . Write ( x + " , ▁ " ) ; Console . Write ( " ] " ) ; } }
Significant Inversions in an Array | C # implementation of the above approach ; Function that sorts the input array and returns the number of inversions in the array ; Recursive function that sorts the input array and returns the number of inversions in the array ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of the inversions in the left - part , the right - part and the number of inversions in merging ; Merge the two parts ; Function that merges the two sorted arrays and returns the inversion count in the arrays ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; First pass to count number of significant inversions ; i is the index for the left subarray ; j is the index for the right subarray ; k is the index for the resultant merged subarray ; Second pass to merge the two sorted arrays ; Copy the remaining elements of the left subarray ( if there are any ) to temp ; Copy the remaining elements of the right subarray ( if there are any ) to temp ; Copy back the merged elements to the original array ; Driver code
using System ; class GFG { static int mergeSort ( int [ ] arr , int array_size ) { int [ ] temp = new int [ array_size ] ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } static int _mergeSort ( int [ ] arr , int [ ] temp , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } static int merge ( int [ ] arr , int [ ] temp , int left , int mid , int right ) { int i , j , k ; int inv_count = 0 ; i = left ; j = mid ; k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] > 2 * arr [ j ] ) { inv_count += ( mid - i ) ; j ++ ; } else { i ++ ; } } i = left ; j = mid ; k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) { temp [ k ++ ] = arr [ i ++ ] ; } else { temp [ k ++ ] = arr [ j ++ ] ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } public static void Main ( ) { int [ ] arr = { 1 , 20 , 6 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( mergeSort ( arr , n ) ) ; } }
Find the number of different numbers in the array after applying the given operation q times | C # implementation for above approach ; To store the tree in lazy propagation ; To store the different numbers ; Function to update in the range [ x , y ) with given value ; check out of bound ; check for complete overlap ; find the mid number ; check for pending updates ; make lazy [ id ] = 0 , so that it has no pending updates ; call for two child nodes ; Function to find non - zero integers in the range [ l , r ) ; if id contains positive number ; There is no need to see the children , because all the interval have same number ; check for out of bound ; find the middle number ; call for two child nodes ; Driver Code ; size of the array and number of queries ; Update operation for l , r , x , id , 0 , n ; Query operation to get answer in the range [ 0 , n - 1 ] ; Print the count of non - zero elements
using System ; using System . Collections . Generic ; public class geeks { static int N = 100005 ; static int [ ] lazy = new int [ 4 * N ] ; static HashSet < int > se = new HashSet < int > ( ) ; public static void update ( int x , int y , int value , int id , int l , int r ) { if ( x >= r l >= y ) return ; if ( x <= l && r <= y ) { lazy [ id ] = value ; return ; } int mid = ( l + r ) / 2 ; if ( lazy [ id ] != 0 ) lazy [ 2 * id ] = lazy [ 2 * id + 1 ] = lazy [ id ] ; lazy [ id ] = 0 ; update ( x , y , value , 2 * id , l , mid ) ; update ( x , y , value , 2 * id + 1 , mid , r ) ; } public static void query ( int id , int l , int r ) { if ( lazy [ id ] != 0 ) { se . Add ( lazy [ id ] ) ; return ; } if ( r - l < 2 ) return ; int mid = ( l + r ) / 2 ; query ( 2 * id , l , mid ) ; query ( 2 * id + 1 , mid , r ) ; } public static void Main ( String [ ] args ) { int n = 5 , q = 3 ; update ( 1 , 4 , 1 , 1 , 0 , n ) ; update ( 0 , 2 , 2 , 1 , 0 , n ) ; update ( 3 , 4 , 3 , 1 , 0 , n ) ; query ( 1 , 0 , n ) ; Console . WriteLine ( se . Count ) ; } }
Shuffle 2 n integers in format { a1 , b1 , a2 , b2 , a3 , b3 , ... ... , an , bn } without using extra space | C # Naive program to shuffle an array of size 2 n ; method to shuffle an array of size 2 n ; Rotate the element to the left ; swap a [ j - 1 ] , a [ j ] ; Driver Code
using System ; class GFG { static void shuffleArray ( int [ ] a , int n ) { for ( int i = 0 , q = 1 , k = n ; i < n ; i ++ , k ++ , q ++ ) for ( int j = k ; j > i + q ; j -- ) { int temp = a [ j - 1 ] ; a [ j - 1 ] = a [ j ] ; a [ j ] = temp ; } } public static void Main ( ) { int [ ] a = { 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 } ; shuffleArray ( a , a . Length / 2 ) ; for ( int i = 0 ; i < a . Length ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } }
Place k elements such that minimum distance is maximized | C # program to find largest minimum distance among k points . ; Returns true if it is possible to arrange k elements of arr [ 0. . n - 1 ] with minimum distance given as mid . ; Place first element at arr [ 0 ] position ; Initialize count of elements placed . ; Try placing k elements with minimum distance mid . ; Place next element if its distance from the previously placed element is greater than current mid ; Return if all elements are placed successfully ; Returns largest minimum distance for k elements in arr [ 0. . n - 1 ] . If elements can 't be placed, returns -1. ; Sort the positions ; Initialize result . ; Consider the maximum possible distance ; Do binary search for largest minimum distance ; If it is possible to place k elements with minimum distance mid , search for higher distance . ; Change value of variable max to mid if all elements can be successfully placed ; If not possible to place k elements , search for lower distance ; driver code
using System ; public class GFG { static bool isFeasible ( int mid , int [ ] arr , int n , int k ) { int pos = arr [ 0 ] ; int elements = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - pos >= mid ) { pos = arr [ i ] ; elements ++ ; if ( elements == k ) return true ; } } return false ; } static int largestMinDist ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int res = - 1 ; int left = 1 , right = arr [ n - 1 ] ; while ( left < right ) { int mid = ( left + right ) / 2 ; if ( isFeasible ( mid , arr , n , k ) ) { res = Math . Max ( res , mid ) ; left = mid + 1 ; } else right = mid ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 8 , 4 , 9 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( largestMinDist ( arr , n , k ) ) ; } }
Find frequency of each element in a limited range array in less than O ( n ) time | C # program to count number of occurrences of each element in the array ; It prints number of occurrences of each element in the array . ; HashMap to store frequencies ; traverse the array ; update the frequency ; traverse the hashmap ; Driver function
using System ; using System . Collections . Generic ; class GFG { static void findFrequency ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] = 0 ; mp [ arr [ i ] ] ++ ; } foreach ( KeyValuePair < int , int > kvp in mp ) Console . WriteLine ( " Element ▁ " + kvp . Key + " ▁ occurs ▁ " + kvp . Value + " ▁ times " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int n = arr . Length ; findFrequency ( arr , n ) ; } }
Square root of an integer | A C # program to find floor ( sqrt ( x ) ; Base Cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; If mid * mid is greater than x ; Driver Code
using System ; class GFG { public static int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; int start = 1 , end = x , ans = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid * mid == x ) return mid ; if ( mid * mid < x ) { start = mid + 1 ; ans = mid ; } else end = mid - 1 ; } return ans ; } static public void Main ( ) { int x = 11 ; Console . WriteLine ( floorSqrt ( x ) ) ; } }
Sine Rule with Derivation , Example and Implementation | C # program for the above approach ; Function to calculate remaining two sides ; Calculate angle B ; Convert angles to their respective radians for using trigonometric functions ; Sine rule ; Print the answer ; Driver code ; Input ; Function Call
using System ; class GFG { static void findSides ( double A , double C , double c ) { double B = 180 - ( A + C ) ; A = A * ( 3.14159 / 180 ) ; C = C * ( 3.14159 / 180 ) ; B = B * ( 3.14159 / 180 ) ; double a = ( c / Math . Sin ( C ) ) * Math . Sin ( A ) ; double b = ( c / Math . Sin ( C ) ) * Math . Sin ( B ) ; Console . WriteLine ( " { 0 : F2 } " , a ) ; Console . WriteLine ( " { 0 : F2 } " , b ) ; } public static void Main ( String [ ] args ) { double A = 45.0 ; double C = 35.0 ; double c = 23 ; findSides ( A , C , c ) ; } }
Equation of a normal to a Circle from a given point | C # program for the above approach ; Function to calculate the slope ; Store the coordinates the center of the circle ; If slope becomes infinity ; Stores the slope ; If slope is zero ; Return the result ; Function to find the equation of the normal to a circle from a given point ; Stores the slope of the normal ; If slope becomes infinity ; If slope is zero ; Otherwise , print the equation of the normal ; Driver code ; Given Input ; Function Call
using System ; class GFG { static double normal_slope ( double a , double b , double x1 , double y1 ) { double g = a / 2 ; double f = b / 2 ; if ( g - x1 == 0 ) return ( - 1 ) ; double slope = ( f - y1 ) / ( g - x1 ) ; if ( slope == 0 ) return ( - 2 ) ; return slope ; } static void normal_equation ( double a , double b , double x1 , double y1 ) { double slope = normal_slope ( a , b , x1 , y1 ) ; if ( slope == - 1 ) { Console . WriteLine ( " x ▁ = ▁ " + x1 ) ; } if ( slope == - 2 ) { Console . WriteLine ( " y ▁ = ▁ " + y1 ) ; } if ( slope != - 1 && slope != - 2 ) { x1 *= - slope ; x1 += y1 ; if ( x1 > 0 ) Console . WriteLine ( " y ▁ = ▁ " + slope + " x ▁ + " + Math . Round ( x1 , 2 ) ) ; else Console . WriteLine ( " y ▁ = ▁ " + slope + " x ▁ " + Math . Round ( x1 , 2 ) ) ; } } public static void Main ( String [ ] args ) { int a = 4 , b = 6 ; int x1 = 12 , y1 = 14 ; normal_equation ( a , b , x1 , y1 ) ; } }
Sum of squares of distances between all pairs from given points | C # program for the above approach ; Function to find the sum of squares of distance between all distinct pairs ; Stores final answer ; Traverse the array ; Adding the effect of this point for all the previous ; Temporarily add the square of x - coordinate ; Add the effect of this point for all the previous y - points ; Print the desired answer ; Driver code
using System ; class GFG { static void findSquareSum ( int [ , ] Coordinates , int N ) { long xq = 0 , yq = 0 ; long xs = 0 , ys = 0 ; long res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int a , b ; a = Coordinates [ i , 0 ] ; b = Coordinates [ i , 1 ] ; res += xq ; res -= 2 * xs * a ; res += i * ( long ) ( a * a ) ; xq += a * a ; xs += a ; res += yq ; res -= 2 * ys * b ; res += i * ( long ) b * b ; yq += b * b ; ys += b ; } Console . Write ( res ) ; } static void Main ( ) { int [ , ] arr = { { 1 , 1 } , { - 1 , - 1 } , { 1 , - 1 } , { - 1 , 1 } } ; int N = arr . GetLength ( 0 ) ; findSquareSum ( arr , N ) ; } }
Count points from an array that lies inside a semi | C # program for above approach ; Traverse the array ; Stores if a point lies above the diameter or not ; Stores if the R is less than or equal to the distance between center and point ; Driver code ;
using System ; class Gfg { public static int getPointsIns ( int x1 , int y1 , int radius , int x2 , int y2 , pair [ ] points ) { int ans = 0 ; for ( int i = 0 ; i < points . Length ; i ++ ) { bool condOne = false , condTwo = false ; if ( ( points [ i ] . b - y2 ) * ( x2 - x1 ) - ( y2 - y1 ) * ( points [ i ] . a - x2 ) >= 0 ) { condOne = true ; } if ( radius >= ( int ) Math . Sqrt ( Math . Pow ( ( y1 - points [ i ] . b ) , 2 ) + Math . Pow ( x1 - points [ i ] . a , 2 ) ) ) { condTwo = true ; } if ( condOne && condTwo ) { ans += 1 ; } } return ans ; } public static void Main ( string [ ] args ) { int X = 0 ; int Y = 0 ; int R = 5 ; int P = 5 ; int Q = 0 ; pair [ ] arr = { new pair ( 2 , 3 ) , new pair ( 5 , 6 ) , new pair ( - 1 , 4 ) , new pair ( 5 , 5 ) } ; Console . Write ( getPointsIns ( X , Y , R , P , Q , arr ) ) ; } } public class pair { public int a ; public int b ; public pair ( int a , int b ) { this . a = a ; this . b = b ; } }