codeparrot/xlcost-text-to-code huggingface dataset:
Length of longest Powerful number subsequence in an Array | C ++ program to find the length of Longest Powerful Subsequence in an Array ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; Check if only 2 ^ 1 divides n , then return false ; Check if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Function to find the longest subsequence which contain all powerful numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } int longestPowerfulSubsequence ( int arr [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) answer ++ ; } return answer ; } int main ( ) { int arr [ ] = { 6 , 4 , 10 , 13 , 9 , 25 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << longestPowerfulSubsequence ( arr , n ) << endl ; return 0 ; }
Maximum OR value of a pair in an Array without using OR operator | C ++ implementation of the approach ; Function to return the maximum bitwise OR for any pair of the given array without using bitwise OR operation ; find maximum element in the array ; finding complement will set all unset bits in a number ; iterate through all other array elements to find maximum AND value ; c will give the maximum value that could be added to max_value to produce maximum OR value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOR ( int arr [ ] , int n ) { int max_value = * max_element ( arr , arr + n ) ; int number_of_bits = floor ( log2 ( max_value ) ) + 1 ; int complement = ( ( 1 << number_of_bits ) - 1 ) ^ max_value ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != max_value ) { c = max ( c , ( complement & arr [ i ] ) ) ; } } return ( max_value + c ) ; } int main ( ) { int arr [ ] = { 3 , 6 , 8 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxOR ( arr , n ) ; return 0 ; }
Find the XOR of the elements in the given range [ L , R ] with the value K for a given set of queries | C ++ implementation to perform the XOR range updates on an array ; Function to perform the update operation on the given array ; Converting the indices to 0 indexing . ; Saving the XOR of K from the starting index in the range [ L , R ] . ; Saving the XOR of K at the ending index in the given [ L , R ] . ; Function to display the resulting array ; Finding the resultant value in the result array ; Combining the effects of the updates with the original array without changing the initial array . ; Driver code ; Query 1 ; Query 2 ; Query 3
#include <bits/stdc++.h> NEW_LINE using namespace std ; void update ( int res [ ] , int L , int R , int K ) { L -= 1 ; R -= 1 ; res [ L ] ^= K ; res [ R + 1 ] ^= K ; } void display ( int arr [ ] , int res [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { res [ i ] = res [ i ] ^ res [ i - 1 ] ; } for ( int i = 0 ; i < n ; i ++ ) { cout << ( arr [ i ] ^ res [ i ] ) << " ▁ " ; } cout << endl ; } int main ( ) { int arr [ ] = { 2 , 4 , 6 , 8 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int res [ N ] ; memset ( res , 0 , sizeof ( res ) ) ; int L = 1 , R = 3 , K = 2 ; update ( res , L , R , K ) ; L = 2 ; R = 4 ; K = 3 ; update ( res , L , R , K ) ; display ( arr , res , N ) ; return 0 ; }
Shortest subarray to be removed to make all Array elements unique | C ++ program to make array elements pairwise distinct by removing at most one subarray of minimum length ; Function to check if elements of Prefix and suffix of each sub array of size K are pairwise distinct or not ; Hash map to store frequencies of elements of prefix and suffix ; Variable to store number of occurrences of an element other than one ; Adding frequency of elements of suffix to hash for subarray starting from first index There is no prefix for this sub array ; Counting extra elements in current Hash map ; If there are no extra elements return true ; Check for remaining sub arrays ; First element of suffix is now part of subarray which is being removed so , check for extra elements ; Decrement frequency of first element of the suffix ; Increment frequency of last element of the prefix ; Check for extra elements ; If there are no extra elements return true ; Function for calculating minimum length of the subarray , which on removing make all elements pairwise distinct ; Possible range of length of subarray ; Binary search to find minimum ans ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int n , int k ) { map < int , int > m ; int extra = 0 ; for ( int i = k ; i < n ; i ++ ) m [ a [ i ] ] ++ ; for ( auto x : m ) extra += x . second - 1 ; if ( extra == 0 ) return true ; for ( int i = 1 ; i + k - 1 < n ; i ++ ) { if ( m [ a [ i + k - 1 ] ] > 1 ) extra -- ; m [ a [ i + k - 1 ] ] -- ; m [ a [ i - 1 ] ] ++ ; if ( m [ a [ i - 1 ] ] > 1 ) extra ++ ; if ( extra == 0 ) return true ; } return false ; } int minlength ( int a [ ] , int n ) { int lo = 0 , hi = n + 1 ; int ans = 0 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( check ( a , n , mid ) ) { ans = mid ; hi = mid ; } else lo = mid + 1 ; } return ans ; } int main ( ) { int a [ 5 ] = { 1 , 2 , 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( int ) ; cout << minlength ( a , n ) ; }
Find N distinct integers with zero sum | C ++ implementation to Print N distinct numbers such that their sum is 0 ; Function to print distinct n numbers such that their sum is 0 ; Print 2 symmetric numbers ; print a extra 0 if N is odd ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNumbers ( int N ) { for ( int i = 1 ; i <= N / 2 ; i ++ ) { cout << i << " , ▁ " << - i << " , ▁ " ; } if ( N % 2 == 1 ) cout << 0 ; } int main ( ) { int N = 10 ; findNumbers ( N ) ; }
Program to calculate Electricity Bill | C ++ implementation to calculate the electricity bill ; Function to calculate the electricity bill ; Condition to find the charges bar in which the units consumed is fall ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateBill ( int units ) { if ( units <= 100 ) { return units * 10 ; } else if ( units <= 200 ) { return ( 100 * 10 ) + ( units - 100 ) * 15 ; } else if ( units <= 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( units - 200 ) * 20 ; } else if ( units > 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( 100 * 20 ) + ( units - 300 ) * 25 ; } return 0 ; } int main ( ) { int units = 250 ; cout << calculateBill ( units ) ; }
Find all divisors of first N natural numbers | C ++ implementation to find all the divisors of the first N natural numbers ; Function to find the factors of the numbers from 1 to N ; Loop to find the factors of the first N natural numbers of the integer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void factors ( int n ) { int i , j ; cout << "1 ▁ - - > 1 STRNEWLINE " ; for ( i = 2 ; i <= n ; i ++ ) { cout << i << " ▁ - - > " ; for ( j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { cout << j << " , ▁ " ; if ( i / j != j ) cout << i / j << " , ▁ " ; } } cout << " STRNEWLINE " ; } } int main ( ) { int n = 5 ; factors ( n ) ; }
Find all divisors of first N natural numbers | C ++ implementation to find the factors of first N natural numbers ; Initialize global divisor vector array of sequence container ; Calculate all divisors of number ; Function to find the factors of first n natural numbers ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1e5 ; vector < int > divisor [ MAX + 1 ] ; void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . push_back ( i ) ; } } void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { cout << i << " - - > " ; for ( auto & divi : divisor [ i ] ) { cout << divi << " , ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int n = 5 ; sieve ( ) ; findNFactors ( n ) ; }
Maximum number of prime factors a number can have with exactly x factors | C ++ implementation to find the maximum count of the prime factors by the count of factors of number ; Function to count number of prime factors of x ; Count variable is incremented for every prime factor of x ; Loop to count the number of the prime factors of the given number ; Driver Code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int countPrimeFactors ( int n ) { if ( n == 1 ) return 0 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { cnt ++ ; n = n / i ; } } if ( n > 2 ) cnt ++ ; return cnt ; } int main ( ) { int x = 8 ; int prime_factor_cnt = countPrimeFactors ( x ) ; cout << prime_factor_cnt << endl ; return 0 ; }
Count of N digit palindromic numbers divisible by 9 | C ++ implementation to count the number of N digit palindromic numbers divisible by 9 ; Function to find the count of N digits palindromic numbers which are divisible by 9 ; if N is odd ; if N is even ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPalindromic ( int n ) { int count ; if ( n % 2 == 1 ) { count = pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } int main ( ) { int n = 3 ; cout << countPalindromic ( n ) ; return 0 ; }
Count of sub | C ++ program to find the count of sub - arrays with odd product ; Function that returns the count of sub - arrays with odd product ; Initialize the count variable ; Initialize variable to store the last index with even number ; Initialize variable to store count of continuous odd numbers ; Loop through the array ; Check if the number is even or not ; Calculate count of continuous odd numbers ; Increase the count of sub - arrays with odd product ; Store the index of last even number ; N considered as index of even number ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubArrayWithOddProduct ( int * A , int N ) { int count = 0 ; int last = -1 ; int K = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { K = ( i - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; last = i ; } } K = ( N - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; return count ; } int main ( ) { int arr [ ] = { 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubArrayWithOddProduct ( arr , n ) ; return 0 ; }
Calculate the CGPA and CGPA % of marks obtained by a Student in N subjects | C ++ program to calculate the CGPA and CGPA percentage of a student ; Variable to store the grades in every subject ; Variables to store CGPA and the sum of all the grades ; Computing the grades ; Computing the sum of grades ; Computing the CGPA ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double CgpaCalc ( double marks [ ] , int n ) { double grade [ n ] ; double cgpa , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { grade [ i ] = ( marks [ i ] / 10 ) ; } for ( int i = 0 ; i < n ; i ++ ) { sum += grade [ i ] ; } cgpa = sum / n ; return cgpa ; } int main ( ) { int n = 5 ; double marks [ ] = { 90 , 80 , 70 , 80 , 90 } ; double cgpa = CgpaCalc ( marks , n ) ; cout << " CGPA ▁ = ▁ " ; printf ( " % .1f STRNEWLINE " , cgpa ) ; cout << " CGPA ▁ Percentage ▁ = ▁ " ; printf ( " % .2f " , cgpa * 9.5 ) ; }
Find the subarray of size K with minimum XOR | C ++ implementation to find the subarray with minimum XOR ; Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Driver Code ; int k = 3 ; Subarray size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinXORSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } cout << min_xor << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int n = sizeof arr / sizeof arr [ 0 ] ; findMinXORSubarray ( arr , n , k ) ; return 0 ; }
Greatest number that can be formed from a pair in a given Array | C ++ implementation to find the greatest number from the given pairs of the array ; Function to find the greatest number formed from the pairs ; first append Y at the end of X ; then append X at the end of Y ; Now see which of the two formed numbers is greater than other ; Function to find pairs from array ; Iterate through all pairs ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string getNumber ( int a , int b ) { string X = to_string ( a ) ; string Y = to_string ( b ) ; string XY = X + Y ; string YX = Y + X ; return XY > YX ? XY : YX ; } void printMaxPair ( int arr [ ] , int n ) { int largest = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int number = stoi ( getNumber ( arr [ i ] , arr [ j ] ) ) ; largest = max ( largest , number ) ; } cout << largest ; } int main ( ) { int a [ ] = { 23 , 14 , 16 , 25 , 3 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; printMaxPair ( a , n ) ; return 0 ; }
Check whether two numbers are in golden ratio | C ++ implementation to check whether two numbers are in golden ratio with each other ; Function to check that two numbers are in golden ratio ; Swapping the numbers such that A contains the maximum number between these numbers ; First Ratio ; Second Ratio ; Condition to check that two numbers are in golden ratio ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkGoldenRatio ( float a , float b ) { if ( a <= b ) { float temp = a ; a = b ; b = temp ; } std :: stringstream ratio1 ; ratio1 << std :: fixed << std :: setprecision ( 3 ) << ( a / b ) ; std :: stringstream ratio2 ; ratio2 << std :: fixed << std :: setprecision ( 3 ) << ( a + b ) / a ; if ( ( ratio1 . str ( ) == ratio2 . str ( ) ) && ratio1 . str ( ) == "1.618" ) { cout << " Yes " << endl ; return true ; } else { cout << " No " << endl ; return false ; } } int main ( ) { float a = 0.618 ; float b = 1 ; checkGoldenRatio ( a , b ) ; return 0 ; }
Find the minimum number to be added to N to make it a power of K | C ++ program to find the minimum number to be added to N to make it a power of K ; Function to return the minimum number to be added to N to make it a power of K . ; Computing the difference between then next greater power of K and N ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE using namespace std ; int minNum ( int n , int k ) { int x = ( int ) ( log ( n ) / log ( k ) ) + 1 ; int mn = pow ( k , x ) - n ; return mn ; } int main ( ) { int n = 20 , k = 5 ; cout << minNum ( n , k ) ; return 0 ; }
Previous perfect square and cube number smaller than number N | C ++ implementation to find the previous perfect square and cube smaller than the given number ; Function to find the previous perfect square of the number N ; If N is already a perfect square decrease prevN by 1. ; Function to find the previous perfect cube ; If N is already a perfect cube decrease prevN by 1. ; Driver Code
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int previousPerfectSquare ( int N ) { int prevN = floor ( sqrt ( N ) ) ; if ( prevN * prevN == N ) prevN -= 1 ; return prevN * prevN ; } int previousPerfectCube ( int N ) { int prevN = floor ( cbrt ( N ) ) ; if ( prevN * prevN * prevN == N ) prevN -= 1 ; return prevN * prevN * prevN ; } int main ( ) { int n = 30 ; cout << previousPerfectSquare ( n ) << " STRNEWLINE " ; cout << previousPerfectCube ( n ) << " STRNEWLINE " ; return 0 ; }
Find the count of even odd pairs in a given Array | C ++ program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; variable to store count of such pairs ; Iterate through all pairs ; Increment count if condition is satisfied ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] % 2 == 0 ) && ( arr [ j ] % 2 == 1 ) ) { res ++ ; } return res ; } int main ( ) { int a [ ] = { 5 , 4 , 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findCount ( a , n ) ; return 0 ; }
Find the count of even odd pairs in a given Array | C ++ program to count the pairs in array of the form ( even , odd ) ; Function to count the pairs in array of the form ( even , odd ) ; check if number is even or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCount ( int arr [ ] , int n ) { int count = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count ++ ; else ans = ans + count ; } return ans ; } int main ( ) { int a [ ] = { 5 , 4 , 1 , 2 , 3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findCount ( a , n ) ; return 0 ; }
Product of all non repeating Subarrays of an Array | C ++ program to find the product of all non - repeating Subarrays of an Array ; Function to find the product of all non - repeating Subarrays of an Array ; Finding the occurrence of every element ; Iterating through the array and finding the product ; We are taking the power of each element in array with the occurrence and then taking product of those . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long product ( int arr [ ] , int n ) { double occurrence = pow ( 2 , n - 1 ) ; double product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product *= pow ( arr [ i ] , occurrence ) ; } return ( long ) product ; } int main ( ) { int arr [ ] = { 10 , 3 , 7 } ; int len = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << product ( arr , len ) ; return 0 ; }
Sum of all N | C ++ program for the above approach ; Function to find a ^ b efficiently ; Base Case ; To store the value of a ^ b ; Multiply temp by a until b is not 0 ; Return the final ans a ^ b ; Function to find sum of all N - digit palindromic number divisible by 9 ; Base Case ; If N is even , decrease ways by 1 ; Find the total number of ways ; Iterate over [ 1 , N ] and find the sum at each index ; Print the final Sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int MOD = 1000000007 ; long long int power ( long long int a , long long int b ) { if ( b == 0 ) return 1 ; long long int temp = power ( a , b / 2 ) ; temp = ( temp * temp ) % MOD ; if ( b % 2 != 0 ) { temp = ( temp * a ) % MOD ; } return temp ; } void palindromicSum ( int N ) { long long int sum = 0 , res , ways ; if ( N == 1 ) { cout << "9" << endl ; return ; } if ( N == 2 ) { cout << "99" << endl ; return ; } ways = N / 2 ; if ( N % 2 == 0 ) ways -- ; res = power ( 9 , ways - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum = sum * 10 + 45 * res ; sum %= MOD ; } cout << sum << endl ; } int main ( ) { int N = 3 ; palindromicSum ( N ) ; return 0 ; }
Maximize array sum by concatenating corresponding elements of given two arrays | C ++ implementation to find the maximum array sum by concatenating corresponding elements of given two arrays ; Function to join the two numbers ; Loop to reverse the digits of the one number ; Loop to join two numbers ; Function to find the maximum array sum ; Loop to iterate over the two elements of the array ; Find the array sum ; Return the array sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int joinNumbers ( int numA , int numB ) { int revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = numB / 10 ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = revB / 10 ; } return numA ; } int findMaxSum ( int A [ ] , int B [ ] , int n ) { int maxArr [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { int X = joinNumbers ( A [ i ] , B [ i ] ) ; int Y = joinNumbers ( B [ i ] , A [ i ] ) ; int mx = max ( X , Y ) ; maxArr [ i ] = mx ; } int maxAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } int main ( ) { int N = 5 ; int A [ 5 ] = { 11 , 23 , 38 , 43 , 59 } ; int B [ 5 ] = { 36 , 24 , 17 , 40 , 56 } ; cout << findMaxSum ( A , B , N ) ; }
Split N natural numbers into two sets having GCD of their sums greater than 1 | C ++ program to split N natural numbers into two sets having GCD of their sums greater than 1 ; Function to create and print the two sets ; For n <= 2 such sets can never be formed ; Check if N is even or odd and store the element which divides the sum of N natural numbers accordingly ; First set ; Print elements of second set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE void createSets ( ll n ) { if ( n <= 2 ) { cout << " - 1" ; return ; } else { ll x = ( n % 2 == 0 ) ? ( n / 2 ) : ( ( n + 1 ) / 2 ) ; cout << x << endl ; for ( ll i = 1 ; i <= n ; i ++ ) { if ( i == x ) continue ; cout << i << " ▁ " ; } } return ; } int main ( ) { ll N = 7 ; createSets ( N ) ; return 0 ; }
Count the factors of K present in the given Array | C ++ implementation to find the count of factors of K present in array ; Function to find the count of factors of K present in array ; Loop to consider every element of array ; Driver Code ; Function Call
#include <iostream> NEW_LINE using namespace std ; int calcCount ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 ) count ++ ; } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 6 ; cout << calcCount ( arr , n , k ) ; return 0 ; }
Multiply N complex numbers given as strings | C ++ Program to multiply N complex Numbers ; Function which returns the string in digit format ; a : real b : imaginary ; sa : sign of a sb : sign of b ; Extract the real number ; Extract the imaginary part ; if size == 1 means we reached at result ; Extract the first two elements ; Remove them ; Calculate and store the real part ; Calculate and store the imaginary part ; Append the real part ; Append the imaginary part ; Insert into vector ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE vector < long long int > findnum ( string s1 ) { vector < long long int > v ; int a = 0 , b = 0 ; int sa = 0 , sb = 0 , i = 0 ; if ( s1 [ 0 ] == ' - ' ) { sa = 1 ; i = 1 ; } while ( isdigit ( s1 [ i ] ) ) { a = a * 10 + ( int ( s1 [ i ] ) - 48 ) ; i ++ ; } if ( s1 [ i ] == ' + ' ) { sb = 0 ; i += 1 ; } if ( s1 [ i ] == ' - ' ) { sb = 1 ; i += 1 ; } while ( i < s1 . length ( ) && isdigit ( s1 [ i ] ) ) { b = b * 10 + ( int ( s1 [ i ] ) - 48 ) ; i ++ ; } if ( sa ) a *= -1 ; if ( sb ) b *= -1 ; v . push_back ( a ) ; v . push_back ( b ) ; return v ; } string complexNumberMultiply ( vector < string > v ) { while ( v . size ( ) != 1 ) { vector < ll > v1 = findnum ( v [ 0 ] ) ; vector < ll > v2 = findnum ( v [ 1 ] ) ; v . erase ( v . begin ( ) ) ; v . erase ( v . begin ( ) ) ; ll r = ( v1 [ 0 ] * v2 [ 0 ] - v1 [ 1 ] * v2 [ 1 ] ) ; ll img = v1 [ 0 ] * v2 [ 1 ] + v1 [ 1 ] * v2 [ 0 ] ; string res = " " ; res += to_string ( r ) ; res += ' + ' ; res += to_string ( img ) + ' i ' ; v . insert ( v . begin ( ) , res ) ; } return v [ 0 ] ; } int main ( ) { int n = 3 ; vector < string > v = { "3 + 1i " , "2 + 1i " , " - 5 + - 7i " } ; cout << complexNumberMultiply ( v ) << " STRNEWLINE " ; return 0 ; }