codeparrot/xlcost-text-to-code huggingface dataset:
Queries to find number of connected grid components of given sizes in a Matrix | C ++ implementation for the above approach ; stores information about which cell are already visited in a particular BFS ; Stores the count of cells in the largest connected component ; Function checks if a cell is valid , i . e . it is inside the grid and equal to 1 ; Map to count the frequency of each connected component ; function to calculate the largest connected component ; Iterate over every cell ; Stores the indices of the matrix cells ; Mark the starting cell as visited and push it into the queue ; Iterate while the queue is not empty ; Go to the adjacent cells ; Drivers Code ; Given input array of 1 s and 0 s ; queries array ; sizeof queries array ; Count the frequency of each connected component ; Iterate over the given queries array and And answer the queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int n = 6 ; const int m = 6 ; const int dx [ ] = { 0 , 1 , -1 , 0 } ; const int dy [ ] = { 1 , 0 , 0 , -1 } ; int visited [ n ] [ m ] ; int COUNT ; bool is_valid ( int x , int y , int matrix [ n ] [ m ] ) { if ( x < n && y < m && x >= 0 && y >= 0 ) { if ( visited [ x ] [ y ] == false && matrix [ x ] [ y ] == 1 ) return true ; else return false ; } else return false ; } map < int , int > mp ; void findComponentSize ( int matrix [ n ] [ m ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ! visited [ i ] [ j ] && matrix [ i ] [ j ] == 1 ) { COUNT = 0 ; queue < pair < int , int > > q ; q . push ( { i , j } ) ; visited [ i ] [ j ] = true ; while ( ! q . empty ( ) ) { pair < int , int > p = q . front ( ) ; q . pop ( ) ; int x = p . first , y = p . second ; COUNT ++ ; for ( int i = 0 ; i < 4 ; i ++ ) { int newX = x + dx [ i ] ; int newY = y + dy [ i ] ; if ( is_valid ( newX , newY , matrix ) ) { q . push ( { newX , newY } ) ; visited [ newX ] [ newY ] = true ; } } } mp [ COUNT ] ++ ; } } } } int main ( ) { int matrix [ n ] [ m ] = { { 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 1 , 1 } , { 0 , 0 , 0 , 1 , 1 , 1 } , { 0 , 0 , 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 , 0 } } ; int queries [ ] = { 6 , 1 , 8 , 2 } ; int N = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; memset ( visited , false , sizeof visited ) ; findComponentSize ( matrix ) ; for ( int i = 0 ; i < N ; i ++ ) cout << mp [ queries [ i ] ] << " ▁ " ; return 0 ; }
Minimum value of X such that sum of arr [ i ] | C ++ program for the above approach ; Function to check if there exists an X that satisfies the given conditions ; Find the required value of the given expression ; Function to find the minimum value of X using binary search . ; Boundaries of the Binary Search ; Find the middle value ; Check for the middle value ; Update the upper ; Update the lower ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int b [ ] , int k , int n , int x ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + pow ( max ( a [ i ] - x , 0 ) , b [ i ] ) ; } if ( sum <= k ) return true ; else return false ; } int findMin ( int a [ ] , int b [ ] , int n , int k ) { int l = 0 , u = * max_element ( a , a + n ) ; while ( l < u ) { int m = ( l + u ) / 2 ; if ( check ( a , b , k , n , m ) ) { u = m ; } else { l = m + 1 ; } } return l ; } int main ( ) { int arr [ ] = { 2 , 1 , 4 , 3 , 5 } ; int brr [ ] = { 4 , 3 , 2 , 3 , 1 } ; int K = 12 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMin ( arr , brr , N , K ) ; return 0 ; }
Count of indices pairs such that product of elements at these indices is equal to absolute difference of indices | C ++ program for the above approach ; Function to count the number of pairs ( i , j ) such that arr [ i ] * arr [ j ] is equal to abs ( i - j ) ; Stores the resultant number of pairs ; Iterate over the range [ 0 , N ) ; Now , iterate from the value arr [ i ] - ( i % arr [ i ] ) till N with an increment of arr [ i ] ; If the given criteria satisfy then increment the value of count ; Return the resultant 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 && ( arr [ i ] * arr [ j ] ) == abs ( i - j ) ) { count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getPairsCount ( arr , N ) ; return 0 ; }
Find frequency of each character with positions in given Array of Strings | C ++ program for the above approach ; Function to print every occurence of every characters in every string ; Iterate over the vector arr [ ] ; Traverse the string arr [ i ] ; Push the pair of { i + 1 , j + 1 } in mp [ arr [ i ] [ j ] ] ; Print the occurences of every character ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOccurences ( vector < string > arr , int N ) { map < char , vector < pair < int , int > > > mp ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < arr [ i ] . length ( ) ; j ++ ) { mp [ arr [ i ] [ j ] ] . push_back ( make_pair ( i + 1 , j + 1 ) ) ; } } for ( auto it : mp ) { cout << " Occurences ▁ of : ▁ " << it . first << " ▁ = ▁ " ; for ( int j = 0 ; j < ( it . second ) . size ( ) ; j ++ ) { cout << " [ " << ( it . second ) [ j ] . first << " ▁ " << ( it . second ) [ j ] . second << " ] ▁ " ; } cout << endl ; } } int main ( ) { vector < string > arr = { " geeksforgeeks " , " gfg " } ; int N = arr . size ( ) ; printOccurences ( arr , N ) ; }
Count of subarrays with X as the most frequent element , for each value of X from 1 to N | C ++ program for the above approach ; Function to calculate the number of subarrays where X ( 1 <= X <= N ) is the most frequent element ; array to store the final answers ; Array to store current frequencies ; Initialise count ; Variable to store the current most frequent element ; Update frequency array ; Update answer ; Print answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mostFrequent ( int arr [ ] , int N ) { int ans [ N ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { int count [ N ] ; memset ( count , 0 , sizeof ( count ) ) ; int best = 0 ; for ( int j = i ; j < N ; j ++ ) { count [ arr [ j ] - 1 ] ++ ; if ( count [ arr [ j ] - 1 ] > count [ best - 1 ] || ( count [ arr [ j ] - 1 ] == count [ best - 1 ] && arr [ j ] < best ) ) { best = arr [ j ] ; } ans [ best - 1 ] ++ ; } } for ( int i = 0 ; i < N ; i ++ ) cout << ans [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 2 , 1 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; mostFrequent ( arr , N ) ; return 0 ; }
Count array elements whose highest power of 2 less than or equal to that number is present in the given array | C ++ program for the above approach ; Function to count array elements whose highest power of 2 is less than or equal to that number is present in the given array ; Stores the resultant count of array elements ; Stores frequency of visited array elements ; Traverse the array ; Calculate log base 2 of the element arr [ i ] ; Highest power of 2 whose value is at most arr [ i ] ; Increment the count by 1 ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countElement ( int arr [ ] , int N ) { int count = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < N ; i ++ ) { m [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { int lg = log2 ( arr [ i ] ) ; int p = pow ( 2 , lg ) ; if ( m [ p ] ) { count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 3 , 4 , 6 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countElement ( arr , N ) ; return 0 ; }
Maximum number of intersections possible for any of the N given segments | C ++ program for the above approach ; Function to find the maximum number of intersections one segment has with all the other given segments ; Stores the resultant maximum count ; Stores the starting and the ending points ; Sort arrays points in the ascending order ; Traverse the array arr [ ] ; Find the count of segments on left of ith segment ; Find the count of segments on right of ith segment ; Find the total segments not intersecting with the current segment ; Store the count of segments that intersect with the ith segment ; Update the value of count ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumIntersections ( int arr [ ] [ 2 ] , int N ) { int count = 0 ; int L [ N ] , R [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { L [ i ] = arr [ i ] [ 0 ] ; R [ i ] = arr [ i ] [ 1 ] ; } sort ( L , L + N ) ; sort ( R , R + N ) ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] [ 0 ] ; int r = arr [ i ] [ 1 ] ; int x = lower_bound ( R , R + N , l ) - R ; int y = N - ( upper_bound ( L , L + N , r ) - L ) ; int cnt = x + y ; cnt = N - cnt - 1 ; count = max ( count , cnt ) ; } return count ; } int main ( ) { int arr [ ] [ 2 ] = { { 1 , 6 } , { 5 , 5 } , { 2 , 3 } } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumIntersections ( arr , N ) ; return 0 ; }
Program to find the Nth Composite Number | C ++ program for the above approach ; Function to find the Nth Composite Numbers using Sieve of Eratosthenes ; Sieve of prime numbers ; Initialize the array to true ; Iterate over the range [ 2 , MAX_SIZE ] ; If IsPrime [ p ] is true ; Iterate over the range [ p * p , MAX_SIZE ] ; Stores the list of composite numbers ; Iterate over the range [ 4 , MAX_SIZE ] ; If i is not prime ; Return Nth Composite Number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_SIZE  1000005 NEW_LINE int NthComposite ( int N ) { bool IsPrime [ MAX_SIZE ] ; memset ( IsPrime , true , sizeof ( IsPrime ) ) ; for ( int p = 2 ; p * p < MAX_SIZE ; p ++ ) { if ( IsPrime [ p ] == true ) { for ( int i = p * p ; i < MAX_SIZE ; i += p ) IsPrime [ i ] = false ; } } vector < int > Composites ; for ( int p = 4 ; p < MAX_SIZE ; p ++ ) if ( ! IsPrime [ p ] ) Composites . push_back ( p ) ; return Composites [ N - 1 ] ; } int main ( ) { int N = 4 ; cout << NthComposite ( N ) ; return 0 ; }
Intersection point of two Linked Lists | Set 3 | C ++ program for the above approach ; Structure of a node of a Linked List ; Constructor ; Function to find the intersection point of the two Linked Lists ; Traverse the first linked list and multiply all values by - 1 ; Update a -> data ; Update a ; Traverse the second Linked List and find the value of the first node having negative value ; Intersection point ; Update b ; Function to create linked lists ; Linked List L1 ; Linked List L2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; Node ( int x ) { data = x ; next = NULL ; } } ; Node * intersectingNode ( Node * headA , Node * headB ) { Node * a = headA ; while ( a ) { a -> data *= -1 ; a = a -> next ; } Node * b = headB ; while ( b ) { if ( b -> data < 0 ) break ; b = b -> next ; } return b ; } void formLinkList ( Node * & head1 , Node * & head2 ) { head1 = new Node ( 3 ) ; head1 -> next = new Node ( 6 ) ; head1 -> next -> next = new Node ( 9 ) ; head1 -> next -> next -> next = new Node ( 15 ) ; head1 -> next -> next -> next -> next = new Node ( 30 ) ; head2 = new Node ( 10 ) ; head2 -> next = head1 -> next -> next -> next ; return ; } int main ( ) { Node * head1 ; Node * head2 ; formLinkList ( head1 , head2 ) ; cout << abs ( intersectingNode ( head1 , head2 ) -> data ) ; return 0 ; }
Left rotate digits of node values of all levels of a Binary Tree in increasing order | C ++ program for the above approach ; TreeNode class ; Function to check if the nodes are in increasing order or not ; Perform Level Order Traversal ; Current length of queue ; If queue is empty ; Level order traversal ; Pop element from front of the queue ; If previous value exceeds current value , return false ; Function to print the Tree after modification ; Performs level order traversal ; Calculate size of the queue ; Iterate until queue is empty ; Function to arrange node values of each level in increasing order ; Perform level order traversal ; Calculate length of queue ; If queue is empty ; Level order traversal ; Pop element from front of the queue ; Initialize the optimal element by the initial element ; Check for all left shift operations ; Left shift ; If the current shifting gives optimal solution ; Replacing initial element by the optimal element ; Push the LST ; Push the RST ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct TreeNode { int val ; TreeNode * left , * right ; TreeNode ( int v ) { val = v ; left = NULL ; right = NULL ; } } ; bool isInc ( TreeNode * root ) { queue < TreeNode * > que ; que . push ( root ) ; while ( true ) { int length = que . size ( ) ; if ( length == 0 ) break ; auto pre = que . front ( ) ; while ( length > 0 ) { auto temp = que . front ( ) ; que . pop ( ) ; if ( pre -> val > temp -> val ) return false ; pre = temp ; if ( temp -> left ) que . push ( temp -> left ) ; if ( temp -> right ) que . push ( temp -> right ) ; length -= 1 ; } } return true ; } void levelOrder ( TreeNode * root ) { queue < TreeNode * > que ; que . push ( root ) ; while ( true ) { int length = que . size ( ) ; if ( length == 0 ) break ; while ( length ) { auto temp = que . front ( ) ; que . pop ( ) ; cout << temp -> val << " ▁ " ; if ( temp -> left ) que . push ( temp -> left ) ; if ( temp -> right ) que . push ( temp -> right ) ; length -= 1 ; } cout << endl ; } cout << endl ; } void makeInc ( TreeNode * root ) { queue < TreeNode * > que ; que . push ( root ) ; while ( true ) { int length = que . size ( ) ; if ( length == 0 ) break ; int prev = -1 ; while ( length > 0 ) { auto temp = que . front ( ) ; que . pop ( ) ; auto optEle = temp -> val ; auto strEle = to_string ( temp -> val ) ; bool flag = true ; int yy = strEle . size ( ) ; for ( int idx = 0 ; idx < strEle . size ( ) ; idx ++ ) { int ls = stoi ( strEle . substr ( idx , yy ) + strEle . substr ( 0 , idx ) ) ; if ( ls >= prev and flag ) { optEle = ls ; flag = false ; } if ( ls >= prev ) optEle = min ( optEle , ls ) ; } temp -> val = optEle ; prev = temp -> val ; if ( temp -> left ) que . push ( temp -> left ) ; if ( temp -> right ) que . push ( temp -> right ) ; length -= 1 ; } } if ( isInc ( root ) ) levelOrder ( root ) ; else cout << ( -1 ) ; } int main ( ) { TreeNode * root = new TreeNode ( 341 ) ; root -> left = new TreeNode ( 241 ) ; root -> right = new TreeNode ( 123 ) ; root -> left -> left = new TreeNode ( 324 ) ; root -> left -> right = new TreeNode ( 235 ) ; root -> right -> right = new TreeNode ( 161 ) ; makeInc ( root ) ; }
Reverse Level Order Traversal | A C ++ program to print REVERSE level order traversal using stack and queue This approach is adopted from following link tech - queries . blogspot . in / 2008 / 12 / level - order - tree - traversal - in - reverse . html http : ; A binary tree node has data , pointer to left and right children ; Given a binary tree , print its nodes in reverse level order ; Do something like normal level order traversal order . Following are the differences with normal level order traversal 1 ) Instead of printing a node , we push the node to stack 2 ) Right subtree is visited before left subtree ; Dequeue node and make it root ; Enqueue right child ; NOTE : RIGHT CHILD IS ENQUEUED BEFORE LEFT ; Enqueue left child ; Now pop all items from stack one by one and print them ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver program to test above functions ; Let us create trees shown in above diagram
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left ; struct node * right ; } ; void reverseLevelOrder ( node * root ) { stack < node * > S ; queue < node * > Q ; Q . push ( root ) ; while ( Q . empty ( ) == false ) { root = Q . front ( ) ; Q . pop ( ) ; S . push ( root ) ; if ( root -> right ) Q . push ( root -> right ) ; if ( root -> left ) Q . push ( root -> left ) ; } while ( S . empty ( ) == false ) { root = S . top ( ) ; cout << root -> data << " ▁ " ; S . pop ( ) ; } } node * newNode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return ( temp ) ; } int main ( ) { struct node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; cout << " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ STRNEWLINE " ; reverseLevelOrder ( root ) ; return 0 ; }
Modify array of strings by replacing characters repeating in the same or remaining strings | C ++ program for the above approach ; Function to remove duplicate characters across the strings ; Stores distinct characters ; Size of the array ; Stores the list of modified strings ; Traverse the array ; Stores the modified string ; Iterate over the characters of the modified string ; If character is already present ; Insert character into the Set ; Print the list of modified strings ; Print each string ; Driver Code ; Given array of strings ; Function Call to modify the given array of strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeDuplicateCharacters ( vector < string > arr ) { unordered_set < char > cset ; int n = arr . size ( ) ; vector < string > out ; for ( auto str : arr ) { string out_curr = " " ; for ( auto ch : str ) { if ( cset . find ( ch ) != cset . end ( ) ) continue ; out_curr += ch ; cset . insert ( ch ) ; } if ( out_curr . size ( ) ) out . push_back ( out_curr ) ; } for ( int i = 0 ; i < out . size ( ) ; i ++ ) { cout << out [ i ] << " ▁ " ; } } int main ( ) { vector < string > arr = { " Geeks " , " For " , " Geeks " , " Post " } ; removeDuplicateCharacters ( arr ) ; }
Sum of array elements which are prime factors of a given number | C ++ program for the above approach ; Function to check if a number is prime or not ; Corner cases ; Check if n is a multiple of 2 or 3 ; Above condition allows to check only for every 6 th number , starting from 5 ; If n is a multiple of i and i + 2 ; Function to find the sum of array elements which are prime factors of K ; Stores the required sum ; Traverse the given array ; If current element is a prime factor of k , add it to the sum ; Print the result ; Driver Code ; Given arr [ ] ; Store the size of the array
#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 ; } void primeFactorSum ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 && isPrime ( arr [ i ] ) ) { sum = sum + arr [ i ] ; } } cout << sum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 5 , 6 , 7 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 35 ; primeFactorSum ( arr , N , K ) ; return 0 ; }
Minimum number of array elements from either ends required to be subtracted from X to reduce X to 0 | C ++ program to implement the above approach ; Function to count the minimum number of operations required to reduce x to 0 ; If sum of the array is less than x ; Stores the count of operations ; Two pointers to traverse the array ; Reduce x by the sum of the entire array ; Iterate until l reaches the front of the array ; If sum of elements from the front exceeds x ; Shift towards left ; If sum exceeds 0 ; Reduce x by elements from the right ; If x is reduced to 0 ; Update the minimum count of operations required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; static int minOperations ( int nums [ ] , int N , int x ) { int sum = 0 ; for ( int i = 0 ; i < x ; i ++ ) sum += nums [ i ] ; if ( sum < x ) return -1 ; int ans = INT_MAX ; int l = N - 1 , r = N ; x -= sum ; while ( l >= 0 ) { if ( x <= 0 ) { x += nums [ l ] ; l -= 1 ; } if ( x > 0 ) { r -= 1 ; x -= nums [ r ] ; } if ( x == 0 ) { ans = min ( ans , ( l + 1 ) + ( N - r ) ) ; } } if ( ans < INT_MAX ) return ans ; else return -1 ; } int main ( ) { int nums [ ] = { 1 , 1 , 4 , 2 , 3 } ; int N = sizeof ( nums ) / sizeof ( nums [ 0 ] ) ; int x = 5 ; cout << minOperations ( nums , N , x ) ; return 0 ; }
Count of subarrays having product as a perfect cube | C ++ program for the above approach ; Function to store the prime factorization of a number ; If N is divisible by i ; Increment v [ i ] by 1 and calculate it modulo by 3 ; Divide the number by i ; If the number is not equal to 1 ; Increment v [ n ] by 1 ; Calculate it modulo 3 ; Function to count the number of subarrays whose product is a perfect cube ; Store the required result ; Stores the prime factors modulo 3 ; Stores the occurrences of the prime factors ; Traverse the array , arr [ ] ; Store the prime factors and update the vector v ; Update the answer ; Increment current state of the prime factors by 1 ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1e5 NEW_LINE void primeFactors ( vector < int > & v , int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { while ( n % i == 0 ) { v [ i ] ++ ; v [ i ] %= 3 ; n /= i ; } } if ( n != 1 ) { v [ n ] ++ ; v [ n ] %= 3 ; } } void countSubarrays ( int arr [ ] , int n ) { int ans = 0 ; vector < int > v ( MAX , 0 ) ; map < vector < int > , int > mp ; mp [ v ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { primeFactors ( v , arr [ i ] ) ; ans += mp [ v ] ; mp [ v ] ++ ; } cout << ans ; } int main ( ) { int arr [ ] = { 1 , 8 , 4 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countSubarrays ( arr , N ) ; return 0 ; }
Number of subarrays having even product | Function to count subarrays with even product ; Total number of subarrays ; Counter variables ; Traverse the array ; If current element is odd ; Update count of subarrays with odd product up to index i ; Print count of subarrays with even product ; Driver code ; Input ; Length of an array ; Function call to count even product subarrays
#include <iostream> NEW_LINE using namespace std ; void evenproduct ( int arr [ ] , int length ) { int total_subarray = length * ( length + 1 ) / 2 ; int total_odd = 0 ; int count_odd = 0 ; for ( int i = 0 ; i < length ; ++ i ) { if ( arr [ i ] % 2 == 0 ) { count_odd = 0 ; } else { ++ count_odd ; total_odd += count_odd ; } } cout << ( total_subarray - total_odd ) << endl ; } int main ( ) { int arr [ ] = { 7 , 5 , 4 , 9 } ; int length = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; evenproduct ( arr , length ) ; return 0 ; }