codeparrot/xlcost-text-to-code huggingface dataset:
Generate a matrix having even sum of all diagonals in each 2 x 2 submatrices | C ++ program for the above approach ; Function to construct a matrix such that the sum elements in both diagonals of every 2 * 2 matrices is even ; Stores odd numbers ; Stores even numbers ; Store matrix elements such that sum of elements in both diagonals of every 2 * 2 submatrices is even ; Fill all the values of matrix elements ; Update odd ; Update even ; Print the matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateMatrix ( int N ) { int odd = 1 ; int even = 2 ; int mat [ N + 1 ] [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { mat [ i ] [ j ] = odd ; odd += 2 ; } else { mat [ i ] [ j ] = even ; even += 2 ; } } } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { cout << mat [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { int N = 4 ; generateMatrix ( N ) ; return 0 ; }
Queries to calculate sum by alternating signs of array elements in a given range | C ++ program to implement the above approach ; Function to build the segment tree ; If current node is a leaf node of the segment tree ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree ; Update on L segment tree ; Update on R segment tree ; Find the sum from L subtree and R subtree ; Function to update elements at index pos by val in the segment tree ; If current node is a leaf node ; If current index is even ; Update tree [ index ] ; Update tree [ index ] ; Divide the segment tree elements into L and R subtree ; If element lies in L subtree ; Update tree [ index ] ; Function to find the sum of array elements in the range [ L , R ] ; If start and end not lies in the range [ L , R ] ; If start and end comleately lies in the range [ L , R ] ; Stores sum from left subtree ; Stores sum from right subtree ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void build ( int tree [ ] , int arr [ ] , int start , int end , int index ) { if ( start == end ) { if ( start % 2 == 0 ) { tree [ index ] = arr [ start ] ; } else { tree [ index ] = - arr [ start ] ; } return ; } int mid = start + ( end - start ) / 2 ; build ( tree , arr , start , mid , 2 * index + 1 ) ; build ( tree , arr , mid + 1 , end , 2 * index + 2 ) ; tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] ; } void update ( int tree [ ] , int index , int start , int end , int pos , int val ) { if ( start == end ) { if ( start % 2 == 0 ) { tree [ index ] = val ; } else { tree [ index ] = - val ; } return ; } int mid = start + ( end - start ) / 2 ; if ( mid >= pos ) { update ( tree , 2 * index + 1 , start , mid , pos , val ) ; } else { update ( tree , 2 * index + 2 , mid + 1 , end , pos , val ) ; } tree [ index ] = tree [ 2 * index + 1 ] + tree [ 2 * index + 2 ] ; } int FindSum ( int tree [ ] , int start , int end , int L , int R , int index ) { if ( L > end R < start ) { return 0 ; } if ( L <= start && R >= end ) { return tree [ index ] ; } int mid = start + ( end - start ) / 2 ; int X = FindSum ( tree , start , mid , L , R , 2 * index + 1 ) ; int Y = FindSum ( tree , mid + 1 , end , L , R , 2 * index + 2 ) ; return X + Y ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int tree [ 4 * N + 5 ] = { 0 } ; build ( tree , arr , 0 , N - 1 , 0 ) ; int Q [ ] [ 3 ] = { { 2 , 0 , 3 } , { 1 , 1 , 5 } , { 2 , 1 , 2 } } ; int cntQuey = 3 ; for ( int i = 0 ; i < cntQuey ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { update ( tree , 0 , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) ; } else { if ( Q [ i ] [ 1 ] % 2 == 0 ) { cout << FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) << " ▁ " ; } else { cout << - FindSum ( tree , 0 , N - 1 , Q [ i ] [ 1 ] , Q [ i ] [ 2 ] , 0 ) << " ▁ " ; } } } }
Split an array into minimum number of non | C ++ program for the above approach ; Function to split the array into minimum count of subarrays such that each subarray is either non - increasing or non - decreasing ; Initialize variable to keep track of current sequence ; Stores the required result ; Traverse the array , arr [ ] ; If current sequence is neither non - increasing nor non - decreasing ; If previous element is greater ; Update current ; If previous element is equal to the current element ; Update current ; Otherwise ; Update current ; If current sequence is in non - decreasing ; If previous element is less than or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; If current sequence is Non - Increasing ; If previous element is greater or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; Print the answer ; Driver Code ; Given array ; Given size of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumSubarrays ( int arr [ ] , int n ) { char current = ' N ' ; int answer = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( current == ' N ' ) { if ( arr [ i ] < arr [ i - 1 ] ) { current = ' D ' ; } else if ( arr [ i ] == arr [ i - 1 ] ) { current = ' N ' ; } else { current = ' I ' ; } } else if ( current == ' I ' ) { if ( arr [ i ] >= arr [ i - 1 ] ) { current = ' I ' ; } else { current = ' N ' ; answer += 1 ; } } else { if ( arr [ i ] <= arr [ i - 1 ] ) { current = ' D ' ; } else { current = ' N ' ; answer += 1 ; } } } cout << answer ; } int main ( ) { int arr [ ] = { 2 , 3 , 9 , 5 , 4 , 6 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumSubarrays ( arr , n ) ; return 0 ; }
Convert a Matrix into another Matrix of given dimensions | C ++ program to implement the above approach ; Function to construct a matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , mat [ ] [ ] ; Update idx ; Print the resultant matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void ConstMatrix ( int * mat , int N , int M , int A , int B ) { if ( N * M != A * B ) return ; int idx = 0 ; int res [ A ] [ B ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { res [ idx / B ] [ idx % B ] = * ( ( mat + i * M ) + j ) ; idx ++ ; } } for ( int i = 0 ; i < A ; i ++ ) { for ( int j = 0 ; j < B ; j ++ ) cout << res [ i ] [ j ] << " ▁ " ; cout << " STRNEWLINE " ; } } int main ( ) { int mat [ ] [ 6 ] = { { 1 , 2 , 3 , 4 , 5 , 6 } } ; int A = 2 ; int B = 3 ; int N = sizeof ( mat ) / sizeof ( mat [ 0 ] ) ; int M = sizeof ( mat [ 0 ] ) / sizeof ( int ) ; ConstMatrix ( ( int * ) mat , N , M , A , B ) ; return 0 ; }
Modify given array by reducing each element by its next smaller element | C ++ program for the above approach ; Function to print the final array after reducing each array element by its next smaller element ; Initialize stack ; Array size ; To store the corresponding element ; If stack is not empty ; If top element is smaller than the current element ; Keep popping until stack is empty or top element is greater than the current element ; If stack is not empty ; Push current element ; Print the final array ; Driver Code ; Given array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printFinalPrices ( vector < int > & arr ) { stack < int > minStk ; int n = arr . size ( ) ; vector < int > reduce ( n , 0 ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ! minStk . empty ( ) ) { if ( minStk . top ( ) <= arr [ i ] ) { reduce [ i ] = minStk . top ( ) ; } else { while ( ! minStk . empty ( ) && ( minStk . top ( ) > arr [ i ] ) ) { minStk . pop ( ) ; } if ( ! minStk . empty ( ) ) { reduce [ i ] = minStk . top ( ) ; } } } minStk . push ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] - reduce [ i ] << " ▁ " ; } int main ( ) { vector < int > arr = { 8 , 4 , 6 , 2 , 3 } ; printFinalPrices ( arr ) ; return 0 ; }
Generate array having differences between count of occurrences of every array element on its left and right | C ++ program of the above approach ; Function to construct array of differences of counts on the left and right of the given array ; Initialize left and right frequency arrays ; Construct left cumulative frequency table ; Construct right cumulative frequency table ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void constructArray ( int A [ ] , int N ) { int left [ N + 1 ] = { 0 } ; int right [ N + 1 ] = { 0 } ; int X [ N + 1 ] = { 0 } , Y [ N + 1 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { X [ i ] = left [ A [ i ] ] ; left [ A [ i ] ] ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { Y [ i ] = right [ A [ i ] ] ; right [ A [ i ] ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { cout << Y [ i ] - X [ i ] << " ▁ " ; } } int main ( ) { int A [ ] = { 3 , 2 , 1 , 2 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; constructArray ( A , N ) ; return 0 ; }
Mean of distinct odd fibonacci nodes in a Linked List | C ++ program to implement the above approach ; Structure of a singly Linked List ; Stores data value of a Node ; Stores pointer to next Node ; Function to insert a node at the beginning of the singly Linked List ; Create a new Node ; Insert the data into the Node ; Insert pointer to the next Node ; Update head_ref ; Function to find the largest element from the linked list ; Stores the largest element in the linked list ; Iterate over the linked list ; If max is less than head -> data ; Update max ; Update head ; Function to store all Fibonacci numbers up to the largest element of the list ; Store all Fibonacci numbers up to Max ; Stores first element of Fibonacci number ; Stores second element of Fibonacci number ; Insert prev into hashmap ; Insert curr into hashmap ; Insert all elements of Fibonacci numbers up to Max ; Stores current fibonacci number ; Insert temp into hashmap ; Update prev ; Update curr ; Function to find the mean of odd Fibonacci nodes ; Stores the largest element in the linked list ; Stores all fibonacci numbers up to Max ; Stores current node of linked list ; Stores count of odd Fibonacci nodes ; Stores sum of all odd fibonacci nodes ; Traverse the linked list ; if the data value of current node is an odd number ; if data value of the node is present in hashmap ; Update cnt ; Update sum ; Remove current fibonacci number from hashmap so that duplicate elements can 't be counted ; Update curr ; Return the required mean ; Driver Code ; Stores head node of the linked list ; Insert all data values in the linked list
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int largestElement ( struct Node * head_ref ) { int Max = INT_MIN ; Node * head = head_ref ; while ( head != NULL ) { if ( Max < head -> data ) { Max = head -> data ; } head = head -> next ; } return Max ; } set < int > createHashMap ( int Max ) { set < int > hashmap ; int prev = 0 ; int curr = 1 ; hashmap . insert ( prev ) ; hashmap . insert ( curr ) ; while ( curr <= Max ) { int temp = curr + prev ; hashmap . insert ( temp ) ; prev = curr ; curr = temp ; } return hashmap ; } double meanofnodes ( struct Node * head ) { int Max = largestElement ( head ) ; set < int > hashmap = createHashMap ( Max ) ; Node * curr = head ; int cnt = 0 ; double sum = 0.0 ; while ( curr != NULL ) { if ( ( curr -> data ) & 1 ) { if ( hashmap . count ( curr -> data ) ) { cnt ++ ; sum += curr -> data ; hashmap . erase ( curr -> data ) ; } } curr = curr -> next ; } return ( sum / cnt ) ; } int main ( ) { Node * head = NULL ; push ( & head , 5 ) ; push ( & head , 21 ) ; push ( & head , 8 ) ; push ( & head , 12 ) ; push ( & head , 3 ) ; push ( & head , 13 ) ; push ( & head , 144 ) ; push ( & head , 6 ) ; cout << meanofnodes ( head ) ; return 0 ; }
Generate an alternate odd | C ++ Program to implement the above approach ; Function to print the required sequence ; Print ith odd number ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void findNumbers ( int n ) { int i = 0 ; while ( i <= n ) { cout << 2 * i * i + 4 * i + 1 + i % 2 << " ▁ " ; i ++ ; } } int main ( ) { int n = 6 ; findNumbers ( n ) ; }
Program to calculate gross salary of a person | C ++ Program to implement the above approach ; Function to calculate the salary of the person ; Condition to compute the allowance for the person ; Calculate gross salary ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int computeSalary ( int basic , char grade ) { int allowance ; double hra , da , pf ; hra = 0.2 * basic ; da = 0.5 * basic ; pf = 0.11 * basic ; if ( grade == ' A ' ) { allowance = 1700 ; } else if ( grade == ' B ' ) { allowance = 1500 ; } else { allowance = 1300 ; } int gross ; gross = round ( basic + hra + da + allowance - pf ) ; return gross ; } int main ( ) { int basic = 10000 ; char grade = ' A ' ; cout << computeSalary ( basic , grade ) ; }
Rearrange and update array elements as specified by the given queries | C ++ program to implement the above approach ; Function to perform the given operations ; Dequeue to store the array elements ; Insert all element of the array into the dequeue ; Stores the size of the queue ; Traverse each query ; Query for left shift . ; Extract the element at the front of the queue ; Pop the element at the front of the queue ; Push the element at the back of the queue ; Query for right shift ; Extract the element at the back of the queue ; Pop the element at the back of the queue ; Push the element at the front of the queue ; Query for update ; Query to get the value ; Driver Code ; All possible Queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Queries ( int arr [ ] , int N , vector < vector < int > > & Q ) { deque < int > dq ; for ( int i = 0 ; i < N ; i ++ ) { dq . push_back ( arr [ i ] ) ; } int sz = Q . size ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { if ( Q [ i ] [ 0 ] == 0 ) { int front = dq [ 0 ] ; dq . pop_front ( ) ; dq . push_back ( front ) ; } else if ( Q [ i ] [ 0 ] == 1 ) { int back = dq [ N - 1 ] ; dq . pop_back ( ) ; dq . push_front ( back ) ; } else if ( Q [ i ] [ 0 ] == 2 ) { dq [ Q [ i ] [ 1 ] ] = Q [ i ] [ 2 ] ; } else { cout << dq [ Q [ i ] [ 1 ] ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < vector < int > > Q ; Q = { { 0 } , { 1 } , { 3 , 1 } , { 2 , 2 , 54 } , { 3 , 2 } } ; Queries ( arr , N , Q ) ; return 0 ; }
Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal | C ++ Program to implement the above approach ; Function ot find the length of the smallest subarray ; Stores ( prefix Sum , index ) as ( key , value ) mappings ; Iterate till N ; Update the prefixSum ; Update the length ; Put the latest index to find the minimum length ; Update the length ; Return the answer ; Function to find the length of the largest subarray ; Stores the sum of all array elements after modification ; Change greater than k to 1 ; Change smaller than k to - 1 ; Change equal to k to 0 ; Update total_sum ; No deletion required , return 0 ; Delete smallest subarray that has sum = total_sum ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallSubarray ( int arr [ ] , int n , int total_sum ) { unordered_map < int , int > m ; int length = INT_MAX ; int prefixSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { prefixSum += arr [ i ] ; if ( prefixSum == total_sum ) { length = min ( length , i + 1 ) ; } m [ prefixSum ] = i ; if ( m . count ( prefixSum - total_sum ) ) { length = min ( length , i - m [ prefixSum - total_sum ] ) ; } } return length ; } int smallestSubarrayremoved ( int arr [ ] , int n , int k ) { int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > k ) { arr [ i ] = 1 ; } else if ( arr [ i ] < k ) { arr [ i ] = -1 ; } else { arr [ i ] = 0 ; } total_sum += arr [ i ] ; } if ( total_sum == 0 ) { return 0 ; } else { return smallSubarray ( arr , n , total_sum ) ; } } int main ( ) { int arr [ ] = { 12 , 16 , 12 , 13 , 10 } ; int K = 13 ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << smallestSubarrayremoved ( arr , n , K ) ; return 0 ; }
Balanced Ternary Number System | C ++ program to convert positive decimals into balanced ternary system ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string balancedTernary ( int n ) { string output = " " ; while ( n > 0 ) { int rem = n % 3 ; n = n / 3 ; if ( rem == 2 ) { rem = -1 ; n ++ ; } output = ( rem == 0 ? '0' : ( rem == 1 ) ? '1' : ' Z ' ) + output ; } return output ; } int main ( ) { int n = 238 ; cout << " Equivalent ▁ Balanced ▁ Ternary ▁ of ▁ " << n << " ▁ is : ▁ " << balancedTernary ( n ) ; return 0 ; }
Spt function or Smallest Parts Function of a given number | C ++ implementation to find the Spt Function to given number ; variable to store spt function of a number ; Function to add value of frequency of minimum element among all representations of n ; find the value of frequency of minimum element ; calculate spt ; Recursive function to find different ways in which n can be written as a sum of at one or more positive integers ; if sum becomes n , consider this representation ; start from previous element in the representation till n ; include current element from representation ; call function again with reduced sum ; backtrack - remove current element from representation ; Function to find the spt function ; Using recurrence find different ways in which n can be written as a sum of at 1 or more positive integers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int spt = 0 ; void printVector ( vector < int > & arr ) { int min_i = INT_MAX ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) min_i = min ( min_i , arr [ i ] ) ; int freq = count ( arr . begin ( ) , arr . end ( ) , min_i ) ; spt += freq ; } void findWays ( vector < int > & arr , int i , int n ) { if ( n == 0 ) printVector ( arr ) ; for ( int j = i ; j <= n ; j ++ ) { arr . push_back ( j ) ; findWays ( arr , j , n - j ) ; arr . pop_back ( ) ; } } void spt_function ( int n ) { vector < int > arr ; findWays ( arr , 1 , n ) ; cout << spt ; } int main ( ) { int N = 4 ; spt_function ( N ) ; return 0 ; }
Find initial integral solution of Linear Diophantine equation if finite solution exists | C ++ program for the above approach ; Function to implement the extended euclid algorithm ; Base Case ; Recursively find the gcd ; Function to print the solutions of the given equations ax + by = c ; Condition for infinite solutions ; Condition for no solutions exist ; Condition for no solutions exist ; Print the solution ; Driver Code ; Given coefficients ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd_extend ( int a , int b , int & x , int & y ) { if ( b == 0 ) { x = 1 ; y = 0 ; return a ; } else { int g = gcd_extend ( b , a % b , x , y ) ; int x1 = x , y1 = y ; x = y1 ; y = x1 - ( a / b ) * y1 ; return g ; } } void print_solution ( int a , int b , int c ) { int x , y ; if ( a == 0 && b == 0 ) { if ( c == 0 ) { cout << " Infinite ▁ Solutions ▁ Exist " << endl ; } else { cout << " No ▁ Solution ▁ exists " << endl ; } } int gcd = gcd_extend ( a , b , x , y ) ; if ( c % gcd != 0 ) { cout << " No ▁ Solution ▁ exists " << endl ; } else { cout << " x ▁ = ▁ " << x * ( c / gcd ) << " , ▁ y ▁ = ▁ " << y * ( c / gcd ) << endl ; } } int main ( void ) { int a , b , c ; a = 4 ; b = 18 ; c = 10 ; print_solution ( a , b , c ) ; return 0 ; }
Count of pairs whose bitwise AND is a power of 2 | C ++ program for the above approach ; Function to check if x is power of 2 ; Returns true if x is a power of 2 ; Function to return the number of valid pairs ; Iterate for all possible pairs ; Bitwise and value of the pair is passed ; Return the final count ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int x ) { return x && ( ! ( x & ( x - 1 ) ) ) ; } int count ( int arr [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( check ( arr [ i ] & arr [ j ] ) ) cnt ++ ; } } return cnt ; } int main ( ) { int arr [ ] = { 6 , 4 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << count ( arr , n ) ; return 0 ; }
Check if the Matrix follows the given constraints or not | C ++ implementation of the above approach ; Function checks if n is prime or not ; Corner case ; Check from 2 to sqrt ( n ) ; Function returns sum of all elements of matrix ; Stores the sum of the matrix ; Function to check if all a [ i ] [ j ] with prime ( i + j ) are prime ; If index is prime ; If element not prime ; Driver code ; Check for both conditions
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } int takeSum ( int a [ 4 ] [ 5 ] ) { int sum = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) sum += a [ i ] [ j ] ; return sum ; } bool checkIndex ( int n , int m , int a [ 4 ] [ 5 ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( isPrime ( i + j ) ) { if ( ! isPrime ( a [ i ] [ j ] ) ) return false ; } } } return true ; } int main ( ) { int n = 4 , m = 5 ; int a [ 4 ] [ 5 ] = { { 1 , 2 , 3 , 2 , 2 } , { 2 , 2 , 7 , 7 , 7 } , { 7 , 7 , 21 , 7 , 10 } , { 2 , 2 , 3 , 6 , 7 } } ; int sum = takeSum ( a ) ; if ( isPrime ( sum ) && checkIndex ( n , m , a ) ) { cout << " YES " << endl ; } else cout << " NO " << endl ; return 0 ; }
Count of all possible pairs of array elements with same parity | C ++ program for the above approach ; Function to return the answer ; Generate all possible pairs ; Increment the count if both even or both odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int A [ ] , int n ) { int count = 0 , i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( ( A [ i ] % 2 == 0 && A [ j ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ j ] % 2 != 0 ) ) count ++ ; } } return count ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 1 , 3 } ; int n = sizeof ( A ) / sizeof ( int ) ; cout << countPairs ( A , n ) ; }
Smallest number whose sum of digits is square of N | C ++ implementation of the above approach ; Function to return smallest number whose sum of digits is n ^ 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestNum ( int n ) { cout << pow ( 10 , n * n / 9 ) << endl ; return ( n * n % 9 + 1 ) * pow ( 10 , n * n / 9 ) - 1 ; } int main ( ) { int n = 4 ; cout << smallestNum ( n ) ; return 0 ; }
Second heptagonal numbers | C ++ implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code ; Function call
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; void findNthTerm ( int n ) { cout << n * ( 5 * n + 3 ) / 2 << endl ; } int main ( ) { int N = 4 ; findNthTerm ( N ) ; return 0 ; } findNthTerm ( N ) ; return 0 ; }
Weakly Prime Numbers | C ++ Program to check if n is Weakly Prime Number ; function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; function to check if n is a Weakly Prime Number ; number should be prime ; converting N to string ; loop to change digit at every character one by one . ; loop to store every digit one by one at index j ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } bool isWeaklyPrimeNum ( int N ) { if ( ! isPrime ( N ) ) return false ; string s = to_string ( N ) ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { string str = s ; for ( int i = 0 ; i <= 9 ; i ++ ) { char c = '0' + i ; str [ j ] = c ; int Num = stoi ( str ) ; if ( str [ j ] != s [ j ] && isPrime ( Num ) ) { return false ; } } } return true ; } int main ( ) { int n = 294001 ; if ( isWeaklyPrimeNum ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }