codeparrot/xlcost-text-to-code huggingface dataset:
Length of array pair formed where one contains all distinct elements and other all same elements | C ++ implementation to Divide the array into two arrays having same size , one with distinct elements and other with same elements ; Function to find the max size possible ; Counting the maximum frequency ; Counting total distinct elements ; Find max of both the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxSize ( int a [ ] , int n ) { vector < int > frq ( n + 1 ) ; for ( int i = 0 ; i < n ; ++ i ) frq [ a [ i ] ] ++ ; int maxfrq = * max_element ( frq . begin ( ) , frq . end ( ) ) ; int dist = n + 1 - count ( frq . begin ( ) , frq . end ( ) , 0 ) ; int ans1 = min ( maxfrq - 1 , dist ) ; int ans2 = min ( maxfrq , dist - 1 ) ; int ans = max ( ans1 , ans2 ) ; return ans ; } int main ( ) { int arr [ ] = { 4 , 2 , 4 , 1 , 4 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxSize ( arr , n ) ; return 0 ; }
Longest subarray with all elements same | C ++ program to find largest subarray with all equal elements . ; Function to find largest sub array with all equal elements . ; Traverse the array ; If element is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int subarray ( int arr [ ] , int n ) { int ans = 1 , temp = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { ++ temp ; } else { ans = max ( ans , temp ) ; temp = 1 ; } } ans = max ( ans , temp ) ; return ans ; } int main ( ) { int arr [ ] = { 2 , 2 , 1 , 1 , 2 , 2 , 2 , 3 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << subarray ( arr , n ) ; return 0 ; }
Min and max length subarray having adjacent element difference atmost K | C ++ program to find the minimum and maximum length subarray such that difference between adjacent elements is atmost K ; Function to find the maximum and minimum length subarray ; Initialise minimum and maximum size of subarray in worst case ; Left will scan the size of possible subarray in left of selected element ; Right will scan the size of possible subarray in right of selected element ; Temp will store size of group associateed with every element ; Loop to find size of subarray for every element of array ; Left will move in left direction and compare difference between itself and element left to it ; right will move in right direction and compare difference between itself and element right to it ; if subarray of much lesser or much greater is found than yet known then update ; Print minimum and maximum possible size of subarray ; Driver Code ; function call to find maximum and minimum possible sub array
#include <iostream> NEW_LINE using namespace std ; void findMaxMinSubArray ( int arr [ ] , int K , int n ) { int min = n ; int max = 0 ; int left ; int right ; int tmp ; for ( int i = 0 ; i < n ; i ++ ) { tmp = 1 ; left = i ; while ( left - 1 >= 0 && abs ( arr [ left ] - arr [ left - 1 ] ) <= K ) { left -- ; tmp ++ ; } right = i ; while ( right + 1 <= n - 1 && abs ( arr [ right ] - arr [ right + 1 ] ) <= K ) { right ++ ; tmp ++ ; } if ( min > tmp ) min = tmp ; if ( max < tmp ) max = tmp ; } cout << min << " , ▁ " << max << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 6 , 7 } ; int K = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMaxMinSubArray ( arr , K , n ) ; return 0 ; }
Count of elements which is the sum of a subarray of the given Array | C ++ implementation to count the elements such that their exist a subarray whose sum is equal to this element ; Function to count the elements such that their exist a subarray whose sum is equal to this element ; Loop to count the frequency ; Loop to iterate over every possible subarray of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countElement ( int arr [ ] , int n ) { map < int , int > freq ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { int tmpsum = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { tmpsum += arr [ j ] ; if ( freq . find ( tmpsum ) != freq . end ( ) ) { ans += freq [ tmpsum ] ; } } } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countElement ( arr , n ) << endl ; return 0 ; }
Nth root of a number using log | C ++ implementation to find the Kth root of a number using log ; Function to find the Kth root of the number using log function ; Driver Code
#include <bits/stdc++.h> NEW_LINE double kthRoot ( double n , int k ) { return pow ( k , ( 1.0 / k ) * ( log ( n ) / log ( k ) ) ) ; } int main ( void ) { double n = 81 ; int k = 4 ; printf ( " % lf ▁ " , kthRoot ( n , k ) ) ; return 0 ; }
Make A , B and C equal by adding total value N to them | C ++ program to distribute integer N among A , B , C such that they become equal ; Find maximum among the three elements ; Summation of three elements ; Check if difference is divisible by 3 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void distributeN ( int A , int B , int C , int n ) { int max1 = max ( A , B ) ; int max2 = max ( B , C ) ; int maximum = max ( max1 , max2 ) ; int sum = A + B + C ; int p = ( 3 * maximum ) - sum ; int diff = n - p ; if ( diff < 0 diff % 3 ) cout << " No " ; else cout << " Yes " ; } int main ( ) { int A = 10 , B = 20 ; int C = 15 , n = 14 ; distributeN ( A , B , C , n ) ; return 0 ; }
Represent N as sum of K odd numbers with repetitions allowed | C ++ implementation to represent N as sum of K even numbers ; Function to print the representation ; N must be greater than equal to 2 * K and must be odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumOddNumbers ( int N , int K ) { int check = N - ( K - 1 ) ; if ( check > 0 && check % 2 == 1 ) { for ( int i = 0 ; i < K - 1 ; i ++ ) { cout << "1 ▁ " ; } cout << check ; } else cout << " - 1" ; } int main ( ) { int N = 5 ; int K = 3 ; sumOddNumbers ( N , K ) ; return 0 ; }
Number of continuous reductions of A from B or B from A to make them ( 1 , 1 ) | C ++ implementation to find the minimum number of operations required to reach ( 1 , 1 ) ; Function to find the minimum number of steps required ; Condition to check if it is not possible to reach ; Condition to check if the pair is reached to 1 , 1 ; Condition to change the A as the maximum element ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSteps ( int a , int b , int c ) { if ( a < 1 b < 1 ) return -1 ; if ( a == 1 && b == 1 ) return c ; if ( a < b ) { a = a + b ; b = a - b ; a = a - b ; } return minimumSteps ( a - b , b , c + 1 ) ; } int main ( ) { int a = 75 ; int b = 17 ; cout << minimumSteps ( a , b , 0 ) << endl ; }
Longest Mountain Subarray | C ++ code for Longest Mountain Subarray ; Function to find the longest mountain subarray ; If the size of array is less than 3 , the array won 't show  mountain like behaviour ; When a new mountain sub - array is found , there is a need to set the variables k , j to - 1 in order to help calculate the length of new mountain sub - array ; j marks the starting index of a new mountain sub - array . So set the value of j to current index i . ; Checks if next element is less than current element ; Checks if starting element exists or not , if the starting element of the mountain sub - array exists then the index of ending element is stored in k ; This condition checks if both starting index and ending index exists or not , if yes , the length is calculated . ; d holds the length of the longest mountain sub - array . If the current length is greater than the calculated length , then value of d is updated . ; ignore if there is no increase or decrease in the value of the next element ; Checks and calculates the length if last element of the array is the last element of a mountain sub - array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LongestMountain ( vector < int > & a ) { int i = 0 , j = -1 , k = -1 , p = 0 , d = 0 , n = 0 ; if ( a . size ( ) < 3 ) { return 0 ; } for ( i = 0 ; i < a . size ( ) - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] ) { if ( k != -1 ) { k = -1 ; j = -1 ; } if ( j == -1 ) { j = i ; } } else { if ( a [ i + 1 ] < a [ i ] ) { if ( j != -1 ) { k = i + 1 ; } if ( k != -1 && j != -1 ) { if ( d < k - j + 1 ) { d = k - j + 1 ; } } } else { k = -1 ; j = -1 ; } } } if ( k != -1 && j != -1 ) { if ( d < k - j + 1 ) { d = k - j + 1 ; } } return d ; } int main ( ) { vector < int > d = { 1 , 3 , 1 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 7 , 6 , 5 } ; cout << LongestMountain ( d ) << endl ; return 0 ; }
Check whether an Array can be made 0 by splitting and merging repeatedly | C ++ program for the above approach ; Function that finds if it is possible to make the array contain only 1 element i . e . 0 ; Check if element is odd ; According to the logic in above approach ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string solve ( vector < int > & A ) { int i , ctr = 0 ; for ( i = 0 ; i < A . size ( ) ; i ++ ) { if ( A [ i ] % 2 ) { ctr ++ ; } } if ( ctr % 2 ) { return " No " ; } else { return " Yes " ; } } int main ( ) { vector < int > arr = { 9 , 17 } ; cout << solve ( arr ) << endl ; return 0 ; }
Count of perfect squares of given length | C ++ Program to count perfect squares of given length ; Function to check if a number is perfect square ; Find floating point value of square root of x . ; If square root is an integer ; Function to return the count of n digit perfect squares ; Initialize result ; Traverse through all numbers of n digits ; Check if current number ' i ' is perfect square ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPerfectSquare ( long double x ) { long double sr = sqrt ( x ) ; return ( ( sr - floor ( sr ) ) == 0 ) ; } int countSquares ( int n ) { int cnt = 0 ; for ( int i = pow ( 10 , ( n - 1 ) ) ; i < pow ( 10 , n ) ; i ++ ) { if ( i != 0 && isPerfectSquare ( i ) ) cnt ++ ; } return cnt ; } int main ( ) { int n = 3 ; cout << countSquares ( n ) ; return 0 ; }
Count of perfect squares of given length | C ++ Program to count perfect squares of given length ; Function to return the count of n digit perfect squares ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSquares ( int n ) { int r = ceil ( sqrt ( pow ( 10 , n ) ) ) ; int l = ceil ( sqrt ( pow ( 10 , n - 1 ) ) ) ; return r - l ; } int main ( ) { int n = 3 ; cout << countSquares ( n ) ; return 0 ; }
Sum of alternating sign cubes of first N Natural numbers | C ++ implementation to compute the sum of cubes with alternating sign ; Function to compute sum of the cubes with alternating sign ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int summation ( int N ) { int co = ( N + 1 ) / 2 ; int ce = ( N ) / 2 ; int se = 2 * ( ( ce * ( ce + 1 ) ) * ( ce * ( ce + 1 ) ) ) ; int so = ( co * co ) * ( 2 * ( ( co * co ) ) - 1 ) ; return so - se ; } int main ( ) { int n = 3 ; cout << summation ( n ) ; return 0 ; }
Program to check if N is a Star Number | C ++ implementation to check that a number is a star number or not ; Function to check that the number is a star number ; Condition to check if the number is a star number ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isStar ( int N ) { float n = ( 6 + sqrt ( 24 * N + 12 ) ) / 6 ; return ( n - ( int ) n ) == 0 ; } int main ( ) { int i = 13 ; if ( isStar ( i ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Smallest number to make Array sum at most K by dividing each element | C ++ program to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Function to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Binary search between 1 and 10 ^ 9 ; Calculating the new sum after dividing every element by mid ; If after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; Else , move mid + 1 to high ; Returning the minimum number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDivisor ( int arr [ ] , int n , int limit ) { int low = 0 , high = 1e9 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ceil ( ( double ) arr [ i ] / ( double ) mid ) ; } if ( sum <= limit ) high = mid ; else low = mid + 1 ; } return low ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 6 ; cout << findMinDivisor ( arr , N , K ) ; }
Count of triplets ( a , b , c ) in the Array such that a divides b and b divides c | C ++ program to find count of triplets ( a , b , c ) in the Array such that a divides b and b divides c ; Function to count triplets ; Iterate for middle element ; Iterate left array for a [ i ] ; Iterate right array for a [ k ] ; return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int arr [ ] , int n ) { int count = 0 ; for ( int j = 1 ; j < n - 1 ; j ++ ) { int p = 0 , q = 0 ; for ( int i = 0 ; i < j ; i ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) p ++ ; } for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ k ] % arr [ j ] == 0 ) q ++ ; } count += p * q ; } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getCount ( arr , N ) << endl ; return 0 ; }
Find pair with maximum ratio in an Array | C ++ implementation to find the maximum pair in the array ; Function to find the maximum pair possible for the array ; Loop to iterate over every possible pair in the array ; Check pair ( x , y ) as well as ( y , x ) for maximum value ; Update the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; float computeMaxValue ( float arr [ ] , int n ) { float ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { float val = max ( arr [ i ] / arr [ j ] , arr [ j ] / arr [ i ] ) ; ans = max ( ans , val ) ; } } return ans ; } int main ( ) { float arr [ ] = { 15 , 10 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << computeMaxValue ( arr , n ) ; return 0 ; }
Find the Kth number which is not divisible by N | C ++ implementation to find the K 'th non-divisible number of N ; Function to find the Kth not divisible by N ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int kthNonDivisible ( int N , int K ) { return K + floor ( ( K - 1 ) / ( N - 1 ) ) ; } int main ( ) { int N = 3 ; int K = 6 ; cout << kthNonDivisible ( N , K ) ; return 0 ; }
Find the maximum sum pair in an Array with even parity | C ++ implementation to find a pair with even parity and maximum sum ; Function that returns true if count of set bits in given number is even ; Parity will store the count of set bits ; Function to print the elements of the array ; Function to remove all the even parity elements from the given array ; Traverse the array ; If the current element has even parity ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sz = 1e3 ; bool isEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x & 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } void printArray ( int arr [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { cout << arr [ i ] << ' ▁ ' ; } } void findPairEvenParity ( int arr [ ] , int len ) { int firstMaximum = INT_MIN ; int secondMaximum = INT_MIN ; for ( int i = 0 ; i < len ; i ++ ) { if ( isEvenParity ( arr [ i ] ) ) { if ( arr [ i ] >= firstMaximum ) { secondMaximum = firstMaximum ; firstMaximum = arr [ i ] ; } else if ( arr [ i ] >= secondMaximum ) { secondMaximum = arr [ i ] ; } } } cout << firstMaximum << " ▁ " << secondMaximum ; } int main ( ) { int arr [ ] = { 18 , 15 , 8 , 9 , 14 } ; int len = sizeof ( arr ) / sizeof ( int ) ; findPairEvenParity ( arr , len ) ; return 0 ; }
Program to check if N is a Hexagonal Number or not | C ++ Program to check if N is a Hexagonal Number ; Function to check if number is hexagonal ; Calculate the value for n ; Check if n - floor ( n ) is equal to 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isHexagonal ( int N ) { float val = 8 * N + 1 ; float x = 1 + sqrt ( val ) ; float n = ( x ) / 4 ; if ( ( n - ( int ) n ) == 0 ) return true ; else return false ; } int main ( ) { int N = 14 ; if ( isHexagonal ( N ) == true ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Program to convert Number in characters | C ++ program to convert number in characters ; To calculate the reverse of the number ; The remainder will give the last digit of the number ; Extract the first digit of the reversed number ; Match it with switch case ; Divide the number by 10 to get the next number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void NumbertoCharacter ( int n ) { int rev = 0 , r = 0 ; while ( n > 0 ) { r = n % 10 ; rev = rev * 10 + r ; n = n / 10 ; } while ( rev > 0 ) { r = rev % 10 ; switch ( r ) { case 1 : cout << " one ▁ " ; break ; case 2 : cout << " two ▁ " ; break ; case 3 : cout << " three ▁ " ; break ; case 4 : cout << " four ▁ " ; break ; case 5 : cout << " five ▁ " ; break ; case 6 : cout << " six ▁ " ; break ; case 7 : cout << " seven ▁ " ; break ; case 8 : cout << " eight ▁ " ; break ; case 9 : cout << " nine ▁ " ; break ; case 0 : cout << " zero ▁ " ; break ; default : cout << " UnValid ▁ " ; break ; } rev = rev / 10 ; } } #include <iostream> NEW_LINE int main ( ) { int n = 12345 ; NumbertoCharacter ( n ) ; return 0 ; }
Count all subarrays whose sum can be split as difference of squares of two Integers | C ++ program to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; Function to count all the non - contiguous subarrays whose sum can be split as the difference of the squares ; Loop to iterate over all the possible subsequences of the array ; Finding the sum of all the possible subsequences ; Condition to check whether the number can be split as difference of squares ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Solve ( int arr [ ] , int n ) { int temp = 0 , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { temp = 0 ; for ( int j = i ; j < n ; j ++ ) { temp += arr [ j ] ; if ( ( temp + 2 ) % 4 != 0 ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << Solve ( arr , N ) ; return 0 ; }
Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M | C ++ program to find the product of all the combinations of M elements from an array whose index in the sorted order divides M completely ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; If y is odd , multiply x with result ; y must be even now ; Iterative Function to calculate ( nCr ) % p and save in f [ n ] [ r ] C ( n , r ) % p = [ C ( n - 1 , r - 1 ) % p + C ( n - 1 , r ) % p ] % p and C ( n , 0 ) = C ( n , n ) = 1 ; If j > i then C ( i , j ) = 0 ; If i is equal to j then C ( i , j ) = 1 ; C ( i , j ) = ( C ( i - 1 , j ) + C ( i - 1 , j - 1 ) ) % p ; Initialize the answer ; For every element arr [ i ] , x is count of occurrence of arr [ i ] in different set such that index of arr [ i ] in those sets divides m completely . ; Finding the count of arr [ i ] by placing it at the index which divides m completely ; Using fermat 's little theorem ; Multiplying with the count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long int lli ; const int m = 4 ; long long int power ( lli x , lli y , lli p ) { lli res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } void nCr ( lli n , lli p , lli f [ ] [ m + 1 ] ) { for ( lli i = 0 ; i <= n ; i ++ ) { for ( lli j = 0 ; j <= m ; j ++ ) { if ( j > i ) f [ i ] [ j ] = 0 ; else if ( j == 0 j == i ) f [ i ] [ j ] = 1 ; else f [ i ] [ j ] = ( f [ i - 1 ] [ j ] + f [ i - 1 ] [ j - 1 ] ) % p ; } } } void operations ( lli arr [ ] , lli n , lli f [ ] [ m + 1 ] ) { lli p = 1000000007 ; nCr ( n , p - 1 , f ) ; sort ( arr , arr + n ) ; lli ans = 1 ; for ( lli i = 0 ; i < n ; i ++ ) { long long int x = 0 ; for ( lli j = 1 ; j <= m ; j ++ ) { if ( m % j == 0 ) x = ( x + ( f [ n - i - 1 ] [ m - j ] * f [ i ] [ j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ; } ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) ; } cout << ans << endl ; } int main ( ) { lli arr [ ] = { 4 , 5 , 7 , 9 , 3 } ; lli n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; lli f [ n + 1 ] [ m + 1 ] ; operations ( arr , n , f ) ; }
Check whether a number can be represented by the product of two squares | C ++ implementation to Check whether a number can be represented by the product of two squares ; Function to check if there exist two numbers product of whose squares is n . ; check whether the product of the square of both numbers is equal to N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prodSquare ( int n ) { for ( long i = 2 ; i * i <= n ; i ++ ) for ( long j = 2 ; j <= n ; j ++ ) if ( i * i * j * j == n ) return true ; return false ; } int main ( ) { int n = 25 ; if ( prodSquare ( n ) ) cout << " Yes " ; else cout << " No " ; }
Check whether a number can be represented by the product of two squares | C ++ implementation to Check whether a number can be represented by the product of two squares ; Function to check if there exist two numbers product of whose squares is n ; Initialize map ; Store square value in hashmap ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prodSquare ( int n ) { unordered_map < float , float > s ; for ( int i = 2 ; i * i <= n ; ++ i ) { s [ i * i ] = 1 ; if ( s . find ( n / ( i * i ) ) != s . end ( ) ) return true ; } return false ; } int main ( ) { int n = 25 ; if ( prodSquare ( n ) ) cout << " Yes " ; else cout << " No " ; }
Print N distinct numbers following the given operations | C ++ implementation to Print N distinct numbers ; Function to print the required array ; Check if number is a multiple of 4 ; Printing Left Half of the array ; Printing Right Half of the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool printArr ( int n ) { if ( n % 4 == 0 ) { for ( int i = 1 ; i <= n / 2 ; i ++ ) cout << i * 2 << ' ▁ ' ; for ( int i = 1 ; i < n / 2 ; i ++ ) cout << i * 2 - 1 << ' ▁ ' ; cout << n + n / 2 - 1 << ' ' ; } else cout << " - 1" ; } int main ( ) { int n = 22 ; printArr ( n ) ; return 0 ; }
Sum of product of all subsets formed by only divisors of N | C ++ program to find the sum of product of all the subsets formed by only divisors of N ; Function to find the sum of product of all the subsets formed by only divisors of N ; Vector to store all the divisors of n ; Loop to find out the divisors of N ; Both ' i ' and ' n / i ' are the divisors of n ; Check if ' i ' and ' n / i ' are equal or not ; Calculating the answer ; Excluding the value of the empty set ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GetSum ( int n ) { vector < int > divisors ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { divisors . push_back ( i ) ; if ( i != n / i ) { divisors . push_back ( n / i ) ; } } } int ans = 1 ; for ( auto i : divisors ) { ans *= ( i + 1 ) ; } ans = ans - 1 ; return ans ; } int main ( ) { int N = 4 ; cout << GetSum ( N ) << endl ; }