codeparrot/xlcost-text-to-code huggingface dataset:
Check if every vertex triplet in graph contains two vertices connected to third vertex | C # program of the above approach ; Function to add edge into the graph ; Finding the maximum and minimum values in each component ; Function for checking whether the given graph is valid or not ; Checking for intersecting intervals ; If intersection is found ; Graph is not valid ; Function for the DFS Traversal ; Traversing for every vertex ; Storing maximum and minimum values of each component ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void addEdge ( ArrayList adj , int u , int v ) { ( ( ArrayList ) adj [ u ] ) . Add ( v ) ; ( ( ArrayList ) adj [ v ] ) . Add ( u ) ; } static void DFSUtil ( int u , ArrayList adj , bool [ ] visited , int componentMin , int componentMax ) { visited [ u ] = true ; componentMax = Math . Max ( componentMax , u ) ; componentMin = Math . Min ( componentMin , u ) ; for ( int i = 0 ; i < ( ( ArrayList ) adj [ u ] ) . Count ; i ++ ) if ( visited [ ( int ) ( ( ArrayList ) adj [ u ] ) [ i ] ] == false ) DFSUtil ( ( int ) ( ( ArrayList ) adj [ u ] ) [ i ] , adj , visited , componentMin , componentMax ) ; } static bool isValid ( ArrayList v ) { int MAX = - 1 ; bool ans = false ; foreach ( pair i in v ) { if ( i . first <= MAX ) { ans = true ; } MAX = Math . Max ( MAX , i . second ) ; } return ( ans == false ? true : false ) ; } static void DFS ( ArrayList adj , int V ) { ArrayList v = new ArrayList ( ) ; bool [ ] visited = new bool [ V + 1 ] ; for ( int u = 1 ; u <= V ; u ++ ) { if ( visited [ u ] == false ) { int componentMax = u ; int componentMin = u ; DFSUtil ( u , adj , visited , componentMin , componentMax ) ; v . Add ( new pair ( componentMin , componentMax ) ) ; } } bool check = isValid ( v ) ; if ( check ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; return ; } public static void Main ( string [ ] args ) { int N = 4 ; ArrayList adj = new ArrayList ( ) ; for ( int i = 0 ; i <= N + 1 ; i ++ ) adj . Add ( new ArrayList ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; DFS ( adj , N ) ; } }
Check if all array elements are present in a given stack or not | C # program of the above approach ; Function to check if all array elements is present in the stack ; Store the frequency of array elements ; Loop while the elements in the stack is not empty ; Condition to check if the element is present in the stack ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool checkArrInStack ( Stack < int > s , int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . ContainsKey ( arr [ i ] ) ) freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; else freq . Add ( arr [ i ] , 1 ) ; while ( s . Count != 0 ) { int poppedEle = s . Peek ( ) ; s . Pop ( ) ; if ( freq . ContainsKey ( poppedEle ) ) freq [ poppedEle ] = freq [ poppedEle ] - 1 ; } if ( freq . Count == 0 ) return false ; return true ; } public static void Main ( String [ ] args ) { Stack < int > s = new Stack < int > ( ) ; s . Push ( 10 ) ; s . Push ( 20 ) ; s . Push ( 30 ) ; s . Push ( 40 ) ; s . Push ( 50 ) ; int [ ] arr = { 20 , 30 } ; int n = arr . Length ; if ( checkArrInStack ( s , arr , n ) ) Console . Write ( " YES STRNEWLINE " ) ; else Console . Write ( " NO STRNEWLINE " ) ; } }
Length of longest substring to be deleted to make a string equal to another string | C # program to implement the above approach ; Function to print the length of longest substring to be deleted ; Stores the length of string ; Store the position of previous matched character of str1 ; Store the position of first occurrence of str2 in str1 ; Find the position of the first occurrence of str2 ; Store the index of str1 ; If both characters not matched ; Store the length of the longest deleted substring ; Store the position of last occurrence of str2 in str1 ; If both characters not matched ; Update res ; Update res . ; Driver code ; Given string ; Function call
using System ; class GFG { static int longDelSub ( string str1 , string str2 ) { int N = str1 . Length ; int M = str2 . Length ; int prev_pos = 0 ; int [ ] pos = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { int index = prev_pos ; while ( index < N && str1 [ index ] != str2 [ i ] ) { index ++ ; } pos [ i ] = index ; prev_pos = index + 1 ; } int res = N - prev_pos ; prev_pos = N - 1 ; for ( int i = M - 1 ; i >= 0 ; i -- ) { int index = prev_pos ; while ( index >= 0 && str1 [ index ] != str2 [ i ] ) { index -- ; } if ( i != 0 ) { res = Math . Max ( res , index - pos [ i - 1 ] - 1 ) ; } prev_pos = index - 1 ; } res = Math . Max ( res , prev_pos + 1 ) ; return res ; } public static void Main ( ) { string str1 = " GeeksforGeeks " ; string str2 = " forks " ; Console . Write ( longDelSub ( str1 , str2 ) ) ; } }
Maximized partitions of a string such that each character of the string appears in one substring | C # program for the above approach ; Function to print all the substrings ; Stores the substrings ; Stores last index of characters of string s List < int > ans = new List < int > ( ) ; ; Find the last position of each character in the string ; Update the last index ; Iterate the given string ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; If the current pos of character equals the min pos then the end of partition ; Add respective character to the string ; Store the partition 's  len and reset variables ; Update the minp and str ; Driver Code ; Input string ; Function call
using System ; using System . Collections . Generic ; class GFG { public static void print_substring ( String s ) { int n = s . Length ; String str = " " ; if ( n == 0 ) { Console . Write ( " - 1" ) ; return ; } int [ ] last_pos = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) last_pos [ i ] = - 1 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s [ i ] - ' a ' ] == - 1 ) { last_pos [ s [ i ] - ' a ' ] = i ; } } int minp = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { int lp = last_pos [ s [ i ] - ' a ' ] ; minp = Math . Max ( minp , lp ) ; if ( i == minp ) { str += s [ i ] ; Console . Write ( str + ' ▁ ' ) ; minp = - 1 ; str = " " ; } else { str += s [ i ] ; } } } public static void Main ( String [ ] args ) { String S = " ababcbacadefegdehijhklij " ; print_substring ( S ) ; } }
Lengths of maximized partitions of a string such that each character of the string appears in one substring | C # program for the above approach ; Function to find the length of all partitions oof a String such that each characters occurs in a single subString ; Stores last index of String s ; Find the last position of each letter in the String ; Update the last index ; Iterate the given String ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; Increase len of partition ; if the current pos of character equals the min pos then the end of partition ; Store the length ; Print all the partition lengths ; Driver Code ; Given String str ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void partitionString ( String s ) { int n = s . Length ; List < int > ans = new List < int > ( ) ; if ( n == 0 ) { Console . Write ( " - 1" ) ; return ; } int [ ] last_pos = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; ++ i ) { last_pos [ i ] = - 1 ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s [ i ] - ' a ' ] == - 1 ) { last_pos [ s [ i ] - ' a ' ] = i ; } } int minp = - 1 , plen = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int lp = last_pos [ s [ i ] - ' a ' ] ; minp = Math . Max ( minp , lp ) ; ++ plen ; if ( i == minp ) { ans . Add ( plen ) ; minp = - 1 ; plen = 0 ; } } for ( int i = 0 ; i < ( int ) ans . Count ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String str = " acbbcc " ; partitionString ( str ) ; } }
Find the maximum occurring character after performing the given operations | C # program for the above approach ; Function to find the maximum occurring character ; Initialize count of zero and one ; Iterate over the given string ; Count the zeros ; Count the ones ; Iterate over the given string ; Check if character is * then continue ; Check if first character after * is X ; Add all * to the frequency of X ; Set prev to the i - th character ; Check if first character after * is Y ; Set prev to the i - th character ; Check if prev character is 1 and current character is 0 ; Half of the * will be converted to 0 ; Half of the * will be converted to 1 ; Check if prev and current are 1 ; All * will get converted to 1 ; No * can be replaced by either 0 or 1 ; Prev becomes the ith character ; Check if prev and current are 0 ; All * will get converted to 0 ; If frequency of 0 is more ; If frequency of 1 is more ; Driver code ; Given string ; Function call
using System ; class GFG { static void solve ( string S ) { int count_0 = 0 , count_1 = 0 ; int prev = - 1 ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( S [ i ] == '0' ) count_0 ++ ; else if ( S [ i ] == '1' ) count_1 ++ ; } for ( int i = 0 ; i < S . Length ; i ++ ) { if ( S [ i ] == ' * ' ) continue ; else if ( S [ i ] == '0' && prev == - 1 ) { count_0 = count_0 + i ; prev = i ; } else if ( S [ i ] == '1' && prev == - 1 ) { prev = i ; } else if ( S [ prev ] == '1' && S [ i ] == '0' ) { count_0 = count_0 + ( i - prev - 1 ) / 2 ; count_1 = count_1 + ( i - prev - 1 ) / 2 ; prev = i ; } else if ( S [ prev ] == '1' && S [ i ] == '1' ) { count_1 = count_1 + ( i - prev - 1 ) ; prev = i ; } else if ( S [ prev ] == '0' && S [ i ] == '1' ) prev = i ; else if ( S [ prev ] == '0' && S [ i ] == '0' ) { count_0 = count_0 + ( i - prev - 1 ) ; prev = i ; } } if ( count_0 > count_1 ) Console . Write ( "0" ) ; else if ( count_1 > count_0 ) Console . Write ( "1" ) ; else { Console . Write ( " - 1" ) ; } } public static void Main ( ) { string str = " * *0 * * 1 * * *0" ; solve ( str ) ; } }
Arithmetic Progression containing X and Y with least possible first term | C # program for the approach ; Function that finds the minimum positive first term including X with given common difference and the number of terms ; Stores the first term ; Initialize the low and high ; Perform binary search ; Find the mid ; Check if first term is greater than 0 ; Store the possible first term ; Search between mid + 1 to high ; Search between low to mid - 1 ; Return the minimum first term ; Function that finds the Arithmetic Progression with minimum possible first term containing X and Y ; Considering X to be smaller than Y always ; Stores the max common difference ; Stores the minimum first term and the corresponding common difference of the resultant AP ; Iterate over all the common difference ; Check if X and Y is included for current common difference ; Store the possible common difference ; Number of terms from X to Y with diff1 common difference ; Number of terms from X to Y with diff2 common difference ; Find the corresponding first terms with diff1 and diff2 ; Store the minimum first term and the corresponding common difference ; Print the resultant AP ; Driver Code ; Given length of AP and the two terms ; Function call
using System ; class GFG { static int minFirstTerm ( int X , int diff , int N ) { int first_term = int . MaxValue ; int low = 0 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( X - mid * diff > 0 ) { first_term = X - mid * diff ; low = mid + 1 ; } else high = mid - 1 ; } return first_term ; } static void printAP ( int N , int X , int Y ) { if ( X > Y ) { X = X + Y ; Y = X - Y ; X = X - Y ; } int maxDiff = Y - X ; int first_term = int . MaxValue , diff = 0 ; for ( int i = 1 ; i * i <= maxDiff ; i ++ ) { if ( maxDiff % i == 0 ) { int diff1 = i ; int diff2 = maxDiff / diff1 ; int terms1 = diff2 + 1 ; int terms2 = diff1 + 1 ; int first_term1 = minFirstTerm ( X , diff1 , N - terms1 ) ; int first_term2 = minFirstTerm ( X , diff2 , N - terms2 ) ; if ( first_term1 < first_term ) { first_term = first_term1 ; diff = diff1 ; } if ( first_term2 < first_term ) { first_term = first_term2 ; diff = diff2 ; } } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( first_term + " ▁ " ) ; first_term += diff ; } } public static void Main ( String [ ] args ) { int N = 5 , X = 10 , Y = 15 ; printAP ( N , X , Y ) ; } }
Check if a number can be represented as sum of two consecutive perfect cubes | C # program of the above approach ; Function to check if a number can be expressed as the sum of cubes of two consecutive numbers ; Driver Code
using System ; class GFG { static bool isCubeSum ( int n ) { for ( int i = 1 ; i * i * i <= n ; i ++ ) { if ( i * i * i + ( i + 1 ) * ( i + 1 ) * ( i + 1 ) == n ) return true ; } return false ; } public static void Main ( String [ ] args ) { int n = 35 ; if ( isCubeSum ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if a number can be represented as sum of two consecutive perfect cubes | C # program to implement above approach ; Function to check that a number is the sum of cubes of 2 consecutive numbers or not ; Condition to check if a number is the sum of cubes of 2 consecutive numbers or not ; Driver Code ; Function call
using System ; class GFG { static bool isSumCube ( int N ) { int a = ( int ) Math . Pow ( N , ( double ) 1 / 3 ) ; int b = a - 1 ; return ( ( a * a * a + b * b * b ) == N ) ; } public static void Main ( String [ ] args ) { int i = 35 ; if ( isSumCube ( i ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Maximize sum of remaining elements after every removal of the array half with greater sum | C # program to implement the above approach ; Function to find the maximum sum ; Base case len of array is 1 ; Stores the readonly result ; Traverse the array ; Store left prefix sum ; Store right prefix sum ; Compare the left and right ; If both are equal apply the optimal method ; Update with minimum ; Return the readonly ans ; Function to print maximum sum ; To store prefix sums ; Traversing the array ; Add prefix sum of the array ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int maxweight ( int s , int e , Dictionary < int , int > pre ) { if ( s == e ) return 0 ; int ans = 0 ; for ( int i = s ; i < e ; i ++ ) { int left = pre [ i ] - pre [ s - 1 ] ; int right = pre [ e ] - pre [ i ] ; if ( left < right ) ans = Math . Max ( ans , left + maxweight ( s , i , pre ) ) ; if ( left == right ) { ans = Math . Max ( ans , Math . Max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; } if ( left > right ) ans = Math . Max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; } return ans ; } static void maxSum ( List < int > arr ) { Dictionary < int , int > pre = new Dictionary < int , int > ( ) ; pre . Add ( - 1 , 0 ) ; pre . Add ( 0 , arr [ 0 ] ) ; for ( int i = 1 ; i < arr . Count ; i ++ ) { if ( pre [ i - 1 ] != 0 ) pre . Add ( i , pre [ i - 1 ] + arr [ i ] ) ; else pre . Add ( i , arr [ i ] ) ; } Console . WriteLine ( maxweight ( 0 , arr . Count - 1 , pre ) ) ; } public static void Main ( String [ ] args ) { List < int > arr = new List < int > ( ) ; arr . Add ( 6 ) ; arr . Add ( 2 ) ; arr . Add ( 3 ) ; arr . Add ( 4 ) ; arr . Add ( 5 ) ; arr . Add ( 5 ) ; maxSum ( arr ) ; } }
Maximum time in HH : MM : SS format that can be represented by given six digits | C # Program of the above approach ; Function to return the maximum possible time in 24 - Hours format that can be represented by array elements ; Stores the frequency of the array elements ; Maximum possible time ; Iterate to minimum possible time ; Conditions to reduce the the time iteratively ; If all required digits are present in the Map ; Retrieve Original Count ; If seconds is reduced to 0 ; If minutes is reduced to 0 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static String largestTimeFromDigits ( int [ ] A ) { Dictionary < int , int > mp1 = new Dictionary < int , int > ( ) ; Dictionary < int , int > mp2 = new Dictionary < int , int > ( ) ; foreach ( int x in A ) { if ( mp1 . ContainsKey ( x ) ) mp1 [ x ] = mp1 [ x ] + 1 ; else mp1 . Add ( x , 1 ) ; } mp2 = new Dictionary < int , int > ( mp1 ) ; int hr = 23 , m = 59 , s = 59 ; while ( hr >= 0 ) { int h0 = hr / 10 , h1 = hr % 10 ; int m0 = m / 10 , m1 = m % 10 ; int s0 = s / 10 , s1 = s % 10 ; int p = 0 ; int [ ] arr = { h0 , h1 , m0 , m1 , s0 , s1 } ; foreach ( int it in arr ) { if ( mp1 . ContainsKey ( it ) && mp1 [ it ] > 0 ) { mp1 [ it ] = mp1 [ it ] - 1 ; } else { p = 1 ; } } if ( p == 0 ) { String st = " " ; st = String . Join ( " " , h0 ) + String . Join ( " " , h1 ) ; st += ' : ' + String . Join ( " " , m0 ) + String . Join ( " " , m1 ) ; st += ' : ' + String . Join ( " " , s0 ) + String . Join ( " " , s1 ) ; return st ; } mp1 = new Dictionary < int , int > ( mp2 ) ; if ( s == 0 ) { s = 59 ; m -- ; } else if ( m < 0 ) { m = 59 ; hr -- ; } if ( s > 0 ) { s -- ; } } return " - 1" ; } public static void Main ( String [ ] args ) { int [ ] v = { 0 , 2 , 1 , 9 , 3 , 2 } ; Console . Write ( largestTimeFromDigits ( v ) ) ; } }
Minimum size substring to be removed to make a given string palindromic | C # program of the above approach ; Function to find palindromic prefix of maximum length ; Finding palindromic prefix of maximum length ; Checking if curr subString is palindrome or not . ; Condition to check if the prefix is a palindrome ; If no palindrome exist ; Function to find the maximum size palindrome such that after removing minimum size subString ; Finding prefix and suffix of same length ; Matching the ranges ; Case 1 : Length is even and whole String is palindrome ; Case 2 : Length is odd and whole String is palindrome ; Adding that mid character ; Add prefix or suffix of the remaining String or suffix , whichever is longer ; Reverse the remaining String to find the palindromic suffix ; Driver Code
using System ; class GFG { static String palindromePrefix ( String S ) { int n = S . Length ; for ( int i = n - 1 ; i >= 0 ; i -- ) { String curr = S . Substring ( 0 , i + 1 ) ; int l = 0 , r = curr . Length - 1 ; bool is_palindrome = true ; while ( l < r ) { if ( curr [ l ] != curr [ r ] ) { is_palindrome = false ; break ; } l ++ ; r -- ; } if ( is_palindrome ) return curr ; } return " " ; } static String maxPalindrome ( String S ) { int n = S . Length ; if ( n <= 1 ) { return S ; } String pre = " " , suff = " " ; int i = 0 , j = n - 1 ; while ( S [ i ] == S [ j ] && i < j ) { i ++ ; j -- ; } i -- ; j ++ ; pre = S . Substring ( 0 , i + 1 ) ; suff = S . Substring ( j ) ; if ( j - i == 1 ) { return pre + suff ; } if ( j - i == 2 ) { String mid_char = S . Substring ( i + 1 , i + 2 ) ; return pre + mid_char + suff ; } String rem_str = S . Substring ( i + 1 , j ) ; String pre_of_rem_str = palindromePrefix ( rem_str ) ; rem_str = reverse ( rem_str ) ; String suff_of_rem_str = palindromePrefix ( rem_str ) ; if ( pre_of_rem_str . Length >= suff_of_rem_str . Length ) { return pre + pre_of_rem_str + suff ; } else { return pre + suff_of_rem_str + suff ; } } static String 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 String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String S = " geeksforskeeg " ; Console . Write ( maxPalindrome ( S ) ) ; } }
Count numbers from a given range that contains a given number as the suffix | C # program of the above approach ; Function to count the number ends with given number in range ; Find number of digits in A ; Find the power of 10 ; Incrementing the A ; Driver Code ; Function call
using System ; class GFG { static void countNumEnds ( int A , int L , int R ) { int temp , count = 0 , digits ; int cycle ; digits = ( int ) ( Math . Log10 ( A ) + 1 ) ; temp = ( int ) Math . Pow ( 10 , digits ) ; cycle = temp ; while ( temp <= R ) { if ( temp >= L ) count ++ ; temp += cycle ; } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int A = 2 , L = 2 , R = 20 ; countNumEnds ( A , L , R ) ; } }
Minimize replacement of characters to its nearest alphabet to make a string palindromic | C # program for the above approach ; Function to find the minimum number of operations required to convert th given string into palindrome ; Iterate till half of the string ; Find the absolute difference between the characters ; Adding the minimum difference of the two result ; Return the result ; Driver code ; Given string ; Function call
using System ; class GFG { public static int minOperations ( String s ) { int len = s . Length ; int result = 0 ; for ( int i = 0 ; i < len / 2 ; i ++ ) { int D1 = Math . Max ( s [ i ] , s [ len - 1 - i ] ) - Math . Min ( s [ i ] , s [ len - 1 - i ] ) ; int D2 = 26 - D1 ; result += Math . Min ( D1 , D2 ) ; } return result ; } public static void Main ( String [ ] args ) { String s = " abccdb " ; Console . WriteLine ( minOperations ( s ) ) ; } }
Check if all the pairs of an array are coprime with each other | C # implementation of the above approach ; Function to store and check the factors ; Check if factors are equal ; Check if the factor is already present ; Insert the factor in set ; Check if the factor is already present ; Insert the factors in set ; Function to check if all the pairs of array elements are coprime with each other ; Check if factors of A [ i ] haven 't occurred previously ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool findFactor ( int value , HashSet < int > factors ) { factors . Add ( value ) ; for ( int i = 2 ; i * i <= value ; i ++ ) { if ( value % i == 0 ) { if ( value / i == i ) { if ( factors . Contains ( i ) ) { return true ; } else { factors . Add ( i ) ; } } else { if ( factors . Contains ( i ) || factors . Contains ( value / i ) ) { return true ; } else { factors . Add ( i ) ; factors . Add ( value / i ) ; } } } } return false ; } static bool allCoprime ( int [ ] A , int n ) { bool all_coprime = true ; HashSet < int > factors = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == 1 ) continue ; if ( findFactor ( A [ i ] , factors ) ) { all_coprime = false ; break ; } } return all_coprime ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = A . Length ; if ( allCoprime ( A , arr_size ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Count subsequences which contains both the maximum and minimum array element | C # program for the above approach ; Function to calculate the count of subsequences ; Find the maximum from the array ; Find the minimum from the array ; If array contains only one distinct element ; Find the count of maximum ; Find the count of minimum ; Finding the result with given condition ; Driver Code ; Function call
using System ; using System . Linq ; class GFG { static double countSubSequence ( int [ ] arr , int n ) { int maximum = arr . Max ( ) ; int minimum = arr . Min ( ) ; if ( maximum == minimum ) return Math . Pow ( 2 , n ) - 1 ; int i = count ( arr , maximum ) ; int j = count ( arr , minimum ) ; double res = ( Math . Pow ( 2 , i ) - 1 ) * ( Math . Pow ( 2 , j ) - 1 ) * Math . Pow ( 2 , n - i - j ) ; return res ; } static int count ( int [ ] arr , int value ) { int sum = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) if ( arr [ i ] == value ) sum ++ ; return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( countSubSequence ( arr , n ) ) ; } }