codeparrot/xlcost-text-to-code huggingface dataset:
Find the k smallest numbers after deleting given elements | C # program to find the k maximum number from the array after n deletions ; Find k minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else push it in the min heap ; Print top k elements in the min heap ; Pop the top element ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findElementsAfterDel ( int [ ] arr , int m , int [ ] del , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( del [ i ] ) ) { mp [ del [ i ] ] ++ ; } else { mp [ del [ i ] ] = 1 ; } } List < int > heap = new List < int > ( ) ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] -- ; if ( mp [ arr [ i ] ] == 0 ) mp . Remove ( arr [ i ] ) ; } else heap . Add ( arr [ i ] ) ; } heap . Sort ( ) ; for ( int i = 0 ; i < k ; ++ i ) { Console . Write ( heap [ 0 ] + " ▁ " ) ; heap . RemoveAt ( 0 ) ; } } static void Main ( ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . Length ; int [ ] del = { 12 , 56 , 5 } ; int n = del . Length ; int k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ; } }
Elements to be added so that all elements of a range are present in array | C # implementation of the approach ; Function to count numbers to be added ; Make a hash of elements and store minimum and maximum element ; Traverse all elements from minimum to maximum and count if it is not in the hash ; Drivers code
using System ; using System . Collections . Generic ; class GFG { static int countNum ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; int count = 0 , maxm = int . MinValue , minm = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { s . Add ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm ) maxm = arr [ i ] ; } for ( int i = minm ; i <= maxm ; i ++ ) if ( ! s . Contains ( i ) ) count ++ ; return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 8 , 6 } ; int n = arr . Length ; Console . WriteLine ( countNum ( arr , n ) ) ; } }
Find minimum speed to finish all Jobs | C # program to find minimum speed to finish all jobs ; Function to check if the person can do all jobs in H hours with speed K ; Function to return the minimum speed of person to complete all jobs ; If H < N it is not possible to complete all jobs as person can not move from one element to another during current hour ; Max element of array ; Use binary search to find smallest K ; Driver program ; Print required maxLenwer
using System ; using System . Linq ; class GFG { static bool isPossible ( int [ ] A , int n , int H , int K ) { int time = 0 ; for ( int i = 0 ; i < n ; ++ i ) time += ( A [ i ] - 1 ) / K + 1 ; return time <= H ; } static int minJobSpeed ( int [ ] A , int n , int H ) { if ( H < n ) return - 1 ; int max = A . Max ( ) ; int lo = 1 , hi = max ; while ( lo < hi ) { int mi = lo + ( hi - lo ) / 2 ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } public static void Main ( ) { int [ ] A = { 3 , 6 , 7 , 11 } ; int H = 8 ; int n = A . Length ; Console . WriteLine ( minJobSpeed ( A , n , H ) ) ; } }
Number of anomalies in an array |  ; Sort the array so that we can apply binary search . ; One by one check every element if it is anomaly or not using binary search . ; If arr [ i ] is not largest element and element just greater than it is within k , then return false . ; If there are more than one occurrences of arr [ i ] , return false . ; If arr [ i ] is not smallest element and just smaller element is not k distance away ; Driver code
using System ; class GFG { static int countAnomalies ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int u = upper_bound ( a , 0 , n , a [ i ] ) ; if ( u < n && a [ u ] - a [ i ] <= k ) continue ; int s = lower_bound ( a , 0 , n , a [ i ] ) ; if ( u - s > 1 ) continue ; if ( s > 0 && a [ s - 1 ] - a [ i ] <= k ) continue ; res ++ ; } return res ; } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 1 , 8 } ; int k = 5 ; int n = arr . Length ; Console . Write ( countAnomalies ( arr , n , k ) ) ; } }
Subarrays with distinct elements | C # program to calculate sum of lengths of subarrays of distinct elements ; Returns sum of lengths of all subarrays with distinct elements . ; For maintaining distinct elements . ; Initialize ending point and result ; Fix starting point ; Calculating and adding all possible length subarrays in arr [ i . . j ] ; Remove arr [ i ] as we pick new stating point from next ; Driver Code
using System ; using System . Collections . Generic ; public class geeks { public static int sumoflength ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; int j = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n && ! s . Contains ( arr [ j ] ) ) { s . Add ( arr [ i ] ) ; j ++ ; } ans += ( ( j - i ) * ( j - i + 1 ) ) / 2 ; s . Remove ( arr [ i ] ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( sumoflength ( arr , n ) ) ; } }
Count subarrays having total distinct elements same as original array | C # program Count total number of sub - arrays having total distinct elements same as that original array . ; Method to calculate distinct sub - array ; Count distinct elements in whole array ; Reset the container by removing all elements ; Use sliding window concept to find count of subarrays having k distinct elements . ; If window size equals to array distinct element size , then update answer ; Decrease the frequency of previous element for next sliding window ; If frequency is zero then decrease the window size ; Driver method
using System ; using System . Collections . Generic ; class Test { static int countDistictSubarray ( int [ ] arr , int n ) { Dictionary < int , int > vis = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) if ( ! vis . ContainsKey ( arr [ i ] ) ) vis . Add ( arr [ i ] , 1 ) ; int k = vis . Count ; vis . Clear ( ) ; int ans = 0 , right = 0 , window = 0 ; for ( int left = 0 ; left < n ; ++ left ) { while ( right < n && window < k ) { if ( vis . ContainsKey ( arr [ right ] ) ) vis [ arr [ right ] ] = vis [ arr [ right ] ] + 1 ; else vis . Add ( arr [ right ] , 1 ) ; if ( vis [ arr [ right ] ] == 1 ) ++ window ; ++ right ; } if ( window == k ) ans += ( n - right + 1 ) ; if ( vis . ContainsKey ( arr [ left ] ) ) vis [ arr [ left ] ] = vis [ arr [ left ] ] - 1 ; if ( vis [ arr [ left ] ] == 0 ) -- window ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 3 , 2 , 3 } ; Console . WriteLine ( countDistictSubarray ( arr , arr . Length ) ) ; } }
Count subarrays with same even and odd elements | C # program to find total number of even - odd subarrays present in given array ; function that returns the count of subarrays that contain equal number of odd as well as even numbers ; initialize difference and answer with 0 ; create two auxiliary hash arrays to count frequency of difference , one array for non - negative difference and other array for negative difference . Size of these two auxiliary arrays is ' n + 1' because difference can reach maximum value ' n ' as well as minimum value ' - n ' ; initialize these auxiliary arrays with 0 ; since the difference is initially 0 , we have to initialize hash_positive [ 0 ] with 1 ; for loop to iterate through whole array ( zero - based indexing is used ) ; incrementing or decrementing difference based on arr [ i ] being even or odd , check if arr [ i ] is odd ; adding hash value of ' difference ' to our answer as all the previous occurrences of the same difference value will make even - odd subarray ending at index ' i ' . After that , we will increment hash array for that ' difference ' value for its occurrence at index ' i ' . if difference is negative then use hash_negative ; else use hash_positive ; return total number of even - odd subarrays ; Driver code ; Printing total number of even - odd subarrays
using System ; class GFG { static int countSubarrays ( int [ ] arr , int n ) { int difference = 0 ; int ans = 0 ; int [ ] hash_positive = new int [ n + 1 ] ; int [ ] hash_negative = new int [ n + 1 ] ; Array . Clear ( hash_positive , 0 , n + 1 ) ; Array . Clear ( hash_negative , 0 , n + 1 ) ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) == 1 ) difference ++ ; else difference -- ; if ( difference < 0 ) { ans += hash_negative [ - difference ] ; hash_negative [ - difference ] ++ ; } else { ans += hash_positive [ difference ] ; hash_positive [ difference ] ++ ; } } return ans ; } static void Main ( ) { int [ ] arr = new int [ ] { 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 } ; int n = arr . Length ; Console . Write ( " Total ▁ Number ▁ of ▁ Even - Odd " + " ▁ subarrays ▁ are ▁ " + countSubarrays ( arr , n ) ) ; } }
Given a binary tree , print out all of its root | C # program to print all root to leaf paths ; A binary tree node has data , pointer to left child and a pointer to right child ; Given a binary tree , print out all of its root - to - leaf paths , one per line . Uses a recursive helper to do the work . ; Recursive helper function -- given a node , and an array containing the path from the root node up to but not including this node , print out all the root - leaf paths . ; append this node to the path array ; it 's a leaf, so print the path that led to here  ; otherwise try both subtrees ; Utility that prints out an array on a line ; Driver program to test all above functions ; Print all root - to - leaf paths of the input tree
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual void printPaths ( Node node ) { int [ ] path = new int [ 1000 ] ; printPathsRecur ( node , path , 0 ) ; } public virtual void printPathsRecur ( Node node , int [ ] path , int pathLen ) { if ( node == null ) { return ; } path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) { printArray ( path , pathLen ) ; } else { printPathsRecur ( node . left , path , pathLen ) ; printPathsRecur ( node . right , path , pathLen ) ; } } public virtual void printArray ( int [ ] ints , int len ) { int i ; for ( i = 0 ; i < len ; i ++ ) { Console . Write ( ints [ i ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } 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 . printPaths ( tree . root ) ; } }
Saddleback Search Algorithm in a 2D array | C # program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ m ] [ n ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for bottom left element ; if mat [ i ] [ j ] < x ; Driver Code
using System ; class GFG { static bool search ( int [ , ] mat , int m , int n , int x ) { int i = m - 1 , j = 0 ; while ( i >= 0 && j < n ) { if ( mat [ i , j ] == x ) return true ; if ( mat [ i , j ] > x ) i -- ; else j ++ ; } return false ; } public static void Main ( ) { int [ , ] mat = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } , { 50 , 60 , 70 , 80 } } ; if ( search ( mat , 5 , 4 , 29 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Minimum number of distinct elements after removing m items | C # program for Minimum number of distinct elements after removing m items ; Function to find distintc id 's ; Store the occurrence of ids ; Start removing elements from the beginning ; Remove if current value is less than or equal to mi ; Return the remaining size ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int distinctIds ( int [ ] arr , int n , int mi ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int count = 0 ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) == false ) { m [ arr [ i ] ] = 1 ; size ++ ; } else { m [ arr [ i ] ] ++ ; } } foreach ( KeyValuePair < int , int > mp in m ) { if ( mp . Key <= mi ) { mi -= mp . Key ; count ++ ; } } return size - count ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 1 , 2 , 3 , 3 } ; int m = 3 ; Console . WriteLine ( distinctIds ( arr , arr . Length , m ) ) ; } }
Number of Larger Elements on right side in a string | C # program to find counts of right greater characters for every character . ; Driver code
using System ; public class GFG { static void printGreaterCount ( String str ) { int len = str . Length ; int [ ] right = new int [ len ] ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { if ( str [ i ] < str [ j ] ) { right [ i ] ++ ; } } } for ( int i = 0 ; i < len ; i ++ ) { Console . Write ( right [ i ] + " ▁ " ) ; } } public static void Main ( ) { String str = " abcd " ; printGreaterCount ( str ) ; } }
Maximum consecutive numbers present in an array | C # program to find largest consecutive numbers present in arr [ ] . ; We insert all the array elements into unordered set . ; check each possible sequence from the start then update optimal length ; if current element is the starting element of a sequence ; Then check for next elements in the sequence ; increment the value of array element and repeat search in the set ; Update optimal length if this length is more . To get the length as it is incremented one by one ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static int findLongestConseqSubseq ( int [ ] arr , int n ) { HashSet < int > S = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) S . Add ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . Contains ( arr [ i ] ) ) { int j = arr [ i ] ; while ( S . Contains ( j ) ) j ++ ; ans = Math . Max ( ans , j - arr [ i ] ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 94 , 93 , 1000 , 5 , 92 , 78 } ; int n = arr . Length ; Console . WriteLine ( findLongestConseqSubseq ( arr , n ) ) ; } }
N / 3 repeated number in an array with O ( 1 ) space | C # program to find if any element appears more than n / 3. ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code
using System ; public class GFG { static int appearsNBy3 ( int [ ] arr , int n ) { int count1 = 0 , count2 = 0 ; int first = int . MaxValue ; int second = int . MaxValue ; for ( int i = 1 ; i < n ; i ++ ) { if ( first == arr [ i ] ) count1 ++ ; else if ( second == arr [ i ] ) count2 ++ ; else if ( count1 == 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 == 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > n / 3 ) return first ; if ( count2 > n / 3 ) return second ; return - 1 ; } static public void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( appearsNBy3 ( arr , n ) ) ; } }
Count pairs in array whose sum is divisible by 4 | C # program to count pairs whose sum divisible by '4' ; Program to count pairs whose sum divisible by '4' ; Create a frequency array to count occurrences of all remainders when divided by 4 ; Count occurrences of all remainders ; If both pairs are divisible by '4' ; If both pairs are 2 modulo 4 ; If one of them is equal to 1 modulo 4 and the other is equal to 3 modulo 4 ; Driver code
using System ; class Count { public static int count4Divisibiles ( int [ ] arr , int n ) { int [ ] freq = { 0 , 0 , 0 , 0 } ; int i = 0 ; int ans ; for ( i = 0 ; i < n ; i ++ ) ++ freq [ arr [ i ] % 4 ] ; ans = freq [ 0 ] * ( freq [ 0 ] - 1 ) / 2 ; ans += freq [ 2 ] * ( freq [ 2 ] - 1 ) / 2 ; ans += freq [ 1 ] * freq [ 3 ] ; return ( ans ) ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 1 , 7 , 5 } ; int n = 5 ; Console . WriteLine ( count4Divisibiles ( arr , n ) ) ; } }
Find Sum of all unique sub | C # for finding sum of all unique subarray sum ; Function for finding grandSum ; Calculate cumulative sum of array cArray [ 0 ] will store sum of zero elements ; Store all subarray sum in vector ; Sort the vector ; Mark all duplicate sub - array sum to zero ; Calculate total sum ; Return totalSum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int findSubarraySum ( int [ ] arr , int n ) { int i , j ; int [ ] cArray = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) cArray [ i + 1 ] = cArray [ i ] + arr [ i ] ; List < int > subArrSum = new List < int > ( ) ; for ( i = 1 ; i <= n ; i ++ ) for ( j = i ; j <= n ; j ++ ) subArrSum . Add ( cArray [ j ] - cArray [ i - 1 ] ) ; subArrSum . Sort ( ) ; int totalSum = 0 ; for ( i = 0 ; i < subArrSum . Count - 1 ; i ++ ) { if ( subArrSum [ i ] == subArrSum [ i + 1 ] ) { j = i + 1 ; while ( subArrSum [ j ] == subArrSum [ i ] && j < subArrSum . Count ) { subArrSum [ j ] = 0 ; j ++ ; } subArrSum [ i ] = 0 ; } } for ( i = 0 ; i < subArrSum . Count ; i ++ ) totalSum += subArrSum [ i ] ; return totalSum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 3 , 1 , 4 } ; int n = arr . Length ; Console . Write ( findSubarraySum ( arr , n ) ) ; } }
Smallest number whose set bits are maximum in a given range | C # program to find number whose set bits are maximum among ' l ' and ' r ' ; Returns smallest number whose set bits are maximum in given range . ; Initialize the maximum count and final answer as ' num ' ; Traverse for every bit of ' i ' number ; If count is greater than previous calculated max_count , update it ; Driver code
using System ; class gfg { static int countMaxSetBits ( int left , int right ) { int max_count = - 1 , num = 0 ; for ( int i = left ; i <= right ; ++ i ) { int temp = i , cnt = 0 ; while ( temp > 0 ) { if ( temp % 2 == 1 ) ++ cnt ; temp >>= 1 ; } if ( cnt > max_count ) { max_count = cnt ; num = i ; } } return num ; } public static void Main ( String [ ] args ) { int l = 1 , r = 5 ; Console . WriteLine ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; Console . Write ( countMaxSetBits ( l , r ) ) ; } }
Find Sum of all unique sub | C # for finding sum of all unique subarray sum ; function for finding grandSum ; Go through all subarrays , compute sums and count occurrences of sums . ; Print all those sums that appear once . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findSubarraySum ( int [ ] arr , int n ) { int res = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( m . ContainsKey ( sum ) ) { m [ sum ] = m [ sum ] + 1 ; } else { m . Add ( sum , 1 ) ; } } } foreach ( KeyValuePair < int , int > x in m ) if ( x . Value == 1 ) res += x . Key ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 3 , 1 , 4 } ; int n = arr . Length ; Console . WriteLine ( findSubarraySum ( arr , n ) ) ; } }
Quickselect Algorithm | C # program of Quick Select ; partition function similar to quick sort Considers last element as pivot and adds elements with less value to the left and high value to the right and also changes the pivot position to its respective position in the readonly array . ; inserting elements of less value to the left of the pivot location ; swapping pivot to the readonly pivot location ; finds the kth position ( of the sorted array ) in a given unsorted array i . e this function can be used to find both kth largest and kth smallest element in the array . ASSUMPTION : all elements in [ ] arr are distinct ; find the partition ; if partition value is equal to the kth position , return value at k . ; if partition value is less than kth position , search right side of the array . ; if partition value is more than kth position , search left side of the array . ; Driver Code
using System ; class GFG { static int partitions ( int [ ] arr , int low , int high ) { int pivot = arr [ high ] , pivotloc = low , temp ; for ( int i = low ; i <= high ; i ++ ) { if ( arr [ i ] < pivot ) { temp = arr [ i ] ; arr [ i ] = arr [ pivotloc ] ; arr [ pivotloc ] = temp ; pivotloc ++ ; } } temp = arr [ high ] ; arr [ high ] = arr [ pivotloc ] ; arr [ pivotloc ] = temp ; return pivotloc ; } static int kthSmallest ( int [ ] arr , int low , int high , int k ) { int partition = partitions ( arr , low , high ) ; if ( partition == k ) return arr [ partition ] ; else if ( partition < k ) return kthSmallest ( arr , partition + 1 , high , k ) ; else return kthSmallest ( arr , low , partition - 1 , k ) ; } public static void Main ( String [ ] args ) { int [ ] array = { 10 , 4 , 5 , 8 , 6 , 11 , 26 } ; int [ ] arraycopy = { 10 , 4 , 5 , 8 , 6 , 11 , 26 } ; int kPosition = 3 ; int length = array . Length ; if ( kPosition > length ) { Console . WriteLine ( " Index ▁ out ▁ of ▁ bound " ) ; } else { Console . WriteLine ( " K - th ▁ smallest ▁ element ▁ in ▁ array ▁ : ▁ " + kthSmallest ( arraycopy , 0 , length - 1 , kPosition - 1 ) ) ; } } }
Recaman 's sequence | C # program to print n - th number in Recaman 's sequence ; Prints first n terms of Recaman sequence ; Create an array to store terms ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
using System ; class GFG { static void recaman ( int n ) { int [ ] arr = new int [ n ] ; arr [ 0 ] = 0 ; Console . Write ( arr [ 0 ] + " ▁ , " ) ; for ( int i = 1 ; i < n ; i ++ ) { int curr = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == curr ) curr < 0 ) { curr = arr [ i - 1 ] + i ; break ; } } arr [ i ] = curr ; Console . Write ( arr [ i ] + " , ▁ " ) ; } } public static void Main ( ) { int n = 17 ; recaman ( n ) ; } }
Recaman 's sequence | C # program to print n - th number in Recaman 's sequence ; Prints first n terms of Recaman sequence ; Print first term and store it in a hash ; Print remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void recaman ( int n ) { if ( n <= 0 ) return ; Console . Write ( " { 0 } , ▁ " , 0 ) ; HashSet < int > s = new HashSet < int > ( ) ; s . Add ( 0 ) ; int prev = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int curr = prev - i ; if ( curr < 0 || s . Contains ( curr ) ) curr = prev + i ; s . Add ( curr ) ; Console . Write ( " { 0 } , ▁ " , curr ) ; prev = curr ; } } public static void Main ( String [ ] args ) { int n = 17 ; recaman ( n ) ; } }