codeparrot/xlcost-text-to-code huggingface dataset:
Smallest number exceeding N whose Kth bit is set | C # program for the above approach ; Function to find the number greater than n whose Kth bit is set ; Stores the resultant number ; If Kth bit is not set ; cur will be the sum of all powers of 2 < k ; If the current bit is set ; Add Kth power of 2 to n and subtract the all powers of 2 less than K that are set ; If the kth bit is set ; First unset bit position ; Sum of bits that are set ; Add Kth power of 2 to n and subtract the all powers of 2 less than K that are set ; If Kth bit became unset then set it again ; Return the resultant number ; Driver Code ; Print ans
using System ; class GFG { static int find_next ( int n , int k ) { int ans = 0 ; if ( ( n & ( 1L << k ) ) == 0 ) { int cur = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( ( n & ( 1L << i ) ) > 0 ) cur += ( int ) 1L << i ; } ans = ( int ) ( n - cur + ( 1L << k ) ) ; } else { int first_unset_bit = - 1 , cur = 0 ; for ( int i = 0 ; i < 64 ; i ++ ) { if ( ( n & ( 1L << i ) ) == 0 ) { first_unset_bit = i ; break ; } else cur += ( int ) ( 1L << i ) ; } ans = ( int ) ( n - cur + ( 1L << first_unset_bit ) ) ; if ( ( ans & ( 1L << k ) ) == 0 ) ans += ( int ) ( 1L << k ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 15 , K = 2 ; Console . Write ( find_next ( N , K ) ) ; } }
Lexicographically largest possible String after removal of K characters | C # program to implement the above approach ; Final result String ; If the current char exceeds the character at the top of the stack ; Remove from the end of the String ; Decrease k for the removal ; Insert current character ; Perform remaining K deletions from the end of the String ; Return the String ; Driver Code
using System ; class GFG { static String largestString ( String num , int k ) { String ans = " " ; foreach ( char i in num . ToCharArray ( ) ) { while ( ans . Length > 0 && ans [ ans . Length - 1 ] < i && k > 0 ) { ans = ans . Substring ( 0 , ans . Length - 1 ) ; k -- ; } ans += i ; } while ( ans . Length > 0 && k -- > 0 ) { ans = ans . Substring ( 0 , ans . Length - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { String str = " zyxedcba " ; int k = 1 ; Console . Write ( largestString ( str , k ) + " STRNEWLINE " ) ; } }
Maximum length of subarray consisting of same type of element on both halves of sub | C # program for the above approach ; Function that finds the maximum length of the sub - array that contains equal element on both halves of sub - array ; To store continuous occurence of the element ; To store continuous forkward occurence ; To store continuous backward occurence ; To store the maximum length ; Find maximum length ; Print the result ; Driver Code ; Given array ; Size of the array ; Function call
using System ; class GFG { static void maxLengthSubArray ( int [ ] A , int N ) { int [ ] forward = new int [ N ] ; int [ ] backward = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == 0 A [ i ] != A [ i - 1 ] ) { forward [ i ] = 1 ; } else forward [ i ] = forward [ i - 1 ] + 1 ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( i == N - 1 A [ i ] != A [ i + 1 ] ) { backward [ i ] = 1 ; } else backward [ i ] = backward [ i + 1 ] + 1 ; } int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] != A [ i + 1 ] ) ans = Math . Max ( ans , Math . Min ( forward [ i ] , backward [ i + 1 ] ) * 2 ) ; } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 4 , 4 , 6 , 6 , 6 , 9 } ; int N = arr . Length ; maxLengthSubArray ( arr , N ) ; } }
Smallest N digit number divisible by all possible prime digits | C # implementation of the above approach ; Function to find the minimum number of n digits divisible by all prime digits ; Driver code
using System ; class GFG { static void minNum ( int n ) { if ( n < 3 ) Console . WriteLine ( - 1 ) ; else Console . WriteLine ( 210 * ( ( int ) ( Math . Pow ( 10 , n - 1 ) / 210 ) + 1 ) ) ; } public static void Main ( String [ ] args ) { int n = 5 ; minNum ( n ) ; } }
Smallest number greater than Y with sum of digits equal to X | C # program for the above approach ; Function to return the minimum string of length d having the sum of digits s ; Return a string of length d ; Greedily put 9 's in the end ; Put remaining sum ; Function to find the smallest number greater than Y whose sum of digits is X ; Convert number y to string ; Maintain prefix sum of digits ; Iterate over Y from the back where k is current length of suffix ; Stores current digit ; Increase current digit ; Sum upto current prefix ; Return answer if remaining sum can be obtained in suffix ; Find suffix of length k having sum of digits x - r ; Append current character ; Return the result ; Driver code ; Given number and sum ; Function call
using System ; using System . Text ; using System . Collections ; class GFG { static string helper ( int d , int s ) { StringBuilder ans = new StringBuilder ( ) ; for ( int i = 0 ; i < d ; i ++ ) { ans . Append ( "0" ) ; } for ( int i = d - 1 ; i >= 0 ; i -- ) { if ( s >= 9 ) { ans [ i ] = '9' ; s -= 9 ; } else { char c = ( char ) ( s + ( int ) '0' ) ; ans [ i ] = c ; s = 0 ; } } return ans . ToString ( ) ; } static string findMin ( int x , int Y ) { string y = Y . ToString ( ) ; int n = y . Length ; ArrayList p = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { p . Add ( 0 ) ; } for ( int i = 0 ; i < n ; i ++ ) { p [ i ] = ( int ) ( ( int ) y [ i ] - ( int ) '0' ) ; if ( i > 0 ) { p [ i ] = ( int ) p [ i ] + ( int ) p [ i - 1 ] ; } } for ( int i = n - 1 , k = 0 ; ; i -- , k ++ ) { int d = 0 ; if ( i >= 0 ) { d = ( int ) y [ i ] - ( int ) '0' ; } for ( int j = d + 1 ; j <= 9 ; j ++ ) { int r = j ; if ( i > 0 ) { r += ( int ) p [ i - 1 ] ; } if ( x - r >= 0 && x - r <= 9 * k ) { string suf = helper ( k , x - r ) ; string pre = " " ; if ( i > 0 ) pre = y . Substring ( 0 , i ) ; char cur = ( char ) ( j + ( int ) '0' ) ; pre += cur ; return pre + suf ; } } } } public static void Main ( string [ ] arg ) { int x = 18 ; int y = 99 ; Console . Write ( findMin ( x , y ) ) ; } }
Largest number made up of X and Y with count of X divisible by Y and of Y by X | C # program to implement the above approach ; Function to generate and return the largest number ; Store the smaller in Y ; Store the larger in X ; Stores respective counts ; If N is divisible by Y ; Append X , N times to the answer ; Reduce N to zero ; Reduce N by X ; Append Y , X times to the answer ; If number can be formed ; Otherwise ; Driver code
using System ; class GFG { public static void largestNumber ( int n , int X , int Y ) { int maxm = Math . Max ( X , Y ) ; Y = X + Y - maxm ; X = maxm ; int Xs = 0 ; int Ys = 0 ; while ( n > 0 ) { if ( n % Y == 0 ) { Xs += n ; n = 0 ; } else { n -= X ; Ys += X ; } } if ( n == 0 ) { while ( Xs -- > 0 ) Console . Write ( X ) ; while ( Ys -- > 0 ) Console . Write ( Y ) ; } else Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int n = 19 , X = 7 , Y = 5 ; largestNumber ( n , X , Y ) ; } }
Minimum flips required to generate continuous substrings of 0 â €™ s and 1 â €™ s | C # implementation of the above approach ; Traverse input string and store the count of 0 ; Traverse the input string again to find minimum number of flips ; Driver code
using System ; class GFG { static int minChanges ( String str , int N ) { int res ; int count0 = 0 , count1 = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '0' ) count0 ++ ; } res = count0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '0' ) count0 -- ; if ( str [ i ] == '1' ) count1 ++ ; res = Math . Min ( res , count1 + count0 ) ; } return res ; } public static void Main ( ) { int N = 9 ; String str = "000101001" ; Console . Write ( minChanges ( str , N ) ) ; } }
Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum | C # implementation of the missing number such that maximum absolute difference between adjacent element is minimum ; Function to find the missing number such that maximum absolute difference is minimum ; Loop to find the maximum and minimum adjacent element to missing number ; Driver Code ; Function Call
using System ; class GFG { static int missingnumber ( int n , int [ ] arr ) { int mn = Int32 . MaxValue , mx = Int32 . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 && arr [ i ] == - 1 && arr [ i - 1 ] != - 1 ) { mn = Math . Min ( mn , arr [ i - 1 ] ) ; mx = Math . Max ( mx , arr [ i - 1 ] ) ; } if ( i < ( n - 1 ) && arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { mn = Math . Min ( mn , arr [ i + 1 ] ) ; mx = Math . Max ( mx , arr [ i + 1 ] ) ; } } int res = ( mx + mn ) / 2 ; return res ; } public static void Main ( ) { int n = 5 ; int [ ] arr = new int [ ] { - 1 , 10 , - 1 , 12 , - 1 } ; int res = missingnumber ( n , arr ) ; Console . WriteLine ( res ) ; } }
Maximize [ length ( X ) / 2 ^ ( XOR ( X , Y ) ) ] by choosing substrings X and Y from string A and B respectively | C # program for the above approach ; Function to find the length of the longest common substring of the string X and Y ; LCSuff [ i ] [ j ] stores the lengths of the longest common suffixes of substings ; Itearate over strings A and B ; If first row or column ; If matching is found ; Otherwise , if matching is not found ; Finally , return the resultant maximum value LCS ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int lcsubtr ( char [ ] a , char [ ] b , int length1 , int length2 ) { int [ , ] dp = new int [ length1 + 1 , length2 + 1 ] ; int max = 0 ; for ( int i = 0 ; i <= length1 ; ++ i ) { for ( int j = 0 ; j <= length2 ; ++ j ) { if ( i == 0 j == 0 ) { dp [ i , j ] = 0 ; } else if ( a [ i - 1 ] == b [ j - 1 ] ) { dp [ i , j ] = dp [ i - 1 , j - 1 ] + 1 ; max = Math . Max ( dp [ i , j ] , max ) ; } else { dp [ i , j ] = 0 ; } } } return max ; } public static void Main ( ) { string m = "0110" ; string n = "1101" ; char [ ] m1 = m . ToCharArray ( ) ; char [ ] m2 = n . ToCharArray ( ) ; Console . Write ( lcsubtr ( m1 , m2 , m1 . Length , m2 . Length ) ) ; } }
Count ways to split array into pair of subsets with difference between their sum equal to K | C # program for the above approach ; To store the states of DP ; Function to find count of subsets with a given sum ; Base case ; If an already computed subproblem occurs ; Set the state as solved ; Recurrence relation ; Function to count ways to split array into pair of subsets with difference K ; Store the total sum of element of the array ; Traverse the array ; Calculate sum of array elements ; Store the required sum ; Print the number of subsets with sum equal to S1 ; Driver code ; Function Call
using System ; class GFG { static int maxN = 20 ; static int maxSum = 50 ; static int minSum = 50 ; static int Base = 50 ; static int [ , ] dp = new int [ maxN , maxSum + minSum ] ; static bool [ , ] v = new bool [ maxN , maxSum + minSum ] ; static int findCnt ( int [ ] arr , int i , int required_sum , int n ) { if ( i == n ) { if ( required_sum == 0 ) return 1 ; else return 0 ; } if ( v [ i , required_sum + Base ] ) return dp [ i , required_sum + Base ] ; v [ i , required_sum + Base ] = true ; dp [ i , required_sum + Base ] = findCnt ( arr , i + 1 , required_sum , n ) + findCnt ( arr , i + 1 , required_sum - arr [ i ] , n ) ; return dp [ i , required_sum + Base ] ; } static void countSubsets ( int [ ] arr , int K , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int S1 = ( sum + K ) / 2 ; Console . Write ( findCnt ( arr , 0 , S1 , n ) ) ; } static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 3 } ; int N = arr . Length ; int K = 1 ; countSubsets ( arr , K , N ) ; } }
Probability that the sum of all numbers obtained on throwing a dice N times lies between two given integers | C # program for above approach ; Function to calculate the probability for the given sum to be equal to sum in N throws of dice ; Base cases ; Driver Code ; Calculate probability of all sums from a to b ; Print the answer
using System ; using System . Collections . Generic ; public class GFG { static float [ , ] dp = new float [ 105 , 605 ] ; static float find ( int N , int sum ) { if ( N < 0 sum < 0 ) return 0 ; if ( dp [ N , sum ] > 0 ) return dp [ N , sum ] ; if ( sum > 6 * N sum < N ) return 0 ; if ( N == 1 ) { if ( sum >= 1 && sum <= 6 ) return ( float ) ( 1.0 / 6 ) ; else return 0 ; } for ( int i = 1 ; i <= 6 ; i ++ ) dp [ N , sum ] = dp [ N , sum ] + find ( N - 1 , sum - i ) / 6 ; return dp [ N , sum ] ; } public static void Main ( String [ ] args ) { int N = 4 , a = 13 , b = 17 ; float probability = 0.0f ; for ( int sum = a ; sum <= b ; sum ++ ) probability = probability + find ( N , sum ) ; Console . Write ( " { 0 : F6 } " , probability ) ; } }
Minimum steps to reduce N to 0 by given operations | C # program for the above approach ; Function to find the minimum number to steps to reduce N to 0 ; Dictionary for storing the precomputed sum ; Bases Cases ; Check if n is not in dp then only call the function so as to reduce no of recursive calls ; Return the answer ; Driver Code ; Given number N ; Function call
using System ; using System . Collections . Generic ; public class GFG { static int count ( int n ) { Dictionary < int , int > dp = new Dictionary < int , int > ( ) ; dp . Add ( 0 , 0 ) ; dp . Add ( 1 , 1 ) ; if ( ! dp . ContainsKey ( n ) ) dp . Add ( n , 1 + Math . Min ( n % 2 + count ( n / 2 ) , n % 3 + count ( n / 3 ) ) ) ; return dp [ n ] ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . WriteLine ( String . Join ( " " , ( count ( N ) ) ) ) ; } }
Minimum count of increment of K size subarrays required to form a given Array | C # implementation to find the minimum number of operations required to change an array of all zeros such that every element is greater than the given array ; Function to find the minimum number of operations required to change all the array of zeros such that every element is greater than the given array ; Declaring the difference array of size N ; Number of operations ; First update the D [ i ] value with the previous value ; The index i has to be incremented ; We have to perform ( b [ i ] - d [ i ] ) operations more ; Increment the range i to i + k by need ; Check if i + k is valid index ; Driver Code ; Function Call
using System ; class GFG { static void find_minimum_operations ( int n , int [ ] b , int k ) { int [ ] d = new int [ n + 1 ] ; int i , operations = 0 , need ; for ( i = 0 ; i < n ; i ++ ) { if ( i > 0 ) { d [ i ] += d [ i - 1 ] ; } if ( b [ i ] > d [ i ] ) { operations += b [ i ] - d [ i ] ; need = b [ i ] - d [ i ] ; d [ i ] += need ; if ( i + k <= n ) { d [ i + k ] -= need ; } } } Console . Write ( operations ) ; } public static void Main ( string [ ] args ) { int n = 5 ; int [ ] b = { 1 , 2 , 3 , 4 , 5 } ; int k = 2 ; find_minimum_operations ( n , b , k ) ; } }
Number of ways of cutting a Matrix such that atleast one cell is filled in each part | C # implementation to find the number of ways to cut the matrix into the K parts such that each part have atleast one filled cell ; Function to find the number of ways to cut the matrix into the K parts such that each part have atleast one filled cell ; Loop to find prefix sum of the given matrix ; dp ( r , c , 1 ) = 1 if preSum [ r ] else 0 ; Loop to iterate over the dp table of the given matrix ; Check if can cut horizontally at r1 , at least one apple in matrix ( r , c ) -> r1 , C - 1 ; Check if we can cut vertically at c1 , at least one apple in matrix ( r , c ) -> R - 1 , c1 ; Driver code ; Function Call
using System ; class GFG { static int ways ( int [ , ] arr , int K ) { int R = arr . GetLength ( 0 ) ; int C = arr . GetLength ( 1 ) ; int [ , ] preSum = new int [ R , C ] ; for ( int r = R - 1 ; r >= 0 ; r -- ) { for ( int c = C - 1 ; c >= 0 ; c -- ) { preSum [ r , c ] = arr [ r , c ] ; if ( r + 1 < R ) preSum [ r , c ] += preSum [ r + 1 , c ] ; if ( c + 1 < C ) preSum [ r , c ] += preSum [ r , c + 1 ] ; if ( r + 1 < R && c + 1 < C ) preSum [ r , c ] -= preSum [ r + 1 , c + 1 ] ; } } int [ , , ] dp = new int [ K + 1 , R , C ] ; for ( int k = 1 ; k <= K ; k ++ ) { for ( int r = R - 1 ; r >= 0 ; r -- ) { for ( int c = C - 1 ; c >= 0 ; c -- ) { if ( k == 1 ) { dp [ k , r , c ] = ( preSum [ r , c ] > 0 ) ? 1 : 0 ; } else { dp [ k , r , c ] = 0 ; for ( int r1 = r + 1 ; r1 < R ; r1 ++ ) { if ( preSum [ r , c ] - preSum [ r1 , c ] > 0 ) dp [ k , r , c ] += dp [ k - 1 , r1 , c ] ; } for ( int c1 = c + 1 ; c1 < C ; c1 ++ ) { if ( preSum [ r , c ] - preSum [ r , c1 ] > 0 ) dp [ k , r , c ] += dp [ k - 1 , r , c1 ] ; } } } } } return dp [ K , 0 , 0 ] ; } public static void Main ( string [ ] args ) { int [ , ] arr = { { 1 , 0 , 0 } , { 1 , 1 , 1 } , { 0 , 0 , 0 } } ; int k = 3 ; Console . WriteLine ( ways ( arr , k ) ) ; } }
Product of all sorted subsets of size K using elements whose index divide K completely | C # implementation of the above approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; If y is odd , multiply x with result ; y must be even now ; Iterative Function to calculate ( nCr ) % p and save in f [ n , r ] ; If j > i then C ( i , j ) = 0 ; If iis equal to j then C ( i , j ) = 1 ; Function calculate the Final answer ; Initialize ans ; x is count of occurence of arr [ i ] in different set such that index of arr [ i ] in those sets divides K completely . ; Finding the count of arr [ i ] by placing it at index which divides K completely ; By Fermat 's theorem ; Driver code
using System ; class GFG { static int p = 1000000007 ; static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void nCr ( int n , int p , int [ , ] f , int m ) { for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( j > i ) { f [ i , j ] = 0 ; } else if ( j == 0 j == i ) { f [ i , j ] = 1 ; } else { f [ i , j ] = ( f [ i - 1 , j ] + f [ i - 1 , j - 1 ] ) % p ; } } } } static void ProductOfSubsets ( int [ ] arr , int n , int m ) { int [ , ] f = new int [ n + 1 , 100 ] ; nCr ( n , p - 1 , f , m ) ; Array . Sort ( arr ) ; long ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int x = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { if ( m % j == 0 ) { x = ( x + ( f [ n - i - 1 , m - j ] * f [ i , j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ; } } ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) ; } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 5 , 7 , 9 , 3 } ; int K = 4 ; int N = arr . Length ; ProductOfSubsets ( arr , N , K ) ; } }
Number of ways to write N as a sum of K non | C # program for the above approach ; Function to count the number of ways to write N as sum of k non - negative integers ; Initialise [ , ] dp array ; Only 1 way to choose the value with sum K ; Initialise sum ; Count the ways from previous states ; Update the sum ; Return the readonly count of ways ; Driver Code ; Function call
using System ; class GFG { static int countWays ( int n , int m ) { int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { dp [ 1 , i ] = 1 ; } int sum ; for ( int i = 2 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { sum = 0 ; for ( int k = 0 ; k <= j ; k ++ ) { sum += dp [ i - 1 , k ] ; } dp [ i , j ] = sum ; } } return dp [ m , n ] ; } public static void Main ( String [ ] args ) { int N = 2 , K = 3 ; Console . Write ( countWays ( N , K ) ) ; } }
Number of ways to write N as a sum of K non | C # program for the above approach ; Function to count the number of ways to write N as sum of k non - negative integers ; Initialise dp [ ] [ ] array ; Fill the dp [ ] [ ] with sum = m ; Iterate the dp [ ] [ ] to fill the dp [ ] [ ] array ; Condition for first column ; Else fill the dp [ ] [ ] with sum till ( i , j ) ; If reach the end , then return the value ; Update at current index ; Driver Code ; Function call
using System ; class GFG { static int countWays ( int n , int m ) { int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { dp [ 1 , i ] = 1 ; if ( i != 0 ) { dp [ 1 , i ] += dp [ 1 , i - 1 ] ; } } for ( int i = 2 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( j == 0 ) { dp [ i , j ] = dp [ i - 1 , j ] ; } else { dp [ i , j ] = dp [ i - 1 , j ] ; if ( i == m && j == n ) { return dp [ i , j ] ; } dp [ i , j ] += dp [ i , j - 1 ] ; } } } return Int32 . MinValue ; } public static void Main ( ) { int N = 2 , K = 3 ; Console . Write ( countWays ( N , K ) ) ; } }
Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime | C # program to implement the above approach ; Function to pre - store primes ; Sieve method to check if prime or not ; Multiples ; Pre - store all the primes ; Function to find the intest subsequence ; Hash map ; Call the function to pre - store the primes ; Initialize last element with 1 as that is the intest possible ; Iterate from the back and find the intest ; Get the number ; Initialize dp [ i ] as 1 as the element will only me in the subsequence . ; Iterate in all the primes and multiply to get the next element ; Next element if multiplied with it ; If exceeds the last element then break ; If the number is there in the array ; Get the maximum most element ; Hash the element ; Find the intest ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static void SieveOfEratosthenes ( int MAX , List < int > primes ) { Boolean [ ] prime = new Boolean [ MAX + 1 ] ; for ( int i = 0 ; i < MAX + 1 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= MAX ; i += p ) prime [ i ] = false ; } } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] ) primes . Add ( i ) ; } } public static int findLongest ( int [ ] A , int n ) { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; List < int > primes = new List < int > ( ) ; SieveOfEratosthenes ( A [ n - 1 ] , primes ) ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 1 ; mpp . Add ( A [ n - 1 ] , n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int num = A [ i ] ; dp [ i ] = 1 ; foreach ( int it in primes ) { int xx = num * it ; if ( xx > A [ n - 1 ] ) break ; else if ( mpp . ContainsKey ( xx ) && mpp [ xx ] != 0 ) { dp [ i ] = Math . Max ( dp [ i ] , 1 + dp [ mpp [ xx ] ] ) ; } } if ( mpp . ContainsKey ( A [ i ] ) ) mpp [ A [ i ] ] = i ; else mpp . Add ( A [ i ] , i ) ; } int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) ans = Math . Max ( ans , dp [ i ] ) ; return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 5 , 6 , 12 , 35 , 60 , 385 } ; int n = a . Length ; Console . WriteLine ( findLongest ( a , n ) ) ; } }