codeparrot/xlcost-text-to-code huggingface dataset:
Convert a binary number to hexadecimal number | C ++ implementation to convert a binary number to hexadecimal number ; Function to create map between binary number and its equivalent hexadecimal ; function to find hexadecimal equivalent of binary ; length of string before ' . ' ; add min 0 's in the beginning to make  left substring length divisible by 4 ; if decimal point exists ; length of string after ' . ' ; add min 0 's in the end to make right  substring length divisible by 4 ; create map between binary and its equivalent hex code ; one by one extract from left , substring of size 4 and add its hex code ; if ' . ' is encountered add it to result ; required hexadecimal number ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createMap ( unordered_map < string , char > * um ) { ( * um ) [ "0000" ] = '0' ; ( * um ) [ "0001" ] = '1' ; ( * um ) [ "0010" ] = '2' ; ( * um ) [ "0011" ] = '3' ; ( * um ) [ "0100" ] = '4' ; ( * um ) [ "0101" ] = '5' ; ( * um ) [ "0110" ] = '6' ; ( * um ) [ "0111" ] = '7' ; ( * um ) [ "1000" ] = '8' ; ( * um ) [ "1001" ] = '9' ; ( * um ) [ "1010" ] = ' A ' ; ( * um ) [ "1011" ] = ' B ' ; ( * um ) [ "1100" ] = ' C ' ; ( * um ) [ "1101" ] = ' D ' ; ( * um ) [ "1110" ] = ' E ' ; ( * um ) [ "1111" ] = ' F ' ; } string convertBinToHex ( string bin ) { int l = bin . size ( ) ; int t = bin . find_first_of ( ' . ' ) ; int len_left = t != -1 ? t : l ; for ( int i = 1 ; i <= ( 4 - len_left % 4 ) % 4 ; i ++ ) bin = '0' + bin ; if ( t != -1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 4 - len_right % 4 ) % 4 ; i ++ ) bin = bin + '0' ; } unordered_map < string , char > bin_hex_map ; createMap ( & bin_hex_map ) ; int i = 0 ; string hex = " " ; while ( 1 ) { hex += bin_hex_map [ bin . substr ( i , 4 ) ] ; i += 4 ; if ( i == bin . size ( ) ) break ; if ( bin . at ( i ) == ' . ' ) { hex += ' . ' ; i ++ ; } } return hex ; } int main ( ) { string bin = "1111001010010100001.010110110011011" ; cout << " Hexadecimal ▁ number ▁ = ▁ " << convertBinToHex ( bin ) ; return 0 ; }
Count of m digit integers that are divisible by an integer n | C ++ program to count m digit numbers having n as divisor . ; Returns count of m digit numbers having n as divisor ; generating largest number of m digit ; generating largest number of m - 1 digit ; returning number of dividend ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( int m , int n ) { int num1 = 0 ; for ( int i = 0 ; i < m ; i ++ ) num1 = ( num1 * 10 ) + 9 ; int num2 = 0 ; for ( int i = 0 ; i < ( m - 1 ) ; i ++ ) num2 = ( num2 * 10 ) + 9 ; return ( ( num1 / n ) - ( num2 / n ) ) ; } int main ( ) { int m = 2 , n = 6 ; printf ( " % d STRNEWLINE " , findCount ( m , n ) ) ; return 0 ; }
Find the n | Simple C ++ program to find n - th number made of even digits only ; function to calculate nth number made of even digits only ; variable to note how many such numbers have been found till now ; bool variable to check if 1 , 3 , 5 , 7 , 9 is there or not ; checking each digit of the number ; If 1 , 3 , 5 , 7 , 9 is found temp is changed to false ; temp is true it means that it does not have 1 , 3 , 5 , 7 , 9 ; If nth such number is found return it ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNthEvenDigitNumber ( int n ) { int count = 0 ; for ( int i = 0 ; ; i ++ ) { int curr = i ; bool isCurrEvenDigit = true ; while ( curr != 0 ) { if ( curr % 10 == 1 curr % 10 == 3 curr % 10 == 5 curr % 10 == 7 curr % 10 == 9 ) isCurrEvenDigit = false ; curr = curr / 10 ; } if ( isCurrEvenDigit == true ) count ++ ; if ( count == n ) return i ; } } int main ( ) { cout << findNthEvenDigitNumber ( 2 ) << endl ; cout << findNthEvenDigitNumber ( 10 ) << endl ; return 0 ; }
Find the n | Efficient C ++ program to find n - th number made of even digits only ; function to find nth number made of even digits only ; If n = 1 return 0 ; vector to store the digits when converted into base 5 ; Reduce n to n - 1 to exclude 0 ; Reduce n to base 5 number and store digits ; pushing the digits into vector ; variable to represent the number after converting it to base 5. Since the digits are be in reverse order , we traverse vector from back ; return 2 * result ( to convert digits 0 , 1 , 2 , 3 , 4 to 0 , 2 , 4 , 6 , 8. ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNthEvenDigitNumber ( int n ) { if ( n == 1 ) return 0 ; vector < int > v ; n = n - 1 ; while ( n > 0 ) { v . push_back ( n % 5 ) ; n = n / 5 ; } int result = 0 ; for ( int i = v . size ( ) - 1 ; i >= 0 ; i -- ) { result = result * 10 ; result = result + v [ i ] ; } return 2 * result ; } int main ( ) { cout << findNthEvenDigitNumber ( 2 ) << endl ; cout << findNthEvenDigitNumber ( 10 ) << endl ; return 0 ; }
Check if a large number is divisible by 25 or not | C ++ program to find if a number is divisible by 25 or not ; Function to find that number divisible by 25 or not . ; If length of string is single digit then it 's not divisible by 25 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisibleBy25 ( string str ) { int n = str . length ( ) ; if ( n == 1 ) return false ; return ( ( str [ n - 1 ] - '0' == 0 && str [ n - 2 ] - '0' == 0 ) || ( ( str [ n - 2 ] - '0' ) * 10 + ( str [ n - 1 ] - '0' ) ) % 25 == 0 ) ; } int main ( ) { string str = "76955" ; isDivisibleBy25 ( str ) ? cout << " Yes " : cout << " No ▁ " ; return 0 ; }
Check a large number is divisible by 16 or not | C ++ program to find if a number is divisible by 16 or not ; Function to find that number divisible by 16 or not ; Empty string ; If there is double digit ; If there is triple digit ; If number formed by last four digits is divisible by 16. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string str ) { int n = str . length ( ) ; if ( n == 0 && n == 1 ) return false ; if ( n == 2 ) return ( ( ( str [ n - 2 ] - '0' ) * 10 + ( str [ n - 1 ] - '0' ) ) % 16 == 0 ) ; if ( n == 3 ) return ( ( ( str [ n - 3 ] - '0' ) * 100 + ( str [ n - 2 ] - '0' ) * 10 + ( str [ n - 1 ] - '0' ) ) % 16 == 0 ) ; int last = str [ n - 1 ] - '0' ; int second_last = str [ n - 2 ] - '0' ; int third_last = str [ n - 3 ] - '0' ; int fourth_last = str [ n - 4 ] - '0' ; return ( ( fourth_last * 1000 + third_last * 100 + second_last * 10 + last ) % 16 == 0 ) ; } int main ( ) { string str = "769528" ; check ( str ) ? cout << " Yes " : cout << " No ▁ " ; return 0 ; }
Find Index of given fibonacci number in constant time | A simple C ++ program to find index of given Fibonacci number . ; if Fibonacci number is less than 2 , its index will be same as number ; iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE int findIndex ( int n ) { if ( n <= 1 ) return n ; int a = 0 , b = 1 , c = 1 ; int res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } int main ( ) { int result = findIndex ( 21 ) ; printf ( " % d STRNEWLINE " , result ) ; }
Date after adding given number of days to the given date | C ++ program to find date after adding given number of days . ; Return if year is leap year or not . ; Given a date , returns number of days elapsed from the beginning of the current year ( 1 stjan ) . ; Given a year and days elapsed in it , finds date by storing results in d and m . ; Add x days to the given date . ; y2 is going to store result year and offset2 is going to store offset days in result year . ; x may store thousands of days . We find correct year and offset in the year . ; Find values of day and month from offset of result year . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isLeap ( int y ) { if ( y % 100 != 0 && y % 4 == 0 y % 400 == 0 ) return true ; return false ; } int offsetDays ( int d , int m , int y ) { int offset = d ; switch ( m - 1 ) { case 11 : offset += 30 ; case 10 : offset += 31 ; case 9 : offset += 30 ; case 8 : offset += 31 ; case 7 : offset += 31 ; case 6 : offset += 30 ; case 5 : offset += 31 ; case 4 : offset += 30 ; case 3 : offset += 31 ; case 2 : offset += 28 ; case 1 : offset += 31 ; } if ( isLeap ( y ) && m > 2 ) offset += 1 ; return offset ; } void revoffsetDays ( int offset , int y , int * d , int * m ) { int month [ 13 ] = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; if ( isLeap ( y ) ) month [ 2 ] = 29 ; int i ; for ( i = 1 ; i <= 12 ; i ++ ) { if ( offset <= month [ i ] ) break ; offset = offset - month [ i ] ; } * d = offset ; * m = i ; } void addDays ( int d1 , int m1 , int y1 , int x ) { int offset1 = offsetDays ( d1 , m1 , y1 ) ; int remDays = isLeap ( y1 ) ? ( 366 - offset1 ) : ( 365 - offset1 ) ; int y2 , offset2 ; if ( x <= remDays ) { y2 = y1 ; offset2 = offset1 + x ; } else { x -= remDays ; y2 = y1 + 1 ; int y2days = isLeap ( y2 ) ? 366 : 365 ; while ( x >= y2days ) { x -= y2days ; y2 ++ ; y2days = isLeap ( y2 ) ? 366 : 365 ; } offset2 = x ; } int m2 , d2 ; revoffsetDays ( offset2 , y2 , & d2 , & m2 ) ; cout << " d2 ▁ = ▁ " << d2 << " , ▁ m2 ▁ = ▁ " << m2 << " , ▁ y2 ▁ = ▁ " << y2 ; } int main ( ) { int d = 14 , m = 3 , y = 2015 ; int x = 366 ; addDays ( d , m , y , x ) ; return 0 ; }
Determine whether a given number is a Hyperperfect Number | C ++ 4.3 . 2 program to check whether a given number is k - hyperperfect ; function to find the sum of all proper divisors ( excluding 1 and N ) ; Iterate only until sqrt N as we are going to generate pairs to produce divisors ; As divisors occur in pairs , we can take the values i and N / i as long as i divides N ; Function to check whether the given number is prime ; base and corner cases ; Since integers can be represented as some 6 * k + y where y >= 0 , we can eliminate all integers that can be expressed in this form ; start from 5 as this is the next prime number ; Returns true if N is a K - Hyperperfect number Else returns false . ; Condition from the definition of hyperperfect ; Driver function to test for hyperperfect numbers ; First two statements test against the condition N = 1 + K * ( sum ( proper divisors ) )
#include <bits/stdc++.h> NEW_LINE using namespace std ; int divisorSum ( int N , int K ) { int sum = 0 ; for ( int i = 2 ; i <= ceil ( sqrt ( N ) ) ; i ++ ) if ( N % i == 0 ) sum += ( i + N / i ) ; return sum ; } bool isPrime ( int n ) { if ( n == 1 n == 0 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } bool isHyperPerfect ( int N , int K ) { int sum = divisorSum ( N , K ) ; if ( ( 1 + K * ( sum ) ) == N ) return true ; else return false ; } int main ( ) { int N1 = 1570153 , K1 = 12 ; int N2 = 321 , K2 = 3 ; if ( isHyperPerfect ( N1 , K1 ) ) cout << N1 << " ▁ is ▁ " << K1 << " - HyperPerfect " << " STRNEWLINE " ; else cout << N1 << " ▁ is ▁ not ▁ " << K1 << " - HyperPerfect " << " STRNEWLINE " ; if ( isHyperPerfect ( N2 , K2 ) ) cout << N2 << " ▁ is ▁ " << K2 << " - HyperPerfect " << " STRNEWLINE " ; else cout << N2 << " ▁ is ▁ not ▁ " << K2 << " - HyperPerfect " << " STRNEWLINE " ; return 0 ; }
Generate k digit numbers with digits in strictly increasing order | C ++ program to generate well ordered numbers with k digits . ; number -- > Current value of number . x -- > Current digit to be considered k -- > Remaining number of digits ; Try all possible greater digits ; Generates all well ordered numbers of length k . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printWellOrdered ( int number , int x , int k ) { if ( k == 0 ) { cout << number << " ▁ " ; return ; } for ( int i = ( x + 1 ) ; i < 10 ; i ++ ) printWellOrdered ( number * 10 + i , i , k - 1 ) ; } void generateWellOrdered ( int k ) { printWellOrdered ( 0 , 0 , k ) ; } int main ( ) { int k = 3 ; generateWellOrdered ( k ) ; return 0 ; }
Multiply large integers under large modulo | C ++ program of finding modulo multiplication ; Returns ( a * b ) % mod ; Update a if it is more than or equal to mod ; If b is odd , add a with result ; Here we assume that doing 2 * a doesn 't cause overflow ; b >>= 1 ; b = b / 2 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long moduloMultiplication ( long long a , long long b , long long mod ) { a %= mod ; while ( b ) { if ( b & 1 ) res = ( res + a ) % mod ; a = ( 2 * a ) % mod ; } return res ; } int main ( ) { long long a = 426 ; long long b = 964 ; long long m = 235 ; cout << moduloMultiplication ( a , b , m ) ; return 0 ; }
Number of occurrences of 2 as a digit in numbers from 0 to n | Write C ++ code here ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOf2sinRange ( int n ) { string s = " " ; for ( int i = 0 ; i < n + 1 ; i ++ ) s += to_string ( i ) ; int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s [ i ] == '2' ) { count ++ ; } } return count ; } int main ( ) { int n = 30 ; cout << numberOf2sinRange ( n ) ; return 0 ; }
Number of occurrences of 2 as a digit in numbers from 0 to n | C ++ program to count 2 s from 0 to n ; Counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; Counts the number of '2' digits between 0 and n ; Convert integer to String to find its length ; Traverse every digit and count for every digit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count2sinRangeAtDigit ( int number , int d ) { int powerOf10 = ( int ) pow ( 10 , d ) ; int nextPowerOf10 = powerOf10 * 10 ; int right = number % powerOf10 ; int roundDown = number - number % nextPowerOf10 ; int roundup = roundDown + nextPowerOf10 ; int digit = ( number / powerOf10 ) % 10 ; if ( digit < 2 ) return roundDown / 10 ; if ( digit == 2 ) return roundDown / 10 + right + 1 ; return roundup / 10 ; } int numberOf2sinRange ( int number ) { stringstream convert ; convert << number ; string s = convert . str ( ) ; int len = s . length ( ) ; int count = 0 ; for ( int digit = 0 ; digit < len ; digit ++ ) count += count2sinRangeAtDigit ( number , digit ) ; return count ; } int main ( ) { cout << numberOf2sinRange ( 22 ) << endl ; cout << numberOf2sinRange ( 100 ) ; return 0 ; }
Modulo 10 ^ 9 + 7 ( 1000000007 ) |  ; f = f * i ; WRONG APPROACH as f may exceed ( 2 ^ 64 - 1 )
unsigned long long factorial ( int n ) { const unsigned int M = 1000000007 ; unsigned long long f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) return f % M ; }
Modulo 10 ^ 9 + 7 ( 1000000007 ) |  ; f = ( f * i ) % M ; Now f never can exceed 10 ^ 9 + 7
unsigned long long factorial ( int n ) { const unsigned int M = 1000000007 ; unsigned long long f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) return f ; }
Modulo 10 ^ 9 + 7 ( 1000000007 ) |
int mod ( int a , int m ) { return ( a % m + m ) % m ; }
Program to find Star number | C ++ program to find star number ; Returns n - th star number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findStarNum ( int n ) { return ( 6 * n * ( n - 1 ) + 1 ) ; } int main ( ) { int n = 3 ; cout << findStarNum ( n ) ; return 0 ; }
Check if a large number is divisible by 5 or not | C ++ program to find if a number is divisible by 5 or not ; Function to find that number divisible by 5 or not . The function assumes that string length is at least one . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isDivisibleBy5 ( string str ) { int n = str . length ( ) ; return ( ( ( str [ n - 1 ] - '0' ) == 0 ) || ( ( str [ n - 1 ] - '0' ) == 5 ) ) ; } int main ( ) { string str = "76955" ; isDivisibleBy5 ( str ) ? cout << " Yes " : cout << " No ▁ " ; return 0 ; }
Tidy Number ( Digits in non | C ++ program to check if a number is Tidy or not . ; Returns true if num is Tidy ; To store previous digit ( Assigning initial value which is more than any digit ) ; Traverse all digits from right to left and check if any digit is smaller than previous . ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isTidy ( int num ) { int prev = 10 ; while ( num ) { int rem = num % 10 ; num /= 10 ; if ( rem > prev ) return false ; prev = rem ; } return true ; } int main ( ) { int num = 1556 ; isTidy ( num ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Nth Square free number | Program to find the nth square free number ; Function to find nth square free number ; To maintain count of square free number ; Loop for square free numbers ; Checking whether square of a number is divisible by any number which is a perfect square ; If number is square free ; If the cnt becomes n , return the number ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int squareFree ( int n ) { int cnt = 0 ; for ( int i = 1 ; ; i ++ ) { bool isSqFree = true ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % ( j * j ) == 0 ) { isSqFree = false ; break ; } } if ( isSqFree == true ) { cnt ++ ; if ( cnt == n ) return i ; } } return 0 ; } int main ( ) { int n = 10 ; cout << squareFree ( n ) << endl ; return 0 ; }
Nth Square free number | Program to find the nth square free number ; Maximum prime number to be considered for square divisibility ; Maximum value of result . We do binary search from 1 to MAX_RES ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store all prime numbers in a [ ] ; Function to count integers upto k which are having perfect squares as factors . i is index of next prime number whose square needs to be checked . curr is current number whos square to be checked . ; variable to store square of prime ; If value of greatest integer becomes zero ; Counting integers with squares as factor ; Inclusion ( Recur for next prime number ) ; Exclusion ( Recur for next prime number ) ; Final count ; Function to return nth square free number ; Computing primes and storing it in an array a [ ] ; Applying binary search ; ' c ' contains Number of square free numbers less than or equal to ' mid ' ; If c < n , then search right side of mid else search left side of mid ; nth square free number ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_PRIME = 100000 ; const int MAX_RES = 2000000000l ; void SieveOfEratosthenes ( vector < long long > & a ) { bool prime [ MAX_PRIME + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( long long p = 2 ; p * p <= MAX_PRIME ; p ++ ) { if ( prime [ p ] == true ) { for ( long long i = p * 2 ; i <= MAX_PRIME ; i += p ) prime [ i ] = false ; } } for ( long long p = 2 ; p <= MAX_PRIME ; p ++ ) if ( prime [ p ] ) a . push_back ( p ) ; } long long countSquares ( long long i , long long cur , long long k , vector < long long > & a ) { long long square = a [ i ] * a [ i ] ; long long newCur = square * cur ; if ( newCur > k ) return 0 ; long long cnt = k / ( newCur ) ; cnt += countSquares ( i + 1 , cur , k , a ) ; cnt -= countSquares ( i + 1 , newCur , k , a ) ; return cnt ; } long long squareFree ( long long n ) { vector < long long > a ; SieveOfEratosthenes ( a ) ; long long low = 1 ; long long high = MAX_RES ; while ( low < high ) { long long mid = low + ( high - low ) / 2 ; long long c = mid - countSquares ( 0 , 1 , mid , a ) ; if ( c < n ) low = mid + 1 ; else high = mid ; } return low ; } int main ( ) { int n = 10 ; cout << squareFree ( n ) << endl ; return 0 ; }
Find if n can be written as product of k numbers | C ++ program to find if it is possible to write a number n as product of exactly k positive numbers greater than 1. ; Prints k factors of n if n can be written as multiple of k numbers . Else prints - 1. ; A vector to store all prime factors of n ; Insert all 2 's in vector ; n must be odd at this point So we skip one element ( i = i + 2 ) ; This is to handle when n > 2 and n is prime ; If size ( P ) < k , k factors are not possible ; printing first k - 1 factors ; calculating and printing product of rest of numbers ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kFactors ( int n , int k ) { vector < int > P ; while ( n % 2 == 0 ) { P . push_back ( 2 ) ; n /= 2 ; } for ( int i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; P . push_back ( i ) ; } } if ( n > 2 ) P . push_back ( n ) ; if ( P . size ( ) < k ) { cout << " - 1" << endl ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) cout << P [ i ] << " , ▁ " ; int product = 1 ; for ( int i = k - 1 ; i < P . size ( ) ; i ++ ) product = product * P [ i ] ; cout << product << endl ; } int main ( ) { int n = 54 , k = 3 ; kFactors ( n , k ) ; return 0 ; }
Largest number smaller than or equal to n and digits in non | C ++ program for brute force approach to find largest number having digits in non - decreasing order . ; Returns the required number ; loop to recursively check the numbers less than or equal to given number ; Keep traversing digits from right to left . For every digit check if it is smaller than prev_dig ; We found the required number ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long nondecdigits ( long long n ) { long long int x = 0 ; for ( x = n ; x >= 1 ; x -- ) { int no = x ; int prev_dig = 11 ; bool flag = true ; while ( no != 0 ) { if ( prev_dig < no % 10 ) { flag = false ; break ; } prev_dig = no % 10 ; no /= 10 ; } if ( flag == true ) break ; } return x ; } int main ( ) { long long n = 200 ; cout << nondecdigits ( n ) ; return 0 ; }
Largest number smaller than or equal to n and digits in non | C ++ program for efficient approach to find largest number having digits in non - decreasing order . ; Prints the largest number smaller than s and digits in non - decreasing order . ; array to store digits of number ; conversion of characters of string int number ; variable holds the value of index after which all digits are set 9 ; Checking the condition if the digit is less than its left digit ; If first digit is 0 no need to print it ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nondecdigits ( string s ) { long long m = s . size ( ) ; long long a [ m ] ; for ( long long i = 0 ; i < m ; i ++ ) a [ i ] = s [ i ] - '0' ; long long level = m - 1 ; for ( long long i = m - 1 ; i > 0 ; i -- ) { if ( a [ i ] < a [ i - 1 ] ) { a [ i - 1 ] -- ; level = i - 1 ; } } if ( a [ 0 ] != 0 ) { for ( long long i = 0 ; i <= level ; i ++ ) cout << a [ i ] ; for ( long long i = level + 1 ; i < m ; i ++ ) cout << "9" ; } else { for ( long long i = 1 ; i < level ; i ++ ) cout << a [ i ] ; for ( long long i = level + 1 ; i < m ; i ++ ) cout << "9" ; } } int main ( ) { string n = "200" ; nondecdigits ( n ) ; return 0 ; }
Count Divisors of n in O ( n ^ 1 / 3 ) | C implementation of Naive method to count all divisors ; function to count the divisors ; If divisors are equal , count only one ; else Otherwise count both ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDivisors ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) cnt ++ ; cnt = cnt + 2 ; } } return cnt ; } int main ( ) { printf ( " Total ▁ distinct ▁ divisors ▁ of ▁ 100 ▁ are ▁ : ▁ % d " , countDivisors ( 100 ) ) ; return 0 ; }
Check if the door is open or closed | C ++ implementation of doors open or closed ; Function to check whether ' n ' has even number of factors or not ; if ' n ' is a perfect square it has odd number of factors ; else ' n ' has even number of factors ; Function to find and print status of each door ; If even number of factors final status is closed ; else odd number of factors final status is open ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool hasEvenNumberOfFactors ( int n ) { int root_n = sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } void printStatusOfDoors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) cout << " closed " << " ▁ " ; else cout << " open " << " ▁ " ; } } int main ( ) { int n = 5 ; printStatusOfDoors ( n ) ; return 0 ; }