codeparrot/xlcost-text-to-code huggingface dataset:
Minimize elements to be added to a given array such that it contains another given array as its subsequence | C # program for the above approach ; Function that finds the minimum number of the element must be added to make A as a subsequence in B ; Base Case ; dp [ i , j ] indicates the length of LCS of A of length i & B of length j ; If there are no elements either in A or B then the length of lcs is 0 ; If the element present at ith and jth index of A and B are equal then include in LCS ; If they are not equal then take the max ; Return difference of length of A and lcs of A and B ; Driver Code ; Given sequence A and B ; Function call
using System ; class GFG { static int transformSubsequence ( int n , int m , int [ ] A , int [ ] B ) { if ( B . Length == 0 ) return n ; int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < m + 1 ; j ++ ) { if ( i == 0 j == 0 ) dp [ i , j ] = 0 ; else if ( A [ i - 1 ] == B [ j - 1 ] ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } return n - dp [ n , m ] ; } public static void Main ( String [ ] args ) { int N = 5 ; int M = 6 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; int [ ] B = { 2 , 5 , 6 , 4 , 9 , 12 } ; Console . Write ( transformSubsequence ( N , M , A , B ) ) ; } }
Check if the sum of a subarray within a given range is a perfect square or not | C # implementation of the above approach ; Function to calculate the square root of the sum of a subarray in a given range ; Calculate the sum of array elements within a given range ; Finding the square root ; If a perfect square is found ; Reduce the search space if the value is greater than sum ; Reduce the search space if the value if smaller than sum ; Driver Code ; Given Array ; Given range ; Function Call
using System ; class GFG { static int checkForPerfectSquare ( int [ ] arr , int i , int j ) { int mid , sum = 0 ; for ( int m = i ; m <= j ; m ++ ) { sum += arr [ m ] ; } int low = 0 , high = sum / 2 ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( mid * mid == sum ) { return mid ; } else if ( mid * mid > sum ) { high = mid - 1 ; } else { low = mid + 1 ; } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 19 , 33 , 48 , 90 , 100 } ; int L = 1 , R = 3 ; Console . Write ( checkForPerfectSquare ( arr , L , R ) ) ; } }
Count of subarrays forming an Arithmetic Progression ( AP ) | C # implementation of the above approach ; Function to find the total count of subarrays ; Iterate over each subarray ; Difference between first two terms of subarray ; Iterate over the subarray from i to j ; Check if the difference of all adjacent elements is same ; Driver Code ; Given array ; Function Call
using System ; class GFG { static int calcSubarray ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { bool flag = true ; int comm_diff = A [ i + 1 ] - A [ i ] ; for ( int k = i ; k < j ; k ++ ) { if ( A [ k + 1 ] - A [ k ] == comm_diff ) { continue ; } else { flag = false ; break ; } } if ( flag ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] A = { 8 , 7 , 4 , 1 , 0 } ; int N = A . Length ; Console . Write ( calcSubarray ( A , N ) ) ; } }
Largest Sum Contiguous Subarray having unique elements | C # program for the above approach ; Function to calculate required maximum subarray sum ; Initialize two pointers ; Stores the unique elements ; Insert the first element ; Current max sum ; Global maximum sum ; Update sum & increment j ; Add the current element ; Update sum and increment i and remove arr [ i ] from set ; Remove the element from start position ; Return the maximum sum ; Driver Code ; Given array [ ] arr ; Function Call ; Print the maximum sum
using System ; using System . Collections . Generic ; class GFG { public static int maxSumSubarray ( int [ ] arr ) { int i = 0 , j = 1 ; HashSet < int > set = new HashSet < int > ( ) ; set . Add ( arr [ 0 ] ) ; int sum = arr [ 0 ] ; int maxsum = sum ; while ( i < arr . Length - 1 && j < arr . Length ) { if ( ! set . Contains ( arr [ j ] ) ) { sum = sum + arr [ j ] ; maxsum = Math . Max ( sum , maxsum ) ; set . Add ( arr [ j ++ ] ) ; } else { sum -= arr [ i ] ; set . Remove ( arr [ i ++ ] ) ; } } return maxsum ; } public static void Main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 1 , 5 } ; int ans = maxSumSubarray ( arr ) ; Console . WriteLine ( ans ) ; } }
Count of numbers having only one unset bit in a range [ L , R ] | C # program for the above approach ; Function to count numbers in the range [ l , r ] having exactly one unset bit ; Stores the required count ; Iterate over the range ; Calculate number of bits ; Calculate number of set bits ; If count of unset bits is 1 ; Increment answer ; Return the answer ; Driver Code ; Function call
using System ; class GFG { static int count_numbers ( int L , int R ) { int ans = 0 ; for ( int n = L ; n <= R ; n ++ ) { int no_of_bits = ( int ) ( Math . Log ( n ) + 1 ) ; int no_of_set_bits = bitCount ( n ) ; if ( no_of_bits - no_of_set_bits == 1 ) { ans ++ ; } } return ans ; } static int bitCount ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int L = 4 , R = 9 ; Console . Write ( count_numbers ( L , R ) ) ; } }
Count of numbers having only one unset bit in a range [ L , R ] | C # program for the above approach ; Function to count numbers in the range [ L , R ] having exactly one unset bit ; Stores the count elements having one zero in binary ; Stores the maximum number of bits needed to represent number ; Loop over for zero bit position ; Number having zero_bit as unset and remaining bits set ; Sets all bits before zero_bit ; Set the bit at position j ; Set the bit position at j ; If cur is in the range [ L , R ] , then increment ans ; Return ans ; Driver Code ; Function Call
using System ; public class GFG { static int count_numbers ( int L , int R ) { int ans = 0 ; int LogR = ( int ) ( Math . Log ( R ) + 1 ) ; for ( int zero_bit = 0 ; zero_bit < LogR ; zero_bit ++ ) { int cur = 0 ; for ( int j = 0 ; j < zero_bit ; j ++ ) { cur |= ( 1 << j ) ; } for ( int j = zero_bit + 1 ; j < LogR ; j ++ ) { cur |= ( 1 << j ) ; if ( cur >= L && cur <= R ) { ans ++ ; } } } return ans ; } public static void Main ( String [ ] args ) { int L = 4 , R = 9 ; Console . Write ( count_numbers ( L , R ) ) ; } }
Sum of all distances between occurrences of same characters in a given string | C # program for the above approach ; Function to calculate the sum of distances between occurrences of same characters in a String ; If similar characters are found ; Add the difference of their positions ; Return the answer ; Driver Code
using System ; class GFG { static int findSum ( String s ) { int sum = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { for ( int j = i + 1 ; j < s . Length ; j ++ ) { if ( s [ i ] == s [ j ] ) { sum += ( j - i ) ; } } } return sum ; } public static void Main ( String [ ] args ) { String s = " ttt " ; Console . Write ( findSum ( s ) + " STRNEWLINE " ) ; } }
Count array elements that can be represented as sum of at least two consecutive array elements | C # program for above approach ; Function to find the number of array elements that can be represented as the sum of two or more consecutive array elements ; Stores the frequencies of array elements ; Stores required count ; Update frequency of each array element ; Find sum of all subarrays ; Increment ans by cnt [ sum ] ; Reset cnt [ sum ] by 0 ; Return ans ; Driver Code ; Given array ; Function call
using System ; class GFG { static int countElements ( int [ ] a , int n ) { int [ ] cnt = new int [ n + 1 ] ; int ans = 0 ; foreach ( int k in a ) { ++ cnt [ k ] ; } for ( int l = 0 ; l < n ; ++ l ) { int sum = 0 ; for ( int r = l ; r < n ; ++ r ) { sum += a [ r ] ; if ( l == r ) continue ; if ( sum <= n ) { ans += cnt [ sum ] ; cnt [ sum ] = 0 ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 1 , 1 , 1 , 1 } ; Console . WriteLine ( countElements ( a , a . Length ) ) ; } }
Maximum GCD among all pairs ( i , j ) of first N natural numbers | C # program for the above approach ; Function to find maximum gcd of all pairs possible from first n natural numbers ; Stores maximum gcd ; Iterate over all possible pairs ; Update maximum GCD ; Driver Code
using System ; class GFG { static int maxGCD ( int n ) { int maxHcf = int . MinValue ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { maxHcf = Math . Max ( maxHcf , __gcd ( i , j ) ) ; } } return maxHcf ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . Write ( maxGCD ( n ) ) ; } }
Maximum GCD among all pairs ( i , j ) of first N natural numbers | C # implementation of the above approach ; Function to find the maximum GCD among all the pairs from first n natural numbers ; Return max GCD ; Driver code
using System ; class GFG { static int maxGCD ( int n ) { return ( n / 2 ) ; } public static void Main ( String [ ] args ) { int n = 4 ; Console . Write ( maxGCD ( n ) ) ; } }
Remove odd indexed characters from a given string | C # program to implement the above approach ; Function to remove odd indexed characters from a given string ; Stores the resultant string ; If the current index is odd ; Skip the character ; Otherwise , append the character ; Return the modified string ; Driver Code ; Remove the characters which have odd index
using System ; class GFG { static string removeOddIndexCharacters ( string s ) { string new_string = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( i % 2 == 1 ) continue ; new_string += s [ i ] ; } return new_string ; } public static void Main ( ) { string str = " abcdef " ; str = removeOddIndexCharacters ( str ) ; Console . Write ( str ) ; } }
Longest subarray forming a Geometic Progression ( GP ) | C # program to implement the above approach ; Function to return the length of the longest subarray forming a GP in a sorted array ; Base Case ; Stores the length of GP and the common ratio ; Stores the maximum length of the GP ; Traverse the array ; Check if the common ratio is valid for GP ; If the current common ratio is equal to previous common ratio ; Increment the length of the GP ; Store the max length of GP ; Otherwise ; Update the common ratio ; Update the length of GP ; Store the max length of GP ; Update the length of GP ; Store the max length of GP ; Return the max length of GP ; Driver code ; Given array ; Length of the array ; Function call
using System ; class GFG { static int longestGP ( int [ ] A , int N ) { if ( N < 2 ) return N ; int length = 1 , common_ratio = 1 ; int maxlength = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i + 1 ] % A [ i ] == 0 ) { if ( A [ i + 1 ] / A [ i ] == common_ratio ) { length = length + 1 ; maxlength = Math . Max ( maxlength , length ) ; } else { common_ratio = A [ i + 1 ] / A [ i ] ; length = 2 ; } } else { maxlength = Math . Max ( maxlength , length ) ; length = 1 ; } } maxlength = Math . Max ( maxlength , length ) ; return maxlength ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 7 , 14 , 28 , 56 , 89 } ; int N = arr . Length ; Console . WriteLine ( longestGP ( arr , N ) ) ; } }
Maximum number of uncrossed lines between two given arrays | C # program for the above approach ; Function to count maximum number of uncrossed lines between the two given arrays ; Stores the length of lcs obtained upto every index ; Iterate over first array ; Iterate over second array ; Update value in dp table ; If both characters are equal ; Update the length of lcs ; If both characters are not equal ; Update the table ; Return the answer ; Driver Code ; Given array A [ ] and B [ ] ; Function call
using System ; class GFG { static int uncrossedLines ( int [ ] a , int [ ] b , int n , int m ) { int [ , ] dp = new int [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i , j ] = 0 ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i , j ] = 1 + dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } return dp [ n , m ] ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 9 , 2 } ; int [ ] B = { 3 , 2 , 9 } ; int N = A . Length ; int M = B . Length ; Console . Write ( uncrossedLines ( A , B , N , M ) ) ; } } }
Minimize flips required to make all shortest paths from top | C # program for the above approach ; Function to count the minimum number of flips required ; Dimensions of matrix ; Stores the count the flips ; Check if element is same or not ; Return the readonly count ; Driver Code ; Given Matrix ; Given path as a string ; Function call
using System ; class GFG { static int minFlips ( int [ , ] mat , String s ) { int N = mat . GetLength ( 0 ) ; int M = mat . GetLength ( 1 ) ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i , j ] != s [ i + j ] - '0' ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 0 , 1 } , { 0 , 1 , 1 } , { 0 , 0 , 0 } } ; String s = "10001" ; Console . Write ( minFlips ( mat , s ) ) ; } }
Generate a pair of integers from a range [ L , R ] whose LCM also lies within the range | C # implementation of the above approach ; Checking if any pair is possible or not in range ( l , r ) ; If not possible print ( - 1 ) ; Print LCM pair ; Driver code ; Function call
using System ; class GFG { static void lcmpair ( int l , int r ) { int x , y ; x = l ; y = 2 * l ; if ( y > r ) { Console . Write ( " - 1 STRNEWLINE " ) ; } else { Console . Write ( " X ▁ = ▁ " + x + " ▁ Y ▁ = ▁ " + y + " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int l = 13 , r = 69 ; lcmpair ( l , r ) ; } }
Count non | C # program to implement the above approach ; Function to reverse a number ; Store the reverse of N ; Return reverse of N ; Function to get the count of non - palindromic numbers having same first and last digit ; Store the required count ; Traverse the array ; Store reverse of arr [ i ] ; Check for palindrome ; IF non - palindromic ; Check if first and last digits are equal ; Driver code
using System ; class GFG { static int revNum ( int N ) { int x = 0 ; while ( N != 0 ) { x = x * 10 + N % 10 ; N = N / 10 ; } return x ; } static int ctNonPalin ( int [ ] arr , int N ) { int Res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int x = revNum ( arr [ i ] ) ; if ( x == arr [ i ] ) { continue ; } else { if ( arr [ i ] % 10 == x % 10 ) Res += 1 ; } } return Res ; } public static void Main ( ) { int [ ] arr = new int [ ] { 121 , 134 , 2342 , 4514 } ; int N = arr . Length ; Console . WriteLine ( ctNonPalin ( arr , N ) ) ; } }
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | C # program for the above approach ; Function to find the smallest subarray with sum greater than or equal target ; DP table to store the computed subproblems ; Initialize first column with 0 ; Initialize first row with 0 ; Check for invalid condition ; Fill up the dp table ; Print the minimum length ; Driver code
using System ; class GFG { static int minlt ( int [ ] arr , int target , int n ) { int [ , ] dp = new int [ arr . Length + 1 , target + 1 ] ; for ( int i = 0 ; i < arr . Length + 1 ; i ++ ) { for ( int j = 0 ; j < target + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } for ( int i = 0 ; i < arr . Length + 1 ; i ++ ) dp [ i , 0 ] = 0 ; for ( int j = 0 ; j < target + 1 ; j ++ ) dp [ 0 , j ] = int . MaxValue ; for ( int i = 1 ; i <= arr . Length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { if ( arr [ i - 1 ] > j ) { dp [ i , j ] = dp [ i - 1 , j ] ; } else { dp [ i , j ] = Math . Min ( dp [ i - 1 , j ] , ( dp [ i , j - arr [ i - 1 ] ] ) != int . MaxValue ? ( dp [ i , j - arr [ i - 1 ] ] + 1 ) : int . MaxValue ) ; } } } if ( dp [ arr . Length , target ] == int . MaxValue ) { return - 1 ; } else { return dp [ arr . Length , target ] ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 4 , 1 } ; int target = 11 ; int n = arr . Length ; Console . Write ( minlt ( arr , target , n ) ) ; } }
Minimum absolute difference of server loads | C # program to implement the above approach ; Function which returns the minimum difference of loads ; Compute the overall server load ; Stores the results of subproblems ; Fill the partition table in bottom up manner ; If i - th server is included ; If i - th server is excluded ; Server A load : total_sum - ans Server B load : ans Diff : abs ( total_sum - 2 * ans ) ; Driver Code ; Function call
using System ; class GFG { static int minServerLoads ( int n , int [ ] servers ) { int totalLoad = 0 ; for ( int i = 0 ; i < servers . Length ; i ++ ) totalLoad += servers [ i ] ; int requiredLoad = totalLoad / 2 ; int [ , ] dp = new int [ n + 1 , requiredLoad + 1 ] ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < requiredLoad + 1 ; j ++ ) { if ( servers [ i - 1 ] > j ) dp [ i , j ] = dp [ i - 1 , j ] ; else dp [ i , j ] = Math . Max ( dp [ i - 1 , j ] , servers [ i - 1 ] + dp [ i - 1 , j - servers [ i - 1 ] ] ) ; } } return totalLoad - 2 * dp [ n , requiredLoad ] ; } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] servers = { 1 , 2 , 3 , 4 , 5 } ; Console . Write ( minServerLoads ( N , servers ) ) ; } }
Length of longest subarray with positive product | C # program to implement the above approach ; Function to find the length of longest subarray whose product is positive ; Stores the length of current subarray with positive product ; Stores the length of current subarray with negative product ; Stores the length of the longest subarray with positive product ; Reset the value ; If current element is positive ; Increment the length of subarray with positive product ; If at least one element is present in the subarray with negative product ; Update res ; If current element is negative ; Increment the length of subarray with negative product ; If at least one element is present in the subarray with positive product ; Update res ; Driver Code
using System ; class GFG { static int maxLenSub ( int [ ] arr , int N ) { int Pos = 0 ; int Neg = 0 ; int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { Pos = Neg = 0 ; } else if ( arr [ i ] > 0 ) { Pos += 1 ; if ( Neg != 0 ) { Neg += 1 ; } res = Math . Max ( res , Pos ) ; } else { Pos = Pos + Neg ; Neg = Pos - Neg ; Neg = Pos - Neg ; Neg += 1 ; if ( Pos != 0 ) { Pos += 1 ; } res = Math . Max ( res , Pos ) ; } } return res ; } public static void Main ( ) { int [ ] arr = { - 1 , - 2 , - 3 , 0 , 1 } ; int N = arr . Length ; Console . Write ( maxLenSub ( arr , N ) ) ; } }
Print all strings of maximum length from an array of strings | C # program to implement the above approach ; Function to find the length of the longest String from the given array of Strings ; Traverse the array ; Stores the length of current String ; Update maximum length ; Return the maximum length ; Function to print the longest Strings from the array ; Find the Strings having length equals to len ; Print the resultant vector of Strings ; Function to print all the longest Strings from the array ; Find the length of longest String ; Find and print all the Strings having length equals to max ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int maxLength ( String [ ] arr ) { int len = int . MinValue ; int N = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] . Length ; if ( len < l ) { len = l ; } } return len ; } static void maxStrings ( String [ ] arr , int len ) { int N = arr . Length ; List < String > ans = new List < String > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( len == arr [ i ] . Length ) { ans . Add ( arr [ i ] ) ; } } for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } static void printStrings ( String [ ] arr ) { int max = maxLength ( arr ) ; maxStrings ( arr , max ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { " aba " , " aa " , " ad " , " vcd " , " aba " } ; printStrings ( arr ) ; } }
Minimize count of flips required such that no substring of 0 s have length exceeding K | C # program to implement the above approach ; Function to return minimum number of flips required ; Base Case ; Stores the count of minimum number of flips ; Stores the count of zeros in current subString ; If current character is 0 ; Continue ongoing subString ; Start a new subString ; If k consecutive zeroes are obtained ; End segment ; Return the result ; Driver Code ; Function call
using System ; class GFG { static int min_flips ( String str , int k ) { if ( str . Length == 0 ) return 0 ; int ans = 0 ; int cnt_zeros = 0 ; foreach ( char ch in str . ToCharArray ( ) ) { if ( ch == '0' ) { ++ cnt_zeros ; } else { cnt_zeros = 0 ; } if ( cnt_zeros == k ) { ++ ans ; cnt_zeros = 0 ; } } return ans ; } public static void Main ( String [ ] args ) { String str = "11100000011" ; int k = 3 ; Console . Write ( min_flips ( str , k ) ) ; } }
Find all possible subarrays having product less than or equal to K | C # Program to implement the above approach ; Function to return all possible subarrays having product less than or equal to K ; Store the required subarrays ; Stores the product of current subarray ; Stores the starting index of the current subarray ; Check for empty array ; Iterate over the array ; Calculate product ; If product exceeds K ; Reduce product ; Increase starting index of current subarray ; Stores the subarray elements ; Store the subarray elements ; Add the subarrays to the list ; Return the final list of subarrays ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static List < List < int > > maxSubArray ( int [ ] arr , int K ) { List < List < int > > solution = new List < List < int > > ( ) ; int multi = 1 ; int start = 0 ; if ( arr . Length <= 1 K < 0 ) { return new List < List < int > > ( ) ; } for ( int i = 0 ; i < arr . Length ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = multi / arr [ start ] ; start ++ ; } List < int > list = new List < int > ( ) ; for ( int j = i ; j >= start ; j -- ) { list . Insert ( 0 , arr [ j ] ) ; solution . Add ( new List < int > ( list ) ) ; } } return solution ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 1 , 4 } ; int K = 7 ; List < List < int > > list = maxSubArray ( arr , K ) ; foreach ( List < int > i in list ) { Console . Write ( " [ " ) ; foreach ( int j in i ) { Console . Write ( j ) ; if ( i . Count > 1 ) Console . Write ( " , " ) ; } Console . Write ( " ] " ) ; } } }
Count of collisions at a point ( X , Y ) | C # program to implement the above approach ; Function to find the count of possible pairs of collisions ; Stores the time at which points reach the origin ; Calculate time for each point ; Sort the times ; Counting total collisions ; Count of elements arriving at a given point at the same time ; Driver Code ; Given set of points with speed ; Function call
using System ; using System . Collections ; class GFG { static double solve ( int [ , ] D , int N , int X , int Y ) { ArrayList T = new ArrayList ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int x = D [ i , 0 ] ; int y = D [ i , 1 ] ; double speed = D [ i , 2 ] ; double time = ( ( x * x - X * X ) + ( y * y - Y * Y ) ) / ( speed * speed ) ; T . Add ( time ) ; } T . Sort ( ) ; int j = 0 ; int total = 0 ; while ( j < ( T . Count - 1 ) ) { int count = 1 ; while ( ( j < ( T . Count - 1 ) ) && ( Convert . ToDouble ( T [ j ] ) . CompareTo ( Convert . ToDouble ( T [ j + 1 ] ) ) == 0 ) ) { count += 1 ; j += 1 ; } total += ( count * ( count - 1 ) ) / 2 ; j += 1 ; } return total ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ , ] D = new int [ , ] { { 5 , 12 , 1 } , { 16 , 63 , 5 } , { - 10 , 24 , 2 } , { 7 , 24 , 2 } , { - 24 , 7 , 2 } } ; int X = 0 , Y = 0 ; Console . WriteLine ( solve ( D , N , X , Y ) ) ; } }