codeparrot/xlcost-text-to-code huggingface dataset:
The Knight 's tour problem | Backtracking | C # program for Knight Tour problem ; A utility function to check if i , j are valid indexes for N * N chessboard ; A utility function to print solution matrix sol [ N ] [ N ] ; This function solves the Knight Tour problem using Backtracking . This function mainly uses solveKTUtil ( ) to solve the problem . It returns false if no complete tour is possible , otherwise return true and prints the tour . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; Initialization of solution matrix ; xMove [ ] and yMove [ ] define next move of Knight . xMove [ ] is for next value of x coordinate yMove [ ] is for next value of y coordinate ; Since the Knight is initially at the first block ; Start from 0 , 0 and explore all tours using solveKTUtil ( ) ; A recursive utility function to solve Knight Tour problem ; Try all next moves from the current coordinate x , y ; backtracking ; Driver Code ; Function Call
using System ; class GFG { static int N = 8 ; static bool isSafe ( int x , int y , int [ , ] sol ) { return ( x >= 0 && x < N && y >= 0 && y < N && sol [ x , y ] == - 1 ) ; } static void printSolution ( int [ , ] sol ) { for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < N ; y ++ ) Console . Write ( sol [ x , y ] + " ▁ " ) ; Console . WriteLine ( ) ; } } static bool solveKT ( ) { int [ , ] sol = new int [ 8 , 8 ] ; for ( int x = 0 ; x < N ; x ++ ) for ( int y = 0 ; y < N ; y ++ ) sol [ x , y ] = - 1 ; int [ ] xMove = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 } ; int [ ] yMove = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 } ; sol [ 0 , 0 ] = 0 ; if ( ! solveKTUtil ( 0 , 0 , 1 , sol , xMove , yMove ) ) { Console . WriteLine ( " Solution ▁ does ▁ " + " not ▁ exist " ) ; return false ; } else printSolution ( sol ) ; return true ; } static bool solveKTUtil ( int x , int y , int movei , int [ , ] sol , int [ ] xMove , int [ ] yMove ) { int k , next_x , next_y ; if ( movei == N * N ) return true ; for ( k = 0 ; k < 8 ; k ++ ) { next_x = x + xMove [ k ] ; next_y = y + yMove [ k ] ; if ( isSafe ( next_x , next_y , sol ) ) { sol [ next_x , next_y ] = movei ; if ( solveKTUtil ( next_x , next_y , movei + 1 , sol , xMove , yMove ) ) return true ; else sol [ next_x , next_y ] = - 1 ; } } return false ; } public static void Main ( ) { solveKT ( ) ; } }
Rat in a Maze | Backtracking | C # program to solve Rat in a Maze problem using backtracking ; Size of the maze ; A utility function to print solution matrix sol [ N , N ] ; A utility function to check if x , y is valid index for N * N maze ; If ( x , y outside maze ) return false ; This function solves the Maze problem using Backtracking . It mainly uses solveMazeUtil ( ) to solve the problem . It returns false if no path is possible , otherwise return true and prints the path in the form of 1 s . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; A recursive utility function to solve Maze problem ; If ( x , y is goal ) return true ; Check if maze [ x , y ] is valid ; Check if the current block is already part of solution path . ; Mark x , y as part of solution path ; Move forward in x direction ; If moving in x direction doesn 't give  solution then Move down in y direction ; If moving in y direction doesm 't give  solution then Move backward in x direction ; If moving in backwards in x direction doesn 't give  solution then Move upwards in y direction ; If none of the above movements works then BACKTRACK : unmark x , y as part of solution path ; Driver Code
using System ; class RatMaze { static int N ; void printSolution ( int [ , ] sol ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) Console . Write ( " ▁ " + sol [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } bool isSafe ( int [ , ] maze , int x , int y ) { return ( x >= 0 && x < N && y >= 0 && y < N && maze [ x , y ] == 1 ) ; } bool solveMaze ( int [ , ] maze ) { int [ , ] sol = new int [ N , N ] ; if ( solveMazeUtil ( maze , 0 , 0 , sol ) == false ) { Console . Write ( " Solution ▁ doesn ' t ▁ exist " ) ; return false ; } printSolution ( sol ) ; return true ; } bool solveMazeUtil ( int [ , ] maze , int x , int y , int [ , ] sol ) { if ( x == N - 1 && y == N - 1 && maze [ x , y ] == 1 ) { sol [ x , y ] = 1 ; return true ; } if ( isSafe ( maze , x , y ) == true ) { if ( sol [ x , y ] == 1 ) return false ; sol [ x , y ] = 1 ; if ( solveMazeUtil ( maze , x + 1 , y , sol ) ) return true ; if ( solveMazeUtil ( maze , x , y + 1 , sol ) ) return true ; if ( solveMazeUtil ( maze , x - 1 , y , sol ) ) return true ; if ( solveMazeUtil ( maze , x , y - 1 , sol ) ) return true ; sol [ x , y ] = 0 ; return false ; } return false ; } public static void Main ( String [ ] args ) { RatMaze rat = new RatMaze ( ) ; int [ , ] maze = { { 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 } , { 1 , 1 , 1 , 1 } } ; N = maze . GetLength ( 0 ) ; rat . solveMaze ( maze ) ; } }
m Coloring Problem | Backtracking |  ; Number of vertices in the graph ; A utility function to print solution ; check if the colored graph is safe or not ; check for every edge ; This function solves the m Coloring problem using recursion . It returns false if the m colours cannot be assigned , otherwise , return true and prints assignments of colours to all vertices . Please note that there may be more than one solutions , this function prints one of the feasible solutions . ; if current index reached end ; if coloring is safe ; Print the solution ; Assign each color from 1 to m ; Recur of the rest vertices ; Driver code ; Create following graph and test whether it is 3 colorable ( 3 ) -- - ( 2 ) | / | | / | | / | ( 0 ) -- - ( 1 ) ; Number of colors ; Initialize all color values as 0. This initialization is needed correct functioning of isSafe ( )
using System ; class GFG { static int V = 4 ; static void printSolution ( int [ ] color ) { Console . WriteLine ( " Solution ▁ Exists : " + " ▁ Following ▁ are ▁ the ▁ assigned ▁ colors ▁ " ) ; for ( int i = 0 ; i < V ; i ++ ) Console . Write ( " ▁ " + color [ i ] ) ; Console . WriteLine ( ) ; } static bool isSafe ( bool [ , ] graph , int [ ] color ) { for ( int i = 0 ; i < V ; i ++ ) for ( int j = i + 1 ; j < V ; j ++ ) if ( graph [ i , j ] && color [ j ] == color [ i ] ) return false ; return true ; } static bool graphColoring ( bool [ , ] graph , int m , int i , int [ ] color ) { if ( i == V ) { if ( isSafe ( graph , color ) ) { printSolution ( color ) ; return true ; } return false ; } for ( int j = 1 ; j <= m ; j ++ ) { color [ i ] = j ; if ( graphColoring ( graph , m , i + 1 , color ) ) return true ; color [ i ] = 0 ; } return false ; } static void Main ( ) { bool [ , ] graph = { { false , true , true , true } , { true , false , true , false } , { true , true , false , true } , { true , false , true , false } , } ; int m = 3 ; int [ ] color = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) color [ i ] = 0 ; if ( ! graphColoring ( graph , m , 0 , color ) ) Console . WriteLine ( " Solution ▁ does ▁ not ▁ exist " ) ; } }
m Coloring Problem | Backtracking | C # program for the above approach ; A node class which stores the color and the edges connected to the node ; Create a visited array of n nodes , initialized to zero ; maxColors used till now are 1 as all nodes are painted color 1 ; Do a full BFS traversal from all unvisited starting points ; If the starting point is unvisited , mark it visited and push it in queue ; BFS Travel starts here ; Checking all adjacent nodes to " top " edge in our queue ; IMPORTANT : If the color of the adjacent node is same , increase it by 1 ; If number of colors used shoots m , return 0 ; If the adjacent node is not visited , mark it visited and push it in queue ; Driver code ; Number of colors ; Create a vector of n + 1 nodes of type " node " The zeroth position is just dummy ( 1 to n to be used ) ; Add edges to each node as per given input ; Connect the undirected graph ; Display final answer
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class node { public int color = 1 ; public HashSet < int > edges = new HashSet < int > ( ) ; } ; static int canPaint ( List < node > nodes , int n , int m ) { List < int > visited = new List < int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { visited . Add ( 0 ) ; } int maxColors = 1 ; for ( int sv = 1 ; sv <= n ; sv ++ ) { if ( visited [ sv ] > 0 ) continue ; visited [ sv ] = 1 ; Queue q = new Queue ( ) ; q . Enqueue ( sv ) ; while ( q . Count != 0 ) { int top = ( int ) q . Peek ( ) ; q . Dequeue ( ) ; foreach ( int it in nodes [ top ] . edges ) { if ( nodes [ top ] . color == nodes [ it ] . color ) nodes [ it ] . color += 1 ; maxColors = Math . Max ( maxColors , Math . Max ( nodes [ top ] . color , nodes [ it ] . color ) ) ; if ( maxColors > m ) return 0 ; if ( visited [ it ] == 0 ) { visited [ it ] = 1 ; q . Enqueue ( it ) ; } } } } return 1 ; } public static void Main ( ) { int n = 4 ; int [ , ] graph = { { 0 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 } , { 1 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; int m = 3 ; List < node > nodes = new List < node > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { nodes . Add ( new node ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( graph [ i , j ] > 0 ) { nodes [ i ] . edges . Add ( i ) ; nodes [ j ] . edges . Add ( j ) ; } } } Console . WriteLine ( canPaint ( nodes , n , m ) ) ; } }
Maximize pair decrements required to reduce all array elements except one to 0 | C # program to implement the above approach ; Function to count maximum number of steps to make ( N - 1 ) array elements to 0 ; Stores maximum count of steps to make ( N - 1 ) elements equal to 0 ; Stores array elements ; Traverse the array ; Insert arr [ i ] into PQ ; Extract top 2 elements from the array while ( N - 1 ) array elements become 0 ; Stores top element of PQ ; Pop the top element of PQ . ; Stores top element of PQ ; Pop the top element of PQ . ; Update X ; Update Y ; If X is not equal to 0 ; Insert X into PQ ; if Y is not equal to 0 ; Insert Y into PQ ; Update cntOp ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int cntMaxOperationToMakeN_1_0 ( int [ ] arr , int N ) { int cntOp = 0 ; List < int > PQ = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { PQ . Add ( arr [ i ] ) ; } PQ . Sort ( ) ; PQ . Reverse ( ) ; while ( PQ . Count > 1 ) { int X = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; int Y = PQ [ 0 ] ; PQ . RemoveAt ( 0 ) ; X -- ; Y -- ; if ( X != 0 ) { PQ . Add ( X ) ; PQ . Sort ( ) ; PQ . Reverse ( ) ; } if ( Y != 0 ) { PQ . Add ( Y ) ; PQ . Sort ( ) ; PQ . Reverse ( ) ; } cntOp += 1 ; } return cntOp ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( cntMaxOperationToMakeN_1_0 ( arr , N ) ) ; } }
Flip all K | C # program for the above approach ; Function to flip all K - bits of an unsigned number N ; Stores ( 2 ^ K ) - 1 ; Update N ; Print the answer ; Driver Code
using System ; class GFG { static void flippingBits ( int N , int K ) { int X = ( 1 << ( K - 1 ) ) - 1 ; N = X - N ; Console . Write ( N ) ; } public static void Main ( string [ ] args ) { int N = 1 , K = 8 ; flippingBits ( N , K ) ; } }
Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices | C # program for the above approach ; Function to construct a matrix such that the sum elements in both diagonals of every 2 * 2 matrices is even ; Stores odd numbers ; Stores even numbers ; Store matrix elements such that sum of elements in both diagonals of every 2 * 2 submatrices is even ; Fill all the values of matrix elements ; Update odd ; Update even ; Print the matrix ; Driver Code
using System ; class GFG { static void generateMatrix ( int N ) { int odd = 1 ; int even = 2 ; int [ , ] mat = new int [ N + 1 , N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { mat [ i , j ] = odd ; odd += 2 ; } else { mat [ i , j ] = even ; even += 2 ; } } } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int N = 4 ; generateMatrix ( N ) ; } }
Queries to calculate sum by alternating signs of array elements in a given range | C # program to implement the above approach ; Function to build the segment tree ; If current node is a leaf node of the segment tree ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree ; Update on L segment tree ; Update on R segment tree ; Find the sum from L subtree and R subtree ; Function to update elements at index pos by val in the segment tree ; If current node is a leaf node ; If current index is even ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree elements into L and R subtree ; If element lies in L subtree ; Update tree [ index ] ; Function to find the sum of array elements in the range [ L , R ] ; If start and end not lies in the range [ L , R ] ; If start and end comleately lies in the range [ L , R ] ; Stores sum from left subtree ; Stores sum from right subtree ; Driver code
using System ; class GFG { static void build ( int [ ] tree , int [ ] arr , int start , int end , int index ) { if ( start == end ) { if ( start % 2 == 0 ) { tree [ index ] = arr [ start ] ; } else { tree [ index ] = - arr [ start ] ; } return ; } int mid = start + ( end - start ) / 2 ; build ( tree , arr , start , mid , 2 * index + 1 ) ; build ( tree , arr , mid + 1 , end , 2 * index + 2 ) ; tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] ; } static void update ( int [ ] tree , int index , int start , int end , int pos , int val ) { if ( start == end ) { if ( start % 2 == 0 ) { tree [ index ] = val ; } else { tree [ index ] = - val ; } return ; } int mid = start + ( end - start ) / 2 ; if ( mid >= pos ) { update ( tree , 2 * index + 1 , start , mid , pos , val ) ; } else { update ( tree , 2 * index + 2 , mid + 1 , end , pos , val ) ; } tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] ; } static int FindSum ( int [ ] tree , int start , int end , int L , int R , int index ) { if ( L > end R < start ) { return 0 ; } if ( L <= start && R >= end ) { return tree [ index ] ; } int mid = start + ( end - start ) / 2 ; int X = FindSum ( tree , start , mid , L , R , 2 * index + 1 ) ; int Y = FindSum ( tree , mid + 1 , end , L , R , 2 * index + 2 ) ; return X + Y ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; int [ ] tree = new int [ 4 * N + 5 ] ; build ( tree , arr , 0 , N - 1 , 0 ) ; int [ , ] Q = { { 2 , 0 , 3 } , { 1 , 1 , 5 } , { 2 , 1 , 2 } } ; int cntQuey = 3 ; for ( int i = 0 ; i < cntQuey ; i ++ ) { if ( Q [ i , 0 ] == 1 ) { update ( tree , 0 , 0 , N - 1 , Q [ i , 1 ] , Q [ i , 2 ] ) ; } else { if ( Q [ i , 1 ] % 2 == 0 ) { Console . Write ( FindSum ( tree , 0 , N - 1 , Q [ i , 1 ] , Q [ i , 2 ] , 0 ) + " ▁ " ) ; } else { Console . Write ( - FindSum ( tree , 0 , N - 1 , Q [ i , 1 ] , Q [ i , 2 ] , 0 ) + " ▁ " ) ; } } } } }
Split an array into minimum number of non | C # program for the above approach ; Function to split the array into minimum count of subarrays such that each subarray is either non - increasing or non - decreasing ; Initialize variable to keep track of current sequence ; Stores the required result ; Traverse the array , [ ] arr ; If current sequence is neither non - increasing nor non - decreasing ; If previous element is greater ; Update current ; If previous element is equal to the current element ; Update current ; Otherwise ; Update current ; If current sequence is in non - decreasing ; If previous element is less than or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; If current sequence is Non - Increasing ; If previous element is greater or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; Print the answer ; Driver Code ; Given array ; Given size of array
using System ; using System . Collections . Generic ; class GFG { static void minimumSubarrays ( int [ ] arr , int n ) { char current = ' N ' ; int answer = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( current == ' N ' ) { if ( arr [ i ] < arr [ i - 1 ] ) { current = ' D ' ; } else if ( arr [ i ] == arr [ i - 1 ] ) { current = ' N ' ; } else { current = ' I ' ; } } else if ( current == ' I ' ) { if ( arr [ i ] >= arr [ i - 1 ] ) { current = ' I ' ; } else { current = ' N ' ; answer += 1 ; } } else { if ( arr [ i ] <= arr [ i - 1 ] ) { current = ' D ' ; } else { current = ' N ' ; answer += 1 ; } } } Console . Write ( answer ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 9 , 5 , 4 , 6 , 8 } ; int n = arr . Length ; minimumSubarrays ( arr , n ) ; } }
Convert a Matrix into another Matrix of given dimensions | C # program to implement the above approach ; Function to cona matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , [ , ] mat ; Update idx ; Print the resultant matrix ; Driver Code
using System ; class GFG { static void ConstMatrix ( int [ , ] mat , int N , int M , int A , int B ) { if ( N * M != A * B ) return ; int idx = 0 ; int [ , ] res = new int [ A , B ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { res [ idx / B , idx % B ] = mat [ i , j ] ; idx ++ ; } } for ( int i = 0 ; i < A ; i ++ ) { for ( int j = 0 ; j < B ; j ++ ) Console . Write ( res [ i , j ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 , 6 } } ; int A = 2 ; int B = 3 ; int N = mat . GetLength ( 0 ) ; int M = mat . GetLength ( 1 ) ; ConstMatrix ( mat , N , M , A , B ) ; } }
Modify given array by reducing each element by its next smaller element | C # program for the above approach ; Function to print the readonly array after reducing each array element by its next smaller element ; Initialize stack ; Array size ; To store the corresponding element ; If stack is not empty ; If top element is smaller than the current element ; Keep popping until stack is empty or top element is greater than the current element ; If stack is not empty ; Push current element ; Print the readonly array ; Driver Code ; Given array ; Function call
using System ; using System . Collections . Generic ; class GFG { static void printFinalPrices ( int [ ] arr ) { Stack < int > minStk = new Stack < int > ( ) ; int n = arr . Length ; int [ ] reduce = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( minStk . Count != 0 ) { if ( minStk . Peek ( ) <= arr [ i ] ) { reduce [ i ] = minStk . Peek ( ) ; } else { while ( minStk . Count != 0 && ( minStk . Peek ( ) > arr [ i ] ) ) { minStk . Pop ( ) ; } if ( minStk . Count != 0 ) { reduce [ i ] = minStk . Peek ( ) ; } } } minStk . Push ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] - reduce [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 4 , 6 , 2 , 3 } ; printFinalPrices ( arr ) ; } }
Generate array having differences between count of occurrences of every array element on its left and right | C # program of the above approach ; Function to construct array of differences of counts on the left and right of the given array ; Initialize left and right frequency arrays ; Construct left cumulative frequency table ; Construct right cumulative frequency table ; Print the result ; Driver Code ; Function Call
using System ; class GFG { static void constructArray ( int [ ] A , int N ) { int [ ] left = new int [ N + 1 ] ; int [ ] right = new int [ N + 1 ] ; int [ ] X = new int [ N + 1 ] ; int [ ] Y = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { X [ i ] = left [ A [ i ] ] ; left [ A [ i ] ] ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { Y [ i ] = right [ A [ i ] ] ; right [ A [ i ] ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( Y [ i ] - X [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] A = { 3 , 2 , 1 , 2 , 3 } ; int N = A . Length ; constructArray ( A , N ) ; } }
Mean of distinct odd fibonacci nodes in a Linked List | C # program to implement the above approach ; Structure of a singly Linked List ; Stores data value of a Node ; Stores pointer to next Node ; Function to insert a node at the beginning of the singly Linked List ; Create a new Node ; Insert the data into the Node ; Insert pointer to the next Node ; Update head_ref ; Function to find the largest element from the linked list ; Stores the largest element in the linked list ; Iterate over the linked list ; If max is less than head . data ; Update max ; Update head ; Function to store all Fibonacci numbers up to the largest element of the list ; Store all Fibonacci numbers up to Max ; Stores first element of Fibonacci number ; Stores second element of Fibonacci number ; Insert prev into hashmap ; Insert curr into hashmap ; Insert all elements of Fibonacci numbers up to Max ; Stores current fibonacci number ; Insert temp into hashmap ; Update prev ; Update curr ; Function to find the mean of odd Fibonacci nodes ; Stores the largest element in the linked list ; Stores all fibonacci numbers up to Max ; Stores current node of linked list ; Stores count of odd Fibonacci nodes ; Stores sum of all odd fibonacci nodes ; Traverse the linked list ; if the data value of current node is an odd number ; if data value of the node is present in hashmap ; Update cnt ; Update sum ; Remove current fibonacci number from hashmap so that duplicate elements can 't be  counted ; Update curr ; Return the required mean ; Driver Code ; Stores head node of the linked list ; Insert all data values in the linked list
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node next ; } ; static Node head ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int largestElement ( Node head_ref ) { int Max = int . MinValue ; Node head = head_ref ; while ( head != null ) { if ( Max < head . data ) { Max = head . data ; } head = head . next ; } return Max ; } static HashSet < int > createDictionary ( int Max ) { HashSet < int > hashmap = new HashSet < int > ( ) ; int prev = 0 ; int curr = 1 ; hashmap . Add ( prev ) ; hashmap . Add ( curr ) ; while ( curr <= Max ) { int temp = curr + prev ; hashmap . Add ( temp ) ; prev = curr ; curr = temp ; } return hashmap ; } static double meanofnodes ( ) { int Max = largestElement ( head ) ; HashSet < int > hashmap = createDictionary ( Max ) ; Node curr = head ; int cnt = 0 ; double sum = 0.0 ; while ( curr != null ) { if ( ( curr . data ) % 2 == 1 ) { if ( hashmap . Contains ( curr . data ) ) { cnt ++ ; sum += curr . data ; hashmap . Remove ( curr . data ) ; } } curr = curr . next ; } return ( sum / cnt ) ; } public static void Main ( String [ ] args ) { head = null ; head = push ( head , 5 ) ; head = push ( head , 21 ) ; head = push ( head , 8 ) ; head = push ( head , 12 ) ; head = push ( head , 3 ) ; head = push ( head , 13 ) ; head = push ( head , 144 ) ; head = push ( head , 6 ) ; Console . Write ( meanofnodes ( ) ) ; } }
Generate an alternate odd | C # program to implement the above approach ; Function to print the required sequence ; Print ith odd number ; Driver code ; Function call
using System ; class GFG { static void findNumbers ( int n ) { int i = 0 ; while ( i <= n ) { Console . Write ( 2 * i * i + 4 * i + 1 + i % 2 + " ▁ " ) ; i ++ ; } } public static void Main ( ) { int n = 6 ; findNumbers ( n ) ; } }