codeparrot/xlcost-text-to-code huggingface dataset:
Find M | C ++ program to Find m - th number whose sum of digits of a number until sum becomes single digit is N ; Function to find the M - th number whosesum till one digit is N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int n , int m ) { int num = ( m - 1 ) * 9 + n ; return num ; } int main ( ) { int n = 2 , m = 5 ; cout << findNumber ( n , m ) ; return 0 ; }
Check if a number can be represented as a sum of 2 triangular numbers | C ++ implementation of the above approach ; Function to check if it is possible or not ; Store all triangular numbers up to N in a Set ; Check if a pair exists ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkTriangularSumRepresentation ( int n ) { unordered_set < int > tri ; int i = 1 ; while ( 1 ) { int x = i * ( i + 1 ) / 2 ; if ( x >= n ) break ; tri . insert ( x ) ; i ++ ; } for ( auto tm : tri ) if ( tri . find ( n - tm ) != tri . end ( ) ) return true ; return false ; } int main ( ) { int n = 24 ; checkTriangularSumRepresentation ( n ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Absolute difference between the first X and last X Digits of N | C ++ implementation of the above approach ; Function to find the number of digits in the integer ; Function to find the absolute difference ; Store the last x digits in last ; Count the no . of digits in N ; Remove the digits except the first x ; Store the first x digits in first ; Return the absolute difference between the first and last ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long digitsCount ( long long n ) { int len = 0 ; while ( n > 0 ) { len ++ ; n /= 10 ; } return len ; } long long absoluteFirstLast ( long long n , int x ) { int i = 0 , mod = 1 ; while ( i < x ) { mod *= 10 ; i ++ ; } int last = n % mod ; long long len = digitsCount ( n ) ; while ( len != x ) { n /= 10 ; len -- ; } int first = n ; return abs ( first - last ) ; } int main ( ) { long long n = 21546 , x = 2 ; cout << absoluteFirstLast ( n , x ) ; return 0 ; }
Generate minimum sum sequence of integers with even elements greater | C ++ implementation of above approach ; Function to print the required sequence ; arr [ ] will hold the sequence sum variable will store the sum of the sequence ; If sum of the sequence is odd ; Print the sequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void make_sequence ( int N ) { int arr [ N + 1 ] , sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 1 ) arr [ i ] = 1 ; else arr [ i ] = 2 ; sum += arr [ i ] ; } if ( sum % 2 == 1 ) arr [ 2 ] = 3 ; for ( int i = 1 ; i <= N ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int N = 9 ; make_sequence ( N ) ; return 0 ; }
Count Odd and Even numbers in a range from L to R | C ++ implementation of the above approach ; Return the number of odd numbers in the range [ L , R ] ; if either R or L is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOdd ( int L , int R ) { int N = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) N += 1 ; return N ; } int main ( ) { int L = 3 , R = 7 ; int odds = countOdd ( L , R ) ; int evens = ( R - L + 1 ) - odds ; cout << " Count ▁ of ▁ odd ▁ numbers ▁ is ▁ " << odds << endl ; cout << " Count ▁ of ▁ even ▁ numbers ▁ is ▁ " << evens << endl ; return 0 ; }
Cost of painting n * m grid | C ++ implementation of the approach ; Function to return the minimum cost ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinCost ( int n , int m ) { int cost = ( n - 1 ) * m + ( m - 1 ) * n ; return cost ; } int main ( ) { int n = 4 , m = 5 ; cout << getMinCost ( n , m ) ; return 0 ; }
Minimum operations required to make all the array elements equal | C ++ implementation of the above approach ; Function to return the minimum number of given operation required to make all the array elements equal ; Check if all the elements from kth index to last are equal ; Finding the 1 st element which is not equal to the kth element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperation ( int n , int k , int a [ ] ) { for ( int i = k ; i < n ; i ++ ) { if ( a [ i ] != a [ k - 1 ] ) cout << ( -1 ) << endl ; } for ( int i = k - 2 ; i > -1 ; i -- ) { if ( a [ i ] != a [ k - 1 ] ) cout << ( i + 1 ) << endl ; } } int main ( ) { int n = 5 ; int k = 3 ; int a [ ] = { 2 , 1 , 1 , 1 , 1 } ; minOperation ( n , k , a ) ; }
Number of Simple Graph with N Vertices and M Edges | C ++ implementation of the approach ; Function to return the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * ... * 1 ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int binomialCoeff ( int n , int k ) { if ( k > n ) return 0 ; int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } int main ( ) { int N = 5 , M = 1 ; int P = ( N * ( N - 1 ) ) / 2 ; cout << binomialCoeff ( P , M ) ; return 0 ; }
Increasing sequence with given GCD | C ++ implementation of the approach ; Function to print the required sequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateSequence ( int n , int g ) { for ( int i = 1 ; i <= n ; i ++ ) cout << i * g << " ▁ " ; } int main ( ) { int n = 6 , g = 5 ; generateSequence ( n , g ) ; return 0 ; }
Program to find LCM of two Fibonnaci Numbers | C ++ Program to find LCM of Fib ( a ) and Fib ( b ) ; Create an array for memorization ; Function to return the n 'th Fibonacci number using table f[]. ; Base cases ; If fib ( n ) is already computed ; Applying recursive formula Note value n & 1 is 1 if n is odd , else 0. ; Function to return gcd of a and b ; Function to return the LCM of Fib ( a ) and Fib ( a ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; int f [ MAX ] = { 0 } ; int fib ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 n == 2 ) return ( f [ n ] = 1 ) ; if ( f [ n ] ) return f [ n ] ; int k = ( n & 1 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( n & 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } int findLCMFibonacci ( int a , int b ) { return ( fib ( a ) * fib ( b ) ) / fib ( gcd ( a , b ) ) ; } int main ( ) { int a = 3 , b = 12 ; cout << findLCMFibonacci ( a , b ) ; return 0 ; }
Check whether XOR of all numbers in a given range is even or odd | C ++ program to check if XOR of all numbers in range [ L , R ] is Even or odd ; Function to check if XOR of all numbers in range [ L , R ] is Even or Odd ; Count odd Numbers in range [ L , R ] ; Check if count of odd Numbers is even or odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isEvenOrOdd ( int L , int R ) { int oddCount = ( R - L ) / 2 ; if ( R % 2 == 1 L % 2 == 1 ) oddCount ++ ; if ( oddCount % 2 == 0 ) return " Even " ; else return " Odd " ; } int main ( ) { int L = 5 , R = 15 ; cout << isEvenOrOdd ( L , R ) ; return 0 ; }
Count number of trailing zeros in ( 1 ^ 1 ) * ( 2 ^ 2 ) * ( 3 ^ 3 ) * ( 4 ^ 4 ) * . . | C ++ implementation of the approach ; Function to return the number of trailing zeros ; To store the number of 2 s and 5 s ; If we get a factor 2 then we have i number of 2 s because the power of the number is raised to i ; If we get a factor 5 then we have i number of 5 s because the power of the number is raised to i ; Take the minimum of them ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int trailing_zeros ( int N ) { int count_of_two = 0 , count_of_five = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { int val = i ; while ( val % 2 == 0 && val > 0 ) { val /= 2 ; count_of_two += i ; } while ( val % 5 == 0 && val > 0 ) { val /= 5 ; count_of_five += i ; } } int ans = min ( count_of_two , count_of_five ) ; return ans ; } int main ( ) { int N = 12 ; cout << trailing_zeros ( N ) ; return 0 ; }
Print numbers such that no two consecutive numbers are co | C ++ implementation of the approach ; Function to generate Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Add the prime numbers to the array b ; Function to return the gcd of a and b ; Function to print the required sequence of integers ; Including the primes in a series of primes which will be later multiplied ; This is done to mark a product as existing ; Maximum number of primes that we consider ; For different interval ; For different starting index of jump ; For storing the numbers ; Checking for occurrence of a product . Also checking for the same prime occurring consecutively ; Including the primes in a series of primes which will be later multiplied ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define limit  1000000000 NEW_LINE #define MAX_PRIME  2000000 NEW_LINE #define MAX  1000000 NEW_LINE #define I_MAX  50000 NEW_LINE map < int , int > mp ; int b [ MAX ] ; int p [ MAX ] ; int j = 0 ; bool prime [ MAX_PRIME + 1 ] ; void SieveOfEratosthenes ( int n ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { b [ j ++ ] = p ; } } } int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void printSeries ( int n ) { SieveOfEratosthenes ( MAX_PRIME ) ; int i , g , k , l , m , d ; int ar [ I_MAX + 2 ] ; for ( i = 0 ; i < j ; i ++ ) { if ( ( b [ i ] * b [ i + 1 ] ) > limit ) break ; p [ i ] = b [ i ] ; mp [ b [ i ] * b [ i + 1 ] ] = 1 ; } d = 550 ; bool flag = false ; for ( k = 2 ; ( k < d - 1 ) && ! flag ; k ++ ) { for ( m = 2 ; ( m < d ) && ! flag ; m ++ ) { for ( l = m + k ; l < d ; l += k ) { if ( ( ( b [ l ] * b [ l + k ] ) < limit ) && ( l + k ) < d && p [ i - 1 ] != b [ l + k ] && p [ i - 1 ] != b [ l ] && mp [ b [ l ] * b [ l + k ] ] != 1 ) { if ( mp [ p [ i - 1 ] * b [ l ] ] != 1 ) { p [ i ] = b [ l ] ; mp [ p [ i - 1 ] * b [ l ] ] = 1 ; i ++ ; } } if ( i >= I_MAX ) { flag = true ; break ; } } } } for ( i = 0 ; i < n ; i ++ ) ar [ i ] = p [ i ] * p [ i + 1 ] ; for ( i = 0 ; i < n - 1 ; i ++ ) cout << ar [ i ] << " ▁ " ; g = gcd ( ar [ n - 1 ] , ar [ n - 2 ] ) ; cout << g * 2 << endl ; } int main ( ) { int n = 4 ; printSeries ( n ) ; return 0 ; }
Print numbers such that no two consecutive numbers are co | C ++ implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print the required sequence ; Store only the required primes ; Base condition ; First integer in the list ; Second integer in the list ; Third integer in the list ; Generate ( N - 1 ) th term ; Generate Nth term ; Modify first term ; Print the sequence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 620000 ; int prime [ MAX ] ; void Sieve ( ) { for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j < MAX ; j += i ) { prime [ j ] = 1 ; } } } } void printSequence ( int n ) { Sieve ( ) ; vector < int > v , u ; for ( int i = 13 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { v . push_back ( i ) ; } } if ( n == 3 ) { cout << 6 << " ▁ " << 10 << " ▁ " << 15 ; return ; } int k ; for ( k = 0 ; k < n - 2 ; k ++ ) { if ( k % 3 == 0 ) { u . push_back ( v [ k ] * 6 ) ; } else if ( k % 3 == 1 ) { u . push_back ( v [ k ] * 15 ) ; } else { u . push_back ( v [ k ] * 10 ) ; } } k -- ; u . push_back ( v [ k ] * 7 ) ; u . push_back ( 7 * 11 ) ; u [ 0 ] = u [ 0 ] * 11 ; for ( int i = 0 ; i < u . size ( ) ; i ++ ) { cout << u [ i ] << " ▁ " ; } } int main ( ) { int n = 4 ; printSequence ( n ) ; return 0 ; }
Midpoint ellipse drawing algorithm | C ++ program for implementing Mid - Point Ellipse Drawing Algorithm ; Initial decision parameter of region 1 ; For region 1 ; Print points based on 4 - way symmetry ; Checking and updating value of decision parameter based on algorithm ; Decision parameter of region 2 ; Plotting points of region 2 ; Print points based on 4 - way symmetry ; Checking and updating parameter value based on algorithm ; Driver code ; To draw a ellipse of major and minor radius 15 , 10 centred at ( 50 , 50 )
#include <bits/stdc++.h> NEW_LINE using namespace std ; void midptellipse ( int rx , int ry , int xc , int yc ) { float dx , dy , d1 , d2 , x , y ; x = 0 ; y = ry ; d1 = ( ry * ry ) - ( rx * rx * ry ) + ( 0.25 * rx * rx ) ; dx = 2 * ry * ry * x ; dy = 2 * rx * rx * y ; while ( dx < dy ) { cout << x + xc << " ▁ , ▁ " << y + yc << endl ; cout << - x + xc << " ▁ , ▁ " << y + yc << endl ; cout << x + xc << " ▁ , ▁ " << - y + yc << endl ; cout << - x + xc << " ▁ , ▁ " << - y + yc << endl ; if ( d1 < 0 ) { x ++ ; dx = dx + ( 2 * ry * ry ) ; d1 = d1 + dx + ( ry * ry ) ; } else { x ++ ; y -- ; dx = dx + ( 2 * ry * ry ) ; dy = dy - ( 2 * rx * rx ) ; d1 = d1 + dx - dy + ( ry * ry ) ; } } d2 = ( ( ry * ry ) * ( ( x + 0.5 ) * ( x + 0.5 ) ) ) + ( ( rx * rx ) * ( ( y - 1 ) * ( y - 1 ) ) ) - ( rx * rx * ry * ry ) ; while ( y >= 0 ) { cout << x + xc << " ▁ , ▁ " << y + yc << endl ; cout << - x + xc << " ▁ , ▁ " << y + yc << endl ; cout << x + xc << " ▁ , ▁ " << - y + yc << endl ; cout << - x + xc << " ▁ , ▁ " << - y + yc << endl ; if ( d2 > 0 ) { y -- ; dy = dy - ( 2 * rx * rx ) ; d2 = d2 + ( rx * rx ) - dy ; } else { y -- ; x ++ ; dx = dx + ( 2 * ry * ry ) ; dy = dy - ( 2 * rx * rx ) ; d2 = d2 + dx - dy + ( rx * rx ) ; } } } int main ( ) { midptellipse ( 10 , 15 , 50 , 50 ) ; return 0 ; }
Program to check if a number is divisible by sum of its digits | C ++ implementation of above approach ; Function to check if the given number is divisible by sum of its digits ; Find sum of digits ; check if sum of digits divides n ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string isDivisible ( long long int n ) { long long int temp = n ; int sum = 0 ; while ( n ) { int k = n % 10 ; sum += k ; n /= 10 ; } if ( temp % sum == 0 ) return " YES " ; return " NO " ; } int main ( ) { long long int n = 123 ; cout << isDivisible ( n ) ; return 0 ; }
Program to check if a number is divisible by sum of its digits | C ++ implementation of above approach ; Converting integer to String ; Initialising sum to 0 ; Traversing through the String ; Converting character to int ; Comparing number and sum ; Driver Code ; Passing this number to get result function
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getResult ( long long int n ) { string st = std :: to_string ( n ) ; int sum = 0 ; for ( char i : st ) { sum = sum + ( int ) i ; } if ( n % sum == 0 ) return " Yes " ; else return " No " ; } int main ( ) { int n = 123 ; cout << getResult ( n ) ; return 0 ; }
Find the final X and Y when they are Altering under given condition | CPP tp implement above approach ; Function to get final value of X and Y ; Following the sequence but by replacing minus with modulo ; Step 1 ; Step 2 ; Step 3 ; Otherwise terminate ; Driver function ; Get the initial X and Y values ; Find the result
#include <iostream> NEW_LINE using namespace std ; void alter ( long long int x , long long int y ) { while ( true ) { if ( x == 0 y == 0 ) break ; if ( x >= 2 * y ) x = x % ( 2 * y ) ; else if ( y >= 2 * x ) y = y % ( 2 * x ) ; else break ; } cout << " X = " << x << " , ▁ " << " Y = " << y ; } int main ( ) { long long int x = 12 , y = 5 ; alter ( x , y ) ; return 0 ; }
Print all multiplicative primes <= N | C ++ implementation of the approach ; Function to return the digit product of n ; Function to print all multiplicative primes <= n ; Create a boolean array " prime [ 0 . . n + 1 ] " . 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 ; If i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digitProduct ( int n ) { int prod = 1 ; while ( n ) { prod = prod * ( n % 10 ) ; n = n / 10 ; } return prod ; } void printMultiplicativePrimes ( int n ) { bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] && prime [ digitProduct ( i ) ] ) cout << i << " ▁ " ; } } int main ( ) { int n = 10 ; printMultiplicativePrimes ( n ) ; }
Largest number less than or equal to N / 2 which is coprime to N | C ++ implementation of the above approacdh ; Function to calculate gcd of two number ; Function to check if two numbers are coprime or not ; two numbers are coprime if their gcd is 1 ; Function to find largest integer less than or equal to N / 2 and coprime with N ; Check one by one all numbers less than or equal to N / 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; ll gcd ( ll a , ll b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } bool coPrime ( ll n1 , ll n2 ) { if ( gcd ( n1 , n2 ) == 1 ) return true ; else return false ; } ll largestCoprime ( ll N ) { ll half = floor ( N / 2 ) ; while ( coPrime ( N , half ) == false ) half -- ; return half ; } int main ( ) { ll n = 50 ; cout << largestCoprime ( n ) ; return 0 ; }
Largest number less than or equal to N / 2 which is coprime to N | C ++ implementation of the above approach ; Function to find largest integer less than or equal to N / 2 and is coprime with N ; Handle the case for N = 6 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long largestCoprime ( long long N ) { if ( N == 6 ) return 1 ; else if ( N % 4 == 0 ) return ( N / 2 ) - 1 ; else if ( N % 2 == 0 ) return ( N / 2 ) - 2 ; else return ( ( N - 1 ) / 2 ) ; } int main ( ) { long long int n = 50 ; cout << largestCoprime ( n ) << endl ; return 0 ; }
Print all safe primes below N | C ++ implementation of the approach ; Function to print first n safe primes ; Initialize all entries of integer array as 1. A value in prime [ i ] will finally be 0 if i is Not a prime , else 1 ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime ; 2 p + 1 ; If 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSafePrimes ( int n ) { int prime [ n + 1 ] ; for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == 1 ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = 0 ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] != 0 ) { int temp = ( 2 * i ) + 1 ; if ( temp <= n && prime [ temp ] != 0 ) prime [ temp ] = 2 ; } } for ( int i = 5 ; i <= n ; i ++ ) if ( prime [ i ] == 2 ) cout << i << " ▁ " ; } int main ( ) { int n = 20 ; printSafePrimes ( n ) ; return 0 ; }
Minimum multiplications with { 2 , 3 , 7 } to make two numbers equal | C ++ implementation of the approach ; Function to find powers of 2 , 3 and 7 in x ; To keep count of each divisor ; To store the result ; Count powers of 2 in x ; Count powers of 3 in x ; Count powers of 7 in x ; Remaining number which is not divisible by 2 , 3 or 7 ; Function to return the minimum number of given operations required to make a and b equal ; a = x * 2 ^ a1 * 3 ^ a2 * 7 ^ a3 va [ 0 ] = a1 va [ 1 ] = a2 va [ 2 ] = a3 va [ 3 ] = x ; Similarly for b ; If a and b cannot be made equal with the given operation . Note that va [ 3 ] and vb [ 3 ] contain remaining numbers after repeated divisions with 2 , 3 and 7. If remaining numbers are not same then we cannot make them equal . ; Minimum number of operations required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > Divisors ( int x ) { int c = 0 ; vector < int > v ; while ( x % 2 == 0 ) { c ++ ; x /= 2 ; } v . push_back ( c ) ; c = 0 ; while ( x % 3 == 0 ) { c ++ ; x /= 3 ; } v . push_back ( c ) ; c = 0 ; while ( x % 7 == 0 ) { c ++ ; x /= 7 ; } v . push_back ( c ) ; v . push_back ( x ) ; return v ; } int MinOperations ( int a , int b ) { vector < int > va = Divisors ( a ) ; vector < int > vb = Divisors ( b ) ; if ( va [ 3 ] != vb [ 3 ] ) return -1 ; int minOperations = abs ( va [ 0 ] - vb [ 0 ] ) + abs ( va [ 1 ] - vb [ 1 ] ) + abs ( va [ 2 ] - vb [ 2 ] ) ; return minOperations ; } int main ( ) { int a = 14 , b = 28 ; cout << MinOperations ( a , b ) ; return 0 ; }
Product of N with its largest odd digit | C ++ program to find the product of N with its largest odd digit ; Function to return the largest odd digit in n ; If all digits are even then - 1 will be returned ; Last digit from n ; If current digit is odd and > maxOdd ; Remove last digit ; Return the maximum odd digit ; Function to return the product of n with its largest odd digit ; If there are no odd digits in n ; Product of n with its largest odd digit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largestOddDigit ( int n ) { int maxOdd = -1 ; while ( n > 0 ) { int digit = n % 10 ; if ( digit % 2 == 1 && digit > maxOdd ) maxOdd = digit ; n = n / 10 ; } return maxOdd ; } int getProduct ( int n ) { int maxOdd = largestOddDigit ( n ) ; if ( maxOdd == -1 ) return -1 ; return ( n * maxOdd ) ; } int main ( ) { int n = 12345 ; cout << getProduct ( n ) ; return 0 ; }
Check if a number is perfect square without finding square root | C ++ program for above approach ; Program to find if x is a perfect square . ; Check if mid is perfect square ; Mid is small -> go right to increase mid ; Mid is large -> to left to decrease mid ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( int x ) { long long left = 1 , right = x ; while ( left <= right ) { long long mid = ( left + right ) / 2 ; if ( mid * mid == x ) { return true ; } if ( mid * mid < x ) { left = mid + 1 ; } else { right = mid - 1 ; } } return false ; } int main ( ) { int x = 2500 ; if ( isPerfectSquare ( x ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find k numbers which are powers of 2 and have sum N | Set 1 | CPP program to find k numbers that are power of 2 and have sum equal to N ; function to print numbers ; Count the number of set bits ; Not - possible condition ; Stores the number ; Get the set bits ; Iterate till we get K elements ; Get the topmost element ; Push the elements / 2 into priority queue ; Print all elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printNum ( int n , int k ) { int x = __builtin_popcount ( n ) ; if ( k < x k > n ) { cout << " - 1" ; return ; } priority_queue < int > pq ; int two = 1 ; while ( n ) { if ( n & 1 ) { pq . push ( two ) ; } two = two * 2 ; n = n >> 1 ; } while ( pq . size ( ) < k ) { int el = pq . top ( ) ; pq . pop ( ) ; pq . push ( el / 2 ) ; pq . push ( el / 2 ) ; } int ind = 0 ; while ( ind < k ) { cout << pq . top ( ) << " ▁ " ; pq . pop ( ) ; ind ++ ; } } int main ( ) { int n = 9 , k = 4 ; printNum ( n , k ) ; return 0 ; }
Sum of LCM ( 1 , n ) , LCM ( 2 , n ) , LCM ( 3 , n ) , ... , LCM ( n , n ) | C ++ implementation of the approach ; Euler totient Function ; Function to return the required LCM sum ; Summation of d * ETF ( d ) where d belongs to set of divisors of n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define n  1000002 NEW_LINE #define ll  long long int NEW_LINE ll phi [ n + 2 ] , ans [ n + 2 ] ; void ETF ( ) { for ( int i = 1 ; i <= n ; i ++ ) { phi [ i ] = i ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( phi [ i ] == i ) { phi [ i ] = i - 1 ; for ( int j = 2 * i ; j <= n ; j += i ) { phi [ j ] = ( phi [ j ] * ( i - 1 ) ) / i ; } } } } ll LcmSum ( int m ) { ETF ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j += i ) { ans [ j ] += ( i * phi [ i ] ) ; } } ll answer = ans [ m ] ; answer = ( answer + 1 ) * m ; answer = answer / 2 ; return answer ; } int main ( ) { int m = 5 ; cout << LcmSum ( m ) ; return 0 ; }