codeparrot/xlcost-text-to-code huggingface dataset:
Alternate Odd and Even Nodes in a Singly Linked List | C # program to rearrange nodes as alternate odd even nodes in a Singly Linked List ; Structure node ; A utility function to print linked list ; Function to create newNode in a linkedlist ; Function to insert at beginning ; Function to rearrange the odd and even nodes ; Step 1 : Segregate even and odd nodes Step 2 : Split odd and even lists Step 3 : Merge even list into odd list ; Step 1 : Segregate Odd and Even Nodes ; Backup next pointer of temp ; If temp is odd move the node to beginning of list ; Advance Temp Pointer ; Step 2 Split the List into Odd and even ; End the odd List ( Make last node null ) ; Step 3 : Merge Even List into odd ; While both lists are not exhausted Backup next pointers of i and j ; ptr points to the latest node added ; Advance i and j pointers ; Odd list exhausts before even , append remainder of even list to odd . ; The case where even list exhausts before odd list is automatically handled since we merge the even list into the odd list ; Driver Code
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static void printList ( Node node ) { while ( node != null ) { Console . Write ( node . data + " ▁ " ) ; node = node . next ; } Console . WriteLine ( ) ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } static Node insertBeg ( Node head , int val ) { Node temp = newNode ( val ) ; temp . next = head ; head = temp ; return head ; } static Node rearrange ( Node head ) { Node even ; Node temp , prev_temp ; Node i , j , k , l , ptr = null ; temp = ( head ) . next ; prev_temp = head ; while ( temp != null ) { Node x = temp . next ; if ( temp . data % 2 != 0 ) { prev_temp . next = x ; temp . next = ( head ) ; ( head ) = temp ; } else { prev_temp = temp ; } temp = x ; } temp = ( head ) . next ; prev_temp = ( head ) ; while ( temp != null && temp . data % 2 != 0 ) { prev_temp = temp ; temp = temp . next ; } even = temp ; prev_temp . next = null ; i = head ; j = even ; while ( j != null && i != null ) { k = i . next ; l = j . next ; i . next = j ; j . next = k ; ptr = j ; i = k ; j = l ; } if ( i == null ) { ptr . next = j ; } return head ; } public static void Main ( String [ ] args ) { Node head = newNode ( 8 ) ; head = insertBeg ( head , 7 ) ; head = insertBeg ( head , 6 ) ; head = insertBeg ( head , 3 ) ; head = insertBeg ( head , 5 ) ; head = insertBeg ( head , 1 ) ; head = insertBeg ( head , 2 ) ; head = insertBeg ( head , 10 ) ; Console . WriteLine ( " Linked ▁ List : " ) ; printList ( head ) ; Console . WriteLine ( " Rearranged ▁ List " ) ; head = rearrange ( head ) ; printList ( head ) ; } }
Rotate all Matrix elements except the diagonal K times by 90 degrees in clockwise direction | C # program for the above approach ; Function to print the matrix ; Iterate over the rows ; Iterate over the columns ; Print the value ; Function to perform the swapping of matrix elements in clockwise manner ; Stores the last row ; Stores the last column ; Perform the swaps ; Function to rotate non - diagonal elements of the matrix K times in clockwise direction ; Update K to K % 4 ; Iterate until K is positive ; Iterate each up to N / 2 - th row ; Iterate each column from i to N - i - 1 ; Check if the element at i , j is not a diagonal element ; Perform the swapping ; Print the matrix ; Driver Code
using System ; public class GFG { static void print ( int [ , ] mat ) { for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 1 ) ; j ++ ) Console . Write ( mat [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } static void performSwap ( int [ , ] mat , int i , int j ) { int N = mat . GetLength ( 0 ) ; int ei = N - 1 - i ; int ej = N - 1 - j ; int temp = mat [ i , j ] ; mat [ i , j ] = mat [ ej , i ] ; mat [ ej , i ] = mat [ ei , ej ] ; mat [ ei , ej ] = mat [ j , ei ] ; mat [ j , ei ] = temp ; } static void rotate ( int [ , ] mat , int N , int K ) { K = K % 4 ; while ( K -- > 0 ) { for ( int i = 0 ; i < N / 2 ; i ++ ) { for ( int j = i ; j < N - i - 1 ; j ++ ) { if ( i != j && ( i + j ) != N - 1 ) { performSwap ( mat , i , j ) ; } } } } print ( mat ) ; } public static void Main ( string [ ] args ) { int K = 5 ; int [ , ] mat = { { 1 , 2 , 3 , 4 } , { 6 , 7 , 8 , 9 } , { 11 , 12 , 13 , 14 } , { 16 , 17 , 18 , 19 } , } ; int N = mat . GetLength ( 0 ) ; rotate ( mat , N , K ) ; } }
Minimum rotations required to get the same string | C # program to determine minimum number of rotations required to yield same string . ; Returns count of rotations to get the same string back . ; tmp is the concatenated string . ; substring from i index of original string size . ; if substring matches with original string then we will come out of the loop . ; Driver Method
using System ; class GFG { static int findRotations ( String str ) { String tmp = str + str ; int n = str . Length ; for ( int i = 1 ; i <= n ; i ++ ) { String substring = tmp . Substring ( i , str . Length ) ; if ( str == substring ) return i ; } return n ; } public static void Main ( ) { String str = " abc " ; Console . Write ( findRotations ( str ) ) ; } }
Count of elements which are power of 2 in a given range subarray for Q queries | C # implementation to find elements that are a power of two ; prefix [ i ] is going to store the number of elements which are a power of two till i ( including i ) . ; Function to find the maximum range whose sum is divisible by M . ; Calculate the prefix sum ; Function to return the number of elements which are a power of two in a subarray ; Driver code
using System ; class GFG { static int MAX = 10000 ; static int [ ] prefix = new int [ MAX + 1 ] ; static bool isPowerOfTwo ( int x ) { if ( x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ) return true ; return false ; } static void computePrefix ( int n , int [ ] a ) { if ( isPowerOfTwo ( a [ 0 ] ) ) prefix [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] ; if ( isPowerOfTwo ( a [ i ] ) ) prefix [ i ] ++ ; } } static int query ( int L , int R ) { if ( L == 0 ) return prefix [ R ] ; return prefix [ R ] - prefix [ L - 1 ] ; } public static void Main ( ) { int [ ] A = { 3 , 8 , 5 , 2 , 5 , 10 } ; int N = A . Length ; computePrefix ( N , A ) ; Console . WriteLine ( query ( 0 , 4 ) ) ; Console . WriteLine ( query ( 3 , 5 ) ) ; } }
Count of integral coordinates that lies inside a Square | C # program for the above approach ; Function to calculate the integral points inside a square ; Driver code
using System ; class GFG { static void countIntgralPoints ( int x1 , int y1 , int x2 , int y2 ) { Console . WriteLine ( ( y2 - y1 - 1 ) * ( x2 - x1 - 1 ) ) ; } static void Main ( ) { int x1 = 1 , y1 = 1 ; int x2 = 4 , y2 = 4 ; countIntgralPoints ( x1 , y1 , x2 , y2 ) ; } }
Find Next number having distinct digits from the given number N | C # implementation to find the next distinct digits number ; Function to find the next distinct digits number ; Loop to find the distinct digits using hash array and the number of digits ; Loop to find the most significant distinct digit of the next number ; Condition to check if the number is possible with the same number of digits count ; Condition to check if the desired most siginificant distinct digit is found ; Loop to find the minimum next digit which is not present in the number ; Computation of the number ; Condition to check if the number is greater than the given number ; Driver Code
using System ; class GFG { static void findNextNumber ( int n ) { int [ ] h = new int [ 10 ] ; int i = 0 , msb = n , rem = 0 ; int next_num = - 1 , count = 0 ; while ( msb > 9 ) { rem = msb % 10 ; h [ rem ] = 1 ; msb /= 10 ; count ++ ; } h [ msb ] = 1 ; count ++ ; for ( i = msb + 1 ; i < 10 ; i ++ ) { if ( h [ i ] == 0 ) { next_num = i ; break ; } } if ( next_num == - 1 ) { for ( i = 1 ; i < msb ; i ++ ) { if ( h [ i ] == 0 ) { next_num = i ; count ++ ; break ; } } } if ( next_num > 0 ) { for ( i = 0 ; i < 10 ; i ++ ) { if ( h [ i ] == 0 ) { msb = i ; break ; } } for ( i = 1 ; i < count ; i ++ ) { next_num = ( ( next_num * 10 ) + msb ) ; } if ( next_num > n ) Console . WriteLine ( next_num ) ; else Console . WriteLine ( " Not ▁ Possible " ) ; } else { Console . WriteLine ( " Not ▁ Possible " ) ; } } public static void Main ( string [ ] args ) { int n = 2019 ; findNextNumber ( n ) ; } }
Find a triplet ( A , B , C ) such that 3 * A + 5 * B + 7 * C is equal to N | C # program for the above approach ; Function to find a triplet ( A , B , C ) such that 3 * A + 5 * B + 7 * C is N ; Iterate over the range [ 0 , N7 ] ; Iterate over the range [ 0 , N5 ] ; Find the value of A ; If A is greater than or equal to 0 and divisible by 3 ; Otherwise , print - 1 ; Driver Code
using System ; class GFG { static void CalculateValues ( int N ) { int A = 0 , B = 0 , C = 0 ; for ( C = 0 ; C < N / 7 ; C ++ ) { for ( B = 0 ; B < N / 5 ; B ++ ) { A = N - 7 * C - 5 * B ; if ( A >= 0 && A % 3 == 0 ) { Console . Write ( " A ▁ = ▁ " + A / 3 + " , ▁ B ▁ = ▁ " + B + " , ▁ C ▁ = ▁ " + C ) ; return ; } } } Console . WriteLine ( - 1 ) ; } static public void Main ( ) { int N = 19 ; CalculateValues ( 19 ) ; } }
Minimize total time taken by two persons to visit N cities such that none of them meet | C # program for the above approach ; Function to find the minimum time to visit all the cities such that both the person never meets ; Initialize sum as 0 ; Find the maximum element ; Traverse the array ; Increment sum by arr [ i ] ; Print maximum of 2 * T and sum ; Driver code ; Function Call
using System ; using System . Linq ; class GFG { static void minimumTime ( int [ ] arr , int n ) { int sum = 0 ; int T = arr . Min ( ) ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } Console . WriteLine ( Math . Max ( 2 * T , sum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 8 , 3 } ; int N = arr . Length ; minimumTime ( arr , N ) ; } }
Lexicographically largest string possible by reversing substrings having even number of 1 s | C # program for the above approach ; Function to find the lexicographically maximum string by reversing substrings having even numbers of 1 s ; Store size of string ; Traverse the string ; Count the number of 1 s ; Stores the starting index ; Stores the end index ; Increment count , when 1 is encountered ; Traverse the remaining string ; Reverse the string from starting and end index ; Printing the string ; Driver Code
using System ; using System . Text ; class GFG { static void lexicographicallyMax ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; int beg = i ; int end = i ; if ( s [ i ] == '1' ) count ++ ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( s [ j ] == '1' ) count ++ ; if ( count % 2 == 0 && count != 0 ) { end = j ; break ; } } s = reverse ( s , beg , end + 1 ) ; } Console . WriteLine ( s ) ; } static String reverse ( String s , int beg , int end ) { StringBuilder x = new StringBuilder ( " " ) ; for ( int i = 0 ; i < beg ; i ++ ) x . Append ( s [ i ] ) ; for ( int i = end - 1 ; i >= beg ; i -- ) x . Append ( s [ i ] ) ; for ( int i = end ; i < s . Length ; i ++ ) x . Append ( s [ i ] ) ; return x . ToString ( ) ; } public static void Main ( String [ ] args ) { String S = "0101" ; lexicographicallyMax ( S ) ; } }
Count maximum possible pairs from an array having sum K | C # program for the above approach ; Function to count the maximum number of pairs from given array with sum K ; Sort array in increasing order ; Stores the final result ; Initialize the left and right pointers ; Traverse array until start < end ; Decrement right by 1 ; Increment left by 1 ; Increment result and left pointer by 1 and decrement right pointer by 1 ; Print the result ; Driver Code ; Function Call
using System ; class GFG { public static void maxPairs ( int [ ] nums , int k ) { Array . Sort ( nums ) ; int result = 0 ; int start = 0 , end = nums . Length - 1 ; while ( start < end ) { if ( nums [ start ] + nums [ end ] > k ) end -- ; else if ( nums [ start ] + nums [ end ] < k ) start ++ ; else { start ++ ; end -- ; result ++ ; } } Console . Write ( result ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 5 ; maxPairs ( arr , K ) ; } }
Count maximum possible pairs from an array having sum K | C # program for the above approach ; Function to find the maximum number of pairs with a sum K such that same element can 't be used twice ; Initialize a hashmap ; Store the readonly result ; Iterate over the array nums [ ] ; Decrement its frequency in map and increment the result by 1 ; Increment its frequency by 1 if it is already present in map . Otherwise , set its frequency to 1 ; Print the result ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { public static void maxPairs ( int [ ] nums , int k ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int result = 0 ; foreach ( int i in nums ) { if ( map . ContainsKey ( i ) && map [ i ] > 0 ) { map [ i ] = map [ i ] - 1 ; result ++ ; } else { if ( ! map . ContainsKey ( k - i ) ) map . Add ( k - i , 1 ) ; else map [ i ] = map [ i ] + 1 ; } } Console . WriteLine ( result ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 5 ; maxPairs ( arr , K ) ; } }
Print indices of array elements whose removal makes the sum of odd and even | C # program to implement the above approach ; Function to find indices of array elements whose removal makes the sum of odd and even indexed array elements equal ; Stores size of array ; Store prefix sum of odd index array elements ; Store prefix sum of even index array elements ; Update even [ 0 ] ; Traverse the given array ; Update odd [ i ] ; Update even [ i ] ; If the current index is an even number ; Update even [ i ] ; If the current index is an odd number ; Update odd [ i ] ; Check if at least one index found or not that satisfies the condition ; Store odd indices sum by removing 0 - th index ; Store even indices sum by removing 0 - th index ; If p and q are equal ; Traverse the array arr [ ] ; If i is an even number ; Update p by removing the i - th element ; Update q by removing the i - th element ; Update q by removing the i - th element ; Update p by removing the i - th element ; If odd index values sum is equal to even index values sum ; Set the find variable ; Print the current index ; If no index found ; Print not possible ; Driver Code
using System ; class GFG { static void removeIndicesToMakeSumEqual ( int [ ] arr ) { int N = arr . Length ; int [ ] odd = new int [ N ] ; int [ ] even = new int [ N ] ; even [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { odd [ i ] = odd [ i - 1 ] ; even [ i ] = even [ i - 1 ] ; if ( i % 2 == 0 ) { even [ i ] += arr [ i ] ; } else { odd [ i ] += arr [ i ] ; } } bool find = false ; int p = odd [ N - 1 ] ; int q = even [ N - 1 ] - arr [ 0 ] ; if ( p == q ) { Console . Write ( "0 ▁ " ) ; find = true ; } for ( int i = 1 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { p = even [ N - 1 ] - even [ i - 1 ] - arr [ i ] + odd [ i - 1 ] ; q = odd [ N - 1 ] - odd [ i - 1 ] + even [ i - 1 ] ; } else { q = odd [ N - 1 ] - odd [ i - 1 ] - arr [ i ] + even [ i - 1 ] ; p = even [ N - 1 ] - even [ i - 1 ] + odd [ i - 1 ] ; } if ( p == q ) { find = true ; Console . Write ( i + " ▁ " ) ; } } if ( ! find ) { Console . Write ( - 1 ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 6 , 2 } ; removeIndicesToMakeSumEqual ( arr ) ; } }
Minimum removals required to make a given array Bitonic | C # program to implement the above approach ; Function to coutnt minimum array elements required to be removed to make an array bitonic ; left [ i ] : Stores the length of LIS up to i - th index ; right [ i ] : Stores the length of decreasing subsequence over the range [ i , N ] ; Calculate the length of LIS up to i - th index ; Traverse the array upto i - th index ; If arr [ j ] is less than arr [ i ] ; Update left [ i ] ; Calculate the length of decreasing subsequence over the range [ i , N ] ; Traverse right [ ] array ; If arr [ i ] is greater than arr [ j ] ; Update right [ i ] ; Stores length of the longest bitonic array ; Traverse left [ ] and right [ ] array ; Update maxLen ; Function to print minimum removals required to make given array bitonic ; Driver Code
using System ; class GFG { static void min_element_removal ( int [ ] arr , int N ) { int [ ] left = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) left [ i ] = 1 ; int [ ] right = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) right [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { left [ i ] = Math . Max ( left [ i ] , left [ j ] + 1 ) ; } } } for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j > i ; j -- ) { if ( arr [ i ] > arr [ j ] ) { right [ i ] = Math . Max ( right [ i ] , right [ j ] + 1 ) ; } } } int maxLen = 0 ; for ( int i = 1 ; i < N - 1 ; i ++ ) { maxLen = Math . Max ( maxLen , left [ i ] + right [ i ] - 1 ) ; } Console . WriteLine ( N - maxLen ) ; } static void makeBitonic ( int [ ] arr , int N ) { if ( N == 1 ) { Console . WriteLine ( "0" ) ; return ; } if ( N == 2 ) { if ( arr [ 0 ] != arr [ 1 ] ) Console . WriteLine ( "0" ) ; else Console . WriteLine ( "1" ) ; return ; } min_element_removal ( arr , N ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 } ; int N = arr . Length ; makeBitonic ( arr , N ) ; } }
Count subarrays having an equal count of 0 s and 1 s segregated | C # program for the above approach ; Function to count subarrays having equal count of 0 s and 1 s with all 0 s and all 1 s grouped together ; Stores the count of subarrays ; If current element is different from the next array element ; Increment count ; Count the frequency of 1 s and 0 s ; Increment count ; Print the final count ; Driver Code ; Function Call
using System ; class GFG { static void countSubarrays ( int [ ] A , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] != A [ i + 1 ] ) { ans ++ ; for ( int j = i - 1 , k = i + 2 ; j >= 0 && k < N && A [ j ] == A [ i ] && A [ k ] == A [ i + 1 ] ; j -- , k ++ ) { ans ++ ; } } } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( ) { int [ ] A = { 1 , 1 , 0 , 0 , 1 , 0 } ; int N = A . Length ; countSubarrays ( A , N ) ; } }
Count quadruples of given type from given array | C # program of the above approach ; lcount [ i , j ] : Stores the count of i on left of index j ; rcount [ i , j ] : Stores the count of i on right of index j ; Function to count unique elements on left and right of any index ; Find the maximum array element ; Calculate prefix sum of counts of each value ; Calculate suffix sum of counts of each value ; Function to count quadruples of the required type ; Driver Code
using System ; class GFG { static int maxN = 2002 ; static int [ , ] lcount = new int [ maxN , maxN ] ; static int [ , ] rcount = new int [ maxN , maxN ] ; static void fill_counts ( int [ ] a , int n ) { int i , j ; int maxA = a [ 0 ] ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maxA ) { maxA = a [ i ] ; } } for ( i = 0 ; i < n ; i ++ ) { lcount [ a [ i ] , i ] = 1 ; rcount [ a [ i ] , i ] = 1 ; } for ( i = 0 ; i <= maxA ; i ++ ) { for ( j = 1 ; j < n ; j ++ ) { lcount [ i , j ] = lcount [ i , j - 1 ] + lcount [ i , j ] ; } for ( j = n - 2 ; j >= 0 ; j -- ) { rcount [ i , j ] = rcount [ i , j + 1 ] + rcount [ i , j ] ; } } } static int countSubsequence ( int [ ] a , int n ) { int i , j ; fill_counts ( a , n ) ; int answer = 0 ; for ( i = 1 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n - 1 ; j ++ ) { answer += lcount [ a [ j ] , i - 1 ] * rcount [ a [ i ] , j + 1 ] ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , 2 , 1 , 3 , 2 } ; Console . Write ( countSubsequence ( a , a . Length ) ) ; } }
Reduce string by removing outermost parenthesis from each primitive substring | C # program to implement the above approach ; Function to remove the outermost parentheses of every primitive substring from the given string ; Stores the resultant string ; Stores the count of opened parentheses ; Traverse the string ; If opening parenthesis is encountered and their count exceeds 0 ; Include the character ; If closing parenthesis is encountered and their count is less than count of opening parentheses ; Include the character ; Return the resultant string ; Driver Code
using System ; class GFG { static string removeOuterParentheses ( string S ) { string res = " " ; int count = 0 ; for ( int c = 0 ; c < S . Length ; c ++ ) { if ( S == ' ( ' && count ++ > 0 ) res += S ; if ( S == ' ) ' && count -- > 1 ) res += S ; } return res ; } public static void Main ( ) { string S = " ( ( ) ( ) ) ( ( ) ) ( ) " ; Console . Write ( removeOuterParentheses ( S ) ) ; } }
Length of longest subarray with increasing contiguous elements | C # implementation for the above approach ; Function to find the longest subarray with increasing contiguous elements ; Stores the length of required longest subarray ; Stores the length of length of longest such subarray from ith index ; If consecutive elements are increasing and differ by 1 ; Otherwise ; Update the longest subarray obtained so far ; Return the length obtained ; Driver Code
using System ; class GFG { public static int maxiConsecutiveSubarray ( int [ ] arr , int N ) { int maxi = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int cnt = 1 , j ; for ( j = i ; j < N - 1 ; j ++ ) { if ( arr [ j + 1 ] == arr [ j ] + 1 ) { cnt ++ ; } else { break ; } } maxi = Math . Max ( maxi , cnt ) ; i = j ; } return maxi ; } public static void Main ( String [ ] args ) { int N = 11 ; int [ ] arr = { 1 , 3 , 4 , 2 , 3 , 4 , 2 , 3 , 5 , 6 , 7 } ; Console . WriteLine ( maxiConsecutiveSubarray ( arr , N ) ) ; } }
Length of longest subsequence having sum of digits of each element as a Composite Number | C # implementation of the above approach ; Function to generate prime numbers using Sieve of Eratosthenes ; Set 0 and 1 as non - prime ; If p is a prime ; Set all multiples of p as non - prime ; Function to find the digit sum of a given number ; Stores the sum of digits ; Extract digits and add to the sum ; Return the sum of the digits ; Function to find the longest subsequence with sum of digits of each element equal to a composite number ; Calculate sum of digits of current array element ; If sum of digits equal to 1 ; If sum of digits is a prime ; Driver Code ; Function call
using System . Collections . Generic ; using System ; class GFG { static int N = 100005 ; static void SieveOfEratosthenes ( bool [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int digitSum ( int number ) { int sum = 0 ; while ( number > 0 ) { sum += ( number % 10 ) ; number /= 10 ; } return sum ; } static void longestCompositeDigitSumSubsequence ( int [ ] arr , int n ) { int count = 0 ; bool [ ] prime = new bool [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , N ) ; for ( int i = 0 ; i < n ; i ++ ) { int res = digitSum ( arr [ i ] ) ; if ( res == 1 ) { continue ; } if ( prime [ res ] == false ) { count ++ ; } } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 13 , 55 , 7 , 3 , 5 , 1 , 10 , 21 , 233 , 144 , 89 } ; int n = arr . Length ; longestCompositeDigitSumSubsequence ( arr , n ) ; } }