codeparrot/xlcost-text-to-code huggingface dataset:
Program to calculate area and volume of a Tetrahedron | C # Program to Calculate area of tetrahedron ; Utility Function ; Driver code
using System ; class GFG { public static double area_of_tetrahedron ( int side ) { return ( Math . Sqrt ( 3 ) * ( side * side ) ) ; } static public void Main ( ) { int side = 3 ; Console . WriteLine ( " Area ▁ of ▁ Tetrahedron ▁ = ▁ " + area_of_tetrahedron ( side ) ) ; } }
Program to calculate area and volume of a Tetrahedron | C # code to find the volume of a tetrahedron ; Function to calculate volume ; Driver Code
using System ; class Tetrahedron { static double vol_tetra ( int side ) { double volume = ( Math . Pow ( side , 3 ) / ( 6 * Math . Sqrt ( 2 ) ) ) ; return volume ; } public static void Main ( ) { int side = 3 ; double vol = vol_tetra ( side ) ; vol = ( double ) Math . Round ( vol * 100 ) / 100 ; Console . WriteLine ( vol ) ; } }
Program to find slope of a line | C # program for slope of line ; function to find the slope of a straight line ; driver code to check the above function
using System ; class GFG { static float slope ( float x1 , float y1 , float x2 , float y2 ) { if ( x1 == x2 ) return 1000000000 ; return ( y2 - y1 ) / ( x2 - x1 ) ; } public static void Main ( string [ ] args ) { float x1 = 4 , y1 = 2 ; float x2 = 2 , y2 = 5 ; Console . Write ( " Slope ▁ is : ▁ " + slope ( x1 , y1 , x2 , y2 ) ) ; } }
Program to calculate area and perimeter of equilateral triangle | C # Program to find area and perimeter of equilateral triangle ; Function to calculate Area of equilateral triangle ; Function to calculate Perimeter of equilateral triangle ; Driver Code
using System ; class GFG { static float area_equi_triangle ( float side ) { return ( float ) ( ( ( Math . Sqrt ( 3 ) ) / 4 ) * side * side ) ; } static float peri_equi_triangle ( float side ) { return 3 * side ; } public static void Main ( ) { float side = 4 ; Console . Write ( " Area ▁ of ▁ Equilateral ▁ Triangle : " ) ; Console . WriteLine ( area_equi_triangle ( side ) ) ; Console . Write ( " Perimeter ▁ of ▁ Equilateral ▁ Triangle : " ) ; Console . WriteLine ( peri_equi_triangle ( side ) ) ; } }
Program for Volume and Surface Area of Cuboid | C # program to find volume and total surface area of cuboid ; utility function ; Driver code
using System ; class GFG { static double areaCuboid ( double l , double h , double w ) { return ( l * h * w ) ; } static double surfaceAreaCuboid ( double l , double h , double w ) { return ( 2 * l * w + 2 * w * h + 2 * l * h ) ; } public static void Main ( ) { double l = 1 ; double h = 5 ; double w = 7 ; Console . WriteLine ( " Area ▁ = ▁ " + areaCuboid ( l , h , w ) ) ; Console . WriteLine ( " Total ▁ Surface ▁ Area ▁ = ▁ " + surfaceAreaCuboid ( l , h , w ) ) ; } }
Program to find Circumference of a Circle | C # program to find circumference of circle ; utility function ; driver function
using System ; class GFG { static double circumference ( double r ) { double PI = 3.1415 ; double cir = 2 * PI * r ; return cir ; } public static void Main ( ) { double r = 5 ; double result = Math . Round ( circumference ( r ) * 1000 ) / 1000.0 ; Console . WriteLine ( " Circumference ▁ = ▁ " + result ) ; } }
Program to check if three points are collinear | Slope based solution to check if three points are collinear . ; function to check if point collinear or not ; Driver Code
using System ; class GFG { static void cool_line ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { if ( ( y3 - y2 ) * ( x2 - x1 ) == ( y2 - y1 ) * ( x3 - x2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static public void Main ( ) { int a1 = 1 , a2 = 1 , a3 = 0 , b1 = 1 , b2 = 6 , b3 = 9 ; cool_line ( a1 , b1 , a2 , b2 , a3 , b3 ) ; } }
Number of rectangles in N * M grid | C # Code to count number of rectangles in N * M grid ; Driver code
using System ; class GFG { public static long rectCount ( int n , int m ) { return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ; } public static void Main ( ) { int n = 5 , m = 4 ; Console . WriteLine ( rectCount ( n , m ) ) ; } }
Number of unique rectangles formed using N unit squares | C # program to count rotationally equivalent rectangles with n unit squares ; height >= length is maintained ; driver code
using System ; class GFG { static int countRect ( int n ) { int ans = 0 ; for ( int length = 1 ; length <= Math . Sqrt ( n ) ; ++ length ) for ( int height = length ; height * length <= n ; ++ height ) ans ++ ; return ans ; } public static void Main ( ) { int n = 5 ; Console . Write ( countRect ( n ) ) ; } }
Find the Missing Point of Parallelogram | C # program to find missing point of a parallelogram ; main method ; coordinates of A ; coordinates of B ; coordinates of C
using System ; class GFG { static public void Main ( ) { int ax = 5 , ay = 0 ; int bx = 1 , by = 1 ; int cx = 2 , cy = 5 ; Console . WriteLine ( ax + ( cx - bx ) + " , ▁ " + ay + ( cy - by ) ) ; } }
Represent a given set of points by the best possible straight line | C # Program to find m and c for a straight line given , x and y ; function to calculate m and c that best fit points represented by x [ ] and y [ ] ; Driver main function
using System ; class GFG { static void bestApproximate ( int [ ] x , int [ ] y ) { int n = x . Length ; double m , c , sum_x = 0 , sum_y = 0 , sum_xy = 0 , sum_x2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum_x += x [ i ] ; sum_y += y [ i ] ; sum_xy += x [ i ] * y [ i ] ; sum_x2 += Math . Pow ( x [ i ] , 2 ) ; } m = ( n * sum_xy - sum_x * sum_y ) / ( n * sum_x2 - Math . Pow ( sum_x , 2 ) ) ; c = ( sum_y - m * sum_x ) / n ; Console . WriteLine ( " m ▁ = ▁ " + m ) ; Console . WriteLine ( " c ▁ = ▁ " + c ) ; } public static void Main ( ) { int [ ] x = { 1 , 2 , 3 , 4 , 5 } ; int [ ] y = { 14 , 27 , 40 , 55 , 68 } ; bestApproximate ( x , y ) ; } }
Check for star graph | C # to find whether given graph is star or not ; define the size of incidence matrix ; function to find star graph ; initialize number of vertex with deg 1 and n - 1 ; check for S1 ; check for S2 ; check for Sn ( n > 2 ) ; Driver code
using System ; class GFG { static int size = 4 ; static bool checkStar ( int [ , ] mat ) { int vertexD1 = 0 , vertexDn_1 = 0 ; if ( size == 1 ) return ( mat [ 0 , 0 ] == 0 ) ; if ( size == 2 ) return ( mat [ 0 , 0 ] == 0 && mat [ 0 , 1 ] == 1 && mat [ 1 , 0 ] == 1 && mat [ 1 , 1 ] == 0 ) ; for ( int i = 0 ; i < size ; i ++ ) { int degreeI = 0 ; for ( int j = 0 ; j < size ; j ++ ) if ( mat [ i , j ] == 1 ) degreeI ++ ; if ( degreeI == 1 ) vertexD1 ++ ; else if ( degreeI == size - 1 ) vertexDn_1 ++ ; } return ( vertexD1 == ( size - 1 ) && vertexDn_1 == 1 ) ; } static void Main ( ) { int [ , ] mat = new int [ 4 , 4 ] { { 0 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } } ; if ( checkStar ( mat ) ) Console . Write ( " Star ▁ Graph " ) ; else Console . Write ( " Not ▁ a ▁ Star ▁ Graph " ) ; } }
Find all angles of a given triangle | C # Code to find all three angles of a triangle given coordinate of all three vertices ;  ; returns square of distance b / w two points ; Square of lengths be a2 , b2 , c2 ; length of sides be a , b , c ; From Cosine law ; Converting to degree ; printing all the angles ; Driver Code
using System ; class GFG { class Point { public int x , y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } static int lengthSquare ( Point p1 , Point p2 ) { int xDiff = p1 . x - p2 . x ; int yDiff = p1 . y - p2 . y ; return xDiff * xDiff + yDiff * yDiff ; } static void printAngle ( Point A , Point B , Point C ) { int a2 = lengthSquare ( B , C ) ; int b2 = lengthSquare ( A , C ) ; int c2 = lengthSquare ( A , B ) ; float a = ( float ) Math . Sqrt ( a2 ) ; float b = ( float ) Math . Sqrt ( b2 ) ; float c = ( float ) Math . Sqrt ( c2 ) ; float alpha = ( float ) Math . Acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; float betta = ( float ) Math . Acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; float gamma = ( float ) Math . Acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; alpha = ( float ) ( alpha * 180 / Math . PI ) ; betta = ( float ) ( betta * 180 / Math . PI ) ; gamma = ( float ) ( gamma * 180 / Math . PI ) ; Console . WriteLine ( " alpha ▁ : ▁ " + alpha ) ; Console . WriteLine ( " betta ▁ : ▁ " + betta ) ; Console . WriteLine ( " gamma ▁ : ▁ " + gamma ) ; } public static void Main ( String [ ] args ) { Point A = new Point ( 0 , 0 ) ; Point B = new Point ( 0 , 1 ) ; Point C = new Point ( 1 , 0 ) ; printAngle ( A , B , C ) ; } }
Triangle with no point inside | C # program to find triangle with no point inside ; method to get square of distance between ( x1 , y1 ) and ( x2 , y2 ) ; Method prints points which make triangle with no point inside ; any point can be chosen as first point of triangle ; choose nearest point as second point of triangle ; Get distance from first point and choose nearest one ; Pick third point by finding the second closest point with different slope . ; if already chosen point then skip them ; get distance from first point ; here cross multiplication is compared instead of division comparison ; Driver code
using System ; public class GFG { public static int getDistance ( int x1 , int y1 , int x2 , int y2 ) { return ( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 ) ; } public static void triangleWithNoPointInside ( int [ ] [ ] points , int N ) { int first = 0 ; int second = 0 ; int third = 0 ; int minD = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == first ) { continue ; } int d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] ) ; if ( minD > d ) { minD = d ; second = i ; } } minD = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == first i == second ) { continue ; } int d = getDistance ( points [ i ] [ 0 ] , points [ i ] [ 1 ] , points [ first ] [ 0 ] , points [ first ] [ 1 ] ) ; if ( ( points [ i ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ second ] [ 1 ] - points [ first ] [ 1 ] ) != ( points [ second ] [ 0 ] - points [ first ] [ 0 ] ) * ( points [ i ] [ 1 ] - points [ first ] [ 1 ] ) && minD > d ) { minD = d ; third = i ; } } Console . WriteLine ( points [ first ] [ 0 ] + " , ▁ " + points [ first ] [ 1 ] ) ; Console . WriteLine ( points [ second ] [ 0 ] + " , ▁ " + points [ second ] [ 1 ] ) ; Console . WriteLine ( points [ third ] [ 0 ] + " , ▁ " + points [ third ] [ 1 ] ) ; } public static void Main ( string [ ] args ) { int [ ] [ ] points = new int [ ] [ ] { new int [ ] { 0 , 0 } , new int [ ] { 0 , 2 } , new int [ ] { 2 , 0 } , new int [ ] { 2 , 2 } , new int [ ] { 1 , 1 } } ; int N = points . Length ; triangleWithNoPointInside ( points , N ) ; } }
Minimize swaps required to make all prime | C # program for the above approach ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to count minimum number of swaps required ; To count the minimum number of swaps required to convert the array into perfectly prime ; To count total number of prime indexes in the array ; To count the total number of prime numbers in the array ; Check whether index is prime or not ; Element is not prime ; If the total number of prime numbers is greater than or equal to the total number of prime indices , then it is possible to convert the array into perfectly prime ; Driver Code ; Pre - calculate prime [ ]
using System ; class GFG { static bool [ ] prime = new bool [ ( int ) 1e4 + 2 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < ( int ) 1e4 + 2 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < ( int ) 1e4 + 2 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < ( int ) 1e4 + 2 ; i += p ) prime [ i ] = false ; } } } static int countMin ( int [ ] arr , int n ) { int cMinSwaps = 0 ; int cPrimeIndices = 0 ; int cPrimeNos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ i + 1 ] ) { cPrimeIndices ++ ; if ( prime [ arr [ i ] ] == false ) cMinSwaps ++ ; else cPrimeNos ++ ; } else if ( prime [ arr [ i ] ] ) { cPrimeNos ++ ; } } if ( cPrimeNos >= cPrimeIndices ) return cMinSwaps ; else return - 1 ; } public static void Main ( ) { SieveOfEratosthenes ( ) ; int n = 5 ; int [ ] arr = { 2 , 7 , 8 , 5 , 13 } ; Console . Write ( countMin ( arr , n ) ) ; } }
Find the size of Largest Subset with positive Bitwise AND | C # program for the above approach ; Function to find the largest possible subset having Bitwise AND positive ; Stores the number of set bits at each bit position ; Traverse the given array arr [ ] ; Current bit position ; Loop till array element becomes zero ; If the last bit is set ; Increment frequency ; Divide array element by 2 ; Decrease the bit position ; Size of the largest possible subset ; Driver code
using System ; class GFG { static void largestSubset ( int [ ] a , int N ) { int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int x = 31 ; while ( a [ i ] > 0 ) { if ( ( int ) ( a [ i ] & 1 ) == ( int ) 1 ) { bit [ x ] ++ ; } a [ i ] = a [ i ] >> 1 ; x -- ; } } int max = Int32 . MinValue ; for ( int i = 0 ; i < 32 ; i ++ ) { max = Math . Max ( max , bit [ i ] ) ; } Console . WriteLine ( max ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 13 , 8 , 2 , 3 } ; int N = arr . Length ; largestSubset ( arr , N ) ; } }
Maximum sum of segments among all segments formed in array after Q queries | C # program for the above approach ; Stores the maximum integer of the sets for each query ; Function to perform the find operation of disjoint set union ; Function to perform the Union operation of disjoint set union ; Find the parent of a and b ; Update the parent ; Update the sum of set a ; Function to find the maximum element from the sets after each operation ; Stores the parent elements of the sets ; Stores the rank of the sets ; Stores the sum of the sets ; Stores the maximum element for each query ; Initially set is empty ; Update the sum as the current element ; After the last query set will be empty and sum will be 0 ; Check if the current element is not in any set then make parent as current element of the queries ; Check left side of the queries [ i ] is not added in any set ; Add the queries [ i ] and the queries [ i ] - 1 in one set ; Check right side of the queries [ i ] is not added in any set ; Add queries [ i ] and the queries [ i ] + 1 in one set ; Update the maxAns ; Push maxAns to the currMax ; Print currMax values in the reverse order ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int maxAns = int . MinValue ; static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ) ? a : ( Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int [ ] setSum , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( a == b ) return ; if ( rank [ a ] > rank [ b ] ) rank [ a ] ++ ; if ( rank [ b ] > rank [ a ] ) { int x = a ; a = b ; b = x ; } parent [ b ] = a ; setSum [ a ] += setSum [ b ] ; } static void maxValues ( int [ ] arr , int [ ] queries , int N ) { int [ ] parent = new int [ N + 1 ] ; int [ ] rank = new int [ N + 1 ] ; int [ ] setSum = new int [ N + 1 ] ; List < int > currMax = new List < int > ( ) ; for ( int i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = - 1 ; setSum [ i ] = arr [ i - 1 ] ; } currMax . Add ( 0 ) ; for ( int i = N - 1 ; i > 0 ; i -- ) { if ( parent [ queries [ i ] ] == - 1 ) { parent [ queries [ i ] ] = queries [ i ] ; } if ( queries [ i ] - 1 >= 0 && parent [ queries [ i ] - 1 ] != - 1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] - 1 ) ; } if ( queries [ i ] + 1 <= N && parent [ queries [ i ] + 1 ] != - 1 ) { Union ( parent , rank , setSum , queries [ i ] , queries [ i ] + 1 ) ; } maxAns = Math . Max ( setSum [ queries [ i ] ] , maxAns ) ; currMax . Add ( maxAns ) ; } for ( int i = currMax . Count - 1 ; i >= 0 ; i -- ) { Console . Write ( currMax [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 5 } ; int [ ] queries = { 3 , 4 , 1 , 2 } ; int N = arr . Length ; maxValues ( arr , queries , N ) ; } }
ZigZag Level Order Traversal of an N | C # program for the above approach ; Structure of a tree node ; Function to create a new node ; Function to perform zig zag traversal of the given tree ; Stores the vectors containing nodes in each level of tree respectively ; Create a queue for BFS ; Enqueue Root of the tree ; Standard Level Order Traversal code using queue ; Stores the element in the current level ; Iterate over all nodes of the current level ; Insert all children of the current node into the queue ; Insert curLevel into result ; Loop to Print the ZigZag Level order Traversal of the given tree ; If i + 1 is even reverse the order of nodes in the current level ; Print the node of ith level ; Function Call
using System ; using System . Collections . Generic ; class GFG { class Node { public int val ; public List < Node > child ; public Node ( int key ) { val = key ; child = new List < Node > ( ) ; } } static Node newNode ( int key ) { Node temp = new Node ( key ) ; return temp ; } static void zigzagLevelOrder ( Node root ) { if ( root == null ) return ; List < List < int > > result = new List < List < int > > ( ) ; List < Node > q = new List < Node > ( ) ; q . Add ( root ) ; while ( q . Count > 0 ) { int size = q . Count ; List < int > curLevel = new List < int > ( ) ; for ( int i = 0 ; i < size ; i ++ ) { Node node = q [ 0 ] ; q . RemoveAt ( 0 ) ; curLevel . Add ( node . val ) ; for ( int j = 0 ; j < ( node . child ) . Count ; j ++ ) q . Add ( node . child [ j ] ) ; } result . Add ( curLevel ) ; } for ( int i = 0 ; i < result . Count ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) { result [ i ] . Reverse ( ) ; } for ( int j = 0 ; j < result [ i ] . Count ; j ++ ) Console . Write ( result [ i ] [ j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } static void Main ( ) { Node root = newNode ( 1 ) ; ( root . child ) . Add ( newNode ( 2 ) ) ; ( root . child ) . Add ( newNode ( 3 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 4 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 5 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 6 ) ) ; ( root . child [ 1 ] ) . child . Add ( newNode ( 7 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 8 ) ) ; zigzagLevelOrder ( root ) ; } }
Check if possible to make Array sum equal to Array product by replacing exactly one element | C # program for the above approach ; Function to check if it is possible to form an array whose sum and the product is the same or not ; Find the sum of the array initialize sum ; Iterate through all elements and add them to sum ; Find the product of the array ; Check a complete integer y for every x ; If got such y ; If no such y exist ; Driver Code
using System ; class GFG { static int canPossibleReplacement ( int N , int [ ] arr ) { int S = 0 ; int i ; for ( i = 0 ; i < arr . Length ; i ++ ) S += arr [ i ] ; int P = 1 ; for ( i = 0 ; i < arr . Length ; i ++ ) { P *= i ; } foreach ( int x in arr ) { int y = ( S - x ) / ( P / x - 1 ) ; if ( ( S - x + y ) == ( P * y ) / x ) return 1 ; } return 0 ; } public static void Main ( string [ ] args ) { int N = 3 ; int [ ] arr = { 1 , 3 , 4 } ; if ( canPossibleReplacement ( N , arr ) == 1 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if all the digits of the given number are same | C # program for the above approach ; Function to check if all the digits in the number N is the same or not ; Get the length of N ; Form the number M of the type K * 111. . . where K is the rightmost digit of N ; Check if the numbers are equal ; Otherwise ; Driver Code
using System ; class GFG { public static String checkSameDigits ( int N ) { int length = ( ( int ) Math . Log10 ( N ) ) + 1 ; int M = ( ( int ) Math . Pow ( 10 , length ) - 1 ) / ( 10 - 1 ) ; M *= N % 10 ; if ( M == N ) return " Yes " ; return " No " ; } public static void Main ( ) { int N = 222 ; Console . WriteLine ( checkSameDigits ( N ) ) ; } }
Maximize sum of path from the Root to a Leaf node in N | C # program for the above approach ; Stores the maximum sum of a path ; Structure of a node in the tree ; Utility function to create a new node in the tree ; Recursive function to calculate the maximum sum in a path using DFS ; If current node is a leaf node ; Traversing all children of the current node ; Recursive call for all the children nodes ; Given Generic Tree ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int maxSumPath = 0 ; class Node { public int val ; public List < Node > child ; public Node ( int key ) { val = key ; child = new List < Node > ( ) ; } } static Node newNode ( int key ) { Node temp = new Node ( key ) ; return temp ; } static void DFS ( Node root , int sum ) { if ( root . child . Count == 0 ) { maxSumPath = Math . Max ( maxSumPath , sum ) ; return ; } for ( int i = 0 ; i < root . child . Count ; i ++ ) { DFS ( root . child [ i ] , sum + root . child [ i ] . val ) ; } } static void Main ( ) { Node root = newNode ( 1 ) ; ( root . child ) . Add ( newNode ( 2 ) ) ; ( root . child ) . Add ( newNode ( 3 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 4 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 6 ) ) ; ( root . child [ 0 ] . child ) . Add ( newNode ( 5 ) ) ; ( root . child [ 1 ] ) . child . Add ( newNode ( 7 ) ) ; ( root . child [ 1 ] . child ) . Add ( newNode ( 8 ) ) ; DFS ( root , root . val ) ; Console . Write ( maxSumPath ) ; } }
Count of even sum triplets in the array for Q range queries | C # program for above approach ; Function to count number of triplets with even sum in range l , r for each query ; Initialization of array ; Initialization of variables ; Traversing array ; If element is odd ; If element is even ; Storing count of even and odd till each i ; Traversing each query ; Count of odd numbers in l to r ; Count of even numbers in l to r ; Finding the ans ; Printing the ans ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void countTriplets ( int size , int queries , int [ ] arr , int [ , ] Q ) { int [ ] arr_even = new int [ size + 1 ] ; int [ ] arr_odd = new int [ size + 1 ] ; int even = 0 ; int odd = 0 ; arr_even [ 0 ] = 0 ; arr_odd [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } arr_even [ i + 1 ] = even ; arr_odd [ i + 1 ] = odd ; } for ( int i = 0 ; i < queries ; i ++ ) { int l = Q [ i , 0 ] , r = Q [ i , 1 ] ; odd = arr_odd [ r ] - arr_odd [ l - 1 ] ; even = arr_even [ r ] - arr_even [ l - 1 ] ; int ans = ( even * ( even - 1 ) * ( even - 2 ) ) / 6 + ( odd * ( odd - 1 ) / 2 ) * even ; Console . Write ( ans + " ▁ " ) ; } } public static void Main ( ) { int N = 6 , Q = 2 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int [ , ] queries = { { 1 , 3 } , { 2 , 5 } } ; countTriplets ( N , Q , arr , queries ) ; } }
Maximize the rightmost element of an array in k operations in Linear Time | Function to calculate maximum value of Rightmost element ; Calculating maximum value of Rightmost element ; Checking if arr [ i ] is operationable ; Performing operation of i - th element ; Decreasing the value of k by 1 ; returning the rightmost element ; Driver Code ; Given Input ; Function Call
using System ; public class GFG { static int maxRightmostElement ( int N , int k , int p , int [ ] arr ) { while ( k > 0 ) { for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= p ) { arr [ i ] = arr [ i ] - p ; arr [ i + 1 ] = arr [ i + 1 ] + p ; break ; } } k -- ; } return arr [ N - 1 ] ; } static public void Main ( ) { int N = 4 , k = 5 , p = 2 ; int [ ] arr = { 3 , 8 , 1 , 4 } ; Console . WriteLine ( maxRightmostElement ( N , k , p , arr ) ) ; } }