codeparrot/xlcost-text-to-code huggingface dataset:
Program to find Circumcenter of a Triangle | C ++ program to find the CIRCUMCENTER of a triangle ; This pair is used to store the X and Y coordinate of a point respectively ; Function to find the line given two points ; Function which converts the input line to its perpendicular bisector . It also inputs the points whose mid - point lies on the bisector ; c = - bx + ay ; Returns the intersection point of two lines ; The lines are parallel . This is simplified by returning a pair of FLT_MAX ; Line PQ is represented as ax + by = c ; Line QR is represented as ex + fy = g ; Converting lines PQ and QR to perpendicular vbisectors . After this , L = ax + by = c M = ex + fy = g ; The point of intersection of L and M gives the circumcenter ; Driver code .
#include <iostream> NEW_LINE #include <cfloat> NEW_LINE using namespace std ; #define pdd  pair<double, double> NEW_LINE void lineFromPoints ( pdd P , pdd Q , double & a , double & b , double & c ) { a = Q . second - P . second ; b = P . first - Q . first ; c = a * ( P . first ) + b * ( P . second ) ; } void perpendicularBisectorFromLine ( pdd P , pdd Q , double & a , double & b , double & c ) { pdd mid_point = make_pair ( ( P . first + Q . first ) / 2 , ( P . second + Q . second ) / 2 ) ; c = - b * ( mid_point . first ) + a * ( mid_point . second ) ; double temp = a ; a = - b ; b = temp ; } pdd lineLineIntersection ( double a1 , double b1 , double c1 , double a2 , double b2 , double c2 ) { double determinant = a1 * b2 - a2 * b1 ; if ( determinant == 0 ) { return make_pair ( FLT_MAX , FLT_MAX ) ; } else { double x = ( b2 * c1 - b1 * c2 ) / determinant ; double y = ( a1 * c2 - a2 * c1 ) / determinant ; return make_pair ( x , y ) ; } } void findCircumCenter ( pdd P , pdd Q , pdd R ) { double a , b , c ; lineFromPoints ( P , Q , a , b , c ) ; double e , f , g ; lineFromPoints ( Q , R , e , f , g ) ; perpendicularBisectorFromLine ( P , Q , a , b , c ) ; perpendicularBisectorFromLine ( Q , R , e , f , g ) ; pdd circumcenter = lineLineIntersection ( a , b , c , e , f , g ) ; if ( circumcenter . first == FLT_MAX && circumcenter . second == FLT_MAX ) { cout << " The ▁ two ▁ perpendicular ▁ bisectors ▁ " " found ▁ come ▁ parallel " << endl ; cout << " Thus , ▁ the ▁ given ▁ points ▁ do ▁ not ▁ form ▁ " " a ▁ triangle ▁ and ▁ are ▁ collinear " << endl ; } else { cout << " The ▁ circumcenter ▁ of ▁ the ▁ triangle ▁ PQR ▁ is : ▁ " ; cout << " ( " << circumcenter . first << " , ▁ " << circumcenter . second << " ) " << endl ; } } int main ( ) { pdd P = make_pair ( 6 , 0 ) ; pdd Q = make_pair ( 0 , 0 ) ; pdd R = make_pair ( 0 , 8 ) ; findCircumCenter ( P , Q , R ) ; return 0 ; }
Program to find area of a triangle | C ++ program to evaluate area of a polygon using shoelace formula ; ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; j = i ; j is previous vertex to i ; Return absolute value ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; double polygonArea ( double X [ ] , double Y [ ] , int n ) { double area = 0.0 ; int j = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ; } return abs ( area / 2.0 ) ; } int main ( ) { double X [ ] = { 0 , 2 , 4 } ; double Y [ ] = { 1 , 3 , 7 } ; int n = sizeof ( X ) / sizeof ( X [ 0 ] ) ; cout << polygonArea ( X , Y , n ) ; }
Maximize sum of LSBs of Bitwise OR of all possible N / 2 pairs from given Array | C ++ program for the above approach ; Function top get LSB value of v ; Binary conversion ; Function to find the sum of LSBs of all possible pairs of the given array ; Stores the LSB of array elements ; Storing the LSB values ; Sort the array lab_arr [ ] ; Taking pairwise sum to get the maximum sum of LSB ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int chk ( int n ) { vector < int > v ; while ( n != 0 ) { v . push_back ( n % 2 ) ; n = n / 2 ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] == 1 ) { return pow ( 2 , i ) ; } } return 0 ; } void sumOfLSB ( int arr [ ] , int N ) { vector < int > lsb_arr ; for ( int i = 0 ; i < N ; i ++ ) { lsb_arr . push_back ( chk ( arr [ i ] ) ) ; } sort ( lsb_arr . begin ( ) , lsb_arr . end ( ) , greater < int > ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i += 2 ) { ans += ( lsb_arr [ i + 1 ] ) ; } cout << ( ans ) ; } int main ( ) { int N = 5 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; sumOfLSB ( arr , N ) ; }
Count of subsequences having odd Bitwise AND values in the given array | C ++ program for the above approach ; Function to find count of subsequences having odd bitwise AND value ; Stores count of odd elements ; Traverse the array arr [ ] ; If x is odd increment count ; Return Answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSubsequences ( vector < int > arr ) { int odd = 0 ; for ( int x : arr ) { if ( x & 1 ) odd ++ ; } return ( 1 << odd ) - 1 ; } int main ( ) { vector < int > arr = { 1 , 3 , 3 } ; cout << countSubsequences ( arr ) ; return 0 ; }
Count pairs from an array with absolute difference not less than the minimum element in the pair | C ++ program for the above approach ; Function to find the number of pairs ( i , j ) such that abs ( a [ i ] - a [ j ] ) is at least the minimum of ( a [ i ] , a [ j ] ) ; Stores the resultant count of pairs ; Iterate over the range [ 0 , n ] ; Iterate from arr [ i ] - ( i % arr [ i ] ) till n with an increment of arr [ i ] ; Count the possible pairs ; Return the total count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getPairsCount ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = arr [ i ] - ( i % arr [ i ] ) ; j < n ; j += arr [ i ] ) { if ( i < j && abs ( arr [ i ] - arr [ j ] ) >= min ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getPairsCount ( arr , N ) ; return 0 ; }
Minimum steps to change N to 1 by changing it to 2 * N or N / 10 at any step | C ++ program for the above approach ; Function to check if N can be changed to 1 or not . ; Count the number of 2 in the prime factorisation of N ; Count the number of 5 in the prime factorisation of N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check ( int N ) { int twos = 0 , fives = 0 ; while ( N % 2 == 0 ) { N /= 2 ; twos ++ ; } while ( N % 5 == 0 ) { N /= 5 ; fives ++ ; } if ( N == 1 && twos <= fives ) { cout << 2 * fives - twos ; } else { cout << -1 ; } } int main ( ) { int N = 50 ; check ( N ) ; return 0 ; }
Sum of elements in given range from Array formed by infinitely concatenating given array | C ++ program for the above approach ; Function to find the sum of elements in a given range of an infinite array ; Stores the sum of array elements from L to R ; Traverse from L to R ; Print the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rangeSum ( int arr [ ] , int N , int L , int R ) { int sum = 0 ; for ( int i = L - 1 ; i < R ; i ++ ) { sum += arr [ i % N ] ; } cout << sum ; } int main ( ) { int arr [ ] = { 5 , 2 , 6 , 9 } ; int L = 10 , R = 13 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rangeSum ( arr , N , L , R ) ; return 0 ; }
Sum of elements in given range from Array formed by infinitely concatenating given array | C ++ program for the above approach ; Function to find the sum of elements in a given range of an infinite array ; Stores the prefix sum ; Calculate the prefix sum ; Stores the sum of elements from 1 to L - 1 ; Stores the sum of elements from 1 to R ; Print the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rangeSum ( int arr [ ] , int N , int L , int R ) { int prefix [ N + 1 ] ; prefix [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i - 1 ] ; } int leftsum = ( ( L - 1 ) / N ) * prefix [ N ] + prefix [ ( L - 1 ) % N ] ; int rightsum = ( R / N ) * prefix [ N ] + prefix [ R % N ] ; cout << rightsum - leftsum ; } int main ( ) { int arr [ ] = { 5 , 2 , 6 , 9 } ; int L = 10 , R = 13 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rangeSum ( arr , N , L , R ) ; return 0 ; }
Exponential factorial of N | C ++ program for the above approach ; Function to find exponential factorial of a given number ; Stores the exponetial factor of N ; Iterare over the range [ 2 , N ] ; Update res ; Return res ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ExpoFactorial ( int N ) { int res = 1 ; int mod = 1000000007 ; for ( int i = 2 ; i < N + 1 ; i ++ ) res = ( int ) pow ( i , res ) % mod ; return res ; } int main ( ) { int N = 4 ; cout << ( ExpoFactorial ( N ) ) ;
Maximum subarray sum in an array created after repeated concatenation | Set | C ++ program for the above approach ; Function to find contiguous subarray with maximum sum if array is repeated K times ; Store the sum of the array arr [ ] ; Traverse the array and find sum ; Store the answer ; If K = 1 ; Apply Kadane algorithm to find sum ; Return the answer ; Stores the twice repeated array ; Traverse the range [ 0 , 2 * N ] ; Stores the maximum suffix sum ; Stores the maximum prefix sum ; Apply Kadane algorithm for 2 repetition of the array ; If the sum of the array is greater than 0 ; Return the answer ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubArraySumRepeated ( int arr [ ] , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; int curr = arr [ 0 ] ; int ans = arr [ 0 ] ; if ( K == 1 ) { for ( int i = 1 ; i < N ; i ++ ) { curr = max ( arr [ i ] , curr + arr [ i ] ) ; ans = max ( ans , curr ) ; } return ans ; } vector < int > V ; for ( int i = 0 ; i < 2 * N ; i ++ ) { V . push_back ( arr [ i % N ] ) ; } int maxSuf = V [ 0 ] ; int maxPref = V [ 2 * N - 1 ] ; curr = V [ 0 ] ; for ( int i = 1 ; i < 2 * N ; i ++ ) { curr += V [ i ] ; maxPref = max ( maxPref , curr ) ; } curr = V [ 2 * N - 1 ] ; for ( int i = 2 * N - 2 ; i >= 0 ; i -- ) { curr += V [ i ] ; maxSuf = max ( maxSuf , curr ) ; } curr = V [ 0 ] ; for ( int i = 1 ; i < 2 * N ; i ++ ) { curr = max ( V [ i ] , curr + V [ i ] ) ; ans = max ( ans , curr ) ; } if ( sum > 0 ) { int temp = 1LL * sum * ( K - 2 ) ; ans = max ( ans , max ( temp + maxPref , temp + maxSuf ) ) ; } return ans ; } int main ( ) { int arr [ ] = { 10 , 20 , -30 , -1 , 40 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 10 ; cout << maxSubArraySumRepeated ( arr , N , K ) ; return 0 ; }
Count of subarrays with largest element at least twice the largest of remaining elements | C ++ program for the above approach ; Function to find count of subarrays which have max element greater than twice maximum of all other elements ; Stores the count of subarrays ; Generate all possible subarrays ; Stores the maximum element of the subarray ; Stores the maximum of all other elements ; Find the maximum element in the subarray [ i , j ] ; Find the maximum of all other elements ; If the maximum of subarray is greater than twice the maximum of other elements ; Print the maximum value obtained ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void countSubarray ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int mxSubarray = 0 ; int mxOther = 0 ; for ( int k = i ; k <= j ; k ++ ) { mxSubarray = max ( mxSubarray , arr [ k ] ) ; } for ( int k = 0 ; k < i ; k ++ ) { mxOther = max ( mxOther , arr [ k ] ) ; } for ( int k = j + 1 ; k < n ; k ++ ) { mxOther = max ( mxOther , arr [ k ] ) ; } if ( mxSubarray > ( 2 * mxOther ) ) count ++ ; } } cout << count ; } int main ( ) { int arr [ ] = { 1 , 6 , 10 , 9 , 7 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countSubarray ( arr , N ) ; return 0 ; }
Count of subarrays with largest element at least twice the largest of remaining elements | C ++ program for the above approach ; Function to find count of subarrays which have max element greater than twice maximum of all other elements ; Stores the maximum element of the array ; Traverse the given array ; If the value of 2 * arr [ i ] is greater than mx ; Update the value of L and break out of loop ; If the value 2 * arr [ i ] is greater than mx ; Update the value of R and break out of loop ; Print the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countSubarray ( int arr [ ] , int n ) { int count = 0 , L = 0 , R = 0 ; int mx = * max_element ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] * 2 > mx ) { L = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] * 2 > mx ) { R = i ; break ; } } cout << ( L + 1 ) * ( n - R ) ; } int main ( ) { int arr [ ] = { 1 , 6 , 10 , 9 , 7 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countSubarray ( arr , N ) ; return 0 ; }
Find Prime number just less than and just greater each element of given Array | C ++ program for the above approach ; Utility function to check for primality of a number X by checking whether X haACCs any factors other than 1 and itself . ; if ( X % i == 0 ) Factor found ; Function to print primes just less than and just greater than of each element in an array ; Traverse the array ; Traverse for finding prime just less than A [ i ] ; Prime just less than A [ i ] found ; Traverse for finding prime just greater than A [ i ] ; Prime just greater than A [ i ] found ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int X ) { for ( int i = 2 ; i * i <= X ; i ++ ) return false ; return true ; } void printPrimes ( int A [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] - 1 ; ; j -- ) { if ( isPrime ( j ) ) { cout << j << " ▁ " ; break ; } } for ( int j = A [ i ] + 1 ; ; j ++ ) { if ( isPrime ( j ) ) { cout << j << " ▁ " ; break ; } } cout << endl ; } } int main ( ) { int A [ ] = { 17 , 28 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; printPrimes ( A , N ) ; return 0 ; }
Kth smallest element in an array that contains A [ i ] exactly B [ i ] times | C ++ program for the above approach ; Function to find the Kth smallest element that contains A [ i ] exactly B [ i ] times ; Traverse the given array ; Stores the frequency of every elements ; Traverse the given array ; Initialize a variable to store the prefix sums ; Iterate over the range [ 0 , M ] ; Increment sum by freq [ i ] ; If sum is greater than or equal to K ; Return the current element as answer ; Return - 1 ; Driver Code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int KthSmallest ( int A [ ] , int B [ ] , int N , int K ) { int M = 0 ; for ( int i = 0 ; i < N ; i ++ ) { M = max ( A [ i ] , M ) ; } int freq [ M + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { freq [ A [ i ] ] += B [ i ] ; } int sum = 0 ; for ( int i = 0 ; i <= M ; i ++ ) { sum += freq [ i ] ; if ( sum >= K ) { return i ; } } return -1 ; } int main ( ) { int A [ ] = { 3 , 4 , 5 } ; int B [ ] = { 2 , 1 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int K = 4 ; cout << KthSmallest ( A , B , N , K ) ; return 0 ; }
Bitwise OR of Bitwise AND of all subarrays of an array | C ++ program for the above approach ; Function to find the Bitwise OR of Bitwise AND of all subarrays ; Stores the required result ; Generate all the subarrays ; Store the current element ; Find the Bitwise OR ; Update the result ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findbitwiseOR ( int * a , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sub_array = a [ i ] ; res = res | curr_sub_array ; for ( int j = i ; j < n ; j ++ ) { curr_sub_array = curr_sub_array & a [ j ] ; res = res | curr_sub_array ; } } cout << res ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findbitwiseOR ( A , N ) ; return 0 ; }
Bitwise OR of Bitwise AND of all subarrays of an array | C ++ program for the above approach ; Function to find the Bitwise OR of Bitwise AND of all consecutive subsets of the array ; Stores the required result ; Traverse the given array ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findbitwiseOR ( int * a , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = res | a [ i ] ; cout << res ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findbitwiseOR ( A , N ) ; return 0 ; }
Check if sum of digits of a number exceeds the product of digits of that number | C ++ program for the above approach ; Function to check if the sum of the digits of N is strictly greater than the product of the digits of N or not ; Stores the sum and the product of the digits of N ; Stores the last digit if N ; Increment the value of sumOfDigits ; Update the prodOfDigit ; Divide N by 10 ; Print the result ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void check ( int n ) { int sumOfDigit = 0 ; int prodOfDigit = 1 ; while ( n > 0 ) { int rem ; rem = n % 10 ; sumOfDigit += rem ; prodOfDigit *= rem ; n /= 10 ; } if ( sumOfDigit > prodOfDigit ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int N = 1234 ; check ( N ) ; return 0 ; }
Print all distinct even and odd prefix Bitwise XORs of first N natural numbers | C ++ program for the above approach ; Print all distinct even & odd prefix Bitwise XORs from 1 to N ; Print the even number ; Print the odd number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void evenOddBitwiseXOR ( int N ) { cout << " Even : ▁ " << 0 << " ▁ " ; for ( int i = 4 ; i <= N ; i = i + 4 ) { cout << i << " ▁ " ; } cout << " STRNEWLINE " ; cout << " Odd : ▁ " << 1 << " ▁ " ; for ( int i = 4 ; i <= N ; i = i + 4 ) { cout << i - 1 << " ▁ " ; } if ( N % 4 == 2 ) cout << N + 1 ; else if ( N % 4 == 3 ) cout << N ; } int main ( ) { int N = 6 ; evenOddBitwiseXOR ( N ) ; return 0 ; }
Lexicographically largest permutation possible by a swap that is smaller than a given array | C ++ program for the above approach ; Function to lexicographic largest permutation possible by a swap that is smaller than given array ; Find the index of first element such that arr [ i ] > arr [ i + 1 ] ; If the array is sorted in increasing order ; Find the index of first element which is smaller than arr [ i ] ; If arr [ j ] = = arr [ j - 1 ] ; Decrement j ; Swap the element ; Print the array arr [ ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPermutation ( vector < int > & arr ) { int N = arr . size ( ) ; int i = N - 2 ; while ( i >= 0 && arr [ i ] <= arr [ i + 1 ] ) i -- ; if ( i == -1 ) { cout << " - 1" ; return ; } int j = N - 1 ; while ( j > i && arr [ j ] >= arr [ i ] ) j -- ; while ( j > i && arr [ j ] == arr [ j - 1 ] ) { j -- ; } swap ( arr [ i ] , arr [ j ] ) ; for ( auto & it : arr ) { cout << it << ' ▁ ' ; } } int main ( ) { vector < int > arr = { 1 , 2 , 5 , 3 , 4 , 6 } ; findPermutation ( arr ) ; return 0 ; }
Absolute difference between the count of odd and even factors of N | C ++ program for the above approach ; Function to find the smallest prime factor of all the numbers using Sieve Of Eratosthenes ; Stores whether any number is prime or not ; Initialize smallest factor as 2 for all the even numbers ; Iterate over the range [ 3 , N ] ; If i is prime ; Iterate all multiples of i ; i is the smallest prime factor of i * j ; Function to find the absolute difference between the count of odd and even factors of N ; Stores the smallest prime factor of i ; Fill values in s [ ] using sieve of eratosthenes ; Stores the total number of factors and the total number of odd and even factors ; Store the current prime factor of the number N ; Store the power of current prime factor ; Loop while N is greater than 1 ; If N also has smallest prime factor as curr , then increment cnt by 1 ; Update only total number of factors if curr is 2 ; Update total number of factors and total number of odd factors ; Update current prime factor as s [ N ] and count as 1 ; Calculate the number of even factors ; Print the difference ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sieveOfEratosthenes ( int N , int s [ ] ) { vector < bool > prime ( N + 1 , false ) ; for ( int i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= N ; j += 2 ) { if ( ! prime [ i * j ] ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } void findDifference ( int N ) { int s [ N + 1 ] ; sieveOfEratosthenes ( N , s ) ; int total = 1 , odd = 1 , even = 0 ; int curr = s [ N ] ; int cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } if ( curr == 2 ) { total = total * ( cnt + 1 ) ; } else { total = total * ( cnt + 1 ) ; odd = odd * ( cnt + 1 ) ; } curr = s [ N ] ; cnt = 1 ; } even = total - odd ; cout << abs ( even - odd ) ; } int main ( ) { int N = 12 ; findDifference ( N ) ; return 0 ; }
Calculate Median from given values of Mean and Mode | C ++ program for the above approach ; Function to find the median of a group of data with given mean and mode ; Calculate the median ; Print the median ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMedian ( int Mean , int Mode ) { double Median = ( 2 * Mean + Mode ) / 3.0 ; cout << Median ; } int main ( ) { int mode = 6 , mean = 3 ; findMedian ( mean , mode ) ; return 0 ; }
Program to find the Magnitude of a Vector | C ++ program for the above approach ; Function to calculate magnitude of a 3 dimensional vector ; Stores the sum of squares of coordinates of a vector ; Return the magnitude ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float vectorMagnitude ( int x , int y , int z ) { int sum = x * x + y * y + z * z ; return sqrt ( sum ) ; } int main ( ) { int x = 1 ; int y = 2 ; int z = 3 ; cout << vectorMagnitude ( x , y , z ) ; return 0 ; }
Program to find the product of a number with a Mersenne Number | C ++ implementation of above approach ; Function to find prodcut of a Mersenne number with another number ; Stores the power of 2 of integer M + 1 ; Return the product ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long multiplyByMersenne ( long N , long M ) { long x = log2 ( M + 1 ) ; return ( ( N << x ) - N ) ; } int main ( ) { long N = 4 ; long M = 15 ; cout << multiplyByMersenne ( N , M ) ; return 0 ; }
Nearest power of 2 of nearest perfect squares of non | C ++ program for the above approach ; Function to find nearest perfect square of num ; Calculate square root of num ; Calculate perfect square ; Find the nearest perfect square ; Function to find the power of 2 nearest to the number num ; Calculate log base 2 of num ; Highest power of 2 which is <= num ; Function to find the nearest perfect square and the nearest power of 2 of every array element whose occurrence is 1 ; Stores frequency of array elements ; Traverse the array and update frequency of current array element ; Traverse the map freq ; If the frequency is 1 ; Find nearest perfect square ; Print the nearest power of 2 ; If the any does not contain any non - repeating elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int perfectSquare ( int num ) { int sr = sqrt ( num ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( num - a ) < ( b - num ) ) { return a ; } else { return b ; } } int powerOfTwo ( int num ) { int lg = log2 ( num ) ; int p = pow ( 2 , lg ) ; return p ; } void uniqueElement ( int arr [ ] , int N ) { bool ans = true ; unordered_map < int , int > freq ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( auto el : freq ) { if ( el . second == 1 ) { ans = false ; int ps = perfectSquare ( el . first ) ; cout << powerOfTwo ( ps ) << ' ▁ ' ; } } if ( ans ) cout << " - 1" ; } int main ( ) { int arr [ ] = { 4 , 11 , 4 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; uniqueElement ( arr , N ) ; return 0 ; }