codeparrot/xlcost-text-to-code huggingface dataset:
Diameter of a Binary Tree | Class containing left and right child of current node and key value ; Class to print the Diameter ; The function Compute the " height " of a tree . Height is the number of nodes along the longest path from the root node down to the farthest leaf node . ; base case tree is empty ; If tree is not empty then height = 1 + max of left height and right heights ; Function to get diameter of a binary tree ; base case where tree is empty ; get the height of left and right sub - trees ; get the diameter of left and right sub - trees ; Return max of following three 1 ) Diameter of left subtree 2 ) Diameter of right subtree 3 ) Height of left subtree + height of right subtree + 1 ; A wrapper over diameter ( Node root ) ; Driver Code ; creating a binary tree and entering the nodes ; Function Call
using System ; namespace Tree { class Tree < T > { public Tree ( T value ) { this . value = value ; } public T value { get ; set ; } public Tree < T > left { get ; set ; } public Tree < T > right { get ; set ; } } public class TreeDiameter { Tree < int > root ; int Height ( Tree < int > node ) { if ( node == null ) return 0 ; return 1 + Math . Max ( Height ( node . left ) , Height ( node . right ) ) ; } int Diameter ( Tree < int > root ) { if ( root == null ) return 0 ; int lHeight = Height ( root . left ) ; int rHeight = Height ( root . right ) ; int lDiameter = Diameter ( root . left ) ; int rDiameter = Diameter ( root . right ) ; return Math . Max ( lHeight + rHeight + 1 , Math . Max ( lDiameter , rDiameter ) ) ; } int Diameter ( ) { return Diameter ( root ) ; } public static void Main ( string [ ] args ) { TreeDiameter tree = new TreeDiameter ( ) ; tree . root = new Tree < int > ( 1 ) ; tree . root . left = new Tree < int > ( 2 ) ; tree . root . right = new Tree < int > ( 3 ) ; tree . root . left . left = new Tree < int > ( 4 ) ; tree . root . left . right = new Tree < int > ( 5 ) ; Console . WriteLine ( $" The diameter of given binary tree is :  { tree . Diameter ( ) } " ) ; } } }
Find if possible to visit every nodes in given Graph exactly once based on given conditions | C # program for above approach . ; Function to find print path ; If a [ 0 ] is 1 ; Printing path ; Seeking for a [ i ] = 0 and a [ i + 1 ] = 1 ; Printing path ; If a [ N - 1 ] = 0 ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void findpath ( int N , int [ ] a ) { if ( a [ 0 ] != 0 ) { Console . Write ( N + 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( i + " ▁ " ) ; return ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( a [ i ] == 0 && a [ i + 1 ] != 0 ) { for ( int j = 1 ; j <= i ; j ++ ) Console . Write ( j + " ▁ " ) ; Console . Write ( N + 1 + " ▁ " ) ; for ( int j = i + 1 ; j <= N ; j ++ ) Console . Write ( j + " ▁ " ) ; return ; } } for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( i + " ▁ " ) ; Console . Write ( N + 1 + " ▁ " ) ; } public static void Main ( ) { int N = 3 ; int [ ] arr = { 0 , 1 , 0 } ; findpath ( N , arr ) ; } }
Modify a numeric string to a balanced parentheses by replacements | C # program for the above approach ; Function to check if the given string can be converted to a balanced bracket sequence or not ; Check if the first and last characters are equal ; Initialize two variables to store the count of open and closed brackets ; If the current character is same as the first character ; If the current character is same as the last character ; If count of open brackets becomes less than 0 ; Print the new string ; If the current character is same as the first character ; If bracket sequence is not balanced ; Check for unbalanced bracket sequence ; Print the sequence ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void balBracketSequence ( string str ) { int n = str . Length ; if ( str [ 0 ] == str [ n - 1 ] ) { Console . Write ( " No " ) ; } else { int cntForOpen = 0 , cntForClose = 0 ; int check = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) cntForOpen ++ ; else if ( str [ i ] == str [ n - 1 ] ) cntForOpen -- ; else cntForOpen ++ ; if ( cntForOpen < 0 ) { check = 0 ; break ; } } if ( check != 0 && cntForOpen == 0 ) { Console . Write ( " Yes , ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ n - 1 ] ) Console . Write ( ' ) ' ) ; else Console . Write ( ' ( ' ) ; } return ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) cntForClose ++ ; else cntForClose -- ; if ( cntForClose < 0 ) { check = 0 ; break ; } } if ( check != 0 && cntForClose == 0 ) { Console . Write ( " Yes , ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == str [ 0 ] ) Console . Write ( ' ( ' ) ; else Console . Write ( ' ) ' ) ; } return ; } } Console . Write ( " No " ) ; } } public static void Main ( ) { string str = "123122" ; balBracketSequence ( str ) ; } }
Diameter of a Binary Tree | Recursive C # program to find the diameter of a Binary Tree ; Class containing left and right child of current node and key value ; A utility class to pass heigh object ; Class to print the Diameter ; define height = 0 globally and call diameterOpt ( root , height ) from main ; lh -- > Height of left subtree rh -- > Height of right subtree ; base condition - when binary tree is empty ; diameter is also 0 ; ldiameter -- > diameter of left subtree rdiameter -- > Diameter of right subtree Get the heights of left and right subtrees in lh and rh And store the returned values in ldiameter and ldiameter ; Height of current node is max of heights of left and right subtrees plus 1 ; A wrapper over diameter ( Node root ) ; The function Compute the " height " of a tree . Height is the number f nodes along the longest path from the root node down to the farthest leaf node . ; base case tree is empty ; If tree is not empty then height = 1 + max of left height and right heights ; Driver Code ; creating a binary tree and entering the nodes ; Function Call
using System ; using System . Collections . Generic ; class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class Height { public int h ; } class BinaryTree { public Node root ; public int diameterOpt ( Node root , Height height ) { Height lh = new Height ( ) , rh = new Height ( ) ; if ( root == null ) { height . h = 0 ; return 0 ; } int ldiameter = diameterOpt ( root . left , lh ) ; int rdiameter = diameterOpt ( root . right , rh ) ; height . h = Math . Max ( lh . h , rh . h ) + 1 ; return Math . Max ( lh . h + rh . h + 1 , Math . Max ( ldiameter , rdiameter ) ) ; } public int diameter ( ) { Height height = new Height ( ) ; return diameterOpt ( root , height ) ; } public int height ( Node node ) { if ( node == null ) return 0 ; return ( 1 + Math . Max ( height ( node . left ) , height ( node . right ) ) ) ; } static void Main ( ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; Console . Write ( " The ▁ diameter ▁ of ▁ given ▁ binary ▁ tree ▁ is ▁ : ▁ " + tree . diameter ( ) ) ; } }
Diameter of a Binary Tree in O ( n ) [ A new method ] | Simple C # program to find diameter of a binary tree . ; Tree node structure used in the program ; Function to find height of a tree ; update the answer , because diameter of a tree is nothing but maximum value of ( left_height + right_height + 1 ) for each node ; Computes the diameter of binary tree with given root . ; This will store the final answer ; A utility function to create a new Binary Tree Node ; Driver code
using System ; class GfG { class Node { public int data ; public Node left , right ; } class A { public int ans = int . MinValue ; } static int height ( Node root , A a ) { if ( root == null ) return 0 ; int left_height = height ( root . left , a ) ; int right_height = height ( root . right , a ) ; a . ans = Math . Max ( a . ans , 1 + left_height + right_height ) ; return 1 + Math . Max ( left_height , right_height ) ; } static int diameter ( Node root ) { if ( root == null ) return 0 ; A a = new A ( ) ; int height_of_tree = height ( root , a ) ; return a . ans ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void Main ( ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . WriteLine ( " Diameter ▁ is ▁ " + diameter ( root ) ) ; } }
Find postorder traversal of BST from preorder traversal | Run loop from 1 to length of pre ; Print from pivot length - 1 to zero ; Print from end to pivot length ; Driver Code
using System ; class GFG { public void getPostOrderBST ( int [ ] pre ) { int pivotPoint = 0 ; for ( int i = 1 ; i < pre . Length ; i ++ ) { if ( pre [ 0 ] <= pre [ i ] ) { pivotPoint = i ; break ; } } for ( int i = pivotPoint - 1 ; i > 0 ; i -- ) { Console . Write ( pre [ i ] + " ▁ " ) ; } for ( int i = pre . Length - 1 ; i >= pivotPoint ; i -- ) { Console . Write ( pre [ i ] + " ▁ " ) ; } Console . Write ( pre [ 0 ] ) ; }
Count substrings made up of a single distinct character | C # Program to implement the above approach ; Function to count the number of substrings made up of a single distinct character ; Stores the required count ; Stores the count of substrings possible by using current character ; Stores the previous character ; Traverse the string ; If current character is same as the previous character ; Increase count of substrings possible with current character ; Reset count of substrings possible with current character ; Update count of substrings ; Update previous character ; Driver code
using System ; class GFG { static void countSubstrings ( string s ) { int ans = 0 ; int subs = 1 ; char pre = '0' ; foreach ( char i in s ) { if ( pre == i ) { subs += 1 ; } else { subs = 1 ; } ans += subs ; pre = i ; } Console . WriteLine ( ans ) ; } static void Main ( ) { string s = " geeksforgeeks " ; countSubstrings ( s ) ; } }
Generate an array from given pairs of adjacent elements | C # program of the above approach ; Utility function to find original array ; Map to store all neighbors for each element ; Vector to store original elements ; Stotrs which array elements are visited ; Adjacency list to store neighbors of each array element ; Find the first corner element ; Stores first element of the original array ; Push it into the original array ; Mark as visited ; Traversing the neighbors and check if the elements are visited or not ; Traverse adjacent elements ; If element is not visited ; Push it into res ; Mark as visited ; Update the next adjacent ; Print original array ; Driver Code ; Given pairs of adjacent elements
using System ; using System . Collections . Generic ; public class Pair { public int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } public class GFG { static void find_original_array ( List < Pair > A ) { Dictionary < int , List < int > > mp = new Dictionary < int , List < int > > ( ) ; List < int > res = new List < int > ( ) ; Dictionary < int , bool > visited = new Dictionary < int , bool > ( ) ; foreach ( Pair it in A ) { List < int > temp ; temp = ( mp . ContainsKey ( it . first ) ) ? mp [ it . first ] : new List < int > ( ) ; temp . Add ( it . second ) ; if ( ! mp . ContainsKey ( it . first ) ) mp . Add ( it . first , temp ) ; else mp [ it . first ] = temp ; temp = ( mp . ContainsKey ( it . second ) ) ? mp [ it . second ] : new List < int > ( ) ; temp . Add ( it . first ) ; if ( ! mp . ContainsKey ( it . second ) ) mp . Add ( it . second , temp ) ; else mp [ it . second ] = temp ; } int It = 0 ; foreach ( int key in mp . Keys ) { if ( mp [ key ] . Count == 1 ) { It = key ; } } int adjacent = It ; res . Add ( It ) ; visited . Add ( It , true ) ; while ( res . Count != A . Count + 1 ) { foreach ( int elements in mp [ adjacent ] ) { if ( ! visited . ContainsKey ( elements ) ) { res . Add ( elements ) ; visited . Add ( elements , true ) ; adjacent = elements ; } } } foreach ( int val in res ) { Console . Write ( val + " ▁ " ) ; } } static public void Main ( ) { List < Pair > A = new List < Pair > ( ) ; A . Add ( new Pair ( 5 , 1 ) ) ; A . Add ( new Pair ( 3 , 4 ) ) ; A . Add ( new Pair ( 3 , 5 ) ) ; find_original_array ( A ) ; } }
Possible edges of a tree for given diameter , height and vertices | C # program to construct tree for given count width and height . ; Function to construct the tree ; Special case when d == 2 , only one edge ; Tree is not possible ; Satisfy the height condition by add edges up to h ; Add d - h edges from 1 to satisfy diameter condition ; Remaining edges at vertex 1 or 2 ( d == h ) ; Driver Code
using System ; class GfG { static void constructTree ( int n , int d , int h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { Console . WriteLine ( "1 ▁ 2" ) ; return ; } Console . WriteLine ( " - 1" ) ; return ; } if ( d > 2 * h ) { Console . WriteLine ( " - 1" ) ; return ; } for ( int i = 1 ; i <= h ; i ++ ) Console . WriteLine ( i + " ▁ " + ( i + 1 ) ) ; if ( d > h ) { Console . WriteLine ( "1" + " ▁ " + ( h + 2 ) ) ; for ( int i = h + 2 ; i <= d ; i ++ ) { Console . WriteLine ( i + " ▁ " + ( i + 1 ) ) ; } } for ( int i = d + 1 ; i < n ; i ++ ) { int k = 1 ; if ( d == h ) k = 2 ; Console . WriteLine ( k + " ▁ " + ( i + 1 ) ) ; } } public static void Main ( String [ ] args ) { int n = 5 , d = 3 , h = 2 ; constructTree ( n , d , h ) ; } }
Query to find length of the longest subarray consisting only of 1 s | C # Program for the above approach ; Function to calculate the longest subarray consisting of 1 s only ; Stores the maximum length of subarray containing 1 s only ; Traverse the array ; If current element is '1' ; Increment length ; Otherwise ; Reset length ; Update maximum subarray length ; Function to perform given queries ; Stores count of queries ; Traverse each query ; Flip the character ; Driver code ; Size of array ; Given array ; Given queries ; Number of queries
using System ; using System . Collections . Generic ; class GFG { static int longestsubarray ( int [ ] a , int N ) { int maxlength = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( a [ i ] == 1 ) { sum ++ ; } else { sum = 0 ; } maxlength = Math . Max ( maxlength , sum ) ; } return maxlength ; } static void solveQueries ( int [ ] arr , int n , List < List < int > > Q , int k ) { int cntQuery = Q . Count ; for ( int i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { Console . Write ( longestsubarray ( arr , n ) + " ▁ " ) ; } else { arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 ; } } } static void Main ( ) { int N = 10 ; int [ ] arr = { 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; List < List < int > > Q = new List < List < int > > ( ) ; Q . Add ( new List < int > { 1 } ) ; Q . Add ( new List < int > { 2 , 3 } ) ; Q . Add ( new List < int > { 1 } ) ; int K = 3 ; solveQueries ( arr , N , Q , K ) ; } }
Smallest element present in every subarray of all possible lengths | C # program for the above approach ; Function to add count of numbers in the map for a subarray of length k ; Set to store unique elements ; Add elements to the set ; Adding count in map ; Function to check if there is any number which repeats itself in every subarray of length K ; Check all number starting from 1 ; Check if i occurred n - k + 1 times ; Print the smallest number ; Print - 1 , if no such number found ; Function to count frequency of each number in each subarray of length K ; Traverse all subarrays of length K ; Check and print the smallest number present in every subarray and print it ; Function to generate the value of K ; Function call ; Driver code ; Given array ; Size of array ; Function call
using System ; using System . Collections . Generic ; class GFG { static void uniqueElements ( int [ ] arr , int start , int K , Dictionary < int , int > mp ) { HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < K ; i ++ ) st . Add ( arr [ start + i ] ) ; foreach ( int itr in st ) { if ( mp . ContainsKey ( itr ) ) { mp [ itr ] ++ ; } else { mp [ itr ] = 1 ; } } } static void checkAnswer ( Dictionary < int , int > mp , int N , int K ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( mp . ContainsKey ( i ) ) if ( mp [ i ] == ( N - K + 1 ) ) { Console . Write ( i + " ▁ " ) ; return ; } } Console . Write ( - 1 + " ▁ " ) ; } static void smallestPresentNumber ( int [ ] arr , int N , int K ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i <= N - K ; i ++ ) { uniqueElements ( arr , i , K , mp ) ; } checkAnswer ( mp , N , K ) ; } static void generateK ( int [ ] arr , int N ) { for ( int k = 1 ; k <= N ; k ++ ) smallestPresentNumber ( arr , N , k ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 } ; int N = arr . Length ; generateK ( arr , N ) ; } }
Deepest right leaf node in a binary tree | Iterative approach | C # program to find deepest right leaf node of binary tree ; tree node ; returns a new tree Node ; return the deepest right leaf node of binary tree ; Create a queue for level order traversal ; Traverse until the queue is empty ; Since we go level by level , the last stored right leaf node is deepest one ; Driver code ; construct a tree
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node getDeepestRightLeafNode ( Node root ) { if ( root == null ) return null ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; Node result = null ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . left != null ) { q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { q . Enqueue ( temp . right ) ; if ( temp . right . left == null && temp . right . right == null ) result = temp . right ; } } return result ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . right = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; Node result = getDeepestRightLeafNode ( root ) ; if ( result != null ) Console . WriteLine ( " Deepest ▁ Right ▁ Leaf ▁ Node ▁ : : ▁ " + result . data ) ; else Console . WriteLine ( " No ▁ result , ▁ right ▁ leaf ▁ not ▁ found STRNEWLINE " ) ; } }
Check if two strings can be made equal by reversing a substring of one of the strings | C # program for the above approach ; Function to check if the Strings can be made equal or not by reversing a subString of X ; Store the first index from the left which contains unequal characters in both the Strings ; Store the first element from the right which contains unequal characters in both the Strings ; Checks for the first index from left in which characters in both the Strings are unequal ; Store the current index ; Break out of the loop ; Checks for the first index from right in which characters in both the Strings are unequal ; Store the current index ; Break out of the loop ; Reverse the subString X [ L , R ] ; If X and Y are equal ; Otherwise ; Driver Code ; Function Call
using System ; class GFG { static void checkString ( String X , String Y ) { int L = - 1 ; int R = - 1 ; for ( int i = 0 ; i < X . Length ; ++ i ) { if ( X [ i ] != Y [ i ] ) { L = i ; break ; } } for ( int i = X . Length - 1 ; i > 0 ; -- i ) { if ( X [ i ] != Y [ i ] ) { R = i ; break ; } } X = X . Substring ( 0 , L ) + reverse ( X . Substring ( L , R + 1 - L ) ) + X . Substring ( R + 1 ) ; if ( X . Equals ( Y ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String X = " adcbef " , Y = " abcdef " ; checkString ( X , Y ) ; } }
Maximum length of same indexed subarrays from two given arrays satisfying the given condition | C # program for the above approach ; Function to find maximum length of subarray such that sum of maximum element in subarray in brr [ ] and sum of subarray in [ ] arr * K is at most C ; Base Case ; Let maximum length be 0 ; Perform Binary search ; Find mid value ; Check if the current mid satisfy the given condition ; If yes , then store length ; Otherwise ; Return maximum length stored ; Function to check if it is possible to have such a subarray of length K ; Finds the maximum element in each window of size k ; Check for window of size K ; For all possible subarrays of length k ; Until deque is empty ; Calculate the total cost and check if less than equal to c ; Find sum of current subarray and the total cost ; Include the new element of current subarray ; Discard the element of last subarray ; Remove all the elements in the old window ; Calculate total cost and check <= c ; If current subarray length satisfies ; If it is not possible ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public static int maxLength ( int [ ] a , int [ ] b , int n , int c ) { if ( n == 0 ) return 0 ; int max_length = 0 ; int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( possible ( a , b , n , c , mid ) ) { max_length = mid ; low = mid + 1 ; } else high = mid - 1 ; } return max_length ; } public static bool possible ( int [ ] a , int [ ] b , int n , int c , int k ) { List < int > dq = new List < int > ( ) ; int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { sum += a [ i ] ; while ( dq . Count > 0 && b [ i ] > b [ dq [ dq . Count - 1 ] ] ) dq . RemoveAt ( dq . Count - 1 ) ; dq . Add ( i ) ; } int total_cost = sum * k + b [ dq [ 0 ] ] ; if ( total_cost <= c ) return true ; for ( int i = k ; i < n ; i ++ ) { sum += a [ i ] ; sum -= a [ i - k ] ; while ( dq . Count > 0 && dq [ 0 ] <= i - k ) dq . RemoveAt ( 0 ) ; while ( dq . Count > 0 && b [ i ] > b [ dq [ dq . Count - 1 ] ] ) dq . RemoveAt ( dq . Count - 1 ) ; dq . Add ( i ) ; total_cost = sum * k + b [ dq [ 0 ] ] ; if ( total_cost <= c ) return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 1 , 6 , 5 , 5 , 6 , 1 } ; int [ ] B = { 14 , 8 , 15 , 15 , 9 , 10 , 7 , 12 } ; int N = A . Length ; int C = 40 ; Console . WriteLine ( maxLength ( A , B , N , C ) ) ; } }
Program to find the Nth natural number with exactly two bits set | Set 2 | C # program for the above approach ; Function to find the Nth number with exactly two bits set ; Initialize the range in which the value of ' a ' is present ; Perform Binary Search ; Find the mid value ; Update the range using the mid value t ; Find b value using a and N ; Print the value 2 ^ a + 2 ^ b ; Driver Code ; Function Call
using System ; class GFG { static void findNthNum ( int N ) { left = 1 ; right = N ; while ( left <= right ) { mid = left + ( right - left ) / 2 ; t = ( mid * ( mid + 1 ) ) / 2 ; if ( t < N ) { left = mid + 1 ; } else if ( t == N ) { a = mid ; break ; } else { a = mid ; right = mid - 1 ; } } t = a - 1 ; b = N - ( t * ( t + 1 ) ) / 2 - 1 ; Console . Write ( ( 1 << a ) + ( 1 << b ) ) ; } public static void Main ( ) { int N = 15 ; findNthNum ( N ) ; } }