codeparrot/xlcost-text-to-code huggingface dataset:
Minimize the cost of partitioning an array into K groups | C # implementation of above approach ; Function to return the minimum number of operations needed to partition the array in k contiguous groups such that all elements of a given group are identical ; n is the size of the array ; dp ( i , j ) represents the minimum cost for partitioning the array [ 0. . i ] into j groups ; Base case , cost is 0 for parititoning the array [ 0. .0 ] into 1 group ; Fill dp ( i , j ) and the answer will be stored at dp ( n - 1 , k ) ; The maximum groups that the segment 0. . i can be divided in is represented by maxGroups ; Initialize dp ( i , j ) to infinity ; Divide segment 0. . i in 1 group ; map and freqOfMode are together used to keep track of the frequency of the most occurring element in [ 0. . i ] ; Change all the elements in the range 0. . i to the most frequent element in this range ; If the jth group is the segment from it . . i , we change all the elements in this range to this range 's most occurring element ; Number of elements we need to change in the jth group i . e . the range it . . i ; For all the possible sizes of the jth group that end at the ith element we pick the size that gives us the minimum cost for dp ( i , j ) elementsToChange is the cost of making all the elements in the jth group identical and we make use of dp ( it - 1 , j - 1 ) to find the overall minimal cost ; Return the minimum cost for partitioning array [ 0. . n - 1 ] into k groups which is stored at dp ( n - 1 , k ) ; Driver code
using System ; class GFG { static int getMinimumOps ( int [ ] ar , int k ) { int n = ar . Length ; int [ , ] dp = new int [ n , k + 1 ] ; dp [ 0 , 1 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int maxGroups = Math . Min ( k , i + 1 ) ; for ( int j = 1 ; j <= maxGroups ; j ++ ) { dp [ i , j ] = int . MaxValue ; if ( j == 1 ) { int [ ] freq = new int [ 100000 ] ; int freqOfMode = 0 ; for ( int it = 0 ; it <= i ; it ++ ) { freq [ ar [ it ] ] ++ ; int newElementFreq = freq [ ar [ it ] ] ; if ( newElementFreq > freqOfMode ) freqOfMode = newElementFreq ; } dp [ i , 1 ] = ( i + 1 ) - freqOfMode ; } else { int [ ] freq = new int [ 100000 ] ; int freqOfMode = 0 ; for ( int it = i ; it >= j - 1 ; it -- ) { freq [ ar [ it ] ] ++ ; int newElementFreq = freq [ ar [ it ] ] ; if ( newElementFreq > freqOfMode ) freqOfMode = newElementFreq ; int elementsToChange = i - it + 1 ; elementsToChange -= freqOfMode ; dp [ i , j ] = Math . Min ( dp [ it - 1 , j - 1 ] + elementsToChange , dp [ i , j ] ) ; } } } } return dp [ n - 1 , k ] ; } public static void Main ( String [ ] args ) { int k = 3 ; int [ ] ar = { 3 , 1 , 3 , 3 , 2 , 1 , 8 , 5 } ; Console . WriteLine ( getMinimumOps ( ar , k ) ) ; } }
Find numbers which are multiples of first array and factors of second array | C # implementation of the approach ; Function to return the LCM of two numbers ; Function to print the required numbers ; To store the lcm of array a [ ] elements and the gcd of array b [ ] elements ; Finding LCM of first array ; Finding GCD of second array ; No such element exists ; All the multiples of lcmA which are less than or equal to gcdB and evenly divide gcdB will satisfy the conditions ; Driver code
using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int lcm ( int x , int y ) { int temp = ( x * y ) / __gcd ( x , y ) ; return temp ; } static void findNumbers ( int [ ] a , int n , int [ ] b , int m ) { int lcmA = 1 , gcdB = 0 ; for ( int i = 0 ; i < n ; i ++ ) lcmA = lcm ( lcmA , a [ i ] ) ; for ( int i = 0 ; i < m ; i ++ ) gcdB = __gcd ( gcdB , b [ i ] ) ; if ( gcdB % lcmA != 0 ) { Console . Write ( " - 1" ) ; return ; } int num = lcmA ; while ( num <= gcdB ) { if ( gcdB % num == 0 ) Console . Write ( num + " ▁ " ) ; num += lcmA ; } } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 2 , 4 } ; int [ ] b = { 16 , 32 , 64 } ; int n = a . Length ; int m = b . Length ; findNumbers ( a , n , b , m ) ; } }
Probability such that two subset contains same number of elements | C # implementation of the above approach ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2 ; Recursive function to return gcd of a and b ; Function to find probability ; Calculate total possible ways and favourable ways . ; Divide by gcd such that they become relatively coprime ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void FindProbability ( int n ) { int up = binomialCoeff ( 2 * n , n ) ; int down = power ( 2 , 2 * n ) ; int g = gcd ( up , down ) ; up /= g ; down /= g ; Console . WriteLine ( up + " / " + down ) ; } public static void Main ( String [ ] args ) { int N = 8 ; FindProbability ( N ) ; } }
How to learn Pattern printing easily ? | C # implementation of the approach
using System ; class GFG { public static void Main ( String [ ] args ) { int N = 4 , i , j , min ; Console . WriteLine ( " Value ▁ of ▁ N : ▁ " + N ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; Console . Write ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; Console . Write ( N - min + 1 ) ; } Console . WriteLine ( ) ; } for ( i = N - 1 ; i >= 1 ; i -- ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; Console . Write ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; Console . Write ( N - min + 1 ) ; } Console . WriteLine ( ) ; } } }
Largest perfect square number in an Array | C # program to find the largest perfect square number among n numbers ; Function to check if a number is perfect square number or not ; takes the sqrt of the number ; checks if it is a perfect square number ; Function to find the largest perfect square number in the array ; stores the maximum of all perfect square numbers ; Traverse all elements in the array ; store the maximum if current element is a perfect square ; Driver Code
using System ; class GFG { static bool checkPerfectSquare ( double n ) { double d = Math . Sqrt ( n ) ; if ( d * d == n ) return true ; return false ; } static int largestPerfectSquareNumber ( int [ ] a , double n ) { int maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectSquare ( a [ i ] ) ) maxi = Math . Max ( a [ i ] , maxi ) ; } return maxi ; } public static void Main ( ) { int [ ] a = { 16 , 20 , 25 , 2 , 3 , 10 } ; double n = a . Length ; Console . WriteLine ( largestPerfectSquareNumber ( a , n ) ) ; } }
Maximum number of parallelograms that can be made using the given length of line segments | Function to find the maximum number of parallelograms can be made ; Finding the length of the frequency array ; Increasing the occurrence of each segment ; To store the count of parallelograms ; Counting parallelograms that can be made using 4 similar sides ; counting segments which have occurrence left >= 2 ; Adding parallelograms that can be made using 2 similar sides ; Driver code
using System ; class GFG { static void convert ( int n , int [ ] a ) { int z = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > z ) z = a [ i ] ; } z = z + 1 ; int [ ] ff = new int [ z ] ; for ( int i = 0 ; i < n ; i ++ ) { ff [ a [ i ] ] += 1 ; } int cc = 0 ; for ( int i = 0 ; i < z ; i ++ ) { cc += ( ff [ i ] / 4 ) ; ff [ i ] = ff [ i ] % 4 ; } int vv = 0 ; for ( int i = 0 ; i < z ; i ++ ) { if ( ff [ i ] >= 2 ) vv += 1 ; } cc += ( vv / 2 ) ; Console . WriteLine ( cc ) ; } static public void Main ( ) { int n = 4 ; int [ ] a = { 1 , 2 , 1 , 2 } ; convert ( n , a ) ; } }
Count pairs ( i , j ) such that ( i + j ) is divisible by A and B both | C # implementation of above approach ; Function to find the LCM ; Function to count the pairs ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static int find_LCM ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } static int CountPairs ( int n , int m , int A , int B ) { int cnt = 0 ; int lcm = find_LCM ( A , B ) ; for ( int i = 1 ; i <= n ; i ++ ) cnt += ( m + ( i % lcm ) ) / lcm ; return cnt ; } public static void Main ( ) { int n = 60 , m = 90 , A = 5 , B = 10 ; Console . WriteLine ( CountPairs ( n , m , A , B ) ) ; } }
Sorting without comparison of elements | C # program to sort an array without comparison operator . ; Represents node of a doubly linked list ; Count of elements in given range ; Count frequencies of all elements ; Traverse through range . For every element , print it its count times . ; Driver Code
using System ; class GFG { static void sortArr ( int [ ] arr , int n , int min , int max ) { int m = max - min + 1 ; int [ ] c = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ arr [ i ] - min ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < c [ i ] ; j ++ ) { Console . Write ( ( i + min ) + " ▁ " ) ; } } } static public void Main ( ) { int [ ] arr = { 10 , 10 , 1 , 4 , 4 , 100 , 0 } ; int min = 0 , max = 100 ; int n = arr . Length ; sortArr ( arr , n , min , max ) ; } }
Minimize the maximum distance between adjacent points after adding K points anywhere in between | C # program for the above approach ; Function to check if it is possible to add K points such that the maximum distance between adjacent points is D ; Stores the count of point used ; Iterate over all given points ; Add number of points required to be placed between ith and ( i + 1 ) th point ; Return answer ; Function to find the minimum value of maximum distance between adjacent points after adding K points any where between ; Stores the lower bound and upper bound of the given range ; Perform binary search ; Find the middle value ; Update the current range to lower half ; Update the current range to upper half ; Driver Code
using System ; public class GFG { public static bool isPossible ( double D , int [ ] arr , int N , int K ) { int used = 0 ; for ( int i = 0 ; i < N - 1 ; ++ i ) { used += ( int ) ( ( arr [ i + 1 ] - arr [ i ] ) / D ) ; } return used <= K ; } public static double minMaxDist ( int [ ] stations , int N , int K ) { double low = 0 , high = 1e8 ; while ( high - low > 1e-6 ) { double mid = ( low + high ) / 2.0 ; if ( isPossible ( mid , stations , N , K ) ) { high = mid ; } else { low = mid ; } } return low ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int K = 9 ; int N = arr . Length ; Console . Write ( " { 0 : F1 } " , minMaxDist ( arr , N , K ) ) ; } }
Find Binary permutations of given size not present in the Array | C # program for the above approach ; Function to find a Binary String of same length other than the Strings present in the array ; Map all the Strings present in the array ; Find all the subString that can be made ; If num already exists then increase counter ; If not found print ; If all the subStrings are present then print - 1 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findMissingBinaryString ( string [ ] nums , int N ) { HashSet < string > s = new HashSet < string > ( ) ; int counter = 0 ; foreach ( string str in nums ) { s . Add ( str ) ; } int total = ( int ) Math . Pow ( 2 , N ) ; for ( int i = 0 ; i < total ; i ++ ) { string num = " " ; for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( ( i & ( 1 << j ) ) > 0 ) { num += '1' ; } else { num += '0' ; } } if ( s . Contains ( num ) ) { counter ++ ; continue ; } else { Console . Write ( num + " , ▁ " ) ; } } if ( counter == total ) { Console . Write ( " - 1" ) ; } } public static void Main ( string [ ] args ) { int N = 3 ; string [ ] arr = { "101" , "111" , "001" , "011" , "100" , "110" } ; findMissingBinaryString ( arr , N ) ; } }
Queries to find number of connected grid components of given sizes in a Matrix | C # implementation for the above approach ; stores information about which cell are already visited in a particular BFS ; Stores the count of cells in the largest connected component ; Function checks if a cell is valid , i . e . it is inside the grid and equal to 1 ; Map to count the frequency of each connected component ; function to calculate the largest connected component ; Iterate over every cell ; Stores the indices of the matrix cells ; Mark the starting cell as visited and push it into the queue ; Iterate while the queue is not empty ; Go to the adjacent cells ; Drivers Code ; Given input array of 1 s and 0 s ; queries array ; sizeof queries array ; Count the frequency of each connected component ; Iterate over the given queries array and And answer the queries
using System ; using System . Collections . Generic ; public class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int n = 6 ; static int m = 6 ; static int [ ] dx = { 0 , 1 , - 1 , 0 } ; static int [ ] dy = { 1 , 0 , 0 , - 1 } ; static int [ , ] visited = new int [ n , m ] ; static int COUNT ; static bool is_valid ( int x , int y , int [ , ] matrix ) { if ( x < n && y < m && x >= 0 && y >= 0 ) { if ( visited [ x , y ] == 0 && matrix [ x , y ] == 1 ) return true ; else return false ; } else return false ; } static Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; static void findComponentSize ( int [ , ] matrix ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( visited [ i , j ] == 0 && matrix [ i , j ] == 1 ) { COUNT = 0 ; List < pair > q = new List < pair > ( ) ; q . Add ( new pair ( i , j ) ) ; visited [ i , j ] = 1 ; while ( q . Count > 0 ) { pair p = q [ 0 ] ; q . RemoveAt ( ) ; int x = p . first , y = p . second ; COUNT ++ ; for ( int k = 0 ; k < 4 ; k ++ ) { int newX = x + dx [ k ] ; int newY = y + dy [ k ] ; if ( is_valid ( newX , newY , matrix ) ) { q . Add ( new pair ( newX , newY ) ) ; visited [ newX , newY ] = 1 ; } } } if ( mp . ContainsKey ( COUNT ) ) { mp [ COUNT ] += 1 ; } else { mp . Add ( COUNT , 1 ) ; } } } } } public static void Main ( ) { int [ , ] matrix = new int [ , ] { { 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 1 , 1 } , { 0 , 0 , 0 , 1 , 1 , 1 } , { 0 , 0 , 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 , 0 } } ; int [ ] queries = { 6 , 1 , 8 , 2 } ; int N = queries . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { visited [ i , j ] = 0 ; } } findComponentSize ( matrix ) ; for ( int i = 0 ; i < N ; i ++ ) if ( mp . ContainsKey ( queries [ i ] ) != false ) Console . Write ( mp [ queries [ i ] ] + " ▁ " ) ; } }
Minimum value of X such that sum of arr [ i ] | C # program for the above approach ; Function to check if it is possible to get desired result ; Find the required value of the given expression ; Function to find the minimum value of X using binary search . ; Boundaries of the Binary Search ; Find the middle value ; Check for the middle value ; Update the upper ; Update the lower ; Driver code
using System ; public class GFG { static bool check ( int [ ] a , int [ ] b , int k , int x ) { int sum = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { sum = sum + ( int ) Math . Pow ( Math . Max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } for ( int i = 0 ; i < a . Length ; ++ ) { = + ( int ) Math . Pow ( Math . Max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } static int ( int [ ] a , int [ ] b , int n , int k ) { int l = 0 , u = ( int ) 1e9 ; while ( l < u ) { int m = ( l + u ) / 2 ; if ( check ( a , b , k , m ) ) u = m ; else l = m + 1 ; } return l ; } public static void ( String [ ] args ) { int n = 5 ; int k = 12 ; int [ ] a = { 2 , 1 , 4 , 3 , 5 } ; int [ ] b = { 4 , 3 , 2 , 3 , 1 } ; Console . WriteLine ( findMin ( a , b , n , k ) ) ; } }
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | C # program for the above approach ; Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Iterate over the range [ 0 , N ) ; Now , iterate from the value arr [ i ] - ( i % arr [ i ] ) till N with an increment of arr [ i ] ; If the given criteria satisfy then increment the value of count ; Return the resultant count ; Driver Code
using System ; class GFG { static int getPairsCount ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = arr [ i ] - ( i % arr [ i ] ) ; j < n ; j += arr [ i ] ) { if ( i < j && ( arr [ i ] * arr [ j ] ) == Math . Abs ( i - j ) ) { count ++ ; } } } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 4 } ; int N = arr . Length ; Console . Write ( getPairsCount ( arr , N ) ) ; } }
Count of subarrays with X as the most frequent element , for each value of X from 1 to N | C # program for the above approach ; Function to calculate the number of subarrays where X ( 1 <= X <= N ) is the most frequent element ; array to store the final answers ; Array to store current frequencies ; Variable to store the current most frequent element ; Update frequency array ; Update answer ; Print answer ; Driver code ; Input ; Function call
using System ; class GFG { public static void mostFrequent ( int [ ] arr , int N ) { int [ ] ans = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int [ ] count = new int [ N ] ; int best = 1 ; for ( int j = i ; j < N ; j ++ ) { count [ arr [ j ] - 1 ] ++ ; if ( best > 0 ) { if ( ( count [ arr [ j ] - 1 ] > count [ best - 1 ] ) || ( count [ arr [ j ] - 1 ] == count [ best - 1 ] && arr [ j ] < best ) ) { best = arr [ j ] ; } ans [ best - 1 ] ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 2 , 3 } ; int N = arr . Length ; mostFrequent ( arr , N ) ; } }
Count array elements whose highest power of 2 less than or equal to that number is present in the given array | C # program for the above approach ; Function to count array elements whose highest power of 2 is less than or equal to that number is present in the given array ; Stores the resultant count of array elements ; Stores frequency of visited array elements ; Traverse the array ; Calculate log base 2 of the element arr [ i ] ; Highest power of 2 whose value is at most arr [ i ] ; Increment the count by 1 ; Return the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countElement ( int [ ] arr , int N ) { int count = 1 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) m [ arr [ i ] ] ++ ; else m . Add ( arr [ i ] , 1 ) ; } for ( int i = 0 ; i < N ; i ++ ) { int lg = ( int ) Math . Log ( arr [ i ] ) ; int p = ( int ) Math . Pow ( 2 , lg ) ; if ( m . ContainsKey ( p ) ) { count ++ ; } } return count ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 6 , 9 } ; int N = arr . Length ; Console . Write ( countElement ( arr , N ) ) ; } }
Maximum number of intersections possible for any of the N given segments | C # program for the above approach ; Function to find the maximum number of intersections one segment has with all the other given segments ; Stores the resultant maximum count ; Stores the starting and the ending points ; Sort arrays points in the ascending order ; Traverse the array arr [ ] ; Find the count of segments on left of ith segment ; Find the count of segments on right of ith segment ; Find the total segments not intersecting with the current segment ; Store the count of segments that intersect with the ith segment ; Update the value of count ; Return the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int lower_bound ( int [ ] a , int low , int high , long element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int maximumIntersections ( int [ , ] arr , int N ) { int count = 0 ; int [ ] L = new int [ N ] ; int [ ] R = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { L [ i ] = arr [ i , 0 ] ; R [ i ] = arr [ i , 1 ] ; } Array . Sort ( L ) ; Array . Sort ( R ) ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i , 0 ] ; int r = arr [ i , 1 ] ; int x = lower_bound ( L , 0 , N , l ) ; int y = N - lower_bound ( R , 0 , N , r + 1 ) ; int cnt = x + y ; cnt = N - cnt - 1 ; count = Math . Max ( count , cnt ) ; } return count ; } public static void Main ( ) { int [ , ] arr = new int [ 3 , 2 ] { { 1 , 6 } , { 5 , 5 } , { 2 , 3 } } ; int N = 3 ; Console . Write ( maximumIntersections ( arr , N ) ) ; } }
Program to find the Nth Composite Number | C # program for the above approach ; Function to find the Nth Composite Numbers using Sieve of Eratosthenes ; Sieve of prime numbers ; Initialize the array to true ; Iterate over the range [ 2 , MAX_SIZE ] ; If IsPrime [ p ] is true ; Iterate over the range [ p * p , MAX_SIZE ] ; Stores the list of composite numbers ; Iterate over the range [ 4 , MAX_SIZE ] ; If i is not prime ; Return Nth Composite Number ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public static int NthComposite ( int N ) { int MAX_SIZE = 1000005 ; bool [ ] IsPrime = new bool [ MAX_SIZE ] ; Array . Fill ( IsPrime , true ) ; for ( int p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( int i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } List < int > Composites = new List < int > ( ) ; for ( int p = 4 ; p < MAX_SIZE ; p ++ ) if ( ! IsPrime [ p ] ) Composites . Add ( p ) ; return Composites [ N - 1 ] ; } static public void Main ( ) { int N = 4 ; Console . WriteLine ( NthComposite ( N ) ) ; } }
Intersection point of two Linked Lists | Set 3 | C # program for the above approach ; Structure of a node of a Linked List ; Constructor ; Function to find the intersection point of the two Linked Lists ; Traverse the first linked list and multiply all values by - 1 ; Update a -> data ; Update a ; Traverse the second Linked List and find the value of the first node having negative value ; Intersection point ; Update b ; Function to create linked lists ; Linked List L1 ; Linked List L2 ; Driver Code
using System ; public class Node { public int data ; public Node next ; public Node ( int x ) { public Node ( int x ) { data = x ; next = null ; } } public class GFG { static Node head1 = null ; static Node head2 = null ; static Node intersectingNode ( Node headA , Node headB ) { Node a = headA ; while ( a != null ) { a . data *= - 1 ; a = a . next ; } Node b = headB ; while ( b != null ) { if ( b . data < 0 ) break ; b = b . next ; } return b ; } static void formLinkList ( ) { head1 = new Node ( 3 ) ; head1 . next = new Node ( 6 ) ; head1 . next . next = new Node ( 9 ) ; head1 . next . next . next = new Node ( 15 ) ; head1 . next . next . next . next = new Node ( 30 ) ; head2 = new Node ( 10 ) ; head2 . next = head1 . next . next . next ; return ; } static public void Main ( ) { formLinkList ( ) ; Console . WriteLine ( Math . Abs ( intersectingNode ( head1 , head2 ) . data ) ) ; } }