codeparrot/xlcost-text-to-code huggingface dataset:
Find next right node of a given key | Set 2 | C # program to find next right of a given key using preorder traversal ; A Binary Tree Node ; Utility function to create a new tree node ; Function to find next node for given node in same level in a binary tree by using pre - order traversal ; return null if tree is empty ; if desired node is found , set value_level to current level ; if value_level is already set , then current node is the next right node ; recurse for left subtree by increasing level by 1 ; if node is found in left subtree , return it ; recurse for right subtree by increasing level by 1 ; Function to find next node of given node in the same level in given binary tree ; A utility function to test above functions ; Driver main ; Let us create binary tree given in the above example
using System ; class GfG { public class V { public int value_level = 0 ; } public class Node { public Node left , right ; public int key ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static Node nextRightNode ( Node root , int k , int level , V value ) { if ( root == null ) return null ; if ( root . key == k ) { value . value_level = level ; return null ; } else if ( value . value_level != 0 ) { if ( level == value . value_level ) return root ; } Node leftNode = nextRightNode ( root . left , k , level + 1 , value ) ; if ( leftNode != null ) return leftNode ; return nextRightNode ( root . right , k , level + 1 , value ) ; } static Node nextRightNodeUtil ( Node root , int k ) { V v = new V ( ) ; return nextRightNode ( root , k , 1 , v ) ; } static void test ( Node root , int k ) { Node nr = nextRightNodeUtil ( root , k ) ; if ( nr != null ) Console . WriteLine ( " Next ▁ Right ▁ of ▁ " + k + " ▁ is ▁ " + nr . key ) ; else Console . WriteLine ( " No ▁ next ▁ right ▁ node " + " ▁ found ▁ for ▁ " + k ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 6 ) ; root . right . right = newNode ( 5 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 4 ) ; test ( root , 10 ) ; test ( root , 2 ) ; test ( root , 6 ) ; test ( root , 5 ) ; test ( root , 8 ) ; test ( root , 4 ) ; } }
Top three elements in binary tree | C # program to find largest three elements in a binary tree . ; Helper function that allocates a new Node with the given data and null left and right pointers . ; function to find three largest element ; if data is greater than first large number update the top three list ; if data is greater than second large number and not equal to first update the bottom two list ; if data is greater than third large number and not equal to first & second update the third highest list ; Driver Code
using System ; class GFG { public class Node { public int data ; public Node left ; public Node right ; } ; static int first , second , third ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void threelargest ( Node root ) { if ( root == null ) return ; if ( root . data > first ) { third = second ; second = first ; first = root . data ; } else if ( root . data > second && root . data != first ) { third = second ; second = root . data ; } else if ( root . data > third && root . data != first && root . data != second ) third = root . data ; threelargest ( root . left ) ; threelargest ( root . right ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; first = 0 ; second = 0 ; third = 0 ; threelargest ( root ) ; Console . WriteLine ( " three ▁ largest ▁ elements ▁ are ▁ " + first + " ▁ " + second + " ▁ " + third ) ; } }
Find maximum ( or minimum ) in Binary Tree | C # code to Find maximum ( or minimum ) in Binary Tree ; A binary tree node ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; Returns the max value in a binary tree ; Base case ; Return maximum of 3 values : 1 ) Root 's data 2) Max in Left Subtree 3) Max in right subtree ; Driver code ; Function call
using System ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } public class BinaryTree { public Node root ; public static int findMax ( Node node ) { if ( node == null ) { return int . MinValue ; } int res = node . data ; int lres = findMax ( node . left ) ; int rres = findMax ( node . right ) ; if ( lres > res ) { res = lres ; } if ( rres > res ) { res = rres ; } return res ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 2 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 1 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 4 ) ; Console . WriteLine ( " Maximum ▁ element ▁ is ▁ " + BinaryTree . findMax ( tree . root ) ) ; } }
Find maximum ( or minimum ) in Binary Tree | Returns the min value in a binary tree
public static int findMin ( Node node ) { if ( node == null ) return int . MaxValue ; int res = node . data ; int lres = findMin ( node . left ) ; int rres = findMin ( node . right ) ; if ( lres < res ) res = lres ; if ( rres < res ) res = rres ; return res ; }
Extract Leaves of a Binary Tree in a Doubly Linked List | C # program to extract leaf nodes from binary tree using double linked list ; A binay tree node ; Binary Tree class ; The main function that links the list list to be traversed ; Utility function for printing tree in In - Order . ; Prints the DLL in both forward and reverse directions . ; Driver code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; right = left = null ; } } public class BinaryTree { Node root ; Node head ; Node prev ; public Node extractLeafList ( Node root ) { if ( root == null ) return null ; if ( root . left == null && root . right == null ) { if ( head == null ) { head = root ; prev = root ; } else { prev . right = root ; root . left = prev ; prev = root ; } return null ; } root . left = extractLeafList ( root . left ) ; root . right = extractLeafList ( root . right ) ; return root ; } void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inorder ( node . right ) ; } public void printDLL ( Node head ) { Node last = null ; while ( head != null ) { Console . Write ( head . data + " ▁ " ) ; last = head ; head = head . right ; } } public static void Main ( String [ ] args ) { 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 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . left . left . left = new Node ( 7 ) ; tree . root . left . left . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 9 ) ; tree . root . right . right . right = new Node ( 10 ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ given ▁ tree ▁ is ▁ : ▁ " ) ; tree . inorder ( tree . root ) ; tree . extractLeafList ( tree . root ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " Extracted ▁ double ▁ link ▁ list ▁ is ▁ : ▁ " ) ; tree . printDLL ( tree . head ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ modified ▁ tree ▁ is ▁ : ▁ " ) ; tree . inorder ( tree . root ) ; } }
Inorder Successor of a node in Binary Tree | C # program to find inorder successor of a node . ; structure of a Binary Node . ; class to find inorder successor of a node ; to change previous node ; function to find inorder successor of a node ; Case1 : If right child is not NULL ; Case2 : If root data is equal to search node ; Driver code ; Let 's construct the binary tree   as shown in above diagram  ; Case 1 ; Case 2 ; Case 3
using System ; class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } public class InorderSuccessor { Node root ; class PreviousNode { public Node pNode ; public PreviousNode ( ) { pNode = null ; } } private void inOrderSuccessorOfBinaryTree ( Node root , PreviousNode pre , int searchNode ) { if ( root . right != null ) inOrderSuccessorOfBinaryTree ( root . right , pre , searchNode ) ; if ( root . data == searchNode ) { Console . Write ( " inorder ▁ successor ▁ of ▁ " + searchNode + " ▁ is : ▁ " ) ; if ( pre . pNode != null ) Console . WriteLine ( pre . pNode . data ) ; else Console . WriteLine ( " null " ) ; } pre . pNode = root ; if ( root . left != null ) inOrderSuccessorOfBinaryTree ( root . left , pre , searchNode ) ; } public static void Main ( String [ ] args ) { InorderSuccessor tree = new InorderSuccessor ( ) ; 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 ) ; tree . root . right . right = new Node ( 6 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 3 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 4 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 6 ) ; } }
Graph and its representations | C # code to demonstrate Graph representation using LinkedList in C # ; A utility function to add an edge in an undirected graph ; A utility function to print the adjacency list representation of graph ; Driver Code ; Creating a graph with 5 vertices ; Adding edges one by one
using System ; using System . Collections . Generic ; class Graph { static void addEdge ( LinkedList < int > [ ] adj , int u , int v ) { adj [ u ] . AddLast ( v ) ; adj [ v ] . AddLast ( u ) ; } static void printGraph ( LinkedList < int > [ ] adj ) { for ( int i = 0 ; i < adj . Length ; i ++ ) { Console . WriteLine ( " STRNEWLINE Adjacency ▁ list ▁ of ▁ vertex ▁ " + i ) ; Console . Write ( " head " ) ; foreach ( var item in adj [ i ] ) { Console . Write ( " ▁ - > ▁ " + item ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int V = 5 ; LinkedList < int > [ ] adj = new LinkedList < int > [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new LinkedList < int > ( ) ; addEdge ( adj , 0 , 1 ) ; addEdge ( adj , 0 , 4 ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 1 , 4 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; printGraph ( adj ) ; Console . ReadKey ( ) ; } }
Find n | C # program for nth nodes of inorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Given a binary tree , print its nth nodes of inorder ; first recur on left child ; when count = n then print element ; now recur on right child ; Driver Code
using System ; class GFG { public static int count = 0 ; public class Node { public int data ; public Node left ; public Node right ; } public static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void NthInorder ( Node node , int n ) { if ( node == null ) { return ; } if ( count <= n ) { NthInorder ( node . left , n ) ; count ++ ; if ( count == n ) { Console . Write ( " { 0 : D } ▁ " , node . data ) ; } NthInorder ( node . right , n ) ; } } public static void Main ( string [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; int n = 4 ; NthInorder ( root , n ) ; } }
Level of Each node in a Tree from source node ( using BFS ) | C # Program to determine level of each node and print level ; function to determine level of each node starting from x using BFS ; array to store level of each node ; create a queue ; enqueue element x ; initialize level of source node to 0 ; marked it as visited ; do until queue is empty ; get the first element of queue ; dequeue element ; traverse neighbors of node x ; b is neighbor of node x ; if b is not marked already ; enqueue b in queue ; level of b is level of x + 1 ; mark b ; display all nodes and their levels ; Driver Code ; adjacency graph for tree ; call levels function with source as 0
using System ; using System . Collections . Generic ; class GFG { static void printLevels ( List < List < int > > graph , int V , int x ) { int [ ] level = new int [ V ] ; Boolean [ ] marked = new Boolean [ V ] ; Queue < int > que = new Queue < int > ( ) ; que . Enqueue ( x ) ; level [ x ] = 0 ; marked [ x ] = true ; while ( que . Count > 0 ) { x = que . Peek ( ) ; que . Dequeue ( ) ; for ( int i = 0 ; i < graph [ x ] . Count ; i ++ ) { int b = graph [ x ] [ i ] ; if ( ! marked [ b ] ) { que . Enqueue ( b ) ; level [ b ] = level [ x ] + 1 ; marked [ b ] = true ; } } } Console . WriteLine ( " Nodes " + " ▁ " + " Level " ) ; for ( int i = 0 ; i < V ; i ++ ) Console . WriteLine ( " ▁ " + i + " ▁ - - > ▁ " + level [ i ] ) ; } public static void Main ( String [ ] args ) { int V = 8 ; List < List < int > > graph = new List < List < int > > ( ) ; for ( int i = 0 ; i < V + 1 ; i ++ ) graph . Add ( new List < int > ( ) ) ; graph [ 0 ] . Add ( 1 ) ; graph [ 0 ] . Add ( 2 ) ; graph [ 1 ] . Add ( 3 ) ; graph [ 1 ] . Add ( 4 ) ; graph [ 1 ] . Add ( 5 ) ; graph [ 2 ] . Add ( 5 ) ; graph [ 2 ] . Add ( 6 ) ; graph [ 6 ] . Add ( 7 ) ; printLevels ( graph , V , 0 ) ; } }
Construct binary palindrome by repeated appending and trimming | C # code to form binary palindrome ; function to apply DFS ; set the parent marked ; if the node has not been visited set it and its children marked ; link which digits must be equal ; connect the two indices ; set everything connected to first character as 1 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void dfs ( int parent , int [ ] ans , List < int > [ ] connectchars ) { ans [ parent ] = 1 ; for ( int i = 0 ; i < connectchars [ parent ] . Count ; i ++ ) { if ( ans [ connectchars [ parent ] [ i ] ] != 1 ) dfs ( connectchars [ parent ] [ i ] , ans , connectchars ) ; } } static void printBinaryPalindrome ( int n , int k ) { int [ ] arr = new int [ n ] ; int [ ] ans = new int [ n ] ; List < int > [ ] connectchars = new List < int > [ k ] ; for ( int i = 0 ; i < k ; i ++ ) connectchars [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = i % k ; for ( int i = 0 ; i < n / 2 ; i ++ ) { connectchars [ arr [ i ] ] . Add ( arr [ n - i - 1 ] ) ; connectchars [ arr [ n - i - 1 ] ] . Add ( arr [ i ] ) ; } dfs ( 0 , ans , connectchars ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( ans [ arr [ i ] ] ) ; } public static void Main ( String [ ] args ) { int n = 10 , k = 4 ; printBinaryPalindrome ( n , k ) ; } }
Find n | C # program to find n - th node of Postorder Traversal of Binary Tree ; A binary tree node structure ; function to find the N - th node in the postorder traversal of a given binary tree ; left recursion ; right recursion ; prints the n - th node of preorder traversal ; Driver code ; prints n - th node found
using System ; public class NthNodePostOrder { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; } } static int flag = 0 ; static void NthPostordernode ( Node root , int N ) { if ( root == null ) return ; if ( flag <= N ) { NthPostordernode ( root . left , N ) ; NthPostordernode ( root . right , N ) ; flag ++ ; if ( flag == N ) Console . Write ( root . data ) ; } } public static void Main ( String [ ] args ) { Node root = new Node ( 25 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . left = new Node ( 18 ) ; root . left . right = new Node ( 22 ) ; root . right . left = new Node ( 24 ) ; root . right . right = new Node ( 32 ) ; int N = 6 ; NthPostordernode ( root , N ) ; } }
DFS for a n | C # Code For DFS for a n - ary tree ( acyclic graph ) represented as adjacency list ; DFS on tree ; Printing traversed node ; Traversing adjacent edges ; Not traversing the parent node ; Driver program to test above function ; Number of nodes ; Adjacency list ; Designing the tree ; Function call
using System ; using System . Collections . Generic ; public class GFG { public static void dfs ( List < int > [ ] list , int node , int arrival ) { Console . WriteLine ( node ) ; for ( int i = 0 ; i < list [ node ] . Count ; i ++ ) { if ( list [ node ] [ i ] != arrival ) dfs ( list , list [ node ] [ i ] , node ) ; } } public static void Main ( String [ ] args ) { int nodes = 5 ; List < int > [ ] list = new List < int > [ nodes + 1 ] ; for ( int i = 0 ; i < list . Length ; i ++ ) { list [ i ] = new List < int > ( ) ; } list [ 1 ] . Add ( 2 ) ; list [ 2 ] . Add ( 1 ) ; list [ 1 ] . Add ( 3 ) ; list [ 3 ] . Add ( 1 ) ; list [ 2 ] . Add ( 4 ) ; list [ 4 ] . Add ( 2 ) ; list [ 3 ] . Add ( 5 ) ; list [ 5 ] . Add ( 3 ) ; dfs ( list , 1 , 0 ) ; } }
Maximum number of edges to be added to a tree so that it stays a Bipartite graph | C # code to print maximum edges such that Tree remains a Bipartite graph ; To store counts of nodes with two colors ; Increment count of nodes with current color ; Traversing adjacent nodes ; Not recurring for the parent node ; Finds maximum number of edges that can be added without violating Bipartite property . ; Do a DFS to count number of nodes of each color ; Driver code
using System ; using System . Collections . Generic ; class GFG { static long [ ] count_color = new long [ 2 ] ; static void dfs ( List < int > [ ] adj , int node , int parent , bool color ) { count_color [ color == false ? 0 : 1 ] ++ ; for ( int i = 0 ; i < adj [ node ] . Count ; i ++ ) { if ( adj [ node ] [ i ] != parent ) dfs ( adj , adj [ node ] [ i ] , node , ! color ) ; } } static int findMaxEdges ( List < int > [ ] adj , int n ) { dfs ( adj , 1 , 0 , false ) ; return ( int ) ( count_color [ 0 ] * count_color [ 1 ] - ( n - 1 ) ) ; } public static void Main ( String [ ] args ) { int n = 5 ; List < int > [ ] adj = new List < int > [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) adj [ i ] = new List < int > ( ) ; adj [ 1 ] . Add ( 2 ) ; adj [ 1 ] . Add ( 3 ) ; adj [ 2 ] . Add ( 4 ) ; adj [ 3 ] . Add ( 5 ) ; Console . WriteLine ( findMaxEdges ( adj , n ) ) ; } }
Min steps to empty an Array by removing a pair each time with sum at most K | C # program implementation of the above approach ; Function to count minimum steps ; Function to sort the array ; Run while loop ; Condition to check whether sum exceed the target or not ; Increment the step by 1 ; Return minimum steps ; Driver code ; Given target value ; Print the minimum flip
using System ; class GFG { static int countMinSteps ( int [ ] arr , int target , int n ) { Array . Sort ( arr ) ; int minimumSteps = 0 ; int i = 0 ; int j = n - 1 ; while ( i <= j ) { if ( arr [ i ] + arr [ j ] <= target ) { i += 1 ; j -= 1 ; } else { j -= 1 ; } minimumSteps += 1 ; } return minimumSteps ; } public static void Main ( ) { int [ ] arr = new int [ ] { 4 , 6 , 2 , 9 , 6 , 5 , 8 , 10 } ; int target = 11 ; int size = arr . Length ; Console . Write ( countMinSteps ( arr , target , size ) ) ; } }
Find if there exists a direction for ranges such that no two range intersect | C # implementation of the approach ; Structure to hold details of each interval ; Function that returns true if the assignment of directions is possible ; Sort the intervals based on velocity ; Test the condition for all intervals with same velocity ; If for any velocity , 3 or more intervals share a common point return false ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public class interval { public int l , r , v ; } public class sortHelper : IComparer < interval > { public int Compare ( interval a , interval b ) { return ( a . v < b . v ? 1 : 0 ) ; } } static int MAX = 100001 ; static bool isPossible ( int [ , ] range , int N ) { interval [ ] test = new interval [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { test [ i ] = new interval ( ) ; test [ i ] . l = range [ i , 0 ] ; test [ i ] . r = range [ i , 1 ] ; test [ i ] . v = range [ i , 2 ] ; } Array . Sort ( test , new sortHelper ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { int [ ] count = new int [ MAX ] ; int current_velocity = test [ i ] . v ; int j = i ; while ( j < N && test [ j ] . v == current_velocity ) { for ( int k = test [ j ] . l ; k <= test [ j ] . r ; k ++ ) { count [ k ] ++ ; if ( count [ k ] >= 3 ) return false ; } j ++ ; } i = j - 1 ; } return true ; } public static void Main ( string [ ] args ) { int [ , ] range = { { 1 , 2 , 3 } , { 2 , 5 , 1 } , { 3 , 10 , 1 } , { 4 , 4 , 1 } , { 5 , 7 , 10 } } ; int n = range . GetLength ( 0 ) ; if ( isPossible ( range , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find the kth smallest number with sum of digits as m | C # implementation of the approach ; Recursively moving to add all the numbers upto a limit with sum of digits as m ; Max number of digits allowed in a number for this implementation ; Function to return the kth number with sum of digits as m ; The kth smallest number is found ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int N = 2005 ; static HashSet < int > ans = new HashSet < int > ( ) ; static void dfs ( int num , int left , int ct ) { if ( ct >= 15 ) return ; if ( left == 0 ) ans . Add ( num ) ; for ( int i = 0 ; i <= Math . Min ( left , 9 ) ; i ++ ) dfs ( num * 10 + i , left - i , ct + 1 ) ; } static int getKthNum ( int m , int k ) { dfs ( 0 , m , 0 ) ; int ct = 0 ; foreach ( int it in ans ) { ct ++ ; if ( ct == k ) { return it ; } } return - 1 ; } public static void Main ( String [ ] args ) { int m = 5 , k = 3 ; Console . WriteLine ( getKthNum ( m , k ) ) ; } }
Remove minimum elements from the array such that 2 * min becomes more than max | C # program to remove minimum elements from the array such that 2 * min becomes more than max ; Function to calculate upper bound ; Function to remove minimum elements from the array such that 2 * min becomes more than max ; Sort the array ; To store the required answer ; Traverse from left to right ; Update the answer ; Return the required answer ; Driver code ; Function call
using System ; class GFG { public static int upperBound ( int [ ] array , int value ) { int low = 0 ; int high = array . Length ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value >= array [ mid ] ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static int Removal ( int [ ] v , int n ) { Array . Sort ( v ) ; int ans = int . MaxValue ; int k = 0 ; foreach ( int i in v ) { int j = upperBound ( v , ( 2 * i ) ) ; ans = Math . Min ( ans , n - ( j - k ) ) ; k ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 } ; int n = a . Length ; Console . WriteLine ( Removal ( a , n ) ) ; } }