codeparrot/xlcost-text-to-code huggingface dataset:
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 ) ; } }