codeparrot/xlcost-text-to-code huggingface dataset:
Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | C # Program to implement the above approach ; Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Check if sum of first i natural number >= X ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X
using System ; public class GFG { static bool isGreaterEqual ( int N , int X ) { return ( N * ( N + 1 ) / 2 ) >= X ; } static int minimumPossible ( int X ) { for ( int i = 1 ; i <= X ; i ++ ) { if ( isGreaterEqual ( i , X ) ) return i ; } return 0 ; } static public void Main ( ) { int X = 14 ; Console . Write ( minimumPossible ( X ) ) ; } }
Queries to count numbers from given range which are divisible by all its digits | C # program to implement the above approach ; Function to check if a number is divisible by all of its non - zero digits or not ; Stores the number ; Iterate over the digits of the numbers ; If digit of number is non - zero ; If number is not divisible by its current digit ; Update n ; Function to count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Stores count of numbers which are divisible by all of its non - zero digits in the range [ 1 , i ] ; Iterate over the range [ 1 , Max ] ; Update ; Traverse the array , arr [ ] ; Driver Code
using System ; class GFG { public static int Max = 1000005 ; static bool CheckDivByAllDigits ( int number ) { int n = number ; while ( n > 0 ) { if ( n % 10 != 0 ) if ( number % ( n % 10 ) != 0 ) { return false ; } n /= 10 ; } return true ; } static void cntNumInRang ( int [ , ] arr , int N ) { int [ ] prefCntDiv = new int [ Max + 1 ] ; for ( int i = 1 ; i <= Max ; i ++ ) { int ans = 0 ; if ( CheckDivByAllDigits ( i ) ) ans = 1 ; prefCntDiv [ i ] = prefCntDiv [ i - 1 ] + ans ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( ( prefCntDiv [ arr [ i , 1 ] ] - prefCntDiv [ arr [ i , 0 ] - 1 ] ) + " ▁ " ) ; } public static void Main ( string [ ] args ) { int [ , ] arr = { { 1 , 5 } , { 12 , 14 } } ; int N = arr . GetLength ( 0 ) ; cntNumInRang ( arr , N ) ; } }
Longest subset of nested elements from a given array | C # program to implement the above approach ; Function to find length of longest subset such that subset { arr [ i ] , arr [ arr [ i ] ] , . . } ; Stores length of the longest subset that satisfy the condition ; Traverse in the array , arr [ ] ; If arr [ i ] equals to i ; Update res ; Count of elements in a subset ; Stores index of elements in the current subset ; Calculate length of a subset that satisfy the condition ; Make visited the current index ; Update curr_index ; Update count ; Update res ; Driver Code
using System ; class sol { public int arrayNesting ( int [ ] arr ) { int res = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] == i ) { res = Math . Max ( res , 1 ) ; } else { int count = 0 ; int curr_index = i ; while ( arr [ curr_index ] != curr_index ) { int next_index = arr [ curr_index ] ; arr [ curr_index ] = curr_index ; curr_index = next_index ; count ++ ; } res = Math . Max ( res , count ) ; } } return res ; } } class GFG { static public void Main ( ) { sol st = new sol ( ) ; int [ ] arr = { 5 , 4 , 0 , 3 , 1 , 6 , 2 } ; int res = st . arrayNesting ( arr ) ; Console . WriteLine ( res ) ; } }
Count numbers from a given range having exactly 5 distinct factors | C # Program to implement the above approach ; Stores all prime numbers up to 2 * 10 ^ 5 ; Function to generate all prime numbers up to 2 * 10 ^ 5 using Sieve of Eratosthenes ; Mark 0 and 1 as non - prime ; If i is prime ; Mark all its factors as non - prime ; If current number is prime ; Store the prime ; Function to count numbers in the range [ L , R ] having exactly 5 factors ; Stores the required count ; Driver Code
using System ; class GFG { static int N = 200000 ; static int [ ] prime = new int [ 20000 ] ; static int index = 0 ; static void Sieve ( ) { index = 0 ; int [ ] p = new int [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) { p [ i ] = 1 ; } p [ 0 ] = p [ 1 ] = 0 ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( p [ i ] == 1 ) { for ( int j = i * i ; j <= N ; j += i ) { p [ j ] = 0 ; } } } for ( int i = 1 ; i < N ; i ++ ) { if ( p [ i ] == 1 ) { prime [ index ++ ] = ( int ) ( Math . Pow ( i , 4 ) ) ; } } } static void countNumbers ( int L , int R ) { int Count = 0 ; for ( int i = 0 ; i < index ; i ++ ) { int p = prime [ i ] ; if ( p >= L && p <= R ) { Count ++ ; } } Console . WriteLine ( Count ) ; } public static void Main ( String [ ] args ) { int L = 16 , R = 85000 ; Sieve ( ) ; countNumbers ( L , R ) ; } }
Minimize remaining array sizes by removing equal pairs of first array elements | C # program for the above approach ; Function to count the remaining elements in the arrays ; Stores the count of 1 s in L1 [ ] ; Store the count of 0 s in L2 [ ] ; Traverse the array L1 [ ] ; If condition is true ; Increment one by 1 ; Increment zero by 1 ; Stores the index after which no further removals are possible ; Traverse the array L2 [ ] ; If condition is true ; Decrement one by 1 ; If one < 0 , then break out of the loop ; Decrement zero by 1 ; If zero < 0 , then break out of loop ; Print the answer ; Driver Code ; Function Call
using System ; class GFG { static void countRemainingElements ( int [ ] L1 , int [ ] L2 , int n ) { int one = 0 ; int zero = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( L1 [ i ] == 1 ) one ++ ; else zero ++ ; } int ans = n ; for ( int i = 0 ; i < n ; i ++ ) { if ( L2 [ i ] == 1 ) { one -- ; if ( one < 0 ) { ans = i ; break ; } } else { zero -- ; if ( zero < 0 ) { ans = i ; break ; } } } Console . WriteLine ( n - ans ) ; } static public void Main ( ) { int [ ] L1 = { 1 , 1 , 0 , 0 } ; int [ ] L2 = { 0 , 0 , 0 , 1 } ; int N = L1 . Length ; countRemainingElements ( L1 , L2 , N ) ; } }
Count pairs of similar rectangles possible from a given array | C # program for the above approach ; Function to calculate the count of similar rectangles ; Driver code ; Input
using System ; class GFG { static int getCount ( int rows , int columns , int [ , ] A ) { int res = 0 ; for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = i + 1 ; j < rows ; j ++ ) { if ( A [ i , 0 ] * A [ j , 1 ] == A [ i , 1 ] * A [ j , 0 ] ) { res ++ ; } } } return res ; } static void Main ( ) { int [ , ] A = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int columns = 2 ; int rows = 4 ; Console . Write ( getCount ( rows , columns , A ) ) ; } }
Find the index with minimum score from a given array | C # program for the above approach ; Function to find the index with minimum score ; Stores the score of current index ; Traverse the array in reverse ; Update minimum score ; Print the index with minimum score ; Driver Code
using System ; class GFG { static void Min_Score_Index ( int N , int [ ] A ) { int [ ] Score = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] + i < N ) Score [ i ] = A [ i ] * Score [ A [ i ] + i ] ; else Score [ i ] = A [ i ] ; } int min_value = Int32 . MaxValue ; foreach ( int i in Score ) { min_value = Math . Min ( i , min_value ) ; } int ind = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( Score [ i ] == min_value ) ind = i ; } Console . WriteLine ( ind ) ; } public static void Main ( ) { int N = 5 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; Min_Score_Index ( N , A ) ; } }
Minimize length of a string by removing occurrences of another string from it as a substring | C # program for the above approach ; Function to minimize length of string S after removing all occurrences of string T as substring ; Count of characters required to be removed ; Iterate over the string ; Insert the current character to temp ; Check if the last M characters forms t or not ; Getting the last M characters . If they are equal to t then remove the last M characters from the temp string ; Incrementing subtract by M ; Removing last M characters from the string ; Print the readonly answer ; Driver Code ; Given string S & T ; Length of string S ; Length of string T ; Prints the count of operations required
using System ; class GFG { static void minLength ( String S , String T , int N , int M ) { String temp = " " ; int subtract = 0 ; for ( int i = 0 ; i < N ; ++ i ) { temp += S [ i ] ; if ( temp . Length >= M ) { if ( T . Equals ( temp . Substring ( temp . Length - M , M ) ) ) { subtract += M ; int cnt = 0 ; while ( cnt != M ) { temp = temp . Substring ( 0 , temp . Length - 1 ) ; ++ cnt ; } } } } Console . WriteLine ( ( N - subtract ) ) ; } public static void Main ( String [ ] args ) { String S = " aabcbcbd " , T = " abc " ; int N = S . Length ; int M = T . Length ; minLength ( S , T , N , M ) ; } }
Minimize swaps between two arrays such that sum of the first array exceeds sum of the second array | C # program for the above approach ; Function to find the minimum count of swaps required between the two arrays to make the sum of arr1 [ ] greater than that of arr2 [ ] ; Stores the sum of the two arrays ; Calculate sum of arr1 [ ] ; Calculate sum of arr2 [ ] ; Sort the arrays arr1 [ ] and arr2 [ ] ; Traverse the array arr [ ] ; If the sum1 is less than or equal to sum2 ; Swapping the elements ; Update the sum1 and sum2 ; Increment the count ; Return the final count ; Driver Code ; Function Call
using System ; class GFG { static int maximumCount ( int [ ] arr1 , int [ ] arr2 , int s1 , int s2 ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < s1 ; i ++ ) { sum1 += arr1 [ i ] ; } for ( int a = 0 ; a < s2 ; a ++ ) { sum2 += arr2 [ a ] ; } int len = 0 ; if ( s1 >= s2 ) { len = s2 ; } else { len = s1 ; } Array . Sort ( arr1 ) ; Array . Sort ( arr2 ) ; int j = 0 , k = s2 - 1 , count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( sum1 <= sum2 ) { if ( arr2 [ k ] >= arr1 [ i ] ) { int dif1 = arr1 [ j ] , dif2 = arr2 [ k ] ; sum1 -= dif1 ; sum1 += dif2 ; sum2 -= dif2 ; sum2 += dif1 ; j ++ ; k -- ; count ++ ; } else { break ; } } else { break ; } } return count ; } static public void Main ( ) { int [ ] arr1 = new int [ ] { 1 , 3 , 2 , 4 } ; int [ ] arr2 = new int [ ] { 6 , 7 , 8 } ; int N = arr1 . Length ; int M = arr2 . Length ; Console . WriteLine ( maximumCount ( arr1 , arr2 , N , M ) ) ; } }
Count array elements whose all distinct digits appear in K | C # program for the above approach ; Function to check a digit occurs in the digit of K or not ; Iterate over all possible digits of K ; If current digit equal to digit ; Update K ; Function to find the count of array elements whose distinct digits are a subset of digits of K ; Stores count of array elements whose distinct digits are subset of digits of K ; Traverse the array , [ ] arr ; Stores the current element ; Check if all the digits arr [ i ] is a subset of the digits of K or not ; Iterate over all possible digits of arr [ i ] ; Stores current digit ; If current digit does not appear in K ; Update flag ; Update no ; If all the digits arr [ i ] appear in K ; Update count ; Finally print count ; Driver Code
using System ; class GFG { static bool isValidDigit ( int digit , int K ) { while ( K != 0 ) { if ( K % 10 == digit ) { return true ; } K = K / 10 ; } return false ; } static int noOfValidNumbers ( int K , int [ ] arr ) { int count = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { int no = arr [ i ] ; bool flag = true ; while ( no != 0 ) { int digit = no % 10 ; if ( ! isValidDigit ( digit , K ) ) { flag = false ; break ; } no = no / 10 ; } if ( flag == true ) { count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int K = 12 ; int [ ] arr = { 1 , 12 , 1222 , 13 , 2 } ; Console . WriteLine ( noOfValidNumbers ( K , arr ) ) ; } }
Find a K | C # program to implement the above approach ; Utility function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Find XOR of whole array ; Find XOR of first K elements ; Adding XOR of next element ; Removing XOR of previous element ; Check if XOR of current subarray matches with the XOR of remaining elements or not ; Function to check if subarray of size K exits whose XOR of elements equal to XOR ofremaning array elements ; Driver Code ; Given array ; Size of the array ; Given K ; Function Call
using System ; class GFG { static bool isSubarrayExistUtil ( int [ ] arr , int K , int N ) { int totalXOR = 0 ; int SubarrayXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) totalXOR ^= arr [ i ] ; for ( int i = 0 ; i < K ; i ++ ) SubarrayXOR ^= arr [ i ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; for ( int i = K ; i < N ; i ++ ) { SubarrayXOR ^= arr [ i ] ; SubarrayXOR ^= arr [ i - 1 ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; } return false ; } static void isSubarrayExist ( int [ ] arr , int K , int N ) { if ( isSubarrayExistUtil ( arr , K , N ) ) Console . Write ( " YES STRNEWLINE " ) ; else Console . Write ( " NO STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 } ; int N = arr . Length ; int K = 5 ; isSubarrayExist ( arr , K , N ) ; } }
Count pairs from a given range having even sum | C # program for the above approach ; Function to find the count of ordered pairs having even sum ; Stores count of even numbers in the range [ L , R ] ; If L is even ; Update count_even ; Update count_odd ; Stores count of odd numbers in the range [ L , R ] ; Update count_odd ; Update count_odd ; Stores count of pairs whose sum is even and both elements of the pairs are also even ; Stores count of pairs whose sum is even and both elements of the pairs are odd ; Print total ordered pairs whose sum is even ; Driver Code ; Given L & R ; Function Call
using System ; class GFG { static void countPairs ( int L , int R ) { int count_even ; if ( L % 2 == 0 ) { count_even = ( R / 2 ) - ( L / 2 ) + 1 ; } else { count_even = ( R / 2 ) - ( L / 2 ) ; } int count_odd ; if ( L % 2 == 0 ) { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) ; } else { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) + 1 ; } count_even *= count_even ; count_odd *= count_odd ; Console . WriteLine ( count_even + count_odd ) ; } public static void Main ( String [ ] args ) { int L = 1 , R = 3 ; countPairs ( L , R ) ; } }
Count of array elements that can be found using Randomized Binary Search on every array element | C # program for the above approach ; Function to find minimum count of array elements found by repeatedly applying Randomized Binary Search ; Stores count of array elements ; smallestRight [ i ] : Stores the smallest array element on the right side of i ; Update smallestRight [ 0 ] ; Traverse the array from right to left ; Update smallestRight [ i ] ; Stores the largest element upto i - th index ; Stores the minimum count of elements found by repeatedly applying Randomized Binary Search ; If largest element on left side is less than smallest element on right side ; Update ans ; Update mn ; Driver Code ; Given array ; Function Call
using System ; class GFG { static int getDefiniteFinds ( int [ ] arr ) { int n = arr . Length ; int [ ] smallestRight = new int [ n + 1 ] ; smallestRight [ n ] = Int32 . MaxValue ; for ( int i = n - 1 ; i >= 0 ; i -- ) { smallestRight [ i ] = Math . Min ( smallestRight [ i + 1 ] , arr [ i ] ) ; } int mn = Int32 . MinValue ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mn < arr [ i ] && arr [ i ] < smallestRight [ i + 1 ] ) { ans ++ ; } mn = Math . Max ( arr [ i ] , mn ) ; } return ans ; } static public void Main ( ) { int [ ] arr = new int [ ] { 5 , 4 , 9 } ; Console . WriteLine ( getDefiniteFinds ( arr ) ) ; } }
Count numbers from a given range having same first and last digits in their binary representation | C # program to implement the above approach ; Function to count numbers in range [ L , R ] having same first and last digit in the binary representation ; Driver code ; Given range [ L , R ] ; Function Call
using System ; class GFG { static void Count_numbers ( int L , int R ) { int count = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) count += 1 ; Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int L = 6 , R = 30 ; Count_numbers ( L , R ) ; } }
Find the index having sum of elements on its left equal to reverse of the sum of elements on its right | C # Program to implement the above approach ; Function to check if a number is equal to the reverse of digits of other number ; Stores reverse of digits of rightSum ; Stores rightSum ; Calculate reverse of digits of temp ; Update rev ; Update temp ; If reverse of digits of rightSum equal to leftSum ; Function to find the index that satisfies the condition ; Stores sum of array elements on right side of each index ; Stores sum of array elements on left side of each index ; Traverse the array ; Update rightSum ; Traverse the array ; Update rightSum ; If leftSum equal to reverse of digits of rightSum ; Update leftSum ; Driver code
using System ; class GFG { static bool checkReverse ( int leftSum , int rightSum ) { int rev = 0 ; int temp = rightSum ; while ( temp != 0 ) { rev = ( rev * 10 ) + ( temp % 10 ) ; temp /= 10 ; } if ( rev == leftSum ) { return true ; } return false ; } static int findIndex ( int [ ] arr , int N ) { int rightSum = 0 ; int leftSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { rightSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { rightSum -= arr [ i ] ; if ( checkReverse ( leftSum , rightSum ) ) { return i ; } leftSum += arr [ i ] ; } return - 1 ; } static void Main ( ) { int [ ] arr = { 5 , 7 , 3 , 6 , 4 , 9 , 2 } ; int N = arr . Length ; Console . Write ( findIndex ( arr , N ) ) ; } }
Longest Common Subsequence of two arrays out of which one array consists of distinct elements only | C # program to to implement the above approach ; Function to find the longest Common Subsequence between the two arrays ; Maps elements of firstArr [ ] to their respective indices ; Traverse the array firstArr [ ] ; Stores the indices of common elements between firstArr [ ] and secondArr [ ] ; Traverse the array secondArr [ ] ; If current element exists in the Map ; Stores lIS from tempArr [ ] ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int LCS ( int [ ] firstArr , int [ ] secondArr ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < firstArr . Length ; i ++ ) { mp . Add ( firstArr [ i ] , i + 1 ) ; } List < int > tempArr = new List < int > ( ) ; for ( int i = 0 ; i < secondArr . Length ; i ++ ) { if ( mp . ContainsKey ( secondArr [ i ] ) ) { tempArr . Add ( mp [ secondArr [ i ] ] ) ; } } List < int > tail = new List < int > ( ) ; tail . Add ( tempArr [ 0 ] ) ; for ( int i = 1 ; i < tempArr . Count ; i ++ ) { if ( tempArr [ i ] > tail [ tail . Count - 1 ] ) tail . Add ( tempArr [ i ] ) ; else if ( tempArr [ i ] < tail [ 0 ] ) tail . Insert ( 0 , tempArr [ i ] ) ; } return ( int ) tail . Count ; } public static void Main ( String [ ] args ) { int [ ] firstArr = { 3 , 5 , 1 , 8 } ; int [ ] secondArr = { 3 , 3 , 5 , 3 , 8 } ; Console . Write ( LCS ( firstArr , secondArr ) ) ; } }
Count ways to partition a Binary String such that each substring contains exactly two 0 s | C # program for the above approach ; Function to find count of ways to partition the string such that each partition contains exactly two 0 s . ; Stores indices of 0 s in the given string . ; Store the count of ways to partition the string such that each partition contains exactly two 0 s . ; Iterate over each characters of the given string ; If current character is '0' ; Insert index ; Stores total count of 0 s in str ; Traverse the array , IdxOf0s [ ] ; Update cntWays ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int totalWays ( int n , string str ) { ArrayList IdxOf0s = new ArrayList ( ) ; int cntWays = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { IdxOf0s . Add ( i ) ; } } int M = IdxOf0s . Count ; if ( ( M == 0 ) || ( ( M % 2 ) != 0 ) ) { return 0 ; } for ( int i = 2 ; i < M ; i += 2 ) { cntWays = cntWays * ( Convert . ToInt32 ( IdxOf0s [ i ] ) - Convert . ToInt32 ( IdxOf0s [ i - 1 ] ) ) ; } return cntWays ; } static public void Main ( ) { string str = "00100" ; int n = str . Length ; Console . Write ( totalWays ( n , str ) ) ; } }
Count non | C # program to implement the above approach ; Function to find the total count of triplets ( i , j , k ) such that i < j < k and ( j - i ) != ( k - j ) ; Stores indices of 0 s ; Stores indices of 1 s ; Stores indices of 2 s ; Traverse the array ; If current array element is 0 ; If current array element is 1 ; If current array element is 2 ; Total count of triplets ; Traverse the array zero_i [ ] ; Traverse the array one_i [ ] ; Stores index of 0 s ; Stores index of 1 s ; Stores third element of triplets that does not satisfy the condition ; If r present in the map ; Update r ; If r present in the map ; Update r ; If r present in the map and equidistant ; Print the obtained count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void countTriplets ( int [ ] arr , int N ) { List < int > zero_i = new List < int > ( ) ; List < int > one_i = new List < int > ( ) ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero_i . Add ( i + 1 ) ; else if ( arr [ i ] == 1 ) one_i . Add ( i + 1 ) ; else mp . Add ( i + 1 , 1 ) ; } int total = zero_i . Count * one_i . Count * mp . Count ; for ( int i = 0 ; i < zero_i . Count ; i ++ ) { for ( int j = 0 ; j < one_i . Count ; j ++ ) { int p = zero_i [ i ] ; int q = one_i [ j ] ; int r = 2 * p - q ; if ( mp . ContainsKey ( r ) && mp [ r ] > 0 ) total -- ; r = 2 * q - p ; if ( mp . ContainsKey ( r ) && mp [ r ] > 0 ) total -- ; r = ( p + q ) / 2 ; if ( mp . ContainsKey ( r ) && mp [ r ] > 0 && Math . Abs ( r - p ) == Math . Abs ( r - q ) ) total -- ; } } Console . Write ( total ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 2 , 1 } ; int N = arr . Length ; countTriplets ( arr , N ) ; } }
Print prime factors of a given integer in decreasing order using Stack | C # program for the above approach ; Function to print prime factors of N in decreasing order ; Stores prime factors of N in decreasing order ; Insert i into stack ; Update N ; Update i ; Print value of stack st ; Driver Code ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void PrimeFactors ( int N ) { Stack < int > st = new Stack < int > ( ) ; int i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . Push ( i ) ; while ( N % i == 0 ) { N = N / i ; } } i ++ ; } while ( st . Count != 0 ) { Console . Write ( st . Peek ( ) ) ; st . Pop ( ) ; } } public static void Main ( ) { int N = 8 ; PrimeFactors ( N ) ; ; } }
Count subarrays consisting of first K natural numbers in descending order | C # program for the above approach ; Function to count subarray having the decreasing sequence K to 1 ; Traverse the array ; Check if required sequence is present or not ; Reset temp to k ; Return the count ; Driver code ; Function Call
using System ; class GFG { static int CountSubarray ( int [ ] arr , int n , int k ) { int temp = k , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == temp ) { if ( temp == 1 ) { count ++ ; temp = k ; } else temp -- ; } else { temp = k ; if ( arr [ i ] == k ) i -- ; } } return count ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 7 , 9 , 3 , 2 , 1 , 8 , 3 , 2 , 1 } ; int N = arr . Length ; int K = 3 ; Console . Write ( CountSubarray ( arr , N , K ) ) ; } }