codeparrot/xlcost-text-to-code huggingface dataset:
Minimum time taken by each job to be completed given by a Directed Acyclic Graph | C # program for the above approach ; Adjacency List to store the graph ; Array to store the in - degree of node ; Array to store the time in which the job i can be done ; Function to add directed edge between two vertices ; Insert edge from u to v ; Increasing the indegree of vertex v ; Function to find the minimum time needed by each node to get the task ; Queue to store the nodes while processing ; Update the time of the jobs who don 't require any job to  be completed before this job ; Iterate until queue is empty ; Get front element of queue ; Pop the front element ; Decrease in - degree of the current node ; Push its adjacent elements ; Print the time to complete the job ; Driver Code ; Given Nodes N and edges M ; Given directed edges of graph ; Function call
using System ; using System . Collections . Generic ; class GFG { static readonly int maxN = 100000 ; static List < int > [ ] graph = new List < int > [ maxN ] ; static int [ ] indegree = new int [ maxN ] ; static int [ ] job = new int [ maxN ] ; static void addEdge ( int u , int v ) { graph [ u ] . Add ( v ) ; indegree [ v ] ++ ; } static void printOrder ( int n , int m ) { Queue < int > q = new Queue < int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( indegree [ i ] == 0 ) { q . Enqueue ( i ) ; job [ i ] = 1 ; } } while ( q . Count != 0 ) { int cur = q . Peek ( ) ; q . Dequeue ( ) ; foreach ( int adj in graph [ cur ] ) { indegree [ adj ] -- ; if ( indegree [ adj ] == 0 ) { job [ adj ] = 1 + job [ cur ] ; q . Enqueue ( adj ) ; } } } for ( int i = 1 ; i <= n ; i ++ ) Console . Write ( job [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int n , m ; n = 10 ; m = 13 ; for ( int i = 0 ; i < graph . Length ; i ++ ) graph [ i ] = new List < int > ( ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 1 , 5 ) ; addEdge ( 2 , 3 ) ; addEdge ( 2 , 8 ) ; addEdge ( 2 , 9 ) ; addEdge ( 3 , 6 ) ; addEdge ( 4 , 6 ) ; addEdge ( 4 , 8 ) ; addEdge ( 5 , 8 ) ; addEdge ( 6 , 7 ) ; addEdge ( 7 , 8 ) ; addEdge ( 8 , 10 ) ; printOrder ( n , m ) ; } }
Count total set bits in all numbers from range L to R | C # program for the above approach ; Returns position of leftmost set bit The rightmost position is taken as 0 ; Function that gives the position of previous leftmost set bit in n ; Function that returns count of set bits present in all numbers from 1 to n ; Get the position of leftmost set bit in n ; Use the position ; Function to count the set bits between the two numbers N and M ; Base Case ; Get position of next leftmost set bit ; If n is of the form 2 ^ x - 1 ; Update n for next recursive call ; Function that counts the set bits between L and R ; Driver Code ; Given L and R ; Function call
using System ; class GFG { static int getLeftmostBit ( int n ) { int m = 0 ; while ( n > 1 ) { n = n >> 1 ; m ++ ; } return m ; } static int getNextLeftmostBit ( int n , int m ) { int temp = 1 << m ; while ( n < temp ) { temp = temp >> 1 ; m -- ; } return m ; } static int countSetBit ( int n ) { int m = getLeftmostBit ( n ) ; return _countSetBit ( n , m ) ; } static int _countSetBit ( int n , int m ) { if ( n == 0 ) return 0 ; m = getNextLeftmostBit ( n , m ) ; if ( n == ( ( int ) 1 << ( m + 1 ) ) - 1 ) return ( int ) ( m + 1 ) * ( 1 << m ) ; n = n - ( 1 << m ) ; return ( ( n + 1 ) + countSetBit ( n ) + m * ( 1 << ( m - 1 ) ) ) ; } static int countSetBits ( int L , int R ) { return Math . Abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; } public static void Main ( String [ ] args ) { int L = 3 , R = 5 ; Console . Write ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) ) ; } }
Permutation of Array such that products of all adjacent elements are even | C # program to permutation of array such that product of all adjacent elements is even ; Function to print the required permutation ; push odd elements in ' odd ' and even elements in ' even ' ; Check if it possible to arrange the elements ; Else print the permutation ; Print remaining odds are even . and even elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void printPermutation ( int [ ] arr , int n ) { List < int > odd = new List < int > ( ) ; List < int > even = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even . Add ( arr [ i ] ) ; else odd . Add ( arr [ i ] ) ; } int size_odd = odd . Count ; int size_even = even . Count ; if ( size_odd > size_even + 1 ) Console . WriteLine ( " - 1" ) ; else { int i = 0 ; int j = 0 ; while ( i < size_odd && j < size_even ) { Console . Write ( odd [ i ] + " ▁ " ) ; ++ i ; Console . Write ( even [ j ] + " ▁ " ) ; ++ j ; } while ( i < size_odd ) { Console . Write ( odd [ i ] + " ▁ " ) ; ++ i ; } while ( j < size_even ) { Console . Write ( even [ j ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 7 , 9 , 8 , 10 , 11 } ; int N = arr . Length ; printPermutation ( arr , N ) ; } }
Maximize GCD of all possible pairs from 1 to N | C # Program to implement the approach ; Function to obtain the maximum gcd of all pairs from 1 to n ; Print the answer ; Driver code ; Function call
using System ; class GFG { static void find ( int n ) { Console . Write ( n / 2 ) ; } public static void Main ( string [ ] args ) { int n = 5 ; find ( n ) ; } }
Count of pairs of Array elements which are divisible by K when concatenated | C # program to count pairs of array elements which are divisible by K when concatenated ; Function to calculate and return the count of pairs ; Compute power of 10 modulo k ; Calculate length of a [ i ] ; Increase count of remainder ; Calculate ( a [ i ] * 10 ^ lenj ) % k ; Calculate ( k - ( a [ i ] * 10 ^ lenj ) % k ) % k ; Increase answer by count ; If a pair ( a [ i ] , a [ i ] ) is counted ; Return the count of pairs ; Driver code
using System ; class GFG { static int [ , ] rem = new int [ 11 , 11 ] ; static int countPairs ( int [ ] a , int n , int k ) { int [ ] len = new int [ n ] ; int [ ] p = new int [ 11 ] ; p [ 0 ] = 1 ; for ( int i = 1 ; i <= 10 ; i ++ ) { p [ i ] = ( p [ i - 1 ] * 10 ) % k ; } for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; while ( x > 0 ) { len [ i ] ++ ; x /= 10 ; } rem [ len [ i ] , a [ i ] % k ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= 10 ; j ++ ) { int r = ( a [ i ] * p [ j ] ) % k ; int xr = ( k - r ) % k ; ans += rem [ j , xr ] ; if ( len [ i ] == j && ( r + a [ i ] % k ) % k == 0 ) ans -- ; } } return ans ; } public static void Main ( string [ ] args ) { int [ ] a = { 4 , 5 , 2 } ; int n = a . Length , k = 2 ; Console . Write ( countPairs ( a , n , k ) ) ; } }
Count of N digit Numbers whose sum of every K consecutive digits is equal | C # program for the above approach ; Function to count the number of N - digit numbers such that sum of every k consecutive digits are equal ; Range of numbers ; Extract digits of the number ; Store the sum of first K digits ; Check for every k - consecutive digits using sliding window ; Driver Code ; Given N and K
using System ; class GFG { static int countDigitSum ( int N , int K ) { int l = ( int ) Math . Pow ( 10 , N - 1 ) , r = ( int ) Math . Pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; int [ ] digits = new int [ N ] ; for ( int j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num /= 10 ; } int sum = 0 , flag = 0 ; for ( int j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( int j = K ; j < N ; j ++ ) { if ( sum - digits [ j - K ] + digits [ j ] != sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } public static void Main ( ) { int N = 2 , K = 1 ; Console . Write ( countDigitSum ( N , K ) ) ; } }
Array formed using sum of absolute differences of that element with all other elements | C # program for the above approach ; Function to return the new array ; Length of the arraylist ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize int sum to 0 ; Add the value of sum to ans ; Return the final ans ; Driver Code ; Given array arr [ ] ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { private static List < int > calculate ( int [ ] arr ) { int n = arr . Length ; List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < arr . Length ; j ++ ) { sum += Math . Abs ( arr [ i ] - arr [ j ] ) ; } ans . Add ( sum ) ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 6 } ; List < int > tmp = calculate ( arr ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < tmp . Count ; i ++ ) { if ( i != tmp . Count - 1 ) { Console . Write ( tmp [ i ] + " , ▁ " ) ; } else { Console . Write ( tmp [ i ] ) ; } } Console . Write ( " ] " ) ; } }
Number of pair of positions in matrix which are not accessible | C # program to count number of pair of positions in matrix which are not accessible ; Counts number of vertices connected in a component containing x . Stores the count in k . ; Incrementing the number of node in a connected component . ; Return the number of count of non - accessible cells . ; Initialize count of connected vertices found by DFS starting from i . ; Update result ; Inserting the edge between edge . ; Mapping the cell coordinate into node number . ; Inserting the edge . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int k ; static void dfs ( List < int > [ ] graph , bool [ ] visited , int x ) { for ( int i = 0 ; i < graph [ x ] . Count ; i ++ ) { if ( ! visited [ graph [ x ] [ i ] ] ) { ( k ) ++ ; visited [ graph [ x ] [ i ] ] = true ; dfs ( graph , visited , graph [ x ] [ i ] ) ; } } } static int countNonAccessible ( List < int > [ ] graph , int N ) { bool [ ] visited = new bool [ N * N + N ] ; int ans = 0 ; for ( int i = 1 ; i <= N * N ; i ++ ) { if ( ! visited [ i ] ) { visited [ i ] = true ; int k = 1 ; dfs ( graph , visited , i ) ; ans += k * ( N * N - k ) ; } } return ans ; } static void insertpath ( List < int > [ ] graph , int N , int x1 , int y1 , int x2 , int y2 ) { int a = ( x1 - 1 ) * N + y1 ; int b = ( x2 - 1 ) * N + y2 ; graph [ a ] . Add ( b ) ; graph [ b ] . Add ( a ) ; } public static void Main ( String [ ] args ) { int N = 2 ; List < int > [ ] graph = new List < int > [ N * N + 1 ] ; for ( int i = 1 ; i <= N * N ; i ++ ) graph [ i ] = new List < int > ( ) ; insertpath ( graph , N , 1 , 1 , 1 , 2 ) ; insertpath ( graph , N , 1 , 2 , 2 , 2 ) ; Console . WriteLine ( countNonAccessible ( graph , N ) ) ; } }
Minimum number of distinct powers of 2 required to express a given binary number | C # program to implement the above approach ; Function to return the minimum distinct powers of 2 required to express s ; Reverse the string to start from lower powers ; Check if the character is 1 ; Add in range provided range ; Initialize the counter ; Check if the character is not 0 ; Increment the counter ; Print the result ; utility reverse ; Driver code
using System ; using System . Text ; class GFG { static void findMinimum ( String s ) { int n = s . Length ; int [ ] x = new int [ n + 1 ] ; StringBuilder s2 = new StringBuilder ( s ) ; s2 = reverse ( s2 . ToString ( ) ) ; String s3 = s2 . ToString ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s3 [ i ] == '1' ) { if ( x [ i ] == 1 ) { x [ i + 1 ] = 1 ; x [ i ] = 0 ; } else if ( 1 <= i && ( i & x [ i - 1 ] ) == 1 ) { x [ i + 1 ] = 1 ; x [ i - 1 ] = - 1 ; } else x [ i ] = 1 ; } } int c = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( x [ i ] != 0 ) c ++ ; } Console . WriteLine ( c ) ; } static StringBuilder reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return new StringBuilder ( String . Join ( " " , a ) ) ; } public static void Main ( String [ ] args ) { String str = "111" ; findMinimum ( str ) ; } }
Split a Numeric String into Fibonacci Sequence | C # program of the above approach ; Function that returns true if Fibonacci sequence is found ; Base condition : If pos is equal to length of S and seq length is greater than 3 ; Return true ; Stores current number ; Add current digit to num ; Avoid integer overflow ; Avoid leading zeros ; If current number is greater than last two number of seq ; If seq length is less 2 or current number is is equal to the last two of the seq ; Add to the seq ; Recur for i + 1 ; Remove last added number ; If no sequence is found ; Function that prints the Fibonacci sequence from the split of string S ; Initialize a vector to store the sequence ; Call helper function ; If sequence length is greater than 3 ; Print the sequence ; If no sequence is found ; Print - 1 ; Driver Code ; Given String ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static bool splitIntoFibonacciHelper ( int pos , string S , ArrayList seq ) { if ( pos == S . Length && ( seq . Count >= 3 ) ) { return true ; } long num = 0 ; for ( int i = pos ; i < S . Length ; i ++ ) { num = num * 10 + ( S [ i ] - '0' ) ; if ( num > Int64 . MaxValue ) break ; if ( S [ pos ] == '0' && i > pos ) break ; if ( seq . Count > 2 && ( num > ( ( long ) seq [ seq . Count - 1 ] + ( long ) seq [ seq . Count - 2 ] ) ) ) break ; if ( seq . Count < 2 || ( num == ( ( long ) seq [ seq . Count - 1 ] + ( long ) seq [ seq . Count - 2 ] ) ) ) { seq . Add ( num ) ; if ( splitIntoFibonacciHelper ( i + 1 , S , seq ) ) return true ; seq . Remove ( seq . Count - 1 ) ; } } return false ; } static void splitIntoFibonacci ( string S ) { ArrayList seq = new ArrayList ( ) ; splitIntoFibonacciHelper ( 0 , S , seq ) ; if ( seq . Count >= 3 ) { for ( int i = 0 ; i < seq . Count ; i ++ ) Console . Write ( seq [ i ] + " ▁ " ) ; } else { Console . Write ( " - 1" ) ; } } public static void Main ( string [ ] args ) { string S = "11235813" ; splitIntoFibonacci ( S ) ; } }
Count pair of strings whose concatenation has every vowel | C # program for the above approach ; Function to return the count of all concatenated string with each vowel at least once ; Creating a hash array with initial value as 0 ; Traversing through each string and getting hash value for each of them ; Initializing the weight of each string ; Find the hash value for each string ; Increasing the count of the hash value ; Getting all possible pairs of indexes in hash array ; Check if the pair which has hash value 31 and multiplying the count of string and add it strCount ; Corner case , for strings which independently has all the vowels ; Return thre final count ; Driver code ; Given array of strings ; Function call
using System ; class GFG { public static int good_pairs ( string [ ] str , int N ) { int [ ] arr = new int [ 32 ] ; int strCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int Weight = 0 ; for ( int j = 0 ; j < str [ i ] . Length ; j ++ ) { switch ( str [ i ] [ j ] ) { case ' a ' : Weight = Weight | 1 ; break ; case ' e ' : Weight = Weight | 2 ; break ; case ' i ' : Weight = Weight | 4 ; break ; case ' o ' : Weight = Weight | 8 ; break ; case ' u ' : Weight = Weight | 16 ; break ; } } arr [ Weight ] ++ ; } for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = i + 1 ; j < 32 ; j ++ ) { if ( ( i j ) == 31 ) strCount += arr [ i ] * arr [ j ] ; } } strCount += ( arr [ 31 ] * ( arr [ 31 ] - 1 ) ) / 2 ; return strCount ; } public static void Main ( string [ ] args ) { string [ ] str = { " aaweiolkju " , " oxdfgujkmi " } ; int N = str . Length ; Console . Write ( good_pairs ( str , N ) ) ; } }
Color a grid such that all same color cells are connected either horizontally or vertically | C # program to color a grid such that all same color cells are connected either horizontally or vertically ; Current color ; Final grid ; If even row ; Traverse from left to right ; If color has been exhausted , move to the next color ; Color the grid at this position ; Reduce the color count ; Traverse from right to left for odd rows ; Print the grid ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void solve ( List < int > arr , int r , int c ) { int idx = 1 ; int [ , ] dp = new int [ r , c ] ; for ( int i = 0 ; i < r ; i ++ ) { if ( i % 2 == 0 ) { for ( int j = 0 ; j < c ; j ++ ) { if ( arr [ idx - 1 ] == 0 ) idx ++ ; dp [ i , j ] = idx ; arr [ idx - 1 ] = arr [ idx - 1 ] - 1 ; } } else { for ( int j = c - 1 ; j >= 0 ; j -- ) { if ( arr [ idx - 1 ] == 0 ) idx ++ ; dp [ i , j ] = idx ; arr [ idx - 1 ] = arr [ idx - 1 ] - 1 ; } } } for ( int i = 0 ; i < r ; ++ i ) { for ( int j = 0 ; j < c ; ++ j ) { Console . Write ( dp [ i , j ] + " ▁ " ) ; } Console . Write ( ' STRNEWLINE ' ) ; } } public static void Main ( string [ ] args ) { int r = 3 , c = 5 ; List < int > arr = new List < int > ( ) ; arr . Add ( 1 ) ; arr . Add ( 2 ) ; arr . Add ( 3 ) ; arr . Add ( 4 ) ; arr . Add ( 5 ) ; solve ( arr , r , c ) ; } }
Count of 0 s to be flipped to make any two adjacent 1 s at least K 0 s apart | C # program for the above problem ; Function to find the count of 0 s to be flipped ; Loop traversal to mark K adjacent positions to the right of already existing 1 s . ; Loop traversal to mark K adjacent positions to the left of already existing 1 s . ; Loop to count the maximum number of 0 s that will be replaced by 1 s ; Driver code
using System ; class GFG { public static int count ( int k , String s ) { int [ ] ar = new int [ s . Length ] ; int end = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '1' ) { for ( int j = i ; j < s . Length && j <= i + k ; j ++ ) { ar [ j ] = - 1 ; end = j ; } i = end ; } } end = 0 ; for ( int i = s . Length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { for ( int j = i ; j >= 0 && j >= i - k ; j -- ) { ar [ j ] = - 1 ; end = j ; } i = end ; } } int ans = 0 ; end = 0 ; for ( int j = 0 ; j < s . Length ; j ++ ) { if ( ar [ j ] == 0 ) { ans ++ ; for ( int g = j ; g <= j + k && g < s . Length ; g ++ ) { ar [ g ] = - 1 ; end = g ; } j = end - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int K = 2 ; String s = "000000" ; Console . WriteLine ( count ( K , s ) ) ; } }
Length of longest connected 1 â €™ s in a Binary Grid | C # program for the above approach ; Keeps a track of directions that is up , down , left , right ; Function to perform the dfs traversal ; Mark the current node as visited ; Increment length from this node ; Update the diameter length ; Move to next cell in x - direction ; Move to next cell in y - direction ; Check if cell is invalid then continue ; Perform DFS on new cell ; Decrement the length ; Function to find the maximum length of connected 1 s in the given grid ; Increment the id ; Traverse the grid [ ] ; Find start point of start dfs call ; DFS Traversal from cell ( x , y ) ; Print the maximum length ; Driver Code ; Given grid [ , ] ; Function Call
using System ; class GFG { static readonly int row = 6 ; static readonly int col = 7 ; static int [ , ] vis = new int [ row + 1 , col + 1 ] ; static int id ; static int diameter = 0 , length = 0 ; static int x = 0 , y = 0 ; static int [ ] dx = { - 1 , 1 , 0 , 0 } ; static int [ ] dy = { 0 , 0 , - 1 , 1 } ; static void dfs ( int a , int b , int [ , ] lis ) { vis [ a , b ] = id ; length ++ ; if ( length > diameter ) { x = a ; y = b ; diameter = length ; } for ( int j = 0 ; j < 4 ; j ++ ) { int cx = a + dx [ j ] ; int cy = b + dy [ j ] ; if ( cx < 0 cy < 0 cx >= row cy >= col lis [ cx , cy ] == 0 vis [ cx , cy ] > 0 ) { continue ; } dfs ( cx , cy , lis ) ; } vis [ a , b ] = 0 ; length -- ; } static void findMaximumLength ( int [ , ] lis ) { id ++ ; length = 0 ; diameter = 0 ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { if ( lis [ i , j ] != 0 ) { dfs ( i , j , lis ) ; i = row ; break ; } } } id ++ ; length = 0 ; diameter = 0 ; dfs ( x , y , lis ) ; Console . Write ( diameter ) ; } public static void Main ( String [ ] args ) { int [ , ] grid = { { 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 1 , 1 , 1 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 } } ; findMaximumLength ( grid ) ; } }
Last two digits of powers of 7 | C # program for the above approach ; Function to find the last two digits of 7 ^ N ; Case 4 ; Case 3 ; Case 2 ; Case 1 ; Driver code ; Given number ; Function Call
using System ; namespace GFG { class GFG { public static String get_last_two_digit ( int N ) { if ( N % 4 == 0 ) return "01" ; else if ( N % 4 == 1 ) return "07" ; else if ( N % 4 == 2 ) return "49" ; return "43" ; } public static void Main ( ) { int N = 12 ; Console . Write ( get_last_two_digit ( N ) ) ; } } }
Subsequence with maximum pairwise absolute difference and minimum size | C # program for the above approach ; Function to find the subsequence with maximum absolute difference ; To store the resultant subsequence ; First element should be included in the subsequence ; Traverse the given array [ ] arr ; If current element is greater than the previous element ; If the current element is not the local maxima then continue ; Else push it in subsequence ; If the current element is less then the previous element ; If the current element is not the local minima then continue ; Else push it in subsequence ; Last element should also be included in subsequence ; Print the element ; Driver Code ; Given array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void getSubsequence ( int [ ] ar ) { int N = ar . Length ; List < int > ans = new List < int > ( ) ; ans . Add ( ar [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( ar [ i ] > ar [ i - 1 ] ) { if ( i < N - 1 && ar [ i ] <= ar [ i + 1 ] ) { continue ; } else { ans . Add ( ar [ i ] ) ; } } else { if ( i < N - 1 && ar [ i + 1 ] < ar [ i ] ) { continue ; } else { ans . Add ( ar [ i ] ) ; } } } ans . Add ( ar [ N - 1 ] ) ; foreach ( int it in ans ) Console . Write ( it + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 3 , 5 } ; getSubsequence ( arr ) ; } }