codeparrot/xlcost-text-to-code huggingface dataset:
Count elements in the given range which have maximum number of divisors | C ++ implementation of above approach ; Function to count the elements with maximum number of divisors ; to store number of divisors ; initialise with zero ; to store the maximum number of divisors ; to store required answer ; Find the first divisible number ; Count number of divisors ; Find number of elements with maximum number of divisors ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaximumDivisors ( int X , int Y ) { int arr [ Y - X + 1 ] ; memset ( arr , 0 , sizeof ( arr ) ) ; int mx = INT_MIN ; int cnt = 0 ; for ( int i = 1 ; i * i <= Y ; i ++ ) { int sq = i * i ; int first_divisible ; if ( ( X / i ) * i >= X ) first_divisible = ( X / i ) * i ; else first_divisible = ( X / i + 1 ) * i ; for ( int j = first_divisible ; j <= Y ; j += i ) { if ( j < sq ) continue ; else if ( j == sq ) arr [ j - X ] ++ ; else arr [ j - X ] += 2 ; } } for ( int i = X ; i <= Y ; i ++ ) { if ( arr [ i - X ] > mx ) { cnt = 1 ; mx = arr [ i - X ] ; } else if ( arr [ i - X ] == mx ) cnt ++ ; } return cnt ; } int main ( ) { int X = 1 , Y = 10 ; cout << MaximumDivisors ( X , Y ) << endl ; return 0 ; }
Find First element in AP which is multiple of given prime |  ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; function to find nearest element in common ; base conditions ; Driver code ; module both A and D ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } int NearestElement ( int A , int D , int P ) { if ( A == 0 ) return 0 ; else if ( D == 0 ) return -1 ; else { int X = power ( D , P - 2 , P ) ; return ( X * ( P - A ) ) % P ; } } int main ( ) { int A = 4 , D = 9 , P = 11 ; A %= P ; D %= P ; cout << NearestElement ( A , D , P ) ; return 0 ; }
Cunningham chain | C ++ program for cunningham chain Function to print the series of first kind ; Function to print Cunningham chain of the first kind ; Iterate till all elements are printed ; check prime or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print ( int p0 ) { int p1 , i = 0 , x , flag , k ; while ( 1 ) { flag = 1 ; x = ( int ) ( pow ( 2 , i ) ) ; p1 = x * p0 + ( x - 1 ) ; for ( k = 2 ; k < p1 ; k ++ ) { if ( p1 % k == 0 ) { flag = 0 ; break ; } } if ( flag == 0 ) break ; printf ( " % d ▁ " , p1 ) ; i ++ ; } } int main ( ) { int p0 = 2 ; print ( p0 ) ; return 0 ; }
Count pairs with Bitwise AND as ODD number | C ++ program to count pairs with AND giving a odd number ; Function to count number of odd pairs ; variable for counting odd pairs ; find all pairs ; find AND operation check odd or even ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair
#include <iostream> NEW_LINE using namespace std ; int findOddPair ( int A [ ] , int N ) { int i , j ; int oddPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] & A [ j ] ) % 2 != 0 ) oddPair ++ ; } } return oddPair ; } int main ( ) { int a [ ] = { 5 , 1 , 3 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findOddPair ( a , n ) << endl ; return 0 ; }
Sudo Placement [ 1.7 ] | Greatest Digital Root | C ++ program to print the digital roots of a number ; Function to return dig - sum ; Function to print the Digital Roots ; store the largest digital roots ; Iterate till sqrt ( n ) ; if i is a factor ; get the digit sum of both factors i and n / i ; if digit sum is greater then previous maximum ; if digit sum is greater then previous maximum ; if digit sum is same as then previous maximum , then check for larger divisor ; if digit sum is same as then previous maximum , then check for larger divisor ; Print the digital roots ; Driver Code ; Function call to print digital roots
#include <bits/stdc++.h> NEW_LINE using namespace std ; int summ ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } void printDigitalRoot ( int n ) { int maxi = 1 ; int dig = 1 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { int d1 = summ ( n / i ) ; int d2 = summ ( i ) ; if ( d1 > maxi ) { dig = n / i ; maxi = d1 ; } if ( d2 > maxi ) { dig = i ; maxi = d2 ; } if ( d1 == maxi ) { if ( dig < ( n / i ) ) { dig = n / i ; maxi = d1 ; } } if ( d2 == maxi ) { if ( dig < i ) { dig = i ; maxi = d2 ; } } } } cout << dig << " ▁ " << maxi << endl ; } int main ( ) { int n = 10 ; printDigitalRoot ( n ) ; return 0 ; }
Sum of all elements up to Nth row in a Pascal triangle | C ++ program to find sum of all elements upto nth row in Pascal triangle . ; Function to find sum of all elements upto nth row . ; Initialize sum with 0 ; Calculate 2 ^ n ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int calculateSum ( int n ) { long long int sum = 0 ; sum = 1 << n ; return ( sum - 1 ) ; } int main ( ) { int n = 10 ; cout << " ▁ Sum ▁ of ▁ all ▁ elements : " << calculateSum ( n ) ; return 0 ; }
Divide two integers without using multiplication , division and mod operator | Set2 | C ++ program for above approach ; Returns the quotient of dividend / divisor . ; Calculate sign of divisor i . e . , sign will be negative only if either one of them is negative otherwise it will be positive ; Remove signs of dividend and divisor ; Zero division Exception . ; Using Formula derived above . ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Divide ( int a , int b ) { long long dividend = ( long long ) a ; long long divisor = ( long long ) b ; long long sign = ( dividend < 0 ) ^ ( divisor < 0 ) ? -1 : 1 ; dividend = abs ( dividend ) ; divisor = abs ( divisor ) ; if ( divisor == 0 ) { cout << " Cannot ▁ Divide ▁ by ▁ 0" << endl ; return ; } if ( dividend == 0 ) { cout << a << " ▁ / ▁ " << b << " ▁ is ▁ equal ▁ to ▁ : ▁ " << 0 << endl ; return ; } if ( divisor == 1 ) { cout << a << " ▁ / ▁ " << b << " ▁ is ▁ equal ▁ to ▁ : ▁ " << sign * dividend << endl ; return ; } cout << a << " ▁ / ▁ " << b << " ▁ is ▁ equal ▁ to ▁ : ▁ " << sign * exp ( log ( dividend ) - log ( divisor ) ) << endl ; } int main ( ) { int a = 10 , b = 5 ; Divide ( a , b ) ; a = 49 , b = -7 ; Divide ( a , b ) ; return 0 ; }
Represent the fraction of two numbers in the string format | C ++ program to calculate fraction of two numbers ; Function to return the required fraction in string format ; If the numerator is zero , answer is 0 ; If any one ( out of numerator and denominator ) is - ve , sign of resultant answer - ve . ; Calculate the absolute part ( before decimal point ) . ; Output string to store the answer ; Append sign ; Append the initial part ; If completely divisible , return answer . ; Initialize Remainder ; Position at which fraction starts repeating if it exists ; If this remainder is already seen , then there exists a repeating fraction . ; Index to insert parentheses ; Calculate quotient , append it to result and calculate next remainder ; If repeating fraction exists , insert parentheses . ; Return result . ; Drivers Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string calculateFraction ( int num , int den ) { if ( num == 0 ) return "0" ; int sign = ( num < 0 ) ^ ( den < 0 ) ? -1 : 1 ; num = abs ( num ) ; den = abs ( den ) ; int initial = num / den ; string res ; if ( sign == -1 ) res += " - " ; res += to_string ( initial ) ; if ( num % den == 0 ) return res ; res += " . " ; int rem = num % den ; map < int , int > mp ; int index ; bool repeating = false ; while ( rem > 0 && ! repeating ) { if ( mp . find ( rem ) != mp . end ( ) ) { index = mp [ rem ] ; repeating = true ; break ; } else mp [ rem ] = res . size ( ) ; rem = rem * 10 ; int temp = rem / den ; res += to_string ( temp ) ; rem = rem % den ; } if ( repeating ) { res += " ) " ; res . insert ( index , " ( " ) ; } return res ; } int main ( ) { int num = 50 , den = 22 ; cout << calculateFraction ( num , den ) << endl ; num = -1 , den = 2 ; cout << calculateFraction ( num , den ) << endl ; return 0 ; }
Check if the n | CPP Program to check if the nth is odd or even in a sequence where each term is sum of previous two term ; Return if the nth term is even or odd . ; If a is even ; If b is even ; If b is odd ; If a is odd ; If b is odd ; If b is eve ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findNature ( int a , int b , int n ) { if ( n == 0 ) return ( a & 1 ) ; if ( n == 1 ) return ( b & 1 ) ; if ( ! ( a & 1 ) ) { if ( ! ( b & 1 ) ) return false ; else return ( n % 3 != 0 ) ; } else { if ( ! ( b & 1 ) ) return ( ( n - 1 ) % 3 != 0 ) ; else return ( ( n + 1 ) % 3 != 0 ) ; } } int main ( ) { int a = 2 , b = 4 ; int n = 3 ; ( findNature ( a , b , n ) ? ( cout << " Odd " << " ▁ " ) : ( cout << " Even " << " ▁ " ) ) ; return 0 ; }
Check if mirror image of a number is same if displayed in seven segment display | C ++ Program to check if mirror image of a number is same if displayed in seven segment display ; Return " Yes " , if the mirror image of number is same as the given number Else return " No " ; Checking if the number contain only 0 , 1 , 8. ; Checking if the number is palindrome or not . ; If corresponding index is not equal . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkEqual ( string S ) { for ( int i = 0 ; i < S . size ( ) ; i ++ ) { if ( S [ i ] != '1' && S [ i ] != '0' && S [ i ] != '8' ) { return " No " ; } } int start = 0 , end = S . size ( ) - 1 ; while ( start < end ) { if ( S [ start ] != S [ end ] ) { return " No " ; } start ++ ; end -- ; } return " Yes " ; } int main ( ) { string S = "101" ; cout << checkEqual ( S ) << endl ; return 0 ; }
Check whether a given number is Polydivisible or Not | CPP program to check whether a number is polydivisible or not ; function to check polydivisible number ; digit extraction of input number ; store the digits in an array ; n contains first i digits ; n should be divisible by i ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check_polydivisible ( int n ) { int N = n ; vector < int > digit ; while ( n > 0 ) { digit . push_back ( n % 10 ) ; n /= 10 ; } reverse ( digit . begin ( ) , digit . end ( ) ) ; bool flag = true ; n = digit [ 0 ] ; for ( int i = 1 ; i < digit . size ( ) ; i ++ ) { n = n * 10 + digit [ i ] ; if ( n % ( i + 1 ) != 0 ) { flag = false ; break ; } } if ( flag ) cout << N << " ▁ is ▁ Polydivisible ▁ number . " ; else cout << N << " ▁ is ▁ Not ▁ Polydivisible ▁ number . " ; } int main ( ) { int n = 345654 ; check_polydivisible ( n ) ; }
Check if given number is a power of d where d is a power of 2 | CPP program to find if a number is power of d where d is power of 2. ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is a multiple of log2 ( d ) then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver program to test above function
#include <stdio.h> NEW_LINE unsigned int Log2n ( unsigned int n ) { return ( n > 1 ) ? 1 + Log2n ( n / 2 ) : 0 ; } bool isPowerOfd ( unsigned int n , unsigned int d ) { int count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % ( Log2n ( d ) ) == 0 ) ; } return false ; } int main ( ) { int n = 64 , d = 8 ; if ( isPowerOfd ( n , d ) ) printf ( " % d ▁ is ▁ a ▁ power ▁ of ▁ % d " , n , d ) ; else printf ( " % d ▁ is ▁ not ▁ a ▁ power ▁ of ▁ % d " , n , d ) ; return 0 ; }
Total nodes traversed in Euler Tour Tree | C ++ program to check the number of nodes in Euler Tour tree . ; Adjacency list representation of tree ; Function to add edges to tree ; Program to check if calculated Value is equal to 2 * size - 1 ; Add out - degree of each node ; Driver code ; Constructing 1 st tree from example ; Out_deg [ node [ i ] ] is equal to adj [ i ] . size ( ) ; clear previous stored tree ; Constructing 2 nd tree from example ; Out_deg [ node [ i ] ] is equal to adj [ i ] . size ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1001 NEW_LINE vector < int > adj [ MAX ] ; void add_edge ( int u , int v ) { adj [ u ] . push_back ( v ) ; } void checkTotalNumberofNodes ( int actualAnswer , int size ) { int calculatedAnswer = size ; for ( int i = 1 ; i <= size ; i ++ ) calculatedAnswer += adj [ i ] . size ( ) ; if ( actualAnswer == calculatedAnswer ) cout << " Calculated ▁ Answer ▁ is ▁ " << calculatedAnswer << " ▁ and ▁ is ▁ Equal ▁ to ▁ Actual ▁ Answer STRNEWLINE " ; else cout << " Calculated ▁ Answer ▁ is ▁ Incorrect STRNEWLINE " ; } int main ( ) { int N = 8 ; add_edge ( 1 , 2 ) ; add_edge ( 1 , 3 ) ; add_edge ( 2 , 4 ) ; add_edge ( 2 , 5 ) ; add_edge ( 3 , 6 ) ; add_edge ( 3 , 7 ) ; add_edge ( 6 , 8 ) ; checkTotalNumberofNodes ( 2 * N - 1 , N ) ; for ( int i = 1 ; i <= N ; i ++ ) adj [ i ] . clear ( ) ; N = 9 ; add_edge ( 1 , 2 ) ; add_edge ( 1 , 3 ) ; add_edge ( 2 , 4 ) ; add_edge ( 2 , 5 ) ; add_edge ( 2 , 6 ) ; add_edge ( 3 , 9 ) ; add_edge ( 5 , 7 ) ; add_edge ( 5 , 8 ) ; checkTotalNumberofNodes ( 2 * N - 1 , N ) ; return 0 ; }
Octahedral Number | C ++ program to find nth octahedral number ; Function to find octahedral number ; Formula to calculate nth octahedral number ; Drivers code ; print result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int octahedral_num ( int n ) { return n * ( 2 * n * n + 1 ) / 3 ; } int main ( ) { int n = 5 ; cout << n << " th ▁ Octahedral ▁ number : ▁ " ; cout << octahedral_num ( n ) ; return 0 ; }
Centered tetrahedral number | C ++ Program to find nth Centered tetrahedral number ; Function to find centered Centered tetrahedral number ; Formula to calculate nth Centered tetrahedral number and return it into main function . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int centeredTetrahedralNumber ( int n ) { return ( 2 * n + 1 ) * ( n * n + n + 3 ) / 3 ; } int main ( ) { int n = 6 ; cout << centeredTetrahedralNumber ( n ) ; return 0 ; }
Swapping four variables without temporary variable | CPP program to swap 4 variables without using temporary variable . ; swapping a and b variables ; swapping b and c variables ; swapping c and d variables ; Driver code ; initialising variables ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( int a , int b , int c , int d ) { a = a + b ; b = a - b ; a = a - b ; b = b + c ; c = b - c ; b = b - c ; c = c + d ; d = c - d ; c = c - d ; cout << " values ▁ after ▁ swapping ▁ are ▁ : ▁ " << endl ; cout << " a ▁ = ▁ " << a << endl ; cout << " b ▁ = ▁ " << b << endl ; cout << " c ▁ = ▁ " << c << endl ; cout << " d ▁ = ▁ " << d << endl ; } int main ( ) { int a = 1 ; int b = 2 ; int c = 3 ; int d = 4 ; cout << " Values ▁ before ▁ swapping ▁ are ▁ : " << endl ; cout << " a ▁ = ▁ " << a << endl ; cout << " b ▁ = ▁ " << b << endl ; cout << " c ▁ = ▁ " << c << endl ; cout << " d ▁ = ▁ " << d << endl << endl ; swap ( a , b , c , d ) ; return 0 ; }
Sum of first n natural numbers | CPP program to find sum series 1 , 3 , 6 , 10 , 15 , 21. . . and then find its sum ; Function to find the sum of series ; Driver code
#include <iostream> NEW_LINE using namespace std ; int seriesSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += i * ( i + 1 ) / 2 ; return sum ; } int main ( ) { int n = 4 ; cout << seriesSum ( n ) ; return 0 ; }
Centrosymmetric Matrix | CPP Program to check whether given matrix is centrosymmetric or not . ; Finding the middle row of the matrix ; for each row upto middle row . ; If each element and its corresponding element is not equal then return false . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE bool checkCentrosymmetricted ( int n , int m [ N ] [ N ] ) { int mid_row ; if ( n & 1 ) mid_row = n / 2 + 1 ; else mid_row = n / 2 ; for ( int i = 0 ; i < mid_row ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( m [ i ] [ j ] != m [ n - i - 1 ] [ n - j - 1 ] ) return false ; } } return true ; } int main ( ) { int n = 3 ; int m [ N ] [ N ] = { { 1 , 3 , 5 } , { 6 , 8 , 6 } , { 5 , 3 , 1 } } ; ( checkCentrosymmetricted ( n , m ) ? ( cout << " Yes " ) : ( cout << " No " ) ) ; return 0 ; }
Centered triangular number | C ++ Program to find the nth Centered Triangular number ; function for Centered Triangular number ; formula for find Centered Triangular number nth term ; Driver Code ; For 3 rd Centered Triangular number ; For 12 th Centered Triangular number
#include <iostream> NEW_LINE using namespace std ; int Centered_Triangular_num ( int n ) { return ( 3 * n * n + 3 * n + 2 ) / 2 ; } int main ( ) { int n = 3 ; cout << Centered_Triangular_num ( n ) << endl ; n = 12 ; cout << Centered_Triangular_num ( n ) << endl ; return 0 ; }
Array with GCD of any of its subset belongs to the given array | C ++ implementation to generate the required array ; Function to return gcd of a and b ; Function to find gcd of array of numbers ; Function to generate the array with required constraints . ; computing GCD of the given set ; Solution exists if GCD of array is equal to the minimum element of the array ; Printing the built array ; Driver function ; Taking in the input and initializing the set STL set in cpp has a property that it maintains the elements in sorted order , thus we do not need to sort them externally ; Calling the computing function .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int findGCD ( vector < int > arr , int n ) { int result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) result = gcd ( arr [ i ] , result ) ; return result ; } void compute ( vector < int > arr , int n ) { vector < int > answer ; int GCD_of_array = findGCD ( arr , n ) ; if ( GCD_of_array == arr [ 0 ] ) { answer . push_back ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { answer . push_back ( arr [ 0 ] ) ; answer . push_back ( arr [ i ] ) ; } for ( int i = 0 ; i < answer . size ( ) ; i ++ ) cout << answer [ i ] << " ▁ " ; } else cout << " No ▁ array ▁ can ▁ be ▁ build " ; } int main ( ) { int n = 3 ; int input [ ] = { 2 , 5 , 6 , 7 , 11 } ; set < int > GCD ( input , input + n ) ; vector < int > arr ; set < int > :: iterator it ; for ( it = GCD . begin ( ) ; it != GCD . end ( ) ; ++ it ) arr . push_back ( * it ) ; compute ( arr , n ) ; return 0 ; }
Combinatorics on ordered trees | C ++ code to find the number of ordered trees with given number of edges and leaves ; Function returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate the number of trees with exactly k leaves . ; Function to calculate total number of nodes of degree d in these trees . ; Function to calculate the number of trees in which the root has degree r . ; Driver program to test above functions ; Number of trees having 3 edges and exactly 2 leaves ; Number of nodes of degree 3 in a tree having 4 edges ; Number of trees having 3 edges where root has degree 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { int C [ n + 1 ] [ k + 1 ] = { 0 } ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } int k_Leaves ( int n , int k ) { int ans = ( binomialCoeff ( n , k ) * binomialCoeff ( n , k - 1 ) ) / n ; cout << " Number ▁ of ▁ trees ▁ having ▁ 4 ▁ edges " << " ▁ and ▁ exactly ▁ 2 ▁ leaves ▁ : ▁ " << ans << endl ; return 0 ; } int numberOfNodes ( int n , int d ) { int ans = binomialCoeff ( 2 * n - 1 - d , n - 1 ) ; cout << " Number ▁ of ▁ nodes ▁ of ▁ degree ▁ 1 ▁ in " << " ▁ a ▁ tree ▁ having ▁ 4 ▁ edges ▁ : ▁ " << ans << endl ; return 0 ; } int rootDegreeR ( int n , int r ) { int ans = r * binomialCoeff ( 2 * n - 1 - r , n - 1 ) ; ans = ans / n ; cout << " Number ▁ of ▁ trees ▁ having ▁ 4 ▁ edges " << " ▁ where ▁ root ▁ has ▁ degree ▁ 2 ▁ : ▁ " << ans << endl ; return 0 ; } int main ( ) { k_Leaves ( 3 , 2 ) ; numberOfNodes ( 3 , 1 ) ; rootDegreeR ( 3 , 2 ) ; return 0 ; }
Repeated Unit Divisibility | CPP program to find least value of k for which R ( k ) is divisible by n ; To find least value of k ; To check n is coprime or not ; to store R ( k ) mod n and 10 ^ k mod n value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int repUnitValue ( int n ) { if ( n % 2 == 0 n % 5 == 0 ) return 0 ; int rem = 1 ; int power = 1 ; int k = 1 ; while ( rem % n != 0 ) { k ++ ; power = power * 10 % n ; rem = ( rem + power ) % n ; } return k ; } int main ( ) { int n = 13 ; cout << repUnitValue ( n ) ; return 0 ; }
First N natural can be divided into two sets with given difference and co | CPP code to determine whether numbers 1 to N can be divided into two sets such that absolute difference between sum of these two sets is M and these two sum are co - prime ; function that returns boolean value on the basis of whether it is possible to divide 1 to N numbers into two sets that satisfy given conditions . ; initializing total sum of 1 to n numbers ; since ( 1 ) total_sum = sum_s1 + sum_s2 and ( 2 ) m = sum_s1 - sum_s2 assuming sum_s1 > sum_s2 . solving these 2 equations to get sum_s1 and sum_s2 ; total_sum = sum_s1 + sum_s2 and therefore ; if total sum is less than the absolute difference then there is no way we can split n numbers into two sets so return false ; check if these two sums are integers and they add up to total sum and also if their absolute difference is m . ; Now if two sum are co - prime then return true , else return false . ; if two sums don 't add up to total  sum or if their absolute difference  is not m, then there is no way to  split n numbers, hence return false ; Driver code ; function call to determine answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSplittable ( int n , int m ) { int total_sum = ( n * ( n + 1 ) ) / 2 ; int sum_s1 = ( total_sum + m ) / 2 ; int sum_s2 = total_sum - sum_s1 ; if ( total_sum < m ) return false ; if ( sum_s1 + sum_s2 == total_sum && sum_s1 - sum_s2 == m ) return ( __gcd ( sum_s1 , sum_s2 ) == 1 ) ; return false ; } int main ( ) { int n = 5 , m = 7 ; if ( isSplittable ( n , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Making zero array by decrementing pairs of adjacent | CPP implementation of the above approach ; converting array element into number ; Check if divisible by 11 ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossibleToZero ( int a [ ] , int n ) { int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + a [ i ] ; return ( num % 11 == 0 ) ; } int main ( ) { int arr [ ] = { 0 , 1 , 1 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isPossibleToZero ( arr , n ) ) cout << " YES " ; else cout << " NO " ; }
Blum Integer | CPP program to check if a number is a Blum integer ; Function to cheek if number is Blum Integer ; to store prime numbers from 2 to n ; If prime [ i ] is not changed , then it is a prime ; Update all multiples of p ; to check if the given odd integer is Blum Integer or not ; checking the factors are of 4 t + 3 form or not ; driver code ; give odd integer greater than 20
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isBlumInteger ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] ) { if ( ( n % i == 0 ) && ( ( i - 3 ) % 4 ) == 0 ) { int q = n / i ; return ( q != i && prime [ q ] && ( q - 3 ) % 4 == 0 ) ; } } } return false ; } int main ( ) { int n = 249 ; if ( isBlumInteger ( n ) ) cout << " Yes " ; else cout << " No " ; }