codeparrot/xlcost-text-to-code huggingface dataset:
Replace all elements by difference of sums of positive and negative numbers after that element | C # program to implement above approach ; Function to print the array elements ; Function to replace all elements with absolute difference of absolute sums of positive and negative elements ; calculate difference of both sums ; if i - th element is positive , add it to positive sum ; if i - th element is negative , add it to negative sum ; replace i - th elements with absolute difference ; Driver Code
using System ; class GFG { static void printArray ( int N , int [ ] arr ) { for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } static void replacedArray ( int N , int [ ] arr ) { int pos_sum , neg_sum , i , diff ; pos_sum = 0 ; neg_sum = 0 ; for ( i = N - 1 ; i >= 0 ; i -- ) { diff = Math . Abs ( pos_sum ) - Math . Abs ( neg_sum ) ; if ( arr [ i ] > 0 ) pos_sum += arr [ i ] ; else neg_sum += arr [ i ] ; arr [ i ] = Math . Abs ( diff ) ; } } public static void Main ( ) { int N = 5 ; int [ ] arr = { 1 , - 1 , 2 , 3 , - 2 } ; replacedArray ( N , arr ) ; printArray ( N , arr ) ; N = 6 ; int [ ] arr1 = { - 3 , - 4 , - 2 , 5 , 1 , - 2 } ; replacedArray ( N , arr1 ) ; printArray ( N , arr1 ) ; } }
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | C # implementation of above approach ; Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; pairs from 1 to n * ( a / n ) and 1 to n * ( b / n ) ; pairs from 1 to n * ( a / n ) and n * ( b / n ) to b ; pairs from n * ( a / n ) to a and 1 to n * ( b / n ) ; pairs from n * ( a / n ) to a and n * ( b / n ) to b ; Return answer ; Driver code
using System ; class GFG { static int findCountOfPairs ( int a , int b , int n ) { int ans = 0 ; ans += n * ( a / n ) * ( b / n ) ; ans += ( a / n ) * ( b % n ) ; ans += ( a % n ) * ( b / n ) ; ans += ( ( a % n ) + ( b % n ) ) / n ; return ans ; } static public void Main ( ) { int a = 5 , b = 13 , n = 3 ; Console . WriteLine ( findCountOfPairs ( a , b , n ) ) ; } }
Generate array with minimum sum which can be deleted in P steps | C # implementation of the approach ; Function to find the required array ; calculating minimum possible sum ; Array ; place first P natural elements ; Fill rest of the elements with 1 ; Driver Code
using System ; class GFG { static void findArray ( int N , int P ) { int ans = ( P * ( P + 1 ) ) / 2 + ( N - P ) ; int [ ] arr = new int [ N + 1 ] ; for ( int i = 1 ; i <= P ; i ++ ) { arr [ i ] = i ; } for ( int i = P + 1 ; i <= N ; i ++ ) { arr [ i ] = 1 ; } Console . Write ( " The ▁ Minimum ▁ Possible ▁ Sum ▁ is : ▁ " + ans + " STRNEWLINE " ) ; Console . Write ( " The ▁ Array ▁ Elements ▁ are : ▁ STRNEWLINE " ) ; for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int N = 5 , P = 3 ; findArray ( N , P ) ; } }
Find Intersection of all Intervals | C # implementation of the approach ; Function to print the intersection ; First interval ; Check rest of the intervals and find the intersection ; If no intersection exists ; Else update the intersection ; Driver code
using System ; class GFG { static void findIntersection ( int [ , ] intervals , int N ) { int l = intervals [ 0 , 0 ] ; int r = intervals [ 0 , 1 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( intervals [ i , 0 ] > r intervals [ i , 1 ] < l ) { Console . WriteLine ( - 1 ) ; return ; } else { l = Math . Max ( l , intervals [ i , 0 ] ) ; r = Math . Min ( r , intervals [ i , 1 ] ) ; } } Console . WriteLine ( " [ " + l + " , ▁ " + r + " ] " ) ; } public static void Main ( ) { int [ , ] intervals = { { 1 , 6 } , { 2 , 8 } , { 3 , 10 } , { 5 , 8 } } ; int N = intervals . GetLength ( 0 ) ; findIntersection ( intervals , N ) ; } }
Maximum size of sub | C # implementation of the approach ; Function that compares a and b ; Function to return length of longest subarray that satisfies one of the given conditions ; Driver Code ; Print the required answer
using System ; class GFG { static int cmp ( int a , int b ) { if ( a > b ) return 1 ; else if ( a == b ) return 0 ; else return - 1 ; } static int maxSubarraySize ( int [ ] arr , int n ) { int ans = 1 ; int anchor = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int c = cmp ( arr [ i - 1 ] , arr [ i ] ) ; if ( c == 0 ) anchor = i ; else if ( i == n - 1 || c * cmp ( arr [ i ] , arr [ i + 1 ] ) != - 1 ) { ans = Math . Max ( ans , i - anchor + 1 ) ; anchor = i ; } } return ans ; } static void Main ( ) { int [ ] arr = { 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 } ; int n = arr . Length ; Console . WriteLine ( maxSubarraySize ( arr , n ) ) ; } }
Maximum count of sub | C # implementation of the approach ; Function to return the count of the required sub - strings ; Iterate over all characters ; Count with current character ; If the substring has a length k then increment count with current character ; Update max count ; Driver Code
using System ; class GFG { static int maxSubStrings ( String s , int k ) { int maxSubStr = 0 , n = s . Length ; for ( int c = 0 ; c < 26 ; c ++ ) { char ch = ( char ) ( ( int ) ' a ' + c ) ; int curr = 0 ; for ( int i = 0 ; i <= n - k ; i ++ ) { if ( s [ i ] != ch ) continue ; int cnt = 0 ; while ( i < n && s [ i ] == ch && cnt != k ) { i ++ ; cnt ++ ; } i -- ; if ( cnt == k ) curr ++ ; } maxSubStr = Math . Max ( maxSubStr , curr ) ; } return maxSubStr ; } public static void Main ( ) { string s = " aaacaabbaa " ; int k = 2 ; Console . WriteLine ( maxSubStrings ( s , k ) ) ; } }
Count valid pairs in the array satisfying given conditions | C # implementation of the approach ; Function to return total valid pairs ; Initialize count of all the elements ; frequency count of all the elements ; Add total valid pairs ; Exclude pairs made with a single element i . e . ( x , x ) ; Driver Code ; Function call to print required answer
using System ; class GFG { static int ValidPairs ( int [ ] arr , int n ) { int [ ] count = new int [ 121 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] += 1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) continue ; if ( Math . Abs ( arr [ i ] - arr [ j ] ) % 2 == 1 ) continue ; ans += count [ arr [ i ] ] * count [ arr [ j ] ] ; if ( arr [ i ] == arr [ j ] ) ans -= count [ arr [ i ] ] ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 16 , 17 , 18 } ; int n = arr . Length ; Console . WriteLine ( ValidPairs ( arr , n ) ) ; } }
Distribution of candies according to ages of students | C # implementation of the approach ; Function to check The validity of distribution ; Stroring the max age of all students + 1 ; Stroring the max candy + 1 ; Creating the frequency array of the age of students ; Creating the frequency array of the packets of candies ; Pointer to tell whether we have reached the end of candy frequency array ; Flag to tell if distribution is possible or not ; Flag to tell if we can choose some candy packets for the students with age j ; If the quantity of packets is greater than or equal to the number of students of age j , then we can choose these packets for the students ; Start searching from k + 1 in next operation ; If we cannot choose any packets then the answer is NO ; Driver code
using System . IO ; using System ; class GFG { static void check_distribution ( int n , int k , int [ ] age , int [ ] candy ) { int mxage = age [ 0 ] ; for ( int i = 0 ; i < age . Length ; i ++ ) { if ( mxage < age [ i ] ) { mxage = age [ i ] ; } } int mxcandy = candy [ 0 ] ; for ( int i = 0 ; i < candy . Length ; i ++ ) { if ( mxcandy < candy [ i ] ) { mxcandy = candy [ i ] ; } } int [ ] fr1 = new int [ mxage + 1 ] ; Array . Fill ( fr1 , 0 ) ; int [ ] fr2 = new int [ mxcandy + 1 ] ; Array . Fill ( fr2 , 0 ) ; for ( int j = 0 ; j < n ; j ++ ) { fr1 [ age [ j ] ] += 1 ; } for ( int j = 0 ; j < k ; j ++ ) { fr2 [ candy [ j ] ] += 1 ; } k = 0 ; bool Tf = true ; for ( int j = 0 ; j < mxage ; j ++ ) { if ( fr1 [ j ] == 0 ) { continue ; } bool flag = false ; while ( k < mxcandy ) { if ( fr1 [ j ] <= fr2 [ k ] ) { flag = true ; break ; } k += 1 ; } k = k + 1 ; if ( flag == false ) { Tf = false ; break ; } } if ( Tf ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } static void Main ( ) { int [ ] age = { 5 , 15 , 10 } ; int [ ] candy = { 2 , 2 , 2 , 3 , 3 , 4 } ; int n = age . Length ; int k = candy . Length ; check_distribution ( n , k , age , candy ) ; } }
Find a palindromic string B such that given String A is a subsequense of B | C # program to find a palindromic string B such that given String A is a subsequense of B ; Function to check if a string is palindrome ; Reversing a string ; check if reversed string is equal to given string ; Function to find a palindromic string B such that given String A is a subsequense of B ; Reversing the string A ; If the string A is already a palindrome return A ; else return B ; Swap values of left and right ; Driver Code
using System ; class GFG { static bool checkPalindrome ( String s ) { String x = reverse ( s ) ; return x . Equals ( s ) ; } static String findStringB ( String A ) { String B = reverse ( A ) ; B = B + A ; if ( checkPalindrome ( A ) ) { return A ; } return B ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { String A = " ab " ; Console . WriteLine ( findStringB ( A ) ) ; } }
Minimum number of 1 's to be replaced in a binary array | C # program to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; Function to find minimum number of 1 ' s ▁ to ▁ be ▁ replaced ▁ to ▁ 0' s ; return final answer ; Driver Code
using System ; class GFG { static int minChanges ( int [ ] A , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { if ( ( i - 1 >= 0 ) && A [ i - 1 ] == 1 && A [ i + 1 ] == 1 && A [ i ] == 0 ) { A [ i + 1 ] = 0 ; cnt ++ ; } } return cnt ; } public static void Main ( ) { int [ ] A = { 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 } ; int n = A . Length ; Console . Write ( minChanges ( A , n ) ) ; } }
Number of closing brackets needed to complete a regular bracket sequence | C # program to find number of closing brackets needed and complete a regular bracket sequence ; Function to find number of closing brackets and complete a regular bracket sequence ; Finding the length of sequence ; Counting opening brackets ; Counting closing brackets ; Checking if at any position the number of closing bracket is more then answer is impossible ; If possible , print ' s ' and required closing brackets . ; Driver Code
using System ; class GFG { static void completeSequence ( String s ) { int n = s . Length ; int open = 0 , close = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) open ++ ; else close ++ ; if ( close > open ) { Console . Write ( " IMPOSSIBLE " ) ; return ; } } Console . Write ( s ) ; for ( int i = 0 ; i < open - close ; i ++ ) Console . Write ( " ) " ) ; } static void Main ( ) { String s = " ( ( ) ( ( ) ( " ; completeSequence ( s ) ; } }
Lexicographically smallest permutation with no digits at Original Index | C # program to find the smallest permutation ; Function to print the smallest permutation ; when n is even ; when n is odd ; handling last 3 digit ; add EOL and print result ; Driver Code
using System ; class GFG { static void smallestPermute ( int n ) { char [ ] res = new char [ n + 1 ] ; if ( n % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = ( char ) ( 48 + i + 2 ) ; else res [ i ] = ( char ) ( 48 + i ) ; } } else { for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = ( char ) ( 48 + i + 2 ) ; else res [ i ] = ( char ) ( 48 + i ) ; } res [ n - 1 ] = ( char ) ( 48 + n - 2 ) ; res [ n - 2 ] = ( char ) ( 48 + n ) ; res [ n - 3 ] = ( char ) ( 48 + n - 1 ) ; } res [ n ] = ' \0' ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( res [ i ] ) ; } } public static void Main ( ) { int n = 7 ; smallestPermute ( n ) ; } }
Minimum array insertions required to make consecutive difference <= K | C # implementation of above approach ; Function to return minimum number of insertions required ; Initialize insertions to 0 ; return total insertions ; Driver Code
using System ; class GFG { static int minInsertions ( int [ ] H , int n , int K ) { int inser = 0 ; for ( int i = 1 ; i < n ; ++ i ) { float diff = Math . Abs ( H [ i ] - H [ i - 1 ] ) ; if ( diff <= K ) continue ; else inser += ( int ) Math . Ceiling ( diff / K ) - 1 ; } return inser ; } static void Main ( ) { int [ ] H = new int [ ] { 2 , 4 , 8 , 16 } ; int K = 3 ; int n = H . Length ; Console . WriteLine ( minInsertions ( H , n , K ) ) ; } }
Minimum number of operations required to reduce N to 1 | C # implementation of above approach ; Function that returns the minimum number of operations to be performed to reduce the number to 1 ; To stores the total number of operations to be performed ; if n is divisible by 3 then reduce it to n / 3 ; if n modulo 3 is 1 decrement it by 1 ; if n modulo 3 is 2 then increment it by 1 ; update the counter ; Driver code
using System ; public class GFG { static int count_minimum_operations ( long n ) { int count = 0 ; while ( n > 1 ) { if ( n % 3 == 0 ) n /= 3 ; else if ( n % 3 == 1 ) n -- ; else { if ( n == 2 ) n -- ; else n ++ ; } count ++ ; } return count ; } static public void Main ( ) { long n = 4 ; long ans = count_minimum_operations ( n ) ; Console . WriteLine ( ans ) ; } }
Minimum number of operations required to reduce N to 1 | C # implementation of above approach ; Function that returns the minimum number of operations to be performed to reduce the number to 1 ; Base cases ; Driver code
using System ; class GFG { static int count_minimum_operations ( int n ) { if ( n == 2 ) { return 1 ; } else if ( n == 1 ) { return 0 ; } if ( n % 3 == 0 ) { return 1 + count_minimum_operations ( n / 3 ) ; } else if ( n % 3 == 1 ) { return 1 + count_minimum_operations ( n - 1 ) ; } else { return 1 + count_minimum_operations ( n + 1 ) ; } } static void Main ( ) { int n = 4 ; int ans = count_minimum_operations ( n ) ; Console . WriteLine ( ans ) ; } }
Maximize the sum of array by multiplying prefix of array with | C # implementation of the approach ; To store sum ; To store ending indices of the chosen prefix array vect ; Adding the absolute value of a [ i ] ; If i == 0 then there is no index to be flipped in ( i - 1 ) position ; print the maximised sum ; print the ending indices of the chosen prefix arrays ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void maxSum ( int [ ] a , int n ) { List < int > l = new List < int > ( ) ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += Math . Abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) continue ; if ( i == 0 ) l . Add ( i + 1 ) ; else { l . Add ( i + 1 ) ; l . Add ( i ) ; } } Console . WriteLine ( s ) ; for ( int i = 0 ; i < l . Count ; i ++ ) Console . Write ( l [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] a = { 1 , - 2 , - 3 , 4 } ; maxSum ( a , n ) ; } }
Find the longest common prefix between two strings after performing swaps on second string | C # program to find the longest common prefix between two strings after performing swaps on the second string ; int a = x . Length ; length of x int b = y . Length ; length of y ; creating frequency array of characters of y ; storing the length of longest common prefix ; checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 ; Driver Code
using System ; class GFG { static void LengthLCP ( String x , String y ) { int [ ] fr = new int [ 26 ] ; for ( int i = 0 ; i < b ; i ++ ) { fr [ y [ i ] - 97 ] += 1 ; } int c = 0 ; for ( int i = 0 ; i < a ; i ++ ) { if ( fr [ x [ i ] - 97 ] > 0 ) { c += 1 ; fr [ x [ i ] - 97 ] -= 1 ; } else break ; } Console . Write ( ( c ) ) ; } public static void Main ( ) { String x = " here " , y = " there " ; LengthLCP ( x , y ) ; } }
All possible co | C # implementation of the approach ; Function to count possible pairs ; total count of numbers in range ; printing count of pairs ; Driver code
using System ; class GFG { static void CountPair ( int L , int R ) { int x = ( R - L + 1 ) ; Console . WriteLine ( x / 2 + " STRNEWLINE " ) ; } public static void Main ( ) { int L , R ; L = 1 ; R = 8 ; CountPair ( L , R ) ; } }
Problems not solved at the end of Nth day | C # program to find problems not solved at the end of Nth day ; Function to find problems not solved at the end of Nth day ; Driver Code
using System ; class GFG { public static int problemsLeft ( int K , int P , int N ) { if ( K <= P ) return 0 ; else return ( ( K - P ) * N ) ; } public static void Main ( ) { int K , P , N ; K = 4 ; P = 1 ; N = 10 ; Console . WriteLine ( problemsLeft ( K , P , N ) ) ; } }
Kruskal 's Algorithm (Simple Implementation for Adjacency Matrix) | Simple C # implementation for Kruskal 's algorithm ; Find set of vertex i ; Does union of i and j . It returns false if i and j are already in same set . ; Finds MST using Kruskal 's algorithm ; Initialize sets of disjoint sets . ; Include minimum weight edges one by one ; Driver code ; Let us create the following graph 2 3 ( 0 ) -- ( 1 ) -- ( 2 ) | / \ | 6 | 8 / \ 5 | 7 | / \ | ( 3 ) -- -- -- - ( 4 ) 9 ; Print the solution
using System ; class GFG { static int V = 5 ; static int [ ] parent = new int [ V ] ; static int INF = int . MaxValue ; static int find ( int i ) { while ( parent [ i ] != i ) i = parent [ i ] ; return i ; } static void union1 ( int i , int j ) { int a = find ( i ) ; int b = find ( j ) ; parent [ a ] = b ; } static void kruskalMST ( int [ , ] cost ) { for ( int i = 0 ; i < V ; i ++ ) parent [ i ] = i ; int edge_count = 0 ; while ( edge_count < V - 1 ) { int min = INF , a = - 1 , b = - 1 ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( find ( i ) != find ( j ) && cost [ i , j ] < min ) { min = cost [ i , j ] ; a = i ; b = j ; } } } union1 ( a , b ) ; Console . Write ( " Edge ▁ { 0 } : ( { 1 } , ▁ { 2 } ) ▁ cost : { 3 } ▁ STRNEWLINE " , edge_count ++ , a , b , min ) ; mincost += min ; } Console . Write ( " cost = { 0 } " , mincost ) ; } public static void Main ( String [ ] args ) { int [ , ] cost = { { INF , 2 , INF , 6 , INF } , { 2 , INF , 3 , 8 , 5 } , { INF , 3 , INF , INF , 7 } , { 6 , 8 , INF , INF , 9 } , { INF , 5 , 7 , 9 , INF } , } ; kruskalMST ( cost ) ; } }
Number of chocolates left after k iterations | C # program to find remaining chocolates after k iterations ; Function to find the chocolates left ; Driver code
using System ; class GFG { static int results ( int n , int k ) { return ( int ) Math . Round ( Math . Pow ( n , ( 1.0 / Math . Pow ( 2.0 , k ) ) ) ) ; } public static void Main ( ) { int k = 3 , n = 100000000 ; Console . Write ( " Chocolates ▁ left ▁ after ▁ " + k + " ▁ iterations ▁ are ▁ " + results ( n , k ) ) ; } }
Subarray whose absolute sum is closest to K | C # code to find sub - array whose sum shows the minimum deviation ; Starting index , ending index , Deviation ; Iterate i and j to get all subarrays ; Found sub - array with less sum ; Exactly same sum ; Driver Code ; Array to store return values
using System ; class GFG { public static int [ ] getSubArray ( int [ ] arr , int n , int K ) { int i = - 1 ; int j = - 1 ; int currSum = 0 ; int [ ] result = { i , j , Math . Abs ( K - Math . Abs ( currSum ) ) } ; for ( i = 0 ; i < n ; i ++ ) { currSum = 0 ; for ( j = i ; j < n ; j ++ ) { currSum += arr [ j ] ; int currDev = Math . Abs ( K - Math . Abs ( currSum ) ) ; if ( currDev < result [ 2 ] ) { result [ 0 ] = i ; result [ 1 ] = j ; result [ 2 ] = currDev ; } if ( currDev == 0 ) return result ; } } return result ; } public static void Main ( string [ ] args ) { int [ ] arr = { 15 , - 3 , 5 , 2 , 7 , 6 , 34 , - 6 } ; int n = arr . Length ; int K = 50 ; int [ ] ans = getSubArray ( arr , n , K ) ; if ( ans [ 0 ] == - 1 ) { Console . Write ( " The ▁ empty ▁ array ▁ " + " shows ▁ minimum ▁ Deviation " ) ; } else { for ( int i = ans [ 0 ] ; i <= ans [ 1 ] ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Longest subsequence whose average is less than K | C # program to perform Q queries to find longest subsequence whose average is less than K ; Function to print the length for evey query ; sort array of N elements ; Array to store average from left ; Sort array of average ; number of queries ; print answer to every query using binary search ; Driver Code ; 4 queries
using System ; class GFG { static void longestSubsequence ( int [ ] a , int n , int [ ] q , int m ) { Array . Sort ( a ) ; int sum = 0 ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; double av = ( double ) ( sum ) / ( double ) ( i + 1 ) ; b [ i ] = ( ( int ) ( av + 1 ) ) ; } Array . Sort ( b ) ; for ( int i = 0 ; i < m ; i ++ ) { int k = q [ i ] ; int longest = upperBound ( b , 0 , n , k ) ; Console . WriteLine ( " Answer ▁ to ▁ Query " + ( i + 1 ) + " : ▁ " + longest ) ; } } private static int upperBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } static public void Main ( ) { int [ ] a = { 1 , 3 , 2 , 5 , 4 } ; int n = a . Length ; int [ ] q = { 4 , 2 , 1 , 5 } ; int m = q . Length ; longestSubsequence ( a , n , q , m ) ; } }
Make array elements equal in Minimum Steps | C # program to make the array elements equal in minimum steps ; Returns the minimum steps required to make an array of N elements equal , where the first array element equals M ; Corner Case 1 : When N = 1 ; Corner Case 2 : When N = 2 else if ( N == 2 ) corner case 2 ; Driver Code
using System ; class GFG { static int steps ( int N , int M ) { if ( N == 1 ) return 0 ; return M ; return 2 * M + ( N - 3 ) ; } public static void Main ( ) { int N = 4 , M = 4 ; Console . WriteLine ( steps ( N , M ) ) ; } }
Minimum increment / decrement to make array non | C # code to count the change required to convert the array into non - increasing array ; min heap ; Here in the loop we will check that whether the upcoming element of array is less than top of priority queue . If yes then we calculate the difference . After that we will remove that element and push the current element in queue . And the sum is incremented by the value of difference ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int DecreasingArray ( int [ ] a , int n ) { int sum = 0 , dif = 0 ; List < int > pq = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( pq . Count > 0 && pq [ 0 ] < a [ i ] ) { dif = a [ i ] - pq [ 0 ] ; sum += dif ; pq . RemoveAt ( 0 ) ; } pq . Add ( a [ i ] ) ; pq . Sort ( ) ; } return sum ; } static void Main ( ) { int [ ] a = { 3 , 1 , 2 , 1 } ; int n = a . Length ; Console . Write ( DecreasingArray ( a , n ) ) ; } }