text
stringlengths 17
3.65k
| code
stringlengths 60
5.26k
|
---|---|
Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters | C # program for the above approach ; Function to check if all characters of the string can be made the same ; Sort the string ; Calculate ASCII value of the median character ; Stores the minimum number of operations required to make all characters equal ; Traverse the string ; Calculate absolute value of current character and median character ; Print the minimum number of operations required ; Driver Code
|
using System ; public class GFG { static void sameChar ( String S , int N ) { char [ ] temp = S . ToCharArray ( ) ; Array . Sort ( temp ) ; String s = new String ( temp ) ; int mid = s [ N / 2 ] ; int total_operations = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total_operations += Math . Abs ( ( s [ i ] - 0 ) - mid ) ; } Console . Write ( total_operations ) ; } static public void Main ( ) { String S = " geeks " ; int N = S . Length ; sameChar ( S , N ) ; } }
|
Maximum score possible from an array with jumps of at most length K | C # program for the above approach ; Function to count the maximum score of an index ; Base Case ; If the value for the current index is pre - calculated ; Calculate maximum score for all the steps in the range from i + 1 to i + k ; Score for index ( i + j ) ; Update dp [ i ] and return the maximum value ; Function to get maximum score possible from the array A [ ] ; Array to store memoization ; Initialize dp [ ] with - 1 ; Driver Code
|
using System ; class GFG { static int maxScore ( int i , int [ ] A , int K , int N , int [ ] dp ) { if ( i >= N - 1 ) return A [ N - 1 ] ; if ( dp [ i ] != - 1 ) return dp [ i ] ; int score = int . MinValue ; for ( int j = 1 ; j <= K ; j ++ ) { score = Math . Max ( score , maxScore ( i + j , A , K , N , dp ) ) ; } return dp [ i ] = score + A [ i ] ; } static void getScore ( int [ ] A , int N , int K ) { int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ i ] = - 1 ; Console . WriteLine ( maxScore ( 0 , A , K , N , dp ) ) ; } static public void Main ( ) { int [ ] A = { 100 , - 30 , - 50 , - 15 , - 20 , - 30 } ; int K = 3 ; int N = A . Length ; getScore ( A , N , K ) ; } }
|
Count triplets from an array which can form quadratic equations with real roots | C # program for the above approach ; Function to find count of triplets ( a , b , c ) such that the equations ax ^ 2 + bx + c = 0 has real roots ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Generate all possible triplets ( a , b , c ) ; If the coefficient of X ^ 2 and X are equal ; If coefficient of X ^ 2 or x are equal to the constant ; Condition for having real roots ; Driver Code ; Function Call
|
using System ; using System . Collections . Generic ; public class GFG { static int getCount ( int [ ] arr , int N ) { int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { for ( int a = 0 ; a < N ; a ++ ) { if ( a == b ) continue ; for ( int c = 0 ; c < N ; c ++ ) { if ( c == a c == b ) continue ; int d = arr [ b ] * arr [ b ] / 4 ; if ( arr [ a ] * arr <= d ) count ++ ; } } } return count ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( getCount ( arr , N ) ) ; } }
|
Check if all array elements can be reduced to less than X | C # Program to implement the above approach ; Function to check if all array elements can be reduced to less than X or not ; Checks if all array elements are already a X or not ; Traverse every possible pair ; Calculate GCD of two array elements ; If gcd is a 1 ; If gcd is a X , then a pair is present to reduce all array elements to a X ; If no pair is present with gcd is a X ; Function to check if all array elements area X ; Function to calculate gcd of two numbers ; Driver Code
|
using System ; class GFG { public static bool findAns ( int [ ] A , int N , int X ) { if ( check ( A , X ) ) { return true ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int gcd = gcdFoo ( A [ i ] , A [ j ] ) ; if ( gcd != 1 ) { if ( gcd <= X ) { return true ; } } } } return false ; } public static bool check ( int [ ] A , int X ) { for ( int i = 0 ; i < A . Length ; i ++ ) { if ( A [ i ] > X ) { return false ; } } return true ; } static int gcdFoo ( int a , int b ) { if ( b == 0 ) return a ; return gcdFoo ( b , a % b ) ; } public static void Main ( String [ ] args ) { int X = 4 ; int [ ] A = { 2 , 1 , 5 , 3 , 6 } ; int N = 5 ; Console . WriteLine ( findAns ( A , N , X ) ) ; } }
|
Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y | C # program for the above approach ; Function to check if it is possible to choose X and Y elements from a [ ] and b [ ] such that maximum element among X element is less than minimum element among Y elements ; Check if there are atleast X elements in arr1 [ ] and atleast Y elements in arr2 [ ] ; Sort arrays in ascending order ; Check if ( X - 1 ) - th element in arr1 [ ] is less than from M - Yth element in arr2 [ ] ; Return false ; Driver Code ; Function Call
|
using System ; class GFG { static string check ( int [ ] a , int [ ] b , int Na , int Nb , int k , int m ) { if ( Na < k < m ) return " No " ; Array . Sort ( a ) ; Array . Sort ( b ) ; if ( a [ k - 1 ] < b [ Nb - m ] ) { return " Yes " ; } return " No " ; } static public void Main ( ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 3 , 4 , 5 } ; int N = arr1 . Length ; int M = arr2 . Length ; int X = 2 , Y = 1 ; Console . WriteLine ( check ( arr1 , arr2 , N , M , X , Y ) ) ; } }
|
Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum | C # program for the above approach ; Function to split the array into two subset such that the Bitwise XOR between the maximum of one subset and minimum of other is minimum ; Sort the array in increasing order ; Calculating the min Bitwise XOR between consecutive elements ; Return the final minimum Bitwise XOR ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
|
using System ; class GFG { static int splitArray ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int result = Int32 . MaxValue ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . Min ( result , arr [ i ] ^ arr [ i - 1 ] ) ; } return result ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . Length ; Console . Write ( splitArray ( arr , N ) ) ; } }
|
Sort an array by left shifting digits of array elements | C # program for the above approach ; Function to check if an array is sorted in increasing order or not ; Traverse the array ; Function to sort the array by left shifting digits of array elements ; Stores previous array element ; Traverse the array [ ] arr ; Stores current element ; Stores current element in String format ; Left - shift digits of current element in all possible ways ; Left - shift digits of current element by idx ; If temp greater than or equal to prev and temp less than optEle ; Update arr [ i ] ; Update prev ; If arr is in increasing order ; Otherwise ; Driver Code
|
using System ; public class GFG { static bool isIncreasing ( int [ ] arr ) { for ( int i = 0 ; i < arr . Length - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } static int [ ] sortArr ( int [ ] arr ) { int prev = - 1 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int optEle = arr [ i ] ; String strEle = String . Join ( " " , arr [ i ] ) ; for ( int idx = 0 ; idx < strEle . Length ; idx ++ ) { String strEle2 = strEle . Substring ( idx ) + strEle . Substring ( 0 , idx ) ; int temp = Int32 . Parse ( strEle2 ) ; if ( temp >= prev && temp < optEle ) optEle = temp ; } arr [ i ] = optEle ; prev = arr [ i ] ; } if ( isIncreasing ( arr ) ) return arr ; else { return new int [ ] { - 1 } ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 511 , 321 , 323 , 432 , 433 } ; int [ ] res = sortArr ( arr ) ; for ( int i = 0 ; i < res . Length ; i ++ ) Console . Write ( res [ i ] + " β " ) ; } }
|
Maximize maximum possible subarray sum of an array by swapping with elements from another array | C # program for the above approach ; Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ] ; Stores elements from the arrays arr [ ] and brr [ ] ; Store elements of array arr [ ] and brr [ ] in the vector crr ; Sort the vector crr in descending order ; Stores maximum sum ; Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element ; Print the sum ; Driver Code ; Given arrays and respective lengths ; Calculate maximum subarray sum
|
using System ; using System . Collections . Generic ; class GFG { static void maxSum ( int [ ] arr , int [ ] brr , int N , int K ) { List < int > crr = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { crr . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < K ; i ++ ) { crr . Add ( brr [ i ] ) ; } crr . Sort ( ) ; crr . Reverse ( ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( crr [ i ] > 0 ) { sum += crr [ i ] ; } else { break ; } } Console . WriteLine ( sum ) ; } static void Main ( ) { int [ ] arr = { 7 , 2 , - 1 , 4 , 5 } ; int N = arr . Length ; int [ ] brr = { 1 , 2 , 3 , 2 } ; int K = brr . Length ; maxSum ( arr , brr , N , K ) ; } }
|
Count pairs with Bitwise XOR greater than both the elements of the pair | C # program for the above approach ; Function that counts the pairs whose Bitwise XOR is greater than both the elements of pair ; Stores the count of pairs ; Generate all possible pairs ; Find the Bitwise XOR ; Find the maximum of two ; If xo < mx , increment count ; Print the value of count ; Driver Code ; Function Call
|
using System ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int xo = ( A [ i ] ^ A [ j ] ) ; int mx = Math . Max ( A [ i ] , A [ j ] ) ; if ( xo > mx ) { count ++ ; } } } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }
|
Possible arrangement of persons waiting to sit in a hall | C # program for the above approach ; Function to find the arrangement of seating ; Stores the row in which the ith person sits ; Stores the width of seats along with their index or row number ; Sort the array ; Store the seats and row for boy 's seat ; Stores the index of row upto which boys have taken seat ; Iterate the string ; Push the row number at index in vector and heap ; Increment the index to let the next boy in the next minimum width vacant row ; Otherwise ; If girl then take top of element of the max heap ; Pop from queue ; Print the values ; Driver code ; Given N ; Given arr [ ] ; Given string ; Function Call
|
using System ; using System . Collections . Generic ; class GFG { static void findTheOrder ( int [ ] arr , string s , int N ) { List < int > ans = new List < int > ( ) ; Tuple < int , int > [ ] A = new Tuple < int , int > [ N ] ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = new Tuple < int , int > ( arr [ i ] , i + 1 ) ; Array . Sort ( A ) ; List < Tuple < int , int > > q = new List < Tuple < int , int > > ( ) ; int index = 0 ; for ( int i = 0 ; i < 2 * N ; i ++ ) { if ( s [ i ] == '0' ) { ans . Add ( A [ index ] . Item2 ) ; q . Add ( A [ index ] ) ; q . Sort ( ) ; q . Reverse ( ) ; index ++ ; } else { ans . Add ( q [ 0 ] . Item2 ) ; q . RemoveAt ( 0 ) ; } } foreach ( int i in ans ) { Console . Write ( i + " β " ) ; } } static void Main ( ) { int N = 3 ; int [ ] arr = { 2 , 1 , 3 } ; string s = "001011" ; findTheOrder ( arr , s , N ) ; } }
|
Difference between sum of K maximum even and odd array elements | C # program for the above approach ; Function to find the absolute difference between sum of first K maximum even and odd numbers ; Stores index from where odd number starts ; Segregate even and odd number ; If current element is even ; Sort in decreasing order even part ; Sort in decreasing order odd part ; Calculate sum of k maximum even number ; Calculate sum of k maximum odd number ; Print the absolute difference ; Driver Code ; Given array [ ] arr ; Size of array ; Function Call
|
using System ; using System . Collections . Generic ; class GFG { static void evenOddDiff ( int [ ] a , int n , int k ) { int j = - 1 ; List < int > even = new List < int > ( ) ; List < int > odd = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) { even . Add ( a [ i ] ) ; } else odd . Add ( a [ i ] ) ; } j ++ ; even . Sort ( ) ; even . Reverse ( ) ; odd . Sort ( ) ; odd . Reverse ( ) ; int evenSum = 0 , oddSum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { evenSum += even [ i ] ; } for ( int i = 0 ; i < k ; i ++ ) { oddSum += odd [ i ] ; } Console . Write ( Math . Abs ( evenSum - oddSum ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 8 , 3 , 4 , 5 } ; int N = arr . Length ; int K = 2 ; evenOddDiff ( arr , N , K ) ; } }
|
Rearrange array to make product of prefix sum array non zero | C # program to implement the above approach ; Function to print array elements ; Function to rearrange array that satisfies the given condition ; Stores sum of elements of the given array ; Calculate totalSum ; If the totalSum is equal to 0 ; No possible way to rearrange array ; If totalSum exceeds 0 ; Rearrange the array in descending order ; Otherwise ; Rearrange the array in ascending order ; Driver Code
|
using System ; class GFG { static void printArr ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " β " ) ; } } static void rearrangeArr ( int [ ] arr , int N ) { int totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } if ( totalSum == 0 ) { Console . Write ( " - 1" + " STRNEWLINE " ) ; } else if ( totalSum > 0 ) { Array . Sort ( arr ) ; arr = reverse ( arr ) ; printArr ( arr , N ) ; } else { Array . Sort ( arr ) ; printArr ( arr , N ) ; } } static int [ ] reverse ( int [ ] a ) { int i , n = a . Length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , - 1 , - 2 , 3 } ; int N = arr . Length ; rearrangeArr ( arr , N ) ; } }
|
Split array into two equal length subsets such that all repetitions of a number lies in a single subset | C # program for the above approach ; Function to create the frequency array of the given array arr [ ] ; Hashmap to store the frequencies ; Store freq for each element ; Get the total frequencies ; Store frequencies in subset [ ] array ; Return frequency array ; Function to check is sum N / 2 can be formed using some subset ; dp [ i ] [ j ] store the answer to form sum j using 1 st i elements ; Initialize dp [ ] [ ] with true ; Fill the subset table in the bottom up manner ; If current element is less than j ; Update current state ; Return the result ; Function to check if the given array can be split into required sets ; Store frequencies of arr [ ] ; If size of arr [ ] is odd then print " Yes " ; Check if answer is true or not ; Print the result ; Driver code ; Given array arr [ ] ; Function Call
|
using System ; using System . Collections . Generic ; class GFG { static int [ ] findSubsets ( int [ ] arr ) { Dictionary < int , int > M = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( M . ContainsKey ( arr [ i ] ) ) { M [ arr [ i ] ] ++ ; } else { M [ arr [ i ] ] = 1 ; } } int [ ] subsets = new int [ M . Count ] ; int I = 0 ; foreach ( KeyValuePair < int , int > playerEntry in M ) { subsets [ I ] = playerEntry . Value ; I ++ ; } return subsets ; } static bool subsetSum ( int [ ] subsets , int target ) { bool [ , ] dp = new bool [ subsets . Length + 1 , target + 1 ] ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) dp [ i , 0 ] = true ; for ( int i = 1 ; i <= subsets . Length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { dp [ i , j ] = dp [ i - 1 , j ] ; if ( j >= subsets [ i - 1 ] ) { dp [ i , j ] |= dp [ i - 1 , j - subsets [ i - 1 ] ] ; } } } return dp [ subsets . Length , target ] ; } static void divideInto2Subset ( int [ ] arr ) { int [ ] subsets = findSubsets ( arr ) ; if ( ( arr . Length ) % 2 == 1 ) { Console . WriteLine ( " No " ) ; return ; } bool isPossible = subsetSum ( subsets , arr . Length / 2 ) ; if ( isPossible ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } static void Main ( ) { int [ ] arr = { 2 , 1 , 2 , 3 } ; divideInto2Subset ( arr ) ; } }
|
Count ways to distribute exactly one coin to each worker | C # program for the above approach ; Function to find number of way to distribute coins giving exactly one coin to each person ; Sort the given arrays ; Start from bigger salary ; Increment the amount ; Reduce amount of valid coins by one each time ; Return the result ; Driver code ; Given two arrays ; Function Call
|
using System ; using System . Collections ; class GFG { static int MOD = 1000000007 ; static int solve ( ArrayList values , ArrayList salary ) { int ret = 1 ; int amt = 0 ; values . Sort ( ) ; salary . Sort ( ) ; while ( salary . Count > 0 ) { while ( values . Count > 0 && ( int ) values [ values . Count - 1 ] >= ( int ) salary [ salary . Count - 1 ] ) { amt ++ ; values . RemoveAt ( values . Count - 1 ) ; } if ( amt == 0 ) return 0 ; ret *= amt -- ; ret %= MOD ; salary . RemoveAt ( salary . Count - 1 ) ; } return ret ; } public static void Main ( string [ ] args ) { ArrayList values = new ArrayList ( ) ; values . Add ( 1 ) ; values . Add ( 2 ) ; ArrayList salary = new ArrayList ( ) ; salary . Add ( 2 ) ; Console . Write ( solve ( values , salary ) ) ; } }
|
Count of index pairs with equal elements in an array | Set 2 | C # program for the above approach ; Function that counts the pair in the array [ ] arr ; Sort the array ; Initialize two pointers ; Add all valid pairs to answer ; Return the answer ; Driver Code ; Given array [ ] arr ; Function call
|
using System ; class GFG { static int countPairs ( int [ ] arr , int n ) { int ans = 0 ; Array . Sort ( arr ) ; int left = 0 , right = 1 ; while ( right < n ) { if ( arr [ left ] == arr [ right ] ) ans += right - left ; else left = right ; right ++ ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 3 , 2 , 3 } ; int N = arr . Length ; Console . Write ( countPairs ( arr , N ) ) ; } }
|
Minimize Cost to sort a String in Increasing Order of Frequencies of Characters | C # program to implement above approach ; For a single character ; Stores count of repetitions of a character ; If repeating character ; Otherwise ; Store frequency ; Reset count ; Insert the last character block ; Sort the frequencies ; Stores the minimum cost of all operations ; Store the absolute difference of i - th frequencies of ordered and unordered sequences ; Return the minimum cost ; Driver Code
|
using System ; using System . Collections . Generic ; class GFG { public static int sortString ( string S ) { List < int > sorted = new List < int > ( ) ; List < int > original = new List < int > ( ) ; bool insert = false ; if ( S . Length == 1 ) { Console . WriteLine ( 0 ) ; } int curr = 1 ; for ( int i = 0 ; i < ( S . Length - 1 ) ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) { curr += 1 ; insert = false ; } else { sorted . Add ( curr ) ; original . Add ( curr ) ; curr = 1 ; insert = true ; } } if ( ( S [ S . Length - 1 ] != S [ S . Length - 2 ] ) insert == false ) { sorted . Add ( curr ) ; original . Add ( curr ) ; } sorted . Sort ( ) ; int t_cost = 0 ; for ( int i = 0 ; i < sorted . Count ; i ++ ) { t_cost += Math . Abs ( sorted [ i ] - original [ i ] ) ; } return ( t_cost / 2 ) ; } static void Main ( ) { string S = " aabbcccdeffffggghhhhhii " ; Console . Write ( sortString ( S ) ) ; } }
|
Check if the Left View of the given tree is sorted or not | C # implementation to check if the left view of the given tree is sorted or not ; Binary Tree Node ; Utility function to create a new node ; Function to find left view and check if it is sorted ; Queue to hold values ; Variable to check whether level order is sorted or not ; Iterate until the queue is empty ; Traverse every level in tree ; Variable for initial level ; Checking values are sorted or not ; Push left value if it is not null ; Push right value if it is not null ; Pop out the values from queue ; Check if the value are not sorted then break the loop ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { class node { public int val ; public node right , left ; } ; static node newnode ( int key ) { node temp = new node ( ) ; temp . val = key ; temp . right = null ; temp . left = null ; return temp ; } static void func ( node root ) { Queue < node > q = new Queue < node > ( ) ; bool t = true ; q . Enqueue ( root ) ; int i = - 1 , j = - 1 ; while ( q . Count != 0 ) { int h = q . Count ; while ( h > 0 ) { root = q . Peek ( ) ; if ( i == - 1 ) { j = root . val ; } if ( i == - 2 ) { if ( j <= root . val ) { j = root . val ; i = - 3 ; } else { t = false ; break ; } } if ( root . left != null ) { q . Enqueue ( root . left ) ; } if ( root . right != null ) { q . Enqueue ( root . right ) ; } h = h - 1 ; q . Dequeue ( ) ; } i = - 2 ; if ( t == false ) { break ; } } if ( t ) Console . Write ( " true " + " STRNEWLINE " ) ; else Console . Write ( " false " + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { node root = newnode ( 10 ) ; root . right = newnode ( 50 ) ; root . right . right = newnode ( 15 ) ; root . left = newnode ( 20 ) ; root . left . left = newnode ( 50 ) ; root . left . right = newnode ( 23 ) ; root . right . left = newnode ( 10 ) ; func ( root ) ; } }
|
Divide a sorted array in K parts with sum of difference of max and min minimized in each part | C # program to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Function to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Array to store the differences between two adjacent elements ; Iterating through the array ; Storing differences to p ; Sorting p in descending order ; Sum of the first k - 1 values of p ; Computing the result ; Driver code
|
using System ; class GFG { static int calculate_minimum_split ( int n , int [ ] a , int k ) { int [ ] p = new int [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i - 1 ] = a [ i ] - a [ i - 1 ] ; Array . Sort ( p ) ; Array . Reverse ( p ) ; int min_sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) min_sum += p [ i ] ; int res = a [ n - 1 ] - a [ 0 ] - min_sum ; return res ; } static void Main ( ) { int [ ] arr = { 4 , 8 , 15 , 16 , 23 , 42 } ; int k = 3 ; int n = arr . Length ; Console . Write ( calculate_minimum_split ( n , arr , k ) ) ; } }
|
Sort an Array of dates in ascending order using Custom Comparator | C # implementation to sort the array of dates in the form of " DD - MM - YYYY " using custom comparator ; Comparator to sort the array of dates ; Condition to check the year ; Condition to check the month ; Condition to check the day ; Function that prints the dates in ascensding order ; Sort the dates using library sort function with custom Comparator ; Loop to print the dates ; Driver Code
|
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int myCompare ( string date1 , string date2 ) { string day1 = date1 . Substring ( 0 , 2 ) ; string month1 = date1 . Substring ( 3 , 2 ) ; string year1 = date1 . Substring ( 6 , 4 ) ; string day2 = date2 . Substring ( 0 , 2 ) ; string month2 = date2 . Substring ( 3 , 2 ) ; string year2 = date2 . Substring ( 6 , 4 ) ; return string . Compare ( year1 , year2 ) ; return string . Compare ( month1 , month2 ) ; return string . Compare ( day1 , day2 ) ; } static void printDatesAscending ( List < string > arr ) { arr . Sort ( myCompare ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) Console . WriteLine ( arr [ i ] ) ; } static public void Main ( ) { List < string > arr = new List < string > ( ) ; arr . Add ( "25-08-1996" ) ; arr . Add ( "03-08-1970" ) ; arr . Add ( "09-04-1994" ) ; arr . Add ( "29-08-1996" ) ; arr . Add ( "14-02-1972" ) ; printDatesAscending ( arr ) ; } }
|
Sort an Array of Strings according to the number of Vowels in them | C # implementation of the approach ; Function to check the Vowel ; Returns count of vowels in str ; if ( isVowel ( str [ i ] ) ) Check for vowel ; Function to sort the array according to the number of the vowels ; Vector to store the number of vowels with respective elements ; Inserting number of vowels with respective strings in the vector pair ; Sort the vector , this will sort the pair according to the number of vowels ; Print the sorted vector content ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static bool isVowel ( char ch ) { ch = char . ToUpper ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) ; } static int countVowels ( string str ) { int count = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) ++ count ; return count ; } static void sortArr ( string [ ] arr , int n ) { List < Tuple < int , string > > vp = new List < Tuple < int , string > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { vp . Add ( new Tuple < int , string > ( countVowels ( arr [ i ] ) , arr [ i ] ) ) ; } vp . Sort ( ) ; for ( int i = 0 ; i < vp . Count ; i ++ ) Console . Write ( vp [ i ] . Item2 + " β " ) ; } static void Main ( ) { string [ ] arr = { " lmno " , " pqrst " , " aeiou " , " xyz " } ; int n = arr . Length ; sortArr ( arr , n ) ; } }
|
Find the minimum cost to cross the River | C # implementation of the approach ; Function to return the minimum cost ; Sort the price array ; Calculate minimum price of n - 2 most costly person ; Both the ways as discussed above ; Calculate the minimum price of the two cheapest person ; Driver code
|
using System ; class GFG { static long minimumCost ( long [ ] price , int n ) { Array . Sort ( price ) ; long totalCost = 0 ; for ( int i = n - 1 ; i > 1 ; i -= 2 ) { if ( i == 2 ) { totalCost += price [ 2 ] + price [ 0 ] ; } else { long price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] ; long price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] ; totalCost += Math . Min ( price_first , price_second ) ; } } if ( n == 1 ) { totalCost += price [ 0 ] ; } else { totalCost += price [ 1 ] ; } return totalCost ; } public static void Main ( ) { long [ ] price = { 30 , 40 , 60 , 70 } ; int n = price . Length ; Console . WriteLine ( minimumCost ( price , n ) ) ; } }
|
Minimize the sum of differences of consecutive elements after removing exactly K elements | C # implementation of the above approach . ; states of DP ; function to find minimum sum ; base - case ; if state is solved before , return ; marking the state as solved ; recurrence relation ; Driver function ; input values ; calling the required function ;
|
using System ; class GFG { static int N = 100 ; static int [ , ] dp = new int [ N , N ] ; static int [ , ] vis = new int [ N , N ] ; static int findSum ( int [ ] arr , int n , int k , int l , int r ) { if ( ( l ) + ( n - 1 - r ) == k ) return arr [ r ] - arr [ l ] ; if ( vis [ l , r ] == 1 ) return dp [ l , r ] ; vis [ l , r ] = 1 ; dp [ l , r ] = Math . Min ( findSum ( arr , n , k , l , r - 1 ) , findSum ( arr , n , k , l + 1 , r ) ) ; return dp [ l , r ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n , k , 0 , n - 1 ) ) ; } }
|
Find Kth element in an array containing odd elements first and then even elements | C # implementation of the approach ; Function to return the kth element in the modified array ; Finding the index from where the even numbers will be stored ; Return the kth element ; Driver code
|
using System ; class GFG { static int getNumber ( int n , int k ) { int pos ; if ( ( n % 2 ) == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } static public void Main ( ) { int n = 8 , k = 5 ; Console . Write ( getNumber ( n , k ) ) ; } }
|
All unique combinations whose sum equals to K | C # implementation of the approach ; Function to find all unique combination of given elements such that their sum is K ; If a unique combination is found ; For all other combinations ; Check if the sum exceeds K ; Check if it is repeated or not ; Take the element into the combination ; Recursive call ; Remove element from the combination ; Function to find all combination of the given elements ; Sort the given elements ; To store combination ; Driver code ; Function call
|
using System ; using System . Collections . Generic ; class GFG { static void unique_combination ( int l , int sum , int K , List < int > local , List < int > A ) { if ( sum == K ) { Console . Write ( " { " ) ; for ( int i = 0 ; i < local . Count ; i ++ ) { if ( i != 0 ) Console . Write ( " β " ) ; Console . Write ( local [ i ] ) ; if ( i != local . Count - 1 ) Console . Write ( " , β " ) ; } Console . WriteLine ( " } " ) ; return ; } for ( int i = l ; i < A . Count ; i ++ ) { if ( sum + A [ i ] > K ) continue ; if ( i > l && A [ i ] == A [ i - 1 ] ) continue ; local . Add ( A [ i ] ) ; unique_combination ( i + 1 , sum + A [ i ] , K , local , A ) ; local . RemoveAt ( local . Count - 1 ) ; } } static void Combination ( List < int > A , int K ) { A . Sort ( ) ; List < int > local = new List < int > ( ) ; unique_combination ( 0 , 0 , K , local , A ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 1 , 2 , 7 , 6 , 1 , 5 } ; List < int > A = new List < int > ( arr ) ; int K = 8 ; Combination ( A , K ) ; } }
|
Find a triplet in an array whose sum is closest to a given number | C # implementation of the above approach ; Function to return the sum of a triplet which is closest to x ; To store the closest sum ; Run three nested loops each loop for each element of triplet ; Update the closestSum ; Return the closest sum found ; Driver code
|
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int solution ( ArrayList arr , int x ) { int closestSum = int . MaxValue ; for ( int i = 0 ; i < arr . Count ; i ++ ) { for ( int j = i + 1 ; j < arr . Count ; j ++ ) { for ( int k = j + 1 ; k < arr . Count ; k ++ ) { if ( Math . Abs ( x - closestSum ) > Math . Abs ( x - ( ( int ) arr [ i ] + ( int ) arr [ j ] + ( int ) arr [ k ] ) ) ) { closestSum = ( ( int ) arr [ i ] + ( int ) arr [ j ] + ( int ) arr [ k ] ) ; } } } } return closestSum ; } public static void Main ( string [ ] args ) { ArrayList arr = new ArrayList ( ) { - 1 , 2 , 1 , - 4 } ; int x = 1 ; Console . Write ( solution ( arr , x ) ) ; } }
|
Build original array from the given sub | C # implementation of the approach ; Function to add edge to graph ; Function to calculate indegrees of all the vertices ; If there is an edge from i to x then increment indegree of x ; Function to perform topological sort ; Push every node to the queue which has no incoming edge ; Since edge u is removed , update the indegrees of all the nodes which had an incoming edge from u ; Function to generate the array from the given sub - sequences ; Create the graph from the input sub - sequences ; Add edge between every two consecutive elements of the given sub - sequences ; Get the indegrees for all the vertices ; Get the topological order of the created graph ; Driver code ; Size of the required array ; Given sub - sequences of the array ; Get the resultant array as vector ; Printing the array
|
using System ; using System . Collections . Generic ; class GFG { static void addEdge ( List < List < int > > adj , int u , int v ) { adj [ u ] . Add ( v ) ; } static void getindeg ( List < List < int > > adj , int V , List < int > indeg ) { for ( int i = 0 ; i < V ; i ++ ) { foreach ( int x in adj [ i ] ) { indeg [ x ] ++ ; } } } static List < int > topo ( List < List < int > > adj , int V , List < int > indeg ) { Queue < int > q = new Queue < int > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { if ( indeg [ i ] == 0 ) { q . Enqueue ( i ) ; } } List < int > res = new List < int > ( ) ; while ( q . Count > 0 ) { int u = q . Dequeue ( ) ; res . Add ( u ) ; foreach ( int x in adj [ u ] ) { indeg [ x ] -- ; if ( indeg [ x ] == 0 ) { q . Enqueue ( x ) ; } } } return res ; } static List < int > makearray ( List < List < int > > v , int V ) { List < List < int > > adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < v . Count ; i ++ ) { for ( int j = 0 ; j < v [ i ] . Count - 1 ; j ++ ) { addEdge ( adj , v [ i ] [ j ] , v [ i ] [ j + 1 ] ) ; } } List < int > indeg = new List < int > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { indeg . Add ( 0 ) ; } getindeg ( adj , V , indeg ) ; List < int > res = topo ( adj , V , indeg ) ; return res ; } static public void Main ( ) { int n = 10 ; List < List < int > > subseqs = new List < List < int > > ( ) ; subseqs . Add ( new List < int > ( ) { 9 , 1 , 2 , 8 , 3 } ) ; subseqs . Add ( new List < int > ( ) { 6 , 1 , 2 } ) ; subseqs . Add ( new List < int > ( ) { 9 , 6 , 3 , 4 } ) ; subseqs . Add ( new List < int > ( ) { 5 , 2 , 7 } ) ; subseqs . Add ( new List < int > ( ) { 0 , 9 , 5 , 4 } ) ; List < int > res = makearray ( subseqs , n ) ; foreach ( int x in res ) { Console . Write ( x + " β " ) ; } } }
|
Sum of Semi | C # implementation of the above approach ; Vector to store the primes ; Create a boolean array " prime [ 0 . . n ] " ; Initialize along prime values to be true ; If prime [ p ] is not changed then it is a prime ; Update along multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked ; Print all prime numbers ; Function to return the semi - prime sum ; Variable to store the sum of semi - primes ; Iterate over the prime values ; Break the loop once the product exceeds N ; Add valid products which are less than or equal to N each product is a semi - prime number ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static List < long > pr = new List < long > ( ) ; static bool [ ] prime = new bool [ 10000000 + 1 ] ; static void sieve ( long n ) { for ( int i = 2 ; i <= n ; i += 1 ) { prime [ i ] = true ; } for ( int p = 2 ; ( int ) p * ( int ) p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = ( int ) p * ( int ) p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) pr . Add ( ( long ) p ) ; } static long SemiPrimeSum ( long N ) { long ans = 0 ; for ( int i = 0 ; i < pr . Count ; i += 1 ) { for ( int j = i ; j < pr . Count ; j += 1 ) { if ( ( long ) pr [ i ] * ( long ) pr [ j ] > N ) break ; ans += ( long ) pr [ i ] * ( long ) pr [ j ] ; } } return ans ; } public static void Main ( String [ ] args ) { long N = 6 ; sieve ( N ) ; Console . WriteLine ( SemiPrimeSum ( N ) ) ; } }
|
Compress the array into Ranges | C # program to compress the array ranges ; Function to compress the array ranges ; start iteration from the ith array element ; loop until arr [ i + 1 ] == arr [ i ] and increment j ; if the program do not enter into the above while loop this means that ( i + 1 ) th element is not consecutive to i th element ; increment i for next iteration ; print the consecutive range found ; move i jump directly to j + 1 ; Driver code
|
using System ; class GFG { static void compressArr ( int [ ] arr , int n ) { int i = 0 , j = 0 ; Array . Sort ( arr ) ; while ( i < n ) { j = i ; while ( ( j + 1 < n ) && ( arr [ j + 1 ] == arr [ j ] + 1 ) ) { j ++ ; } if ( i == j ) { Console . Write ( arr [ i ] + " β " ) ; i ++ ; } else { Console . Write ( arr [ i ] + " - " + arr [ j ] + " β " ) ; i = j + 1 ; } } } public static void Main ( ) { int n = 7 ; int [ ] arr = { 1 , 3 , 4 , 5 , 6 , 9 , 10 } ; compressArr ( arr , n ) ; } }
|
Remove elements to make array sorted | C # implementation of the approach ; Function to sort the array by removing misplaced elements ; brr [ ] is used to store the sorted array elements ; Print the sorted array ; Driver code
|
using System ; class GFG { static void removeElements ( int [ ] arr , int n ) { int [ ] brr = new int [ n ] ; int l = 1 ; brr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( brr [ l - 1 ] <= arr [ i ] ) { brr [ l ] = arr [ i ] ; l ++ ; } } for ( int i = 0 ; i < l ; i ++ ) Console . Write ( brr [ i ] + " β " ) ; } static public void Main ( ) { int [ ] arr = { 10 , 12 , 9 , 10 , 2 , 13 , 14 } ; int n = arr . Length ; removeElements ( arr , n ) ; } }
|
Remove elements to make array sorted | C # implementation of the approach ; Function to sort the array by removing misplaced elements ; l stores the index ; Print the sorted array ; Driver code
|
using System ; class GFG { public static void removeElements ( int [ ] arr , int n ) { int l = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ l - 1 ] <= arr [ i ] ) { arr [ l ] = arr [ i ] ; l ++ ; } } for ( int i = 0 ; i < l ; i ++ ) Console . Write ( arr [ i ] + " β " ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 10 , 12 , 9 , 10 , 2 , 13 , 14 } ; int n = arr . Length ; removeElements ( arr , n ) ; } }
|
Find number from its divisors | C # implementation of the approach ; Function that returns X ; Sort the given array ; Get the possible X ; Container to store divisors ; Find the divisors of a number ; Check if divisor ; sort the vec because a is sorted and we have to compare all the elements ; if size of both vectors is not same then we are sure that both vectors can 't be equal ; Check if a and vec have same elements in them ; Driver code ; Function call
|
using System ; using System . Collections . Generic ; class GFG { static int findX ( int [ ] a , int n ) { Array . Sort ( a ) ; int x = a [ 0 ] * a [ n - 1 ] ; List < int > vec = new List < int > ( ) ; for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { vec . Add ( i ) ; if ( ( x / i ) != i ) vec . Add ( x / i ) ; } } vec . Sort ( ) ; if ( vec . Count != n ) { return - 1 ; } else { int i = 0 ; foreach ( int it in vec ) { if ( a [ i ++ ] != it ) return - 1 ; } } return x ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 5 , 4 , 10 } ; int n = a . Length ; Console . Write ( findX ( a , n ) ) ; } }
|
Program to print an array in Pendulum Arrangement with constant space | C # implementation of the approach ; Function to print the Pendulum arrangement of the given array ; Sort the array sort ( arr , arr + n ) ; ; pos stores the index of the last element of the array ; odd stores the last odd index in the array ; Move all odd index positioned elements to the right ; Shift the elements by one position from odd to pos ; Reverse the element from 0 to ( n - 1 ) / 2 ; Printing the pendulum arrangement ; Driver code
|
using System ; class GFG { static void pendulumArrangement ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int odd , temp , p , pos ; pos = n - 1 ; if ( n % 2 == 0 ) odd = n - 1 ; else odd = n - 2 ; while ( odd > 0 ) { temp = arr [ odd ] ; p = odd ; while ( p != pos ) { arr [ p ] = arr [ p + 1 ] ; p ++ ; } arr [ p ] = temp ; odd = odd - 2 ; pos = pos - 1 ; } int start = 0 , end = ( n - 1 ) / 2 ; for ( ; start < end ; start ++ , end -- ) { temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " β " ) ; } public static void Main ( ) { int [ ] arr = { 11 , 2 , 4 , 55 , 6 , 8 } ; int n = arr . Length ; pendulumArrangement ( arr , n ) ; } }
|
Find all the pairs with given sum in a BST | Set 2 | C # program to implement the above approach ; A binary tree node ; Function to add a node to the BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Function to find the target pairs ; LeftList which stores the left side values ; RightList which stores the right side values ; curr_left pointer is used for left side execution and curr_right pointer is used for right side execution ; Storing the left side values into LeftList till leaf node not found ; Storing the right side values into RightList till leaf node not found ; Last node of LeftList ; Last node of RightList ; To prevent repetition like 2 , 6 and 6 , 2 ; Delete the last value of LeftList and make the execution to the right side ; Delete the last value of RightList and make the execution to the left side ; ( left value + right value ) = target then print the left value and right value Delete the last value of left and right list and make the left execution to right side and right side execution to left side ; Driver code
|
using System . Collections . Generic ; using System ; class GFG { public class Node { public int data ; public Node left , right , root ; public Node ( int data ) { this . data = data ; } } public static Node AddNode ( Node root , int data ) { if ( root == null ) { root = new Node ( data ) ; return root ; } if ( root . data < data ) root . right = AddNode ( root . right , data ) ; else if ( root . data > data ) root . left = AddNode ( root . left , data ) ; return root ; } public static void TargetPair ( Node node , int tar ) { List < Node > LeftList = new List < Node > ( ) ; List < Node > RightList = new List < Node > ( ) ; Node curr_left = node ; Node curr_right = node ; while ( curr_left != null curr_right != null LeftList . Count > 0 && RightList . Count > 0 ) { while ( curr_left != null ) { LeftList . Add ( curr_left ) ; curr_left = curr_left . left ; } while ( curr_right != null ) { RightList . Add ( curr_right ) ; curr_right = curr_right . right ; } Node LeftNode = LeftList [ LeftList . Count - 1 ] ; Node RightNode = RightList [ RightList . Count - 1 ] ; int leftVal = LeftNode . data ; int rightVal = RightNode . data ; if ( leftVal >= rightVal ) break ; if ( leftVal + rightVal < tar ) { LeftList . RemoveAt ( LeftList . Count - 1 ) ; curr_left = LeftNode . right ; } else if ( leftVal + rightVal > tar ) { RightList . RemoveAt ( RightList . Count - 1 ) ; curr_right = RightNode . left ; } else { Console . WriteLine ( LeftNode . data + " β " + RightNode . data ) ; RightList . RemoveAt ( RightList . Count - 1 ) ; LeftList . RemoveAt ( LeftList . Count - 1 ) ; curr_left = LeftNode . right ; curr_right = RightNode . left ; } } } public static void Main ( String [ ] b ) { Node root = null ; root = AddNode ( root , 2 ) ; root = AddNode ( root , 6 ) ; root = AddNode ( root , 5 ) ; root = AddNode ( root , 3 ) ; root = AddNode ( root , 4 ) ; root = AddNode ( root , 1 ) ; root = AddNode ( root , 7 ) ; int sum = 8 ; TargetPair ( root , sum ) ; } }
|
Minimum number greater than the maximum of array which cannot be formed using the numbers in the array | C # implementation of the approach ; Function that returns the minimum number greater than maximum of the array that cannot be formed using the elements of the array ; Sort the given array ; Maximum number in the array ; table [ i ] will store the minimum number of elements from the array to form i ; Calculate the minimum number of elements from the array required to form the numbers from 1 to ( 2 * max ) ; If there exists a number greater than the maximum element of the array that can be formed using the numbers of array ; Driver code
|
using System ; class GFG { static int findNumber ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int max = arr [ n - 1 ] ; int [ ] table = new int [ ( 2 * max ) + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) table [ i ] = int . MaxValue ; int ans = - 1 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] <= i ) { int res = table [ i - arr [ j ] ] ; if ( res != int . MaxValue && res + 1 < table [ i ] ) table [ i ] = res + 1 ; } } if ( i > arr [ n - 1 ] && table [ i ] == int . MaxValue ) { ans = i ; break ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 6 , 7 , 15 } ; int n = arr . Length ; Console . WriteLine ( findNumber ( arr , n ) ) ; } }
|
Product of all Subsequences of size K except the minimum and maximum Elements | C # program to find product of all Subsequences of size K except the minimum and maximum Elements ; 2D array to store value of combinations nCr ; Function to pre - calculate value of all combinations nCr ; Function to calculate product of all subsequences except the minimum and maximum elements ; Sorting array so that it becomes easy to calculate the number of times an element will come in first or last place ; An element will occur ' powa ' times in total of which ' powla ' times it will be last element and ' powfa ' times it will be first element ; In total it will come powe = powa - powla - powfa times ; Multiplying a [ i ] powe times using Fermat Little Theorem under MODulo MOD for fast exponentiation ; Driver Code ; pre - calculation of all combinations
|
using System ; class GFG { static int MOD = 1000000007 ; static int max = 101 ; static long [ , ] C = new long [ max , max ] ; static long power ( long x , long y ) { long res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % MOD ; } y = y >> 1 ; x = ( x * x ) % MOD ; } return res % MOD ; } static void combi ( int n , int k ) { int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = ( C [ i - 1 , j - 1 ] % MOD + C [ i - 1 , j ] % MOD ) % MOD ; } } } static long product ( long [ ] a , int n , int k ) { long ans = 1 ; Array . Sort ( a ) ; long powa = C [ n - 1 , k - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { long powla = C [ i , k - 1 ] ; long powfa = C [ n - i - 1 , k - 1 ] ; long powe = ( ( powa % MOD ) - ( powla + powfa ) % MOD + MOD ) % MOD ; long mul = power ( a [ i ] , powe ) % MOD ; ans = ( ( ans % MOD ) * ( mul % MOD ) ) % MOD ; } return ans % MOD ; } static public void Main ( ) { combi ( 100 , 100 ) ; long [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 3 ; long ans = product ( arr , n , k ) ; Console . WriteLine ( ans ) ; } }
|
Maximum number of segments that can contain the given points | C # implementation of the approach ; Function to return the maximum number of segments ; Sort both the vectors ; Initially pointing to the first element of b [ ] ; Try to find a match in b [ ] ; The segment ends before b [ j ] ; The point lies within the segment ; The segment starts after b [ j ] ; Return the required count ; Driver code
|
using System ; class GFG { static int countPoints ( int n , int m , int [ ] a , int [ ] b , int x , int y ) { Array . Sort ( a ) ; Array . Sort ( b ) ; int j = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( a [ i ] + y < b [ j ] ) break ; if ( b [ j ] >= a [ i ] - x && b [ j ] <= a [ i ] + y ) { count ++ ; j ++ ; break ; } else j ++ ; } } return count ; } public static void Main ( ) { int x = 1 , y = 4 ; int [ ] a = { 1 , 5 } ; int n = a . Length ; int [ ] b = { 1 , 1 , 2 } ; int m = a . Length ; Console . WriteLine ( countPoints ( n , m , a , b , x , y ) ) ; } }
|
Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | C # program to merge K sorted arrays ; Merge and sort k arrays ; Put the elements in sorted array . ; Sort the output array ; Driver code ; Input 2D - array ; Number of arrays ; Output array ; Print merged array
|
using System ; class GFG { static int N = 4 ; static void merge_and_sort ( int [ ] output , int [ , ] arr , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { output [ i * n + j ] = arr [ i , j ] ; } } Array . Sort ( output ) ; } static void Main ( ) { int [ , ] arr = { { 5 , 7 , 15 , 18 } , { 1 , 8 , 9 , 17 } , { 1 , 4 , 7 , 7 } } ; int n = N ; int k = arr . GetLength ( 0 ) ; int [ ] output = new int [ n * k ] ; merge_and_sort ( output , arr , n , k ) ; for ( int i = 0 ; i < n * k ; i ++ ) Console . Write ( output [ i ] + " β " ) ; } }
|
Minimum operations of given type to make all elements of a matrix equal | C # implementation of the approach ; Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; will not work for negative elements , so . . adding this ; adding this to handle negative elements too . ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; changed here as in case of even elements there will be 2 medians ; Return minimum operations required ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static int minOperations ( int n , int m , int k , List < List < int > > matrix ) { List < int > arr = new List < int > ( ) ; int mod ; if ( matrix [ 0 ] [ 0 ] < 0 ) { mod = k - ( Math . Abs ( matrix [ 0 ] [ 0 ] ) % k ) ; } else { mod = matrix [ 0 ] [ 0 ] % k ; } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr . Add ( matrix [ i ] [ j ] ) ; int val = matrix [ i ] [ j ] ; if ( val < 0 ) { int res = k - ( Math . Abs ( val ) % k ) ; if ( res != mod ) { return - 1 ; } } else { int foo = matrix [ i ] [ j ] ; if ( foo % k != mod ) { return - 1 ; } } } } arr . Sort ( ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . Abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . Abs ( arr [ i ] - median2 ) / k ; minOperations = Math . Min ( minOperations , minOperations2 ) ; } return minOperations ; } static void Main ( ) { List < List < int > > matrix = new List < List < int > > { new List < int > { 2 , 4 , 6 } , new List < int > { 8 , 10 , 12 } , new List < int > { 14 , 16 , 18 } , new List < int > { 20 , 22 , 24 } , } ; int n = matrix . Count ; int m = matrix [ 0 ] . Count ; int k = 2 ; Console . Write ( minOperations ( n , m , k , matrix ) ) ; } }
|
Smallest subarray containing minimum and maximum values | C # implementation of above approach ; Function to return length of smallest subarray containing both maximum and minimum value ; find maximum and minimum values in the array ; iterate over the array and set answer to smallest difference between position of maximum and position of minimum value ; last occurrence of minValue ; last occurrence of maxValue ; Driver code
|
using System ; using System . Linq ; public class GFG { static int minSubarray ( int [ ] A , int n ) { int minValue = A . Min ( ) ; int maxValue = A . Max ( ) ; int pos_min = - 1 , pos_max = - 1 , ans = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == minValue ) pos_min = i ; if ( A [ i ] == maxValue ) pos_max = i ; if ( pos_max != - 1 && pos_min != - 1 ) ans = Math . Min ( ans , Math . Abs ( pos_min - pos_max ) + 1 ) ; } return ans ; } static public void Main ( ) { int [ ] A = { 1 , 5 , 9 , 7 , 1 , 9 , 4 } ; int n = A . Length ; Console . WriteLine ( minSubarray ( A , n ) ) ; } }
|
Minimum number of consecutive sequences that can be formed in an array | C # program find the minimum number of consecutive sequences in an array ; Driver program ; function call to print required answer
|
using System ; class GFG { static int countSequences ( int [ ] arr , int n ) { int count = 1 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; return count ; } static public void Main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 3 , 5 , 10 } ; int n = arr . Length ; Console . WriteLine ( countSequences ( arr , n ) ) ; } }
|
Minimum number of increment / decrement operations such that array contains all elements from 1 to N | C # implementation of the above approach ; Function to find the minimum operations ; Sort the given array ; Count operations by assigning a [ i ] = i + 1 ; Driver Code
|
using System ; class GFG { static long minimumMoves ( int [ ] a , int n ) { long operations = 0 ; Array . Sort ( a ) ; for ( int i = 0 ; i < n ; i ++ ) operations += ( long ) Math . Abs ( a [ i ] - ( i + 1 ) ) ; return operations ; } static public void Main ( ) { int [ ] arr = { 5 , 3 , 2 } ; int n = arr . Length ; Console . WriteLine ( minimumMoves ( arr , n ) ) ; } }
|
Print a case where the given sorting algorithm fails | C # program to find a case where the given algorithm fails ; Function to print a case where the given sorting algorithm fails ; only case where it fails ; Driver Code
|
using System ; class GFG { static void printCase ( int n ) { if ( n <= 2 ) { Console . Write ( - 1 ) ; return ; } for ( int i = n ; i >= 1 ; i -- ) Console . Write ( i + " β " ) ; } public static void Main ( ) { int n = 3 ; printCase ( n ) ; } }
|
Find the missing elements from 1 to M in given N ranges | C # program to find missing elements from given Ranges ; Function to find missing elements from given Ranges ; First of all sort all the given ranges ; Store ans in a different vector ; prev is use to store end of last range ; j is used as a counter for ranges ; For last segment ; Finally print all answer ; Driver code ; Store ranges in vector of pair
|
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { Pair first = ( Pair ) a ; Pair second = ( Pair ) b ; if ( first . first == second . first ) { return first . second - second . second ; } return first . first - second . first ; } } public class Pair { public int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findMissingNumber ( ArrayList ranges , int m ) { IComparer myComparer = new sortHelper ( ) ; ranges . Sort ( myComparer ) ; ArrayList ans = new ArrayList ( ) ; int prev = 0 ; for ( int j = 0 ; j < ranges . Count ; j ++ ) { int start = ( ( Pair ) ranges [ j ] ) . first ; int end = ( ( Pair ) ranges [ j ] ) . second ; for ( int i = prev + 1 ; i < start ; i ++ ) ans . Add ( i ) ; prev = end ; } for ( int i = prev + 1 ; i <= m ; i ++ ) ans . Add ( i ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) { if ( ( int ) ans [ i ] <= m ) Console . Write ( ans [ i ] + " β " ) ; } } public static void Main ( string [ ] args ) { int M = 6 ; ArrayList ranges = new ArrayList ( ) ; ranges . Add ( new Pair ( 1 , 2 ) ) ; ranges . Add ( new Pair ( 4 , 5 ) ) ; findMissingNumber ( ranges , M ) ; } }
|
Check whether it is possible to make both arrays equal by modifying a single element | C # implementation of the above approach ; Function to check if both sequences can be made equal ; Sorting both the arrays ; Flag to tell if there are more than one mismatch ; To stores the index of mismatched element ; If there is more than one mismatch then return False ; If there is no mismatch or the difference between the mismatching elements is <= k then return true ; Driver code
|
using System ; class GFG { static bool check ( int n , int k , int [ ] a , int [ ] b ) { Array . Sort ( a ) ; Array . Sort ( b ) ; bool fl = false ; int ind = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { if ( fl == true ) { return false ; } fl = true ; ind = i ; } } if ( ind == - 1 | Math . Abs ( a [ ind ] - b [ ind ] ) <= k ) { return true ; } return false ; } public static void Main ( ) { int n = 2 , k = 4 ; int [ ] a = { 1 , 5 } ; int [ ] b = { 1 , 1 } ; if ( check ( n , k , a , b ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
|
Sum of width ( max and min diff ) of all Subsequences | C # implementation of above approach ; Function to return sum of width of all subsets ; Sort the array ; Driver Code
|
using System ; class GFG { static int MOD = 1000000007 ; static int SubseqWidths ( int [ ] A , int n ) { Array . Sort ( A ) ; int [ ] pow2 = new int [ n ] ; pow2 [ 0 ] = 1 ; for ( int i = 1 ; i < n ; ++ i ) pow2 [ i ] = ( pow2 [ i - 1 ] * 2 ) % MOD ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) ans = ( ans + ( pow2 [ i ] - pow2 [ n - 1 - i ] ) * A [ i ] ) % MOD ; return ans ; } static void Main ( ) { int [ ] A = new int [ ] { 5 , 6 , 4 , 3 , 8 } ; int n = A . Length ; Console . WriteLine ( SubseqWidths ( A , n ) ) ; } }
|
Covering maximum array elements with given value | C # implementation of the approach ; Function to find value for covering maximum array elements ; sort the students in ascending based on the candies ; To store the number of happy students ; To store the running sum ; If the current student can 't be made happy ; increment the count if we can make the ith student happy ; If the sum = x then answer is n ; If the count is equal to n then the answer is n - 1 ; Driver function
|
using System ; using System . Linq ; class GFG { static int maxArrayCover ( int [ ] a , int n , int x ) { Array . Sort ( a ) ; int cc = 0 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += a [ i ] ; if ( s > x ) { break ; } cc += 1 ; } if ( a . Sum ( ) == x ) { return n ; } else { if ( cc == n ) { return n - 1 ; } else { return cc ; } } } public static void Main ( ) { int n = 3 ; int x = 70 ; int [ ] a = new int [ ] { 10 , 20 , 30 } ; Console . WriteLine ( maxArrayCover ( a , n , x ) ) ; } }
|
Quotient | C # program to implement Quotient - Remainder Sort ; max_element finds maximum element in an array ; min_element finds minimum element in an array ; Creating a ROW * COL matrix of all zeros ; Driver Code
|
using System ; using System . Linq ; class GFG { static void QRsort ( int [ ] arr , int size ) { int MAX = arr . Max ( ) ; int MIN = arr . Min ( ) ; Console . WriteLine ( " Maximum β Element β found β is β : β " + MAX ) ; Console . WriteLine ( " Minimum β Element β found β is β : β " + MIN ) ; int COL = MIN ; int ROW = MAX / MIN + 1 ; int [ , ] matrix = new int [ ROW , COL ] ; for ( int i = 0 ; i < size ; i ++ ) { int quotient = arr [ i ] / MIN ; int remainder = arr [ i ] % MIN ; matrix [ quotient , remainder ] = arr [ i ] ; } int k = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( matrix [ i , j ] != 0 ) { arr [ k ++ ] = matrix [ i , j ] ; } } } } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) { Console . Write ( arr [ i ] + " β " ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 7 , 4 , 8 , 2 , 6 } ; int size = arr . Length ; Console . WriteLine ( " Initial β Array β : β " ) ; printArray ( arr , size ) ; QRsort ( arr , size ) ; Console . WriteLine ( " Array β after β sorting β : β " ) ; printArray ( arr , size ) ; } }
|
Check if a Linked List is Pairwise Sorted | C # program to check if linked list is pairwise sorted ; A linked list node ; Function to check if linked list is pairwise sorted ; Traverse further only if there are at - least two nodes left ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; The constructed linked list is : 10.15 . 9.9 .1 .5
|
using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node start ; static Boolean isPairWiseSorted ( Node head ) { Boolean flag = true ; Node temp = head ; while ( temp != null && temp . next != null ) { if ( temp . data > temp . next . data ) { flag = false ; break ; } temp = temp . next . next ; } return flag ; } static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; start = head_ref ; } public static void Main ( String [ ] args ) { start = null ; push ( start , 5 ) ; push ( start , 1 ) ; push ( start , 9 ) ; push ( start , 9 ) ; push ( start , 15 ) ; push ( start , 10 ) ; if ( isPairWiseSorted ( start ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
|
Count number of triplets with product equal to given number | Set 2 | C # implementation of above approach ; Function to count such triplets ; Sort the array ; three pointer technique ; Calculate the product of a triplet ; Check if that product is greater than m , decrement mid ; Check if that product is smaller than m , increment start ; Check if that product is equal to m , decrement mid , increment start and increment the count of pairs ; Driver code
|
using System ; class GFG { static int countTriplets ( int [ ] arr , int n , int m ) { int count = 0 ; Array . Sort ( arr ) ; int end , start , mid ; for ( end = n - 1 ; end >= 2 ; end -- ) { start = 0 ; mid = end - 1 ; while ( start < mid ) { long prod = arr [ end ] * arr [ start ] * arr [ mid ] ; if ( prod > m ) mid -- ; else if ( prod < m ) start ++ ; else if ( prod == m ) { count ++ ; mid -- ; start ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = arr . Length ; int m = 1 ; Console . WriteLine ( countTriplets ( arr , n , m ) ) ; } }
|
Equally divide into two sets such that one set has maximum distinct elements | C # program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Insert all the resources in the set There will be unique resources in the set ; return minimum of distinct resources and n / 2 ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static int distribution ( int [ ] arr , int n ) { HashSet < int > resources = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) resources . Add ( arr [ i ] ) ; return Math . Min ( resources . Count , n / 2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 1 , 3 , 4 } ; int n = arr . Length ; Console . Write ( distribution ( arr , n ) + " STRNEWLINE " ) ; } }
|
Sort 3 numbers | C # program to sort an array of size 3 ; Insert arr [ 1 ] ; Insert arr [ 2 ] ; Driver Code
|
using System ; class GFG { static void sort3 ( int [ ] arr , int [ ] temp ) { if ( arr [ 1 ] < arr [ 0 ] ) { temp [ 0 ] = arr [ 0 ] ; arr [ 0 ] = arr [ 1 ] ; arr [ 1 ] = temp [ 0 ] ; } if ( arr [ 2 ] < arr [ 1 ] ) { temp [ 0 ] = arr [ 1 ] ; arr [ 1 ] = arr [ 2 ] ; arr [ 2 ] = temp [ 0 ] ; if ( arr [ 1 ] < arr [ 0 ] ) { temp [ 0 ] = arr [ 0 ] ; arr [ 0 ] = arr [ 1 ] ; arr [ 1 ] = temp [ 0 ] ; } } } public static void Main ( String [ ] args ) { int [ ] a = new int [ ] { 10 , 12 , 5 } ; int [ ] temp1 = new int [ 10 ] ; sort3 ( a , temp1 ) ; for ( int i = 0 ; i < 3 ; i ++ ) Console . Write ( a [ i ] + " β " ) ; } }
|
Merge Sort with O ( 1 ) extra space merge and O ( n lg n ) time [ Unsigned Integers Only ] | C # program to sort an array using merge sort such that merge operation takes O ( 1 ) extra space . ; Obtaining actual values ; Recursive merge sort with extra parameter , naxele ; This functions finds max element and calls recursive merge sort . ; Driver code
|
using System ; using System . Linq ; class GFG { static void merge ( int [ ] arr , int beg , int mid , int end , int maxele ) { int i = beg ; int j = mid + 1 ; int k = beg ; while ( i <= mid && j <= end ) { if ( arr [ i ] % maxele <= arr [ j ] % maxele ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } else { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } } while ( i <= mid ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } while ( j <= end ) { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } for ( i = beg ; i <= end ; i ++ ) arr [ i ] = arr [ i ] / maxele ; } static void mergeSortRec ( int [ ] arr , int beg , int end , int maxele ) { if ( beg < end ) { int mid = ( beg + end ) / 2 ; mergeSortRec ( arr , beg , mid , maxele ) ; mergeSortRec ( arr , mid + 1 , end , maxele ) ; merge ( arr , beg , mid , end , maxele ) ; } } static void mergeSort ( int [ ] arr , int n ) { int maxele = arr . Max ( ) + 1 ; mergeSortRec ( arr , 0 , n - 1 , maxele ) ; } public static void Main ( ) { int [ ] arr = { 999 , 612 , 589 , 856 , 56 , 945 , 243 } ; int n = arr . Length ; mergeSort ( arr , n ) ; Console . WriteLine ( " Sorted β array β " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " β " ) ; } }
|
Print triplets with sum less than k | C # program to print triplets with sum smaller than a given value ; Sort input array ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there are total k - j third elements . ; Driver Code
|
using System ; class GFG { static void printTriplets ( int [ ] arr , int n , int sum ) { Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { for ( int x = j + 1 ; x <= k ; x ++ ) Console . WriteLine ( arr [ i ] + " , β " + arr [ j ] + " , β " + arr [ x ] ) ; j ++ ; } } } } public static void Main ( ) { int [ ] arr = { 5 , 1 , 3 , 4 , 7 } ; int n = arr . Length ; int sum = 12 ; printTriplets ( arr , n , sum ) ; } }
|
Sort string of characters using Stack | C # program to sort string of characters using stack ; Function to print the characters in sorted order ; Primary stack ; Secondary stack ; Append first character ; Iterate for all character in string ; i - th character ASCII ; stack 's top element ASCII ; If greater or equal to top element then push to stack ; If smaller , then push all element to the temporary stack ; Push all greater elements ; Push operation ; Push till the stack is not - empty ; Push the i - th character ; Push the tempstack back to stack ; Print the stack in reverse order ; Driver Code
|
using System ; using System . Collections ; class GFG { static void printSorted ( string s , int l ) { Stack stack = new Stack ( ) ; Stack tempstack = new Stack ( ) ; stack . Push ( s [ 0 ] ) ; for ( int i = 1 ; i < l ; i ++ ) { int a = s [ i ] ; int b = ( int ) ( ( char ) stack . Peek ( ) ) ; if ( ( a - b ) >= 1 || ( a == b ) ) stack . Push ( s [ i ] ) ; else if ( ( b - a ) >= 1 ) { while ( ( b - a ) >= 1 ) { tempstack . Push ( stack . Peek ( ) ) ; stack . Pop ( ) ; if ( stack . Count > 0 ) b = ( int ) ( ( char ) stack . Peek ( ) ) ; else break ; } stack . Push ( s [ i ] ) ; while ( tempstack . Count > 0 ) { stack . Push ( tempstack . Peek ( ) ) ; tempstack . Pop ( ) ; } } } string answer = " " ; while ( stack . Count > 0 ) { answer = stack . Peek ( ) + answer ; stack . Pop ( ) ; } Console . WriteLine ( answer ) ; } static void Main ( ) { string s = " geeksforgeeks " ; int l = s . Length ; printSorted ( s , l ) ; } }
|
Check whether an array can be fit into another array rearranging the elements in the array | C # Program to check whether an array can be fit into another array with given condition . ; Returns true if the array A can be fit into array B , otherwise false ; Sort both the arrays ; Iterate over the loop and check whether every array element of A is less than or equal to its corresponding array element of B ; Driver Code
|
using System ; class GFG { static bool checkFittingArrays ( int [ ] A , int [ ] B , int N ) { Array . Sort ( A ) ; Array . Sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] > B [ i ] ) return false ; return true ; } public static void Main ( ) { int [ ] A = { 7 , 5 , 3 , 2 } ; int [ ] B = { 5 , 4 , 8 , 7 } ; int N = A . Length ; if ( checkFittingArrays ( A , B , N ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
|
Maximise the number of toys that can be purchased with amount K | C # Program to maximize the number of toys with K amount ; This functions returns the required number of toys ; sort the cost array ; Check if we can buy ith toy or not ; Increment count ; Driver Code
|
using System ; class GFG { static int maximum_toys ( int [ ] cost , int N , int K ) { int count = 0 , sum = 0 ; Array . Sort ( cost ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( sum + cost [ i ] <= K ) { sum = sum + cost [ i ] ; count ++ ; } } return count ; } public static void Main ( ) { int K = 50 ; int [ ] cost = { 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 12 , 15 } ; int N = cost . Length ; Console . Write ( maximum_toys ( cost , N , K ) ) ; } }
|
Find if k bookings possible with given arrival and departure times | C # code implementation of the above approach ; Driver Code
|
using System ; class GFG { static string areBookingsPossible ( int [ ] A , int [ ] B , int K ) { Array . Sort ( A ) ; Array . Sort ( B ) ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( i + K < A . Length && A [ i + K ] < B [ i ] ) { return " No " ; } } return " Yes " ; } static void Main ( ) { int [ ] arrival = { 1 , 2 , 3 } ; int [ ] departure = { 2 , 3 , 4 } ; int K = 1 ; Console . Write ( areBookingsPossible ( arrival , departure , K ) ) ; } }
|
Insertion Sort by Swapping Elements | Recursive C # program to sort an array by swapping elements ; Utility function to print a Vector ; Function performs insertion sort on vector V ; General Case Sort V till second last element and then insert last element into V ; Insertion step ; Insert V [ i ] into list 0. . i - 1 ; Swap V [ j ] and V [ j - 1 ] ; Decrement j by 1 ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static void printVector ( List < int > V ) { for ( int i = 0 ; i < V . Count ; i ++ ) { Console . Write ( V [ i ] + " β " ) ; } Console . WriteLine ( ) ; } static void insertionSortRecursive ( List < int > V , int N ) { if ( N <= 1 ) return ; insertionSortRecursive ( V , N - 1 ) ; int j = N - 1 ; while ( j > 0 && V [ j ] < V [ j - 1 ] ) { int temp = V [ j ] ; V [ j ] = V [ j - 1 ] ; V [ j - 1 ] = temp ; j -= 1 ; } } public static void Main ( String [ ] args ) { List < int > A = new List < int > ( ) ; A . Insert ( 0 , 9 ) ; A . Insert ( 1 , 8 ) ; A . Insert ( 2 , 7 ) ; A . Insert ( 3 , 5 ) ; A . Insert ( 4 , 2 ) ; A . Insert ( 5 , 1 ) ; A . Insert ( 6 , 2 ) ; A . Insert ( 7 , 3 ) ; Console . Write ( " Array : β " ) ; printVector ( A ) ; Console . Write ( " After β Sorting β : " ) ; insertionSortRecursive ( A , A . Count ) ; printVector ( A ) ; } }
|
Most frequent word in an array of strings | C # implementation ; Function returns word with highest frequency ; Create Dictionary to store word and it 's frequency ; Iterate through array of words ; If word already exist in Dictionary then increase it 's count by 1 ; Otherwise add word to Dictionary ; Create set to iterate over Dictionary ; Check for word having highest frequency ; Return word having highest frequency ; Driver code ; Print word having highest frequency
|
using System ; using System . Collections . Generic ; class GFG { static String findWord ( String [ ] arr ) { Dictionary < String , int > hs = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( hs . ContainsKey ( arr [ i ] ) ) { hs [ arr [ i ] ] = hs [ arr [ i ] ] + 1 ; } else { hs . Add ( arr [ i ] , 1 ) ; } } String key = " " ; int value = 0 ; foreach ( KeyValuePair < String , int > me in hs ) { if ( me . Value > value ) { value = me . Value ; key = me . Key ; } } return key ; } public static void Main ( String [ ] args ) { String [ ] arr = { " geeks " , " for " , " geeks " , " a " , " portal " , " to " , " learn " , " can " , " be " , " computer " , " science " , " zoom " , " yup " , " fire " , " in " , " be " , " data " , " geeks " } ; String sol = findWord ( arr ) ; Console . WriteLine ( sol ) ; } }
|
Check if given array is almost sorted ( elements are at | C # Code to check if given array is almost sorted or not ; function for checking almost sort ; One by one compare adjacents . ; Check whether resultant is sorted or not ; is resultant is sorted return true ; Driver Code
|
using System ; class GFG { public static bool almostSort ( int [ ] A , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { int temp = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = temp ; i ++ ; } } for ( int i = 0 ; i < n - 1 ; i ++ ) if ( A [ i ] > A [ i + 1 ] ) return false ; return true ; } public static void Main ( ) { int [ ] A = { 1 , 3 , 2 , 4 , 6 , 5 } ; int n = A . Length ; if ( almostSort ( A , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Efficiently merging two sorted arrays with O ( 1 ) extra space | C # program for Merging two sorted arrays with O ( 1 ) extra space ; Function to find next gap . ; comparing elements in the first array . ; comparing elements in both arrays . ; comparing elements in the second array . ; Driver code ; Function Call
|
using System ; class GFG { static int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( gap / 2 ) + ( gap % 2 ) ; } private static void merge ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { int i , j , gap = n + m ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( i = 0 ; i + gap < n ; i ++ ) if ( arr1 [ i ] > arr1 [ i + gap ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr1 [ i + gap ] ; arr1 [ i + gap ] = temp ; } for ( j = gap > n ? gap - n : 0 ; i < n && j < m ; i ++ , j ++ ) if ( arr1 [ i ] > arr2 [ j ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ j ] ; arr2 [ j ] = temp ; } if ( j < m ) { for ( j = 0 ; j + gap < m ; j ++ ) if ( arr2 [ j ] > arr2 [ j + gap ] ) { int temp = arr2 [ j ] ; arr2 [ j ] = arr2 [ j + gap ] ; arr2 [ j + gap ] = temp ; } } } } public static void Main ( ) { int [ ] a1 = { 10 , 27 , 38 , 43 , 82 } ; int [ ] a2 = { 3 , 9 } ; merge ( a1 , a2 , a1 . Length , a2 . Length ) ; Console . Write ( " First β Array : β " ) ; for ( int i = 0 ; i < a1 . Length ; i ++ ) { Console . Write ( a1 [ i ] + " β " ) ; } Console . WriteLine ( ) ; Console . Write ( " Second β Array : β " ) ; for ( int i = 0 ; i < a2 . Length ; i ++ ) { Console . Write ( a2 [ i ] + " β " ) ; } } }
|
Merge two sorted arrays | C # program to merge two sorted arrays ; Merge arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] into arr3 [ 0. . n1 + n2 - 1 ] ; Traverse both array ; Check if current element of first array is smaller than current element of second array . If yes , store first array element and increment first array index . Otherwise do same with second array ; Store remaining elements of first array ; Store remaining elements of second array ; Driver code
|
using System ; class GFG { public static void mergeArrays ( int [ ] arr1 , int [ ] arr2 , int n1 , int n2 , int [ ] arr3 ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) arr3 [ k ++ ] = arr1 [ i ++ ] ; else arr3 [ k ++ ] = arr2 [ j ++ ] ; } while ( i < n1 ) arr3 [ k ++ ] = arr1 [ i ++ ] ; while ( j < n2 ) arr3 [ k ++ ] = arr2 [ j ++ ] ; } public static void Main ( ) { int [ ] arr1 = { 1 , 3 , 5 , 7 } ; int n1 = arr1 . Length ; int [ ] arr2 = { 2 , 4 , 6 , 8 } ; int n2 = arr2 . Length ; int [ ] arr3 = new int [ n1 + n2 ] ; mergeArrays ( arr1 , arr2 , n1 , n2 , arr3 ) ; Console . Write ( " Array β after β merging STRNEWLINE " ) ; for ( int i = 0 ; i < n1 + n2 ; i ++ ) Console . Write ( arr3 [ i ] + " β " ) ; } }
|
Sort array after converting elements to their squares | C # program to Sort square of the numbers of the array ; Function to sort an square array ; First convert each array elements into its square ; Sort an array using " inbuild β sort β function " in Arrays class . ; Driver Code
|
using System ; class GFG { public static void sortSquares ( int [ ] arr ) { int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] * arr [ i ] ; Array . Sort ( arr ) ; } public static void Main ( ) { int [ ] arr = { - 6 , - 3 , - 1 , 2 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( " Before β sort β " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " β " ) ; sortSquares ( arr ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " After β Sort β " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " β " ) ; } }
|
Sort an array when two halves are sorted | C # program to Merge Two Sorted Halves Of Array Into Single Sorted Array ; Merge two sorted halves of Array into single sorted array ; starting index of second half ; Temp Array store sorted resultant array ; First Find the point where array is divide into two half ; If Given array is all - ready sorted ; Merge two sorted arrays in single sorted array ; Copy the remaining elements of A [ i to half_ ! ] ; Copy the remaining elements of A [ half_ ! to n ] ; Driver code ; Print sorted Array
|
using System class { static void mergeTwoHalf ( int [ ] A , int n ) { int half_i = 0 int int [ ] temp = new int [ n ] for ( i = 0 i < n - 1 i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { half_i = i + 1 break } } if ( half_i == 0 ) return = 0 int = half_i int = 0 while ( i < half_i & & j < n ) { if ( A [ i ] < A [ j ] ) temp [ k ++ ] = A [ i ++ ] else [ k ++ ] = A [ j ++ ] } while ( i < half_i ) temp [ k ++ ] = A [ i ++ ] while ( j < n ) temp [ k ++ ] = A [ j ++ ] for ( i = 0 i < n i ++ ) A [ i ] = temp [ i ] } static public void ( ) { int [ ] A = { 2 , 3 , 8 , - 1 , 7 , 10 } int n = A . Length mergeTwoHalf ( A , n ) for ( int i = 0 i < n i ++ ) Console . Write ( A [ i ] + " β " ) } }
|
Chocolate Distribution Problem | C # Code For Chocolate Distribution Problem ; arr [ 0. . n - 1 ] represents sizes of packets . m is number of students . Returns minimum difference between maximum and minimum values of distribution . ; if there are no chocolates or number of students is 0 ; Sort the given packets ; Number of students cannot be more than number of packets ; Largest number of chocolates ; Find the subarray of size m such that difference between last ( maximum in case of sorted ) and first ( minimum in case of sorted ) elements of subarray is minimum . ; Driver program to test above function ; int m = 7 ; Number of students
|
using System ; class GFG { static int findMinDiff ( int [ ] arr , int n , int m ) { if ( m == 0 n == 0 ) return 0 ; Array . Sort ( arr ) ; if ( n < m ) return - 1 ; int min_diff = int . MaxValue ; for ( int i = 0 ; i + m - 1 < n ; i ++ ) { int diff = arr [ i + m - 1 ] - arr [ i ] ; if ( diff < min_diff ) min_diff = diff ; } return min_diff ; } public static void Main ( ) { int [ ] arr = { 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 , 30 , 40 , 28 , 42 , 30 , 44 , 48 , 43 , 50 } ; int n = arr . Length ; Console . WriteLine ( " Minimum β difference β is β " + findMinDiff ( arr , n , m ) ) ; } }
|
Absolute distinct count in a sorted array | C # code to find absolute distinct count of an array in O ( n ) time . ; The function returns number of distinct absolute values among the elements of the array ; Note that set keeps only one copy even if we try to insert multiple values ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static int distinctCount ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . Add ( Math . Abs ( arr [ i ] ) ) ; return s . Count ; } public static void Main ( ) { int [ ] arr = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . Length ; Console . Write ( " Count β of β absolute β distinct β values β : β " + distinctCount ( arr , n ) ) ; } }
|
Absolute distinct count in a sorted array | C # program to find absolute distinct count of an array using O ( 1 ) space . ; The function returns return number of distinct absolute values among the elements of the array ; initialize count as number of elements ; Remove duplicate elements from the left of the current window ( i , j ) and also decrease the count ; Remove duplicate elements from the right of the current window ( i , j ) and also decrease the count ; break if only one element is left ; Now look for the zero sum pair in current window ( i , j ) ; decrease the count if ( positive , negative ) pair is encountered ; Driver code
|
using System ; class GFG { static int distinctCount ( int [ ] arr , int n ) { int count = n ; int i = 0 , j = n - 1 , sum = 0 ; while ( i < j ) { while ( i != j && arr [ i ] == arr [ i + 1 ] ) { count -- ; i ++ ; } while ( i != j && arr [ j ] == arr [ j - 1 ] ) { count -- ; j -- ; } if ( i == j ) break ; sum = arr [ i ] + arr [ j ] ; if ( sum == 0 ) { count -- ; i ++ ; j -- ; } else if ( sum < 0 ) i ++ ; else j -- ; } return count ; } public static void Main ( ) { int [ ] arr = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . Length ; Console . WriteLine ( " Count β of β absolute β distinct β values β : β " + distinctCount ( arr , n ) ) ; } }
|
Sorting Strings using Bubble Sort | C # implementation ; Sorting strings using bubble sort ; Driver code
|
using System ; class GFG { static int MAX = 100 ; public static void sortStrings ( String [ ] arr , int n ) { String temp ; for ( int j = 0 ; j < n - 1 ; j ++ ) { for ( int i = j + 1 ; i < n ; i ++ ) { if ( arr [ j ] . CompareTo ( arr [ i ] ) > 0 ) { temp = arr [ j ] ; arr [ j ] = arr [ i ] ; arr [ i ] = temp ; } } } } public static void Main ( String [ ] args ) { String [ ] arr = { " GeeksforGeeks " , " Quiz " , " Practice " , " Gblogs " , " Coding " } ; int n = arr . Length ; sortStrings ( arr , n ) ; Console . WriteLine ( " Strings β in β sorted β order β are β : β " ) ; for ( int i = 0 ; i < n ; i ++ ) Console . WriteLine ( " String β " + ( i + 1 ) + " β is β " + arr [ i ] ) ; } }
|
Sort an almost sorted array where only two elements are swapped | C # program to sort using one swap ; This function sorts an array that can be sorted by single swap ; Traverse the given array from rightmost side ; Check if arr [ i ] is not in order ; Find the other element to be swapped with arr [ i ] ; Swap the pair ; A utility function to print an array of size n ; Driver Code
|
using System ; class GFG { static void sortByOneSwap ( int [ ] arr , int n ) { for ( int i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] < arr [ i - 1 ] ) { int j = i - 1 ; while ( j >= 0 && arr [ i ] < arr [ j ] ) j -- ; int temp = arr [ i ] ; arr [ i ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; break ; } } } static void printArray ( int [ ] arr , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " β " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 10 , 30 , 20 , 40 , 50 , 60 , 70 } ; int n = arr . Length ; Console . WriteLine ( " Given β array β is β " ) ; printArray ( arr , n ) ; sortByOneSwap ( arr , n ) ; Console . WriteLine ( " Sorted β array β is β " ) ; printArray ( arr , n ) ; } }
|
Pancake sorting | C # program to sort array using pancake sort ; Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already at the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; Utility function to print array arr [ ] ; Driver function to check for above functions
|
using System ; class GFG { static void flip ( int [ ] arr , int i ) { int temp , start = 0 ; while ( start < i ) { temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i -- ; } } static int findMax ( int [ ] arr , int n ) { int mi , i ; for ( mi = 0 , i = 0 ; i < n ; ++ i ) if ( arr [ i ] > arr [ mi ] ) mi = i ; return mi ; } static int pancakeSort ( int [ ] arr , int n ) { for ( int curr_size = n ; curr_size > 1 ; -- curr_size ) { int mi = findMax ( arr , curr_size ) ; if ( mi != curr_size - 1 ) { flip ( arr , mi ) ; flip ( arr , curr_size - 1 ) ; } } return 0 ; } static void printArray ( int [ ] arr , int arr_size ) { for ( int i = 0 ; i < arr_size ; i ++ ) Console . Write ( arr [ i ] + " β " ) ; Console . Write ( " " ) ; } public static void Main ( ) { int [ ] arr = { 23 , 10 , 20 , 11 , 12 , 6 , 7 } ; int n = arr . Length ; pancakeSort ( arr , n ) ; Console . Write ( " Sorted β Array : β " ) ; printArray ( arr , n ) ; } }
|
Lexicographically smallest numeric string having odd digit counts | C # program for the above approach ; Function to construct lexicographically smallest numeric string having an odd count of each characters ; Stores the resultant string ; If N is even ; Otherwise ; Driver code
|
using System ; class GFG { static string genString ( int N ) { string ans = " " ; if ( N % 2 == 0 ) { for ( int i = 0 ; i < N - 1 ; i ++ ) ans += '1' ; ans += '2' ; } else { for ( int i = 0 ; i < N ; i ++ ) ans += '1' ; } return ans ; } public static void Main ( ) { int N = 5 ; Console . WriteLine ( genString ( N ) ) ; } }
|
Modify given string such that odd and even indices is lexicographically largest and smallest | C # program for the above approach ; Function to modify the given string satisfying the given criteria ; Traverse the string S ; If i is even ; If the S [ i ] is ' a ' , then change S [ i ] to ' b ' ; Otherwise , change S [ i ] to ' a ' ; If S [ i ] is ' z ' , then change S [ i ] to ' y ' ; Otherwise , change S [ i ] to ' z ' ; Return the result ; Driver Code
|
using System ; using System . Collections . Generic ; class GFG { static string performOperation ( string S , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( S [ i ] == ' a ' ) { S = S . Substring ( 0 , i ) + ' b ' + S . Substring ( i + 1 ) ; } else { S = S . Substring ( 0 , i ) + ' a ' + S . Substring ( i + 1 ) ; } } else { if ( S [ i ] == ' z ' ) { S = S . Substring ( 0 , i ) + ' y ' + S . Substring ( i + 1 ) ; } else { S = S . Substring ( 0 , i ) + ' z ' + S . Substring ( i + 1 ) ; } } } return S ; } public static void Main ( ) { string S = " giad " ; int N = S . Length ; Console . Write ( performOperation ( S , N ) ) ; } }
|
Count of values chosen for X such that N is reduced to 0 after given operations | C # program for the above approach ; Function to check if the value of X reduces N to 0 or not ; Update the value of N as N - x ; Check if x is a single digit integer ; Function to find the number of values X such that N can be reduced to 0 after performing the given operations ; Number of digits in N ; Stores the count of value of X ; Iterate over all possible value of X ; Check if x follow the conditions ; Return total count ; Driver code
|
using System ; using System . Collections . Generic ; class GFG { static bool check ( int x , int N ) { while ( true ) { N -= x ; if ( x < 10 ) break ; int temp2 = 0 ; while ( x > 0 ) { temp2 += ( x % 10 ) ; x = ( int ) x / 10 ; } x = temp2 ; } if ( ( x < 10 ) && ( N == 0 ) ) { return true ; } return false ; } static int countNoOfsuchX ( int N ) { int k = ( int ) ( Math . Log10 ( N ) ) + 1 ; int count = 1 ; for ( int x = ( N - ( k * ( k + 1 ) * 5 ) ) ; x <= N ; x ++ ) { if ( check ( x , N ) ) { count += 1 ; } } return count ; } public static void Main ( ) { int N = 9399 ; Console . Write ( countNoOfsuchX ( N ) ) ; } }
|
Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2 | Function to find the minimum number of increment and decrement of pairs required to make all array elements equal ; Stores the sum of the array ; If sum is not divisible by N ; Update sum ; Store the minimum number of operations ; Iterate while i is less than N ; Add absolute difference of current element with k to ans ; Increase i bye 1 ; Return the value in ans2 ; Driver Code ; Given Input ; Function Call
|
using System ; using System . Collections . Generic ; class GFG { static int find ( List < int > arr , int N ) { int Sum = 0 ; foreach ( int item in arr ) Sum += item ; if ( Sum % N == 1 ) return - 1 ; int k = Sum / N ; int ans = 0 ; int i = 0 ; while ( i < N ) { ans = ans + Math . Abs ( k - arr [ i ] ) ; i += 1 ; } return ans / 2 ; } public static void Main ( ) { List < int > arr = new List < int > ( ) { 5 , 4 , 1 , 10 } ; int N = arr . Count ; Console . Write ( find ( arr , N ) ) ; } }
|
Convert A into B by incrementing or decrementing 1 , 2 , or 5 any number of times | C # program for the above approach ; Function to find minimum number of moves required to convert A into B ; Stores the minimum number of moves required ; Stores the absolute difference ; FInd the number of moves ; Return cnt ; Driver Code ; Input ; Function call
|
using System ; using System . Collections . Generic ; class GFG { static int minimumSteps ( int a , int b ) { int cnt = 0 ; a = Math . Abs ( a - b ) ; cnt = ( a / 5 ) + ( a % 5 ) / 2 + ( a % 5 ) % 2 ; return cnt ; } public static void Main ( ) { int A = 3 , B = 9 ; Console . Write ( minimumSteps ( A , B ) ) ; } }
|
Minimum distance between duplicates in a String | C # Program to find the minimum distance between two repeating characters in a String using two pointers technique ; This function is used to find minimum distance between any two repeating characters using two - pointers and hashing technique ; hash array to store character 's last index ; Traverse through the String ; If the character is present in visited array find if its forming minimum distance ; update current character 's last index ; Return minimum distance found , else - 1 ; Driver code ; Given Input ; Function Call
|
using System ; public class GFG { static int shortestDistance ( string s , int n ) { int [ ] visited = new int [ 128 ] ; for ( int i = 0 ; i < 128 ; i ++ ) visited [ i ] = - 1 ; int ans = int . MaxValue ; for ( int right = 0 ; right < n ; right ++ ) { char c = s [ right ] ; int left = visited ; if ( left != - 1 ) ans = Math . Min ( ans , right - left - 1 ) ; visited = right ; } return ans == int . MaxValue ? - 1 : ans ; } public static void Main ( String [ ] args ) { string s = " geeksforgeeks " ; int n = 13 ; Console . Write ( shortestDistance ( s , n ) ) ; } }
|
Print all numbers that can be obtained by adding A or B to N exactly M times | C # program for tha above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be obtained ; If A and B are equal , then only one number can be obtained , i . e . N + M * A ; For finding others numbers , subtract A from number once and add B to number once ; Driver Code ; Given Input ; Function Call
|
using System ; class GFG { static void possibleNumbers ( int N , int M , int A , int B ) { if ( A > B ) { int temp = A ; A = B ; B = temp ; } int number = N + M * A ; Console . Write ( number + " β " ) ; if ( A != B ) { for ( int i = 0 ; i < M ; i ++ ) { number = number - A + B ; Console . Write ( number + " β " ) ; } } } public static void Main ( String [ ] args ) { int N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ; } }
|
Maximize number of circular buildings that can be covered by L length wire | C # program for the above approach ; Function to find the maximum number of buildings covered ; Store the current sum ; Traverse the array ; Add the length of wire required for current building to cur_sum ; Add extra unit distance 1 ; If curr_sum <= length of wire increment count by 1 ; If curr_sum > length of wire increment start by 1 and decrement count by 1 and update the new curr_sum ; Update the max_count ; Return the max_count ; Driver code ; Given Input ; Size of the array ; Function Call
|
using System ; class GFG { static double Pi = 3.141592 ; static int MaxBuildingsCovered ( int [ ] arr , int N , int L ) { double curr_sum = 0 ; int start = 0 , curr_count = 0 , max_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { curr_sum = curr_sum + ( ( double ) arr [ i ] * Pi ) ; if ( i != 0 ) curr_sum += 1 ; if ( curr_sum <= L ) { curr_count ++ ; } else if ( curr_sum > L ) { curr_sum = curr_sum - ( ( double ) arr [ start ] * Pi ) ; curr_sum -= 1 ; start ++ ; curr_count -- ; } max_count = Math . Max ( curr_count , max_count ) ; } return max_count ; } static void Main ( ) { int [ ] arr = { 4 , 1 , 6 , 2 } ; int L = 24 ; int N = arr . Length ; Console . Write ( MaxBuildingsCovered ( arr , N , L ) ) ; } }
|
Count numbers less than N whose Bitwise AND with N is zero | C # program for the above approach ; Function to count number of unset bits in the integer N ; Stores the number of unset bits in N ; Check if N is even ; Increment the value of c ; Right shift N by 1 ; Return the value of count of unset bits ; Function to count numbers whose Bitwise AND with N equal to 0 ; Stores the number of unset bits in N ; Print the value of 2 to the power of unsetBits ; Driver Code
|
using System ; class GFG { static int countUnsetBits ( int N ) { int c = 0 ; while ( N != 0 ) { if ( N % 2 == 0 ) { c += 1 ; } N = N >> 1 ; } return c ; } static void countBitwiseZero ( int N ) { int unsetBits = countUnsetBits ( N ) ; Console . Write ( 1 << unsetBits ) ; } public static void Main ( String [ ] args ) { int N = 9 ; countBitwiseZero ( N ) ; } }
|
Check if a Binary String can be split into disjoint subsequences which are equal to "010" | C # program for the above approach ; Function to check if the given string can be partitioned into a number of subsequences all of which are equal to "010" ; Store the size of the string ; Store the count of 0 s and 1 s ; Traverse the given string in the forward direction ; If the character is '0' , increment count_0 by 1 ; If the character is '1' increment count_1 by 1 ; If at any point , count_1 > count_0 , return false ; If count_0 is not equal to twice count_1 , return false ; Reset the value of count_0 and count_1 ; Traverse the string in the reverse direction ; If the character is '0' increment count_0 ; If the character is '1' increment count_1 ; If count_1 > count_0 , return false ; Driver code ; Given string ; Function Call
|
using System ; class GFG { static bool isPossible ( String s ) { int n = s . Length ; int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } if ( count_0 != ( 2 * count_1 ) ) return false ; count_0 = 0 ; count_1 = 0 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } return true ; } static public void Main ( ) { String s = "010100" ; if ( isPossible ( s ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
|
Minimum number of bricks that can be intersected | C # program for the above approach ; Function to find a line across a wall such that it intersects minimum number of bricks ; Declare a hashmap ; Store the maximum number of brick ending a point on x - axis ; Iterate over all the rows ; Initialize width as 0 ; Iterate over individual bricks ; Add the width of the current brick to the total width ; Increment number of bricks ending at this width position ; Update the variable , res ; Print the answer ; Driver Code ; Given Input ; Function Call
|
using System ; using System . Collections . Generic ; class GFG { static void leastBricks ( List < List < int > > wall ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int res = 0 ; foreach ( List < int > subList in wall ) { int width = 0 ; for ( int i = 0 ; i < subList . Count - 1 ; i ++ ) { width += subList [ i ] ; if ( map . ContainsKey ( width ) ) map [ width ] ++ ; else map . Add ( width , 1 ) ; res = Math . Max ( res , map [ width ] ) ; } } Console . Write ( wall . Count - res ) ; } public static void Main ( ) { List < List < int > > myList = new List < List < int > > ( ) ; myList . Add ( new List < int > { 1 , 2 , 2 , 1 } ) ; myList . Add ( new List < int > { 3 , 1 , 2 } ) ; myList . Add ( new List < int > { 1 , 3 , 2 } ) ; myList . Add ( new List < int > { 2 , 4 } ) ; myList . Add ( new List < int > { 3 , 1 , 2 } ) ; myList . Add ( new List < int > { 1 , 3 , 1 , 1 } ) ; leastBricks ( myList ) ; } }
|
Find an N | C # implementation of the above approach ; Function to print the required permutation ; Driver code
|
using System ; class GFG { static void findPermutation ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( i + " β " ) ; } public static void Main ( ) { int N = 5 ; findPermutation ( N ) ; } }
|
Maximize count of distinct profits possible by N transactions | C # program for the above approach ; Function to count distinct profits possible ; Stores the minimum total profit ; Stores the maximum total profit ; Return count of distinct profits ; Driver Code ; Input ; Function call
|
using System ; class GFG { static int numberOfWays ( int N , int X , int Y ) { int S1 = ( N - 1 ) * X + Y ; int S2 = ( N - 1 ) * Y + X ; return ( S2 - S1 + 1 ) ; } public static void Main ( ) { int N = 3 ; int X = 13 ; int Y = 15 ; Console . WriteLine ( numberOfWays ( N , X , Y ) ) ; } }
|
Minimum number of operations required to make a permutation of first N natural numbers equal | C # program for the above approach ; Function to find the minimum number of operations required to make all array elements equal ; Store the count of operations required ; Increment by K - 1 , as the last element will be used again for the next K consecutive elements ; Increment count by 1 ; Return the result ; Driver Code ; Given Input
|
using System ; class GFG { static int MinimumOperations ( int [ ] A , int N , int K ) { int Count = 0 ; int i = 0 ; while ( i < N - 1 ) { i = i + K - 1 ; Count ++ ; } return Count ; } public static void Main ( ) { int [ ] A = { 5 , 4 , 3 , 1 , 2 } ; int K = 3 ; int N = A . Length ; Console . WriteLine ( MinimumOperations ( A , N , K ) ) ; } }
|
Consecutive Prime numbers greater than equal to given number . | C # program for the above approach ; Function to check prime . ; It means it is not a prime ; No factor other than 1 therefore prime number ; Function to find out the required consecutive primes . ; Finding first prime just less than sqrt ( n ) . ; Finding prime just greater than sqrt ( n ) . ; Product of both prime is greater than n then print it ; Finding prime greater than second ; Driver Program
|
using System ; class GFG { static bool is_prime ( long n ) { if ( n == 1 ) { return false ; } for ( long i = 2 ; i <= ( long ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static void consecutive_primes ( long n ) { long first = - 1 , second = - 1 ; for ( long i = ( long ) Math . Sqrt ( n ) ; i >= 2 ; i -- ) { if ( is_prime ( i ) ) { first = i ; break ; } } for ( long i = ( long ) Math . Sqrt ( n ) + 1 ; i <= n / 2 ; i ++ ) { if ( is_prime ( i ) ) { second = i ; break ; } } if ( first * second >= n ) { Console . WriteLine ( first + " β " + second ) ; } else { for ( long i = second + 1 ; i <= n ; i ++ ) { if ( is_prime ( i ) ) { Console . WriteLine ( second + " β " + i ) ; return ; } } } } public static void Main ( ) { long n = 14 ; consecutive_primes ( n ) ; } }
|
Reorder characters of a string to valid English representations of digits | C # program to implement the above approach ; Function to construct the original set of digits from the string in ascending order ; Store the unique characters corresponding to word and number ; Store the required result ; Store the frequency of each character of S ; Traverse the unique characters ; Store the count of k [ i ] in S ; Traverse the corresponding word ; Decrement the frequency of characters by x ; Append the digit x times to ans ; Sort the digits in ascending order ; Driver Code ; Given string , s ; Function Call
|
using System ; using System . Collections . Generic ; class GFG { static string construct_digits ( string s ) { char [ ] k = { ' z ' , ' w ' , ' u ' , ' x ' , ' g ' , ' h ' , ' o ' , ' f ' , ' v ' , ' i ' } ; string [ ] l = { " zero " , " two " , " four " , " six " , " eight " , " three " , " one " , " five " , " seven " , " nine " } ; int [ ] c = { 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 } ; List < string > ans = new List < string > ( ) ; Dictionary < char , int > d = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( ! d . ContainsKey ( s [ i ] ) ) d [ s [ i ] ] = 0 ; d [ s [ i ] ] += 1 ; } for ( int i = 0 ; i < k . Length ; i ++ ) { int x = 0 ; if ( d . ContainsKey ( k [ i ] ) ) x = d [ k [ i ] ] ; for ( int j = 0 ; j < l [ i ] . Length ; j ++ ) { if ( d . ContainsKey ( l [ i ] [ j ] ) ) d [ l [ i ] [ j ] ] -= x ; } ans . Add ( ( ( c [ i ] ) * x ) . ToString ( ) ) ; } ans . Sort ( ) ; string str = ( String . Join ( " " , ans . ToArray ( ) ) ) ; return str . Replace ( "0" , " " ) ; } public static void Main ( string [ ] args ) { string s = " fviefuro " ; Console . WriteLine ( construct_digits ( s ) ) ; } }
|
Minimum decrements required to make all pairs of adjacent matrix elements distinct | C # program for the above approach ; Function to count minimum number of operations required ; Case 1 : ; Case 2 : ; Print the minimum number of operations required ; Driver Code ; The given matrix ; Function Call to count the minimum number of decrements required
|
using System ; class GFG { static void countDecrements ( long [ , ] arr ) { int n = arr . GetLength ( 0 ) ; int m = arr . GetLength ( 1 ) ; int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( i + j ) % 2 == arr [ i , j ] % 2 ) count_1 ++ ; if ( 1 - ( i + j ) % 2 == arr [ i , j ] % 2 ) count_2 ++ ; } } Console . WriteLine ( Math . Min ( count_1 , count_2 ) ) ; } public static void Main ( ) { long [ , ] arr = { { 1 , 2 , 3 } , { 1 , 2 , 3 } , { 1 , 2 , 3 } } ; countDecrements ( arr ) ; } }
|
Check if X can be converted to Y by converting to 3 * ( X / 2 ) or X | C # program for the above approach ; Function to check if X can be made equal to Y by converting X to ( 3 * X / 2 ) or ( X - 1 ) ; Conditions for possible conversion ; Otherwise , conversion is not possible ; Driver Code
|
using System ; class GFG { static void check ( int X , int Y ) { if ( X > 3 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 1 && Y == 1 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 2 && Y <= 3 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 3 && Y <= 3 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( string [ ] args ) { int X = 6 , Y = 8 ; check ( X , Y ) ; } }
|
Count distinct sum of pairs possible from a given range | C # program for the above approach ; Function to count distinct sum of pairs possible from the range [ L , R ] ; Return the count of distinct sum of pairs ; Driver Code
|
using System ; using System . Collections . Generic ; class GFG { static int distIntegers ( int L , int R ) { return 2 * R - 2 * L + 1 ; } static public void Main ( ) { int L = 3 , R = 8 ; Console . Write ( distIntegers ( L , R ) ) ; } }
|
Count subarrays having even Bitwise XOR | C # program for the above approach ; Function to count subarrays having even Bitwise XOR ; Store the required result ; Stores count of subarrays with even and odd XOR values ; Stores Bitwise XOR of current subarray ; Traverse the array ; Update current Xor ; If XOR is even ; Update ans ; Increment count of subarrays with even XOR ; Otherwise , increment count of subarrays with odd XOR ; Print the result ; Driver Code ; Given array ; Stores the size of the array
|
using System ; class GFG { static void evenXorSubarray ( int [ ] arr , int n ) { int ans = 0 ; int [ ] freq = { 0 , 0 } ; int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { XOR = XOR ^ arr [ i ] ; if ( XOR % 2 == 0 ) { ans += freq [ 0 ] + 1 ; freq [ 0 ] ++ ; } else { ans += freq [ 1 ] ; freq [ 1 ] ++ ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; evenXorSubarray ( arr , N ) ; } }
|
Count occurrences of an element in a matrix of size N * N generated such that each element is equal to product of its indices | C # program for above approach ; Function to count the occurrences of X in the generated square matrix ; Stores the required result ; Iterate upto square root of X ; Check if i divides X ; Store the quotient obtained on dividing X by i ; If both the numbers fall in the range , update count ; Return the result ; Driver code ; Given N and X ; Function Call
|
using System ; public class GFG { static int countOccurrences ( int N , int X ) { int count = 0 ; for ( int i = 1 ; i < Math . Sqrt ( X ) ; i ++ ) { if ( X % i == 0 ) { int a = i ; int b = X / i ; if ( a <= N && b <= N ) { if ( a == b ) count += 1 ; else count += 2 ; } } } return count ; } public static void Main ( String [ ] args ) { int N = 7 ; int X = 12 ; Console . Write ( countOccurrences ( N , X ) ) ; } }
|
Maximize array product by changing any array element arr [ i ] to ( | C # program for the above approach ; Function to find array with maximum product by changing array elements to ( - 1 ) arr [ i ] - 1 ; Traverse the array ; Applying the operation on all the positive elements ; Stores maximum element in array ; Stores index of maximum element ; Check if current element is greater than the maximum element ; Find index of the maximum element ; Perform the operation on the maximum element ; Print the elements of the array ; Driver Code ; Function Call
|
using System ; class GFG { static void findArrayWithMaxProduct ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] >= 0 ) { arr [ i ] = - arr [ i ] - 1 ; } if ( N % 2 == 1 ) { int max_element = - 1 ; int index = - 1 ; for ( int i = 0 ; i < N ; i ++ ) if ( Math . Abs ( arr [ i ] ) > max_element ) { max_element = Math . Abs ( arr [ i ] ) ; index = i ; } arr [ index ] = - arr [ index ] - 1 ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( arr [ i ] + " β " ) ; } static public void Main ( ) { int [ ] arr = { - 3 , 0 , 1 } ; int N = arr . Length ; findArrayWithMaxProduct ( arr , N ) ; } }
|
Lexicographically smallest permutation of first N natural numbers having K perfect indices | C # program for the above approach ; Function to print the lexicographically smallest permutation with K perfect indices ; Iterator to traverse the array ; Traverse first K array indices ; Traverse remaining indices ; Driver Code
|
using System ; class GFG { static void findPerfectIndex ( int N , int K ) { int i = 0 ; for ( ; i < K ; i ++ ) { Console . Write ( ( N - K + 1 ) + i + " β " ) ; } for ( ; i < N ; i ++ ) { Console . Write ( i - K + 1 + " β " ) ; } } public static void Main ( String [ ] args ) { int N = 10 , K = 3 ; findPerfectIndex ( N , K ) ; } }
|
Count pairs having distinct sum from a given range | C # program for the above approach ; Function to count pairs made up of elements from the range [ L , R ] having distinct sum ; Stores the least sum which can be formed by the pairs ; Stores the highest sum which can be formed by the pairs ; Stores the count of pairs having distinct sum ; Print the count of pairs ; Driver Code ; Function call to count the number of pairs having distinct sum in the range [ L , R ]
|
using System ; public class GFG { static void countPairs ( long L , long R ) { long firstNum = 2 * L ; long lastNum = 2 * R ; long Cntpairs = lastNum - firstNum + 1 ; Console . WriteLine ( Cntpairs ) ; } static public void Main ( ) { long L = 2 , R = 3 ; countPairs ( L , R ) ; } }
|
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | C # program for the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Total number of pairs possible from the array ; Stores frequency of each array element ; Traverse the array A [ ] ; Increment ump [ A [ i ] ] by 1 ; Traverse the Hashmap ump ; Subtract those pairs ( i , j ) from count which has the same element on index i and j ( i < j ) ; Print the result ; Driver code ; Function Call
|
using System ; using System . Collections . Generic ; class GFG { static void countPairs ( int [ ] A , int n ) { long count = ( n * ( n - 1 ) ) / 2 ; Dictionary < long , long > ump = new Dictionary < long , long > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ump . ContainsKey ( A [ i ] ) ) { ump [ A [ i ] ] ++ ; } else { ump [ A [ i ] ] = 1 ; } } foreach ( KeyValuePair < long , long > it in ump ) { long c = it . Value ; count = count - ( c * ( c - 1 ) ) / 2 ; } Console . WriteLine ( count ) ; } static void Main ( ) { int [ ] A = { 1 , 4 , 7 } ; int N = A . Length ; countPairs ( A , N ) ; } }
|
Check if a Rook can reach the given destination in a single move | C # program to implement the above approach ; Function to check if it is possible to reach destination in a single move by a rook ; Driver Code ; Given arrays
|
using System ; class GFG { static string check ( int current_row , int current_col , int destination_row , int destination_col ) { if ( current_row == destination_row ) return " POSSIBLE " ; else if ( current_col == destination_col ) return " POSSIBLE " ; else return " NOT β POSSIBLE " ; } public static void Main ( ) { int current_row = 8 ; int current_col = 8 ; int destination_row = 8 ; int destination_col = 4 ; string output = check ( current_row , current_col , destination_row , destination_col ) ; Console . WriteLine ( output ) ; } }
|
Append X digits to the end of N to make it divisible by M | C # program to implement the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver code ; Stores the number by appending X digits on the right side of N
|
using System ; class GFG { static int isDiv ( int N , int X , int M , int res ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return res ; } return res ; } for ( int i = 0 ; i < 9 ; i ++ ) { int temp = isDiv ( N * 10 + i , X - 1 , M , res ) ; if ( temp != - 1 ) { return temp ; } } return res ; } public static void Main ( ) { int N = 4 , M = 50 , X = 2 ; int res = - 1 ; res = isDiv ( N , X , M , res ) ; Console . WriteLine ( res ) ; } }
|
Minimum increments to modify array such that value of any array element can be splitted to make all remaining elements equal | C # program for the above approach ; Function to find the minimum moves ; Stores the sum of the array ; Store the maximum element ; Base Case ; If N is 2 , the answer will always be 0 ; Traverse the array ; Calculate sum of the array ; Finding maximum element ; Calculate ceil ( sum / N - 1 ) ; If k is smaller than maxelement ; Final sum - original sum ; Print the minimum number of increments required ; Driver code ; Given array ; Size of given array ; Function Call
|
using System ; class GFG { static void minimumMoves ( int [ ] arr , int N ) { int sum = 0 ; int maxelement = - 1 ; if ( N == 2 ) { Console . Write ( "0" ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; maxelement = Math . Max ( maxelement , arr [ i ] ) ; } int k = ( sum + N - 2 ) / ( N - 1 ) ; k = Math . Max ( maxelement , k ) ; int ans = k * ( N - 1 ) - sum ; Console . WriteLine ( ans ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 7 } ; int N = arr . Length ; minimumMoves ( arr , N ) ; } }
|
Split first N natural numbers into two subsequences with non | C # program for the above approach ; Function to split 1 to N into two subsequences with non - coprime sums ; Driver code
|
using System ; class GFG { public static void printSubsequence ( int N ) { Console . Write ( " { β " ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { Console . Write ( i + " , β " ) ; } Console . WriteLine ( N - 1 + " β } " ) ; Console . Write ( " { β " + N + " β } " ) ; } public static void Main ( string [ ] args ) { int N = 8 ; printSubsequence ( N ) ; } }
|
Generate an array with product of all subarrays of length exceeding one divisible by K | C # program for the above approach ; Function to check if the required array can be generated or not ; To check if divisor exists ; To store divisiors of K ; Check if K is prime or not ; If array can be generated ; Print d1 and d2 alternatively ; No such array can be generated ; Driver Code ; Given N and K ; Function Call
|
using System ; class GFG { public static void array_divisbleby_k ( int N , int K ) { bool flag = false ; int d1 = 0 , d2 = 0 ; for ( int i = 2 ; i * i <= K ; i ++ ) { if ( K % i == 0 ) { flag = true ; d1 = i ; d2 = K / i ; break ; } } if ( flag ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) { Console . Write ( d2 + " β " ) ; } else { Console . Write ( d1 + " β " ) ; } } } else { Console . Write ( - 1 ) ; } } public static void Main ( string [ ] args ) { int N = 5 , K = 21 ; array_divisbleby_k ( N , K ) ; } }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.