codeparrot/xlcost-text-to-code huggingface dataset:
Maximum time such that absolute difference between hour and minute lies in given range | C # program for the above approach ; Function checks whether given time is correct ; To check minute value of time ; To check hour value of time ; Changes in value is not allowed at position where ' ? ' is not present ; Function checks whether the absolute difference between hour and minute value is within [ L , R ] ; Checks if the difference is outside the give range ; Displays time in proper 24 - hour format ; Function find the desired value of time whose difference lies in the range [ L , R ] ; Decrease hour value from 23 to 0 ; Check if the hour value is valid if not valid then no need to change minute value , since time will still remain in valid , to check hour value flag is set to 1. ; Decrease minute value from 59 to 0 ; Check if the minute value is valid , if not valid then skip the current iteration , to check ' minute ' value flag is set to 0. ; Driver code ; Input time ; Difference range
using System ; class GFG { static bool isValid ( char a1 , char a2 , string str , int flag ) { char v1 , v2 ; if ( flag == 0 ) { v1 = str [ 4 ] ; v2 = str [ 3 ] ; } else { v1 = str [ 1 ] ; v2 = str [ 0 ] ; } if ( v1 != a1 && v1 != ' ? ' ) { return false ; } if ( v2 != a2 && v2 != ' ? ' ) { return false ; } return true ; } static bool inRange ( int hh , int mm , int L , int R ) { int a = Math . Abs ( hh - mm ) ; if ( a < L a > R ) { return false ; } return true ; } static void displayTime ( int hh , int mm ) { if ( hh > 10 ) { Console . Write ( hh + " : " ) ; } else if ( hh < 10 ) { Console . Write ( "0" + hh + " : " ) ; } if ( mm > 10 ) { Console . Write ( mm ) ; } else if ( mm < 10 ) { Console . Write ( "0" + mm ) ; } } static void maximumTimeWithDifferenceInRange ( string str , int L , int R ) { int i = 0 , j = 0 ; int h1 , h2 , m1 , m2 ; for ( i = 23 ; i >= 0 ; i -- ) { h1 = i % 10 ; h2 = i / 10 ; if ( ! isValid ( ( char ) h1 , ( char ) h2 , str , 1 ) ) { continue ; } for ( j = 59 ; j >= 0 ; j -- ) { m1 = j % 10 ; m2 = j / 10 ; if ( ! isValid ( ( char ) m1 , ( char ) m2 , str , 0 ) ) { continue ; } if ( inRange ( i , j , L , R ) ) { displayTime ( i , j ) ; return ; } } } if ( inRange ( i , j , L , R ) ) { displayTime ( i , j ) ; } else { Console . WriteLine ( " - 1" ) ; } } static public void Main ( ) { string timeValue = " ? ? : ? ? " ; int L = 20 , R = 39 ; maximumTimeWithDifferenceInRange ( timeValue , L , R ) ; } }
Check if a string can be split into even length palindromic substrings | C # program for the above approach ; Function to check String str can be split a String into even length palindromic subStrings ; Initialize a stack ; Iterate the String ; If the i - th character is same as that at the top of the stack then pop the top element ; Else push the current charactor into the stack ; If the stack is empty , then even palindromic subStrings are possible ; Else not - possible ; Driver Code ; Given String ; Function call
using System ; using System . Collections . Generic ; class GFG { static bool check ( String s , int n ) { Stack < int > st = new Stack < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( st . Count != 0 && st . Peek ( ) == s [ i ] ) st . Pop ( ) ; else st . Push ( s [ i ] ) ; } if ( st . Count == 0 ) { return true ; } else { return false ; } } public static void Main ( String [ ] args ) { String str = " aanncddc " ; int n = str . Length ; if ( check ( str , n ) ) { Console . Write ( " Yes " + " STRNEWLINE " ) ; } else { Console . Write ( " No " + " STRNEWLINE " ) ; } } }
Number of strings in two array satisfy the given conditions | C # program for the above approach ; To store the frequency of String after bitmasking ; To store result for each string in arr2 [ ] ; Traverse the arr1 [ ] and bitmask each string in it ; Bitmasking for each String s ; Update the frequency of String with it 's bitmasking value ; Traverse the arr2 [ ] ; Bitmasking for each String s ; Check if temp is present in an unordered_map or not ; Check for next set bit ; Push the count for current String in resultant array ; Print the count for each String ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void findNumOfValidWords ( List < String > w , List < String > p ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; List < int > res = new List < int > ( ) ; foreach ( String s in w ) { int val = 0 ; foreach ( char c in s . ToCharArray ( ) ) { val = val | ( 1 << ( c - ' a ' ) ) ; } if ( m . ContainsKey ( val ) ) m [ val ] = m [ val ] + 1 ; else m . Add ( val , 1 ) ; } foreach ( String s in p ) { int val = 0 ; foreach ( char c in s . ToCharArray ( ) ) { val = val | ( 1 << ( c - ' a ' ) ) ; } int temp = val ; int first = s [ 0 ] - ' a ' ; int count = 0 ; while ( temp != 0 ) { if ( ( ( temp >> first ) & 1 ) == 1 ) { if ( m . ContainsKey ( temp ) ) { count += m [ temp ] ; } } temp = ( temp - 1 ) & val ; } res . Add ( count ) ; } foreach ( int it in res ) { Console . WriteLine ( it ) ; } } public static void Main ( String [ ] args ) { List < String > arr1 = new List < String > ( ) ; arr1 . Add ( " aaaa " ) ; arr1 . Add ( " asas " ) ; arr1 . Add ( " able " ) ; arr1 . Add ( " ability " ) ; arr1 . Add ( " actt " ) ; arr1 . Add ( " actor " ) ; arr1 . Add ( " access " ) ; List < String > arr2 = new List < String > ( ) ; arr2 . Add ( " aboveyz " ) ; arr2 . Add ( " abrodyz " ) ; arr2 . Add ( " absolute " ) ; arr2 . Add ( " absoryz " ) ; arr2 . Add ( " actresz " ) ; arr2 . Add ( " gaswxyz " ) ; findNumOfValidWords ( arr1 , arr2 ) ; } }
Maximize the decimal equivalent by flipping only a contiguous set of 0 s | C # implementation to maximize the value of the decimal equivalent given in the binary form ; Function to print the binary number ; Check if the current number is 0 ; Find the continuous 0 s ; Replace initially occurring 0 with 1 ; Break out of loop if 1 occurs ; Driver code
using System ; class GFG { static String flip ( char [ ] s ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '0' ) { while ( s [ i ] == '0' ) { s [ i ] = '1' ; i ++ ; } break ; } } return new String ( s ) ; } public static void Main ( String [ ] args ) { String s = "100010001" ; Console . WriteLine ( flip ( s . ToCharArray ( ) ) ) ; } }
Sentence Case of a given Camel cased string | C # implementation of the approach ; Function to return the original string after converting it back from camelCase ; Print the first character as it is ; Traverse the rest of the characters one by one ; If current character is uppercase print space followed by the current character in lowercase ; Else print the current character ; Driver code
using System ; class GFG { static void getOrgString ( String s ) { Console . Write ( s [ 0 ] ) ; int i = 1 ; while ( i < s . Length ) { if ( s [ i ] >= ' A ' && s [ i ] <= ' Z ' ) Console . Write ( " ▁ " + char . ToLower ( s [ i ] ) ) ; else Console . Write ( s [ i ] ) ; i ++ ; } } public static void Main ( String [ ] args ) { String s = " ILoveGeeksForGeeks " ; getOrgString ( s ) ; } }
Count occurrences of a character in a repeated string | C # program to find the occurrences of character x in the infinite repeated string upto length n ; Function to count the character ' a ' ; atleast k repetition are required ; if n is not the multiple of the string size check for the remaining repeating character . ; Driver code
using System ; class GFG { static int countChar ( string str , char x ) { int count = 0 ; int n = 10 ; for ( int i = 0 ; i < str . Length ; i ++ ) if ( str [ i ] == x ) count ++ ; int repetitions = n / str . Length ; count = count * repetitions ; for ( int i = 0 ; i < n % str . Length ; i ++ ) { if ( str [ i ] == x ) count ++ ; } return count ; } public static void Main ( ) { string str = " abcac " ; Console . WriteLine ( countChar ( str , ' a ' ) ) ; } }
Frequency Measuring Techniques for Competitive Programming | C # program to count frequencies of array items having small values . ; Create an array to store counts . The size of array is limit + 1 and all values are initially 0 ; Traverse through array elements and count frequencies ( assuming that elements are limited by limit ) ; Driver Code
using System ; class GFG { static void countFreq ( int [ ] arr , int n , int limit ) { int [ ] count = new int [ limit + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; for ( int i = 0 ; i <= limit ; i ++ ) if ( count [ i ] > 0 ) Console . WriteLine ( i + " ▁ " + count [ i ] ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 5 , 6 , 6 , 5 , 6 , 1 , 2 , 3 , 10 , 10 } ; int n = arr . Length ; int limit = 10 ; countFreq ( arr , n , limit ) ; } }
Check if a string has m consecutive 1 ' s ▁ or ▁ 0' s | Program to check if the binary string contains m consecutive 1 ' s ▁ or ▁ 0' s ; Function that checks if the binary string contains m consecutive 1 ' s ▁ or ▁ 0' s ; length of binary string ; counts zeros ; counts 1 's ; count consecutive 0 's ; count consecutive 1 's ; Driver Code ; function call
using System ; class GFG { static bool check ( string s , int m ) { int l = s . Length ; int c1 = 0 ; int c2 = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( s [ i ] == '0' ) { c2 = 0 ; c1 ++ ; } else { c1 = 0 ; c2 ++ ; } if ( c1 == m c2 == m ) return true ; } return false ; } public static void Main ( ) { String s = "001001" ; int m = 2 ; if ( check ( s , m ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Product of nodes at k | C # implementation to find product of digits of elements at k - th level ; Function to find product of digits of elements at k - th level ; Initialize result ; increasing level number ; decreasing level number ; check if current level is the desired level or not ; required product ; Driver Code
using System ; class GFG { static int productAtKthLevel ( string tree , int k ) { int level = - 1 ; int product = 1 ; int n = tree . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( tree [ i ] == ' ( ' ) level ++ ; else if ( tree [ i ] == ' ) ' ) level -- ; else { if ( level == k ) product *= ( tree [ i ] - '0' ) ; } } return product ; } static void Main ( ) { string tree = " ( 0(5(6 ( ) ( ) ) ( 4 ( ) (9 ( ) ( ) ) ) ) ( 7(1 ( ) ( ) ) ( 3 ( ) ( ) ) ) ) " ; int k = 2 ; Console . WriteLine ( productAtKthLevel ( tree , k ) ) ; } }
Removing row or column wise duplicates from matrix of characters | C # code to form string after removing duplicates from rows and columns . ; Function to check duplicates in row and column ; Create an array isPresent and initialize all entries of it as false . The value of isPresent [ i , j ] is going to be true if s [ i , j ] is present in its row or column . ; Checking every row for duplicates of a [ i , j ] ; Checking every column for duplicate characters ; If the character is unique in its row and column ; Driver code ; character array ; Calling function
using System ; class GFG { static void findDuplciates ( string [ ] a , int n , int m ) { bool [ , ] isPresent = new bool [ n , m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( a [ i ] [ j ] == a [ k ] [ j ] && i != k ) { isPresent [ i , j ] = true ; isPresent [ k , j ] = true ; } } for ( int k = 0 ; k < m ; k ++ ) { if ( a [ i ] [ j ] == a [ i ] [ k ] && j != k ) { isPresent [ i , j ] = true ; isPresent [ i , k ] = true ; } } } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( ! isPresent [ i , j ] ) Console . Write ( a [ i ] [ j ] ) ; } static void Main ( ) { int n = 2 , m = 2 ; string [ ] a = new string [ ] { " zx " , " xz " } ; findDuplciates ( a , n , m ) ; } }
Program to check for ISBN | C # program to check if a given ISBN isvalid or not . ; length must be 10 ; Computing weighted sum of first 9 digits ; Checking last digit . ; If last digit is ' X ' , add 10 to sum , else add its value . ; Return true if weighted sum of digits is divisible by 11. ; Driver code
using System ; class GFG { static bool isValidISBN ( string isbn ) { int n = isbn . Length ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn [ i ] - '0' ; if ( 0 > digit 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = isbn [ 9 ] ; if ( last != ' X ' && ( last < '0' last > '9' ) ) return false ; sum += ( ( last == ' X ' ) ? 10 : ( last - '0' ) ) ; return ( sum % 11 == 0 ) ; } public static void Main ( ) { string isbn = "007462542X " ; if ( isValidISBN ( isbn ) ) Console . WriteLine ( " Valid " ) ; else Console . WriteLine ( " Invalid " ) ; } }
Reverse vowels in a given string | C # program to reverse order of vowels ; utility function to check for vowel ; Function to reverse order of vowels ; Storing the vowels separately ; Placing the vowels in the reverse order in the string ; Driver code
using System ; class GFG { static bool isVowel ( char c ) { return ( c == ' a ' c == ' A ' c == ' e ' c == ' E ' c == ' i ' c == ' I ' c == ' o ' c == ' O ' c == ' u ' c == ' U ' ) ; } static String reverseVowel ( String str1 ) { int j = 0 ; char [ ] str = str1 . ToCharArray ( ) ; String vowel = " " ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( isVowel ( str [ i ] ) ) { j ++ ; vowel += str [ i ] ; } } for ( int i = 0 ; i < str . Length ; i ++ ) { if ( isVowel ( str [ i ] ) ) { str [ i ] = vowel [ -- j ] ; } } return String . Join ( " " , str ) ; } public static void Main ( String [ ] args ) { String str = " hello ▁ world " ; Console . WriteLine ( reverseVowel ( str ) ) ; } }
String containing first letter of every word in a given string with spaces | C # program to find the string which contain the first character of each word of another string . ; Function to find string which has first character of each word . ; Traverse the string . ; If it is space , set v as true . ; Else check if v is true or not . If true , copy character in output string and set v as false . ; Driver code
using System ; class GFG { static String firstLetterWord ( String str ) { String result = " " ; bool v = true ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == ' ▁ ' ) { v = true ; } else if ( str [ i ] != ' ▁ ' && v == true ) { result += ( str [ i ] ) ; v = false ; } } return result ; } public static void Main ( ) { String str = " geeks ▁ for ▁ geeks " ; Console . WriteLine ( firstLetterWord ( str ) ) ; } }
Unique paths covering every non | C # implementation of the approach ; Function for dfs . i , j == > Current cell indexes vis == > To mark visited cells ans == > Result z == > Current count 0 s visited z_count == > Total 0 s present ; Mark the block as visited ; update the count ; If end block reached ; If path covered all the non - obstacle blocks ; Up ; Down ; Left ; Right ; Unmark the block ( unvisited ) ; Function to return the count of the unique paths ; int z_count = 0 ; Total 0 s present ; Count non - obstacle blocks ; Starting position ; Driver code
using System ; class GFG { static int ans = 0 ; static void dfs ( int i , int j , int [ , ] grid , bool [ , ] vis , int z , int z_count ) { int n = grid . GetLength ( 0 ) , m = grid . GetLength ( 1 ) ; vis [ i , j ] = true ; if ( grid [ i , j ] == 0 ) z ++ ; if ( grid [ i , j ] == 2 ) { if ( z == z_count ) ans ++ ; vis [ i , j ] = false ; return ; } if ( i >= 1 && ! vis [ i - 1 , j ] && grid [ i - 1 , j ] != - 1 ) dfs ( i - 1 , j , grid , vis , z , z_count ) ; if ( i < n - 1 && ! vis [ i + 1 , j ] && grid [ i + 1 , j ] != - 1 ) dfs ( i + 1 , j , grid , vis , z , z_count ) ; if ( j >= 1 && ! vis [ i , j - 1 ] && grid [ i , j - 1 ] != - 1 ) dfs ( i , j - 1 , grid , vis , z , z_count ) ; if ( j < m - 1 && ! vis [ i , j + 1 ] && grid [ i , j + 1 ] != - 1 ) dfs ( i , j + 1 , grid , vis , z , z_count ) ; vis [ i , j ] = false ; } static int uniquePaths ( int [ , ] grid ) { int n = grid . GetLength ( 0 ) , m = grid . GetLength ( 1 ) ; bool [ , ] vis = new bool [ n , m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { vis [ i , j ] = false ; } } int x = 0 , y = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { if ( grid [ i , j ] == 0 ) z_count ++ ; else if ( grid [ i , j ] == 1 ) { x = i ; y = j ; } } } dfs ( x , y , grid , vis , 0 , z_count ) ; return ans ; } static void Main ( ) { int [ , ] grid = { { 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 2 , - 1 } } ; Console . WriteLine ( uniquePaths ( grid ) ) ; } }
Count of unordered pairs ( x , y ) of Array which satisfy given equation | C # Program to find the number of unordered pairs ( x , y ) which satisfy the given equation for the array ; Return the number of unordered pairs satisfying the conditions ; ans stores the number of unordered pairs ; Making each value of array to positive ; Sort the array ; For each index calculating the right boundary for the unordered pairs ; Return the final result ; Driver code
using System ; class GFG { static int numPairs ( int [ ] a , int n ) { int ans , i , index ; ans = 0 ; for ( i = 0 ; i < n ; i ++ ) a [ i ] = Math . Abs ( a [ i ] ) ; Array . Sort ( a ) ; for ( i = 0 ; i < n ; i ++ ) { index = 2 ; ans += index - i - 1 ; } return ans ; } public static void Main ( ) { int [ ] a = new int [ ] { 3 , 6 } ; int n = a . Length ; Console . Write ( numPairs ( a , n ) ) ; } }
Area of a Square | Using Side , Diagonal and Perimeter | C # program for the above approach ; Function to find the area of a square ; Use above formula ; Driver Code ; Given Side of square ; Function call
using System ; class GFG { static int areaOfSquare ( int S ) { int area = S * S ; return area ; } public static void Main ( string [ ] args ) { int S = 5 ; Console . Write ( areaOfSquare ( S ) ) ; } }
Maximum points of intersections possible among X circles and Y straight lines | C # program to implement the above approach ; Driver code ; Number of circles ; Number of straight lines ; Function Call
using System ; class GFG { static int maxPointOfIntersection ( int x , int y ) { int k = y * ( y - 1 ) / 2 ; k = k + x * ( 2 * y + x - 1 ) ; return k ; } public static void Main ( String [ ] args ) { int x = 3 ; int y = 4 ; Console . Write ( maxPointOfIntersection ( x , y ) ) ; } }
Icosihenagonal Number | C # program to find nth Icosihenagonal number ; Function to find Icosihenagonal number ; Formula to calculate nth Icosihenagonal number ; Driver Code
using System ; class GFG { static int Icosihenagonal_num ( int n ) { return ( 19 * n * n - 17 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( Icosihenagonal_num ( n ) + " STRNEWLINE " ) ; n = 10 ; Console . Write ( Icosihenagonal_num ( n ) + " STRNEWLINE " ) ; } }
Find the centroid of a non | C # implementation of the approach ; For all vertices ; Calculate value of A using shoelace formula ; Calculating coordinates of centroid of polygon ; Driver code ; Coordinate of the vertices
using System ; class GFG { static double [ ] find_Centroid ( double [ , ] v ) { double [ ] ans = new double [ 2 ] ; int n = v . GetLength ( 0 ) ; double signedArea = 0 ; for ( int i = 0 ; i < n ; i ++ ) { double x0 = v [ i , 0 ] , y0 = v [ i , 1 ] ; double x1 = v [ ( i + 1 ) % n , 0 ] , y1 = v [ ( i + 1 ) % n , 1 ] ; double A = ( x0 * y1 ) - ( x1 * y0 ) ; signedArea += A ; ans [ 0 ] += ( x0 + x1 ) * A ; ans [ 1 ] += ( y0 + y1 ) * A ; } signedArea *= 0.5 ; ans [ 0 ] = ( ans [ 0 ] ) / ( 6 * signedArea ) ; ans [ 1 ] = ( ans [ 1 ] ) / ( 6 * signedArea ) ; return ans ; } public static void Main ( String [ ] args ) { double [ , ] vp = { { 1 , 2 } , { 3 , - 4 } , { 6 , - 7 } } ; double [ ] ans = find_Centroid ( vp ) ; Console . WriteLine ( ans [ 0 ] + " ▁ " + ans [ 1 ] ) ; } }
Program to find the angles of a quadrilateral | C # implementation of the approach ; Driver code ; according to formula derived above ; print all the angles
using System ; class GFG { public static void Main ( ) { int d = 10 ; double a ; a = ( double ) ( 360 - ( 6 * d ) ) / 4 ; Console . WriteLine ( a + " , ▁ " + ( a + d ) + " , ▁ " + ( a + ( 2 * d ) ) + " , ▁ " + ( a + ( 3 * d ) ) ) ; } }
Distance between two parallel Planes in 3 | C # program to find the Distance between two parallel Planes in 3 D . ; Function to find distance ; Driver code
using System ; class GFG { static void distance ( float a1 , float b1 , float c1 , float d1 , float a2 , float b2 , float c2 , float d2 ) { float z1 , d ; if ( a1 / a2 == b1 / b2 && b1 / b2 == c1 / c2 ) { z1 = - d1 / c1 ; d = Math . Abs ( ( c2 * z1 + d2 ) ) / ( float ) ( Math . Sqrt ( a2 * a2 + b2 * b2 + c2 * c2 ) ) ; Console . Write ( " Perpendicular ▁ distance ▁ is ▁ " + d ) ; } else Console . Write ( " Planes ▁ are ▁ not ▁ parallel " ) ; } public static void Main ( ) { float a1 = 1 ; float b1 = 2 ; float c1 = - 1 ; float d1 = 1 ; float a2 = 3 ; float b2 = 6 ; float c2 = - 3 ; float d2 = - 4 ; distance ( a1 , b1 , c1 , d1 ,
Count of ways to form 2 necklace from N beads containing N / 2 beads each | C # program for the above approach ; Function to calculate factorial ; Function to count number of ways to make 2 necklace having exactly N / 2 beads if each bead is considered different ; Number of ways to choose N / 2 beads from N beads ; Number of ways to permute N / 2 beads ; Divide ans by 2 to remove repetitions ; Return ans ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static long numOfNecklace ( int N ) { long ans = factorial ( N ) / ( factorial ( N / 2 ) * factorial ( N / 2 ) ) ; ans = ans * factorial ( N / 2 - 1 ) ; ans = ans * factorial ( N / 2 - 1 ) ; ans /= 2 ; return ans ; } static public void Main ( ) { int N = 4 ; Console . Write ( numOfNecklace ( N ) ) ; } }
Check if a number S can be made divisible by D by repeatedly adding the remainder to S | C # program for the above approach ; Function to check if S is divisible by D while changing S to ( S + S % D ) ; V ( 0 ) = S % D ; Stores the encountered values ; V ( i ) = ( V ( i - 1 ) + V ( i - 1 ) % D ) % D ; Check if the value has already been encountered ; Edge Case ; Otherwise , insert it into the hashmap ; Driver Code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static string isDivisibleByDivisor ( int S , int D ) { S %= D ; List < int > hashMap = new List < int > ( ) ; ; hashMap . Add ( S ) ; for ( int i = 0 ; i <= D ; i ++ ) { S += ( S % D ) ; S %= D ; if ( hashMap . Contains ( S ) ) { if ( S == 0 ) { return " Yes " ; } return " No " ; } else hashMap . Add ( S ) ; } return " Yes " ; } static void Main ( ) { int S = 3 , D = 6 ; Console . Write ( isDivisibleByDivisor ( S , D ) ) ; } }
Minimum number of given moves required to reach ( 1 , 1 ) from ( X , Y ) | C # program for the above approach ; Function to count the number of steps required to convert ( x , y ) to ( 1 , 1 ) ; Store the required result ; Iterate while both x and y are not equal to 0 ; If x is greater than y ; Update count and value of x ; Otherwise ; Update count and value of y ; If both x and y > 1 ; Print the result ; Driver Code ; Given X and Y
using System ; class GFG { public static void minimumSteps ( int x , int y ) { int cnt = 0 ; while ( x != 0 && y != 0 ) { if ( x > y ) { cnt += x / y ; x %= y ; } else { cnt += y / x ; y %= x ; } } cnt -- ; if ( x > 1 y > 1 ) cnt = - 1 ; Console . WriteLine ( cnt ) ; } public static void Main ( ) { int x = 3 , y = 1 ; minimumSteps ( x , y ) ; } }