codeparrot/xlcost-text-to-code huggingface dataset:
Find the first day of a given year from a base year having first day as Monday | C ++ Implementation of the above approach ; Function to find the day of 1 st January of Y year ; Count years between years Y and B ; Count leap years ; Non leap years ; Total number of days in the years lying between the years Y and B ; Actual day ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findDay ( int Y , int B ) { int lyear , rest , totaldays , day ; Y = ( Y - 1 ) - B ; lyear = Y / 4 ; rest = Y - lyear ; totaldays = ( rest * 365 ) + ( lyear * 366 ) + 1 ; day = ( totaldays % 7 ) ; if ( day == 0 ) printf ( " Monday " ) ; else if ( day == 1 ) printf ( " Tuesday " ) ; else if ( day == 2 ) printf ( " Wednesday " ) ; else if ( day == 3 ) printf ( " Thursday " ) ; else if ( day == 4 ) printf ( " Friday " ) ; else if ( day == 5 ) printf ( " Saturday " ) ; else if ( day == 6 ) printf ( " Sunday " ) ; else printf ( " INPUT ▁ YEAR ▁ IS ▁ WRONG ! " ) ; } int main ( ) { int Y = 2020 , B = 1900 ; findDay ( Y , B ) ; return 0 ; }
Queries to update array elements in a range [ L , R ] to satisfy given conditions | C ++ program to implement the above approach ; Function to print the array ; Function to perform the query in range [ L , R ] such that arr [ i ] += i - L + 1 ; Initialize array ; Traverse the query array ; Stores range in 1 - based index ; Update arr1 [ L ] ; Update arr1 [ R + 1 ] ; Update arr2 [ R + 1 ] ; Calculate prefix sum ; Traverse the array , arr2 [ ] ; Copy arr2 [ ] into arr [ ] ; Driver Code ; Given array ; Size of the array ; Stores count of query ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } void modifyArray ( int arr [ ] , int N , int Q [ ] [ 2 ] , int cntQuery ) { int arr1 [ N + 1 ] = { 0 } ; int arr2 [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < cntQuery ; i ++ ) { int L = Q [ i ] [ 0 ] + 1 , R = Q [ i ] [ 1 ] + 1 ; arr1 [ L ] ++ ; arr1 [ R + 1 ] -- ; arr2 [ R + 1 ] -= R - L + 1 ; } for ( int i = 1 ; i <= N ; i ++ ) arr1 [ i ] += arr1 [ i - 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) arr2 [ i ] += arr2 [ i - 1 ] + arr1 [ i ] ; for ( int i = 1 ; i <= N ; i ++ ) arr [ i - 1 ] = arr2 [ i ] ; printArray ( arr , N ) ; } int main ( ) { int arr [ ] = { 0 , 0 , 0 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int Q [ ] [ 2 ] = { { 1 , 3 } , { 0 , 1 } } ; int cntQuery = sizeof ( Q ) / sizeof ( Q [ 0 ] ) ; modifyArray ( arr , N , Q , cntQuery ) ; return 0 ; }
Maximize product obtained by taking one element from each array of a given list | CPP program for the above approach ; Function to return the product of 2 numbers ; If any of the two numbers is None ; Otherwise , return the product ; Function to calculate maximum product by taking only one element from each array present in the list ; Find the maximum and minimum present in the current array ; If last index is reached , then return the highest ( positive ) and lowest ( negative ) values ; Store the positive and negative products returned by calculating for the remaining arrays ; Store highest positive product ; Store product of highest with negative ; Store product of lowest with positive ; Store lowest negative product ; Return the maximum positive and minimum negative product ; Driver Code ; Count of given arrays ; Given list of N arrays ; Store the maximum positive and minimum negative product possible ; Print the maximum product
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findProduct ( int number_1 , int number_2 ) { if ( number_1 == INT_MIN or number_2 == INT_MIN ) return 0 ; else return number_1 * number_2 ; } pair < int , int > calculateProduct ( vector < vector < int > > List , int index ) { int highest = * max_element ( List [ index ] . begin ( ) , List [ index ] . end ( ) ) ; int lowest = * min_element ( List [ index ] . begin ( ) , List [ index ] . end ( ) ) ; if ( index + 1 == List . size ( ) ) { if ( lowest < 0 and highest > = 0 ) return { highest , lowest } ; else if ( lowest <= 0 and highest <= 0 ) return { INT_MIN , lowest } ; else if ( lowest >= 0 and highest >= 0 ) return { highest , INT_MIN } ; } pair < int , int > temp = calculateProduct ( List , index + 1 ) ; int positive = temp . first ; int negative = temp . second ; int highPos = findProduct ( highest , positive ) ; int highNeg = findProduct ( highest , negative ) ; int lowPos = findProduct ( lowest , positive ) ; int lowNeg = findProduct ( lowest , negative ) ; if ( lowest < 0 and highest > = 0 ) return { max ( highPos , lowNeg ) , min ( highNeg , lowPos ) } ; else if ( lowest <= 0 and highest <= 0 ) return { lowNeg , lowPos } ; else if ( lowest >= 0 and highest >= 0 ) return { max ( lowPos , highPos ) , min ( lowNeg , highNeg ) } ; } int main ( ) { int N = 2 ; vector < vector < int > > arr { { -3 , -4 } , { 1 , 2 , -3 } } ; pair < int , int > ans = calculateProduct ( arr , 0 ) ; cout << ans . first << endl ; }
Reduce a number N by at most D to maximize count of trailing nines | CPP program for the above approach ; Function to find a number with maximum count of trailing nine ; Stores count of digits in n ; Stores power of 10 ; If last i digits greater than or equal to d ; Update res ; Update p10 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; void maxNumTrailNine ( int n , int d ) { int res = n ; int cntDigits = log10 ( n ) + 1 ; int p10 = 10 ; for ( int i = 1 ; i <= cntDigits ; i ++ ) { if ( n % p10 >= d ) { break ; } else { res = n - n % p10 - 1 ; } p10 = p10 * 10 ; } cout << res ; } int main ( ) { int n = 1025 , d = 6 ; maxNumTrailNine ( n , d ) ; }
Split array into minimum number of subsets such that elements of all pairs are present in different subsets at least once | C ++ program to to implement the above approach ; Function to find minimum count of ways to split the array into two subset such that elements of each pair occurs in two different subset ; Stores minimum count of ways to split array into two subset such that elements of each pair occurs in two different subset ; If N is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumNoOfWays ( int arr [ ] , int n ) { int mini_no_of_ways ; if ( n % 2 == 0 ) { mini_no_of_ways = n / 2 ; } else { mini_no_of_ways = n / 2 + 1 ; } return mini_no_of_ways ; } int main ( ) { int arr [ ] = { 3 , 4 , 2 , 1 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << MinimumNoOfWays ( arr , N ) ; return 0 ; }
Chocolate Distribution Problem | Set 2 | C ++ program for above approach ; Function to return minimum number of chocolates ; decreasing sequence ; add the chocolates received by that person ; end point of decreasing sequence ; val = 1 ; reset value at that index ; increasing sequence ; flat sequence ; add value of chocolates at position n - 1 ; Helper function to get sum of decreasing sequence ; value obtained from decreasing sequence also the count of values in the sequence ; assigning max of values obtained from increasing and decreasing sequences ; sum of count - 1 values & peak value sum of natural numbers : ( n * ( n + 1 ) ) / 2 ; Driver code
#include <iostream> NEW_LINE using namespace std ; int minChocolates ( int a [ ] , int n ) { int i = 0 , j = 0 ; int res = 0 , val = 1 ; while ( j < n - 1 ) { if ( a [ j ] > a [ j + 1 ] ) { j += 1 ; continue ; } if ( i == j ) res += val ; else { res += get_sum ( val , i , j ) ; } if ( a [ j ] < a [ j + 1 ] ) val += 1 ; else val = 1 ; j += 1 ; i = j ; } if ( i == j ) res += val ; else res += get_sum ( val , i , j ) ; return res ; } int get_sum ( int peak , int start , int end ) { int count = end - start + 1 ; peak = max ( peak , count ) ; int s = peak + ( ( ( count - 1 ) * count ) >> 1 ) ; return s ; } int main ( ) { int a [ ] = { 5 , 5 , 4 , 3 , 2 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " Minimum ▁ number ▁ of ▁ chocolates ▁ = ▁ " << minChocolates ( a , n ) << " STRNEWLINE " ; return 0 ; }
Remove array elements to reduce frequency of each array element to at most K | C ++ program for the above approach ; Function to remove array elements such that frequency of each distinct array element is at most K ; Base Case ; Stores index of array element by removing the array element such that the frequency of array elements is at most K ; Traverse the array ; If j < k or arr [ i ] > arr [ j - k ] ; Update arr [ j ] ; Remove array elements ; Function to print the array ; Traverse the array ; Utility Function to remove array elements such that frequency of each distinct array element is at most K ; Print updated array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > RemoveElemArr ( vector < int > & arr , int n , int k ) { if ( n == 0 n == 1 ) return arr ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( j < k arr [ i ] > arr [ j - k ] ) { arr [ j ++ ] = arr [ i ] ; } } while ( arr . size ( ) > j ) { arr . pop_back ( ) ; } return arr ; } void printArray ( vector < int > & arr ) { for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } void UtilRemov ( vector < int > & arr , int n , int k ) { arr = RemoveElemArr ( arr , n , k ) ; printArray ( arr ) ; } int main ( ) { vector < int > arr = { 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 } ; int k = 2 ; int n = arr . size ( ) ; UtilRemov ( arr , n , k ) ; return 0 ; }
Count pairs of equal array elements remaining after every removal | C ++ program to implement the above approach ; Function to count pairs of equal elements by removing arr [ i ] from the array ; Stores total count of pairs of equal elements ; Store frequency of each distinct array element ; Traverse the array ; Update frequency of arr [ i ] ; Traverse the map ; Stores key of an element ; Traverse the array ; Stores count of pairs of equal element by removing arr [ i ] ; Driver Code ; Given Array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pairs_after_removing ( int arr [ ] , int N ) { int cntPairs = 0 ; unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } for ( auto element : mp ) { int i = element . first ; cntPairs += mp [ i ] * ( mp [ i ] - 1 ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { int pairs_after_arr_i_removed = cntPairs + 1 - mp [ arr [ i ] ] ; cout << pairs_after_arr_i_removed << ' ▁ ' ; } return ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 3 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pairs_after_removing ( arr , N ) ; return 0 ; }
Modify N by adding its smallest positive divisor exactly K times | C ++ program to implement the above approach ; Function to find the smallest divisor of N greater than 1 ; If i is a divisor of N ; If N is a prime number ; Function to find the value of N by performing the operations K times ; If N is an even number ; Update N ; If N is an odd number ; Update N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestDivisorGr1 ( int N ) { for ( int i = 2 ; i <= sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } int findValOfNWithOperat ( int N , int K ) { if ( N % 2 == 0 ) { N += K * 2 ; } else { N += smallestDivisorGr1 ( N ) + ( K - 1 ) * 2 ; } return N ; } int main ( ) { int N = 6 , K = 4 ; cout << findValOfNWithOperat ( N , K ) ; return 0 ; }
Partition array into minimum number of equal length subsets consisting of a single distinct value | C ++ program to implement the above approach ; Function to find the minimum count of subsets by partitioning the array with given conditions ; Store frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Stores GCD of frequency of each distinct element of the array ; Update freqGCD ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CntOfSubsetsByPartitioning ( int arr [ ] , int N ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } int freqGCD = 0 ; for ( auto i : freq ) { freqGCD = __gcd ( freqGCD , i . second ) ; } return ( N ) / freqGCD ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CntOfSubsetsByPartitioning ( arr , N ) ; return 0 ; }
Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix | C ++ program for the above approach ; Initialize rows and columns ; Store all 8 directions ; Function to check if a cell ( i , j ) is valid or not ; Function to find sum of adjacent cells for cell ( i , j ) ; Initialize sum ; Visit all 8 directions ; Check if cell is valid ; Return sum ; Function to print sum of adjacent elements ; Stores the resultant matrix ; Iterate each elements of matrix ; Find adjacent sum ; Driver Code ; Given matrix ; Size of matrix ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int r , c ; vector < vector < int > > dir = { { 1 , 0 } , { -1 , 0 } , { 0 , 1 } , { 0 , -1 } , { -1 , -1 } , { -1 , 1 } , { 1 , 1 } , { 1 , -1 } } ; bool valid ( int i , int j ) { if ( i >= 0 && j >= 0 && i < r && j < c ) return 1 ; return 0 ; } int find ( int i , int j , vector < vector < int > > & v ) { int s = 0 ; for ( auto x : dir ) { int ni = i + x [ 0 ] , nj = j + x [ 1 ] ; if ( valid ( ni , nj ) ) s += v [ ni ] [ nj ] ; } return s ; } void findsumofneighbors ( vector < vector < int > > & M ) { vector < vector < int > > v ( r , vector < int > ( c , 0 ) ) ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { v [ i ] [ j ] = find ( i , j , M ) ; cout << v [ i ] [ j ] << " ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { vector < vector < int > > M = { { 1 , 4 , 1 } , { 2 , 4 , 5 } , { 3 , 1 , 2 } } ; r = M . size ( ) , c = M [ 0 ] . size ( ) ; findsumofneighbors ( M ) ; }
Nth term of a recurrence relation generated by two given arrays | C ++ program for the above approach ; Declare T [ ] [ ] as global matrix ; Result matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to calculate matrix ^ n using binary exponentaion ; Initialize result matrix and unity matrix ; Function to calculate nth term of general recurrence ; Fill T [ ] [ ] with appropriate value ; Function Call to calculate T ^ n ; Calculate nth term as first element of F * ( T ^ n ) ; Print the result ; Driver Code ; Given Initial terms ; Given coefficients ; Given K ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mod = 1e9 + 7 ; int T [ 2000 ] [ 2000 ] ; int result [ 2000 ] [ 2000 ] ; void mul_2 ( int K ) { int temp [ K + 1 ] [ K + 1 ] ; memset ( temp , 0 , sizeof temp ) ; for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { for ( int k = 1 ; k <= K ; k ++ ) { temp [ i ] [ j ] = ( temp [ i ] [ j ] + ( T [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ; } } } for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { T [ i ] [ j ] = temp [ i ] [ j ] ; } } } void mul_1 ( int K ) { int temp [ K + 1 ] [ K + 1 ] ; memset ( temp , 0 , sizeof temp ) ; for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { for ( int k = 1 ; k <= K ; k ++ ) { temp [ i ] [ j ] = ( temp [ i ] [ j ] + ( result [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ; } } } for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { result [ i ] [ j ] = temp [ i ] [ j ] ; } } } void matrix_pow ( int K , int n ) { for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { if ( i == j ) result [ i ] [ j ] = 1 ; } } while ( n > 0 ) { if ( n % 2 == 1 ) mul_1 ( K ) ; mul_2 ( K ) ; n /= 2 ; } } int NthTerm ( int F [ ] , int C [ ] , int K , int n ) { for ( int i = 1 ; i <= K ; i ++ ) T [ i ] [ K ] = C [ K - i ] ; for ( int i = 1 ; i <= K ; i ++ ) T [ i + 1 ] [ i ] = 1 ; matrix_pow ( K , n ) ; int answer = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { answer += F [ i - 1 ] * result [ i ] [ 1 ] ; } cout << answer << endl ; return 0 ; } int main ( ) { int F [ ] = { 1 , 2 , 3 } ; int C [ ] = { 1 , 1 , 1 } ; int K = 3 ; int N = 10 ; NthTerm ( F , C , K , N ) ; return 0 ; }
Calculate Root Mean Kth power of all array elements | C ++ program for the above approach ; Function to find the Nth root ; Initially guessing random numberbetween 0 and 9 ; Smaller eps for more accuracy ; Initialize difference between the two roots by INT_MAX ; xK denotes current value of x ; Iterate until desired accuracy is reached ; Find the current value from previous value by newton 's method ; Function to calculate the Root Mean kth power of array elements ; Calculate sum of kth power ; Calculate Mean ; Calculate kth Root of mean ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double nthRoot ( int A , int N ) { double xPre = rand ( ) % 10 ; double eps = 1e-3 ; double delX = INT_MAX ; double xK ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / pow ( xPre , N - 1 ) ) / ( double ) N ; delX = abs ( xK - xPre ) ; xPre = xK ; } return xK ; } float RMNValue ( int arr [ ] , int n , int k ) { int Nth = 0 ; float mean = 0.0 , root = 0.0 ; for ( int i = 0 ; i < n ; i ++ ) { Nth += pow ( arr [ i ] , k ) ; } mean = ( Nth / ( float ) ( n ) ) ; root = nthRoot ( mean , k ) ; return root ; } int main ( ) { int arr [ ] = { 10 , 4 , 6 , 8 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; cout << RMNValue ( arr , N , K ) ; return 0 ; }
Add two numbers represented by Stacks | C ++ program to implement the above approach ; Function to return the stack that contains the sum of two numbers ; Calculate the sum of the top elements of both the stacks ; Push the sum into the stack ; Store the carry ; Pop the top elements ; If N1 is not empty ; If N2 is not empty ; If carry remains ; Reverse the stack . so that most significant digit is at the bottom of the stack ; Function to display the resultamt stack ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; stack < int > addStack ( stack < int > N1 , stack < int > N2 ) { stack < int > res ; int sum = 0 , rem = 0 ; while ( ! N1 . empty ( ) and ! N2 . empty ( ) ) { sum = ( rem + N1 . top ( ) + N2 . top ( ) ) ; res . push ( sum % 10 ) ; rem = sum / 10 ; N1 . pop ( ) ; N2 . pop ( ) ; } while ( ! N1 . empty ( ) ) { sum = ( rem + N1 . top ( ) ) ; res . push ( sum % 10 ) ; rem = sum / 10 ; N1 . pop ( ) ; } while ( ! N2 . empty ( ) ) { sum = ( rem + N2 . top ( ) ) ; res . push ( sum % 10 ) ; rem = sum / 10 ; N2 . pop ( ) ; } while ( rem > 0 ) { res . push ( rem ) ; rem /= 10 ; } while ( ! res . empty ( ) ) { N1 . push ( res . top ( ) ) ; res . pop ( ) ; } res = N1 ; return res ; } void display ( stack < int > & res ) { int N = res . size ( ) ; string s = " " ; while ( ! res . empty ( ) ) { s = to_string ( res . top ( ) ) + s ; res . pop ( ) ; } cout << s << endl ; } int main ( ) { stack < int > N1 ; N1 . push ( 5 ) ; N1 . push ( 8 ) ; N1 . push ( 7 ) ; N1 . push ( 4 ) ; stack < int > N2 ; N2 . push ( 2 ) ; N2 . push ( 1 ) ; N2 . push ( 3 ) ; stack < int > res = addStack ( N1 , N2 ) ; display ( res ) ; return 0 ; }
Check if a number has an odd count of odd divisors and even count of even divisors | C ++ implementation of the above approach ; Function to check if the number is a perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to check if count of even divisors is even and count of odd divisors is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define lli  long long int NEW_LINE bool isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ; } void checkFactors ( lli N ) { if ( isPerfectSquare ( N ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } int main ( ) { lli N = 36 ; checkFactors ( N ) ; return 0 ; }
Queries to find the XOR of an Array after replacing all occurrences of X by Y | C ++ Program to implement the above approach ; Stores the bitwise XOR of array elements ; Function to find the total xor ; Loop to find the xor of all the elements ; Function to find the XOR after replacing all occurrences of X by Y for Q queries ; Remove contribution of X from total_xor ; Adding contribution of Y to total_xor ; Print total_xor ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int total_xor ; void initialize_xor ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { total_xor = total_xor ^ arr [ i ] ; } } void find_xor ( int X , int Y ) { total_xor = total_xor ^ X ; total_xor = total_xor ^ Y ; cout << total_xor << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 5 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; initialize_xor ( arr , n ) ; vector < vector < int > > Q = { { 5 , 6 } , { 8 , 1 } } ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { find_xor ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) ; } return 0 ; }
Check whether a given number is an ugly number or not | C ++ implementation to check if a number is an ugly number or not ; Function to check if a number is an ugly number or not ; Base Cases ; Condition to check if the number is divided by 2 , 3 , or 5 ; Driver Code
#include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE int isUgly ( int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; if ( n % 2 == 0 ) { return ( isUgly ( n / 2 ) ) ; } if ( n % 3 == 0 ) { return ( isUgly ( n / 3 ) ) ; } if ( n % 5 == 0 ) { return ( isUgly ( n / 5 ) ) ; } return 0 ; } int main ( ) { int no = isUgly ( 14 ) ; if ( no == 1 ) printf ( " Yes " ) ; else printf ( " No " ) ; return 0 ; }
Maximum possible GCD for a pair of integers with sum N | C ++ Program to find the maximum possible GCD of any pair with sum N ; Function to find the required GCD value ; If i is a factor of N ; Return the largest factor possible ; If N is a prime number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxGCD ( int N ) { for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { return N / i ; } } return 1 ; } int main ( ) { int N = 33 ; cout << " Maximum ▁ Possible ▁ GCD ▁ value ▁ is ▁ : ▁ " << maxGCD ( N ) << endl ; return 0 ; }
Minimize K whose XOR with given array elements leaves array unchanged | C ++ program for the above approach ; Function to find the minimum value of K in given range ; Declare a set ; Initialize count variable ; Iterate in range [ 1 , 1024 ] ; counter set as 0 ; Iterating through the Set ; Check if the XOR calculated is present in the Set ; If the value of Bitwise XOR inside the given set then increment count ; Check if the value of count is equal to the size of set ; Return minimum value of K ; If no such K is found ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int min_value ( int arr [ ] , int N ) { int x , X , K ; set < int > S ; for ( int i = 0 ; i < N ; i ++ ) { S . insert ( arr [ i ] ) ; } int count = 0 ; for ( int i = 1 ; i <= 1024 ; i ++ ) { count = 0 ; for ( auto it = S . begin ( ) ; it != S . end ( ) ; it ++ ) { X = ( ( i * it ) - ( i & * it ) ) ; if ( S . find ( X ) != S . end ( ) ) { count ++ ; } } if ( count == S . size ( ) ) { K = i ; return K ; } } return -1 ; } int main ( ) { int arr [ ] = { 1 , 0 , 3 , 3 , 0 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << min_value ( arr , N ) ; return 0 ; }
Junction Numbers | C ++ implementation for the above approach ; Function to find the sum Of digits of N ; To store sum of N and sumOfdigitsof ( N ) ; extracting digit ; Function to check Junction numbers ; To store count of ways n can be represented as i + SOD ( i ) ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum ( int n ) { int sum = 0 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r ; n = n / 10 ; } return sum ; } bool isJunction ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i + sum ( i ) == n ) count ++ ; } return count >= 2 ; } int main ( ) { int N = 111 ; if ( isJunction ( N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }