codeparrot/xlcost-text-to-code huggingface dataset:
Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Find XOR of all numbers from 1 to n ; If XOR is equal n , then return true ; Driver code to print multiples of 4 ; Printing multiples of 4 using above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isMultipleOf4 ( int n ) { if ( n == 1 ) return false ; int XOR = 0 ; for ( int i = 1 ; i <= n ; i ++ ) XOR = XOR ^ i ; return ( XOR == n ) ; } int main ( ) { for ( int n = 0 ; n <= 42 ; n ++ ) if ( isMultipleOf4 ( n ) ) cout << n << " ▁ " ; return 0 ; }
Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Driver code to print multiples of 4 ; Printing multiples of 4 using above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isMultipleOf4 ( long long n ) { if ( n == 0 ) return true ; return ( ( ( n >> 2 ) << 2 ) == n ) ; } int main ( ) { for ( int n = 0 ; n <= 42 ; n ++ ) if ( isMultipleOf4 ( n ) ) cout << n << " ▁ " ; return 0 ; }
Check sum of Covered and Uncovered nodes of Binary Tree | C ++ program to find sum of Covered and Uncovered node of binary tree ; A binary tree node has key , pointer to left child and a pointer to right child ; To create a newNode of tree and return pointer ; Utility function to calculate sum of all node of tree ; Recursive function to calculate sum of left boundary elements ; If leaf node , then just return its key value ; If left is available then go left otherwise go right ; Recursive function to calculate sum of right boundary elements ; If leaf node , then just return its key value ; If right is available then go right otherwise go left ; Returns sum of uncovered elements ; Initializing with 0 in case we don 't have  left or right boundary  ; returning sum of root node , left boundary and right boundary ; Returns true if sum of covered and uncovered elements is same . ; Sum of uncovered elements ; Sum of all elements ; Check if sum of covered and uncovered is same ; Helper function to print inorder traversal of binary tree ; Driver program to test above functions ; Making above given diagram 's binary tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; struct Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int sum ( Node * t ) { if ( t == NULL ) return 0 ; return t -> key + sum ( t -> left ) + sum ( t -> right ) ; } int uncoveredSumLeft ( Node * t ) { if ( t -> left == NULL && t -> right == NULL ) return t -> key ; if ( t -> left != NULL ) return t -> key + uncoveredSumLeft ( t -> left ) ; else return t -> key + uncoveredSumLeft ( t -> right ) ; } int uncoveredSumRight ( Node * t ) { if ( t -> left == NULL && t -> right == NULL ) return t -> key ; if ( t -> right != NULL ) return t -> key + uncoveredSumRight ( t -> right ) ; else return t -> key + uncoveredSumRight ( t -> left ) ; } int uncoverSum ( Node * t ) { int lb = 0 , rb = 0 ; if ( t -> left != NULL ) lb = uncoveredSumLeft ( t -> left ) ; if ( t -> right != NULL ) rb = uncoveredSumRight ( t -> right ) ; return t -> key + lb + rb ; } bool isSumSame ( Node * root ) { int sumUC = uncoverSum ( root ) ; int sumT = sum ( root ) ; return ( sumUC == ( sumT - sumUC ) ) ; } void inorder ( Node * root ) { if ( root ) { inorder ( root -> left ) ; printf ( " % d ▁ " , root -> key ) ; inorder ( root -> right ) ; } } int main ( ) { Node * root = newNode ( 8 ) ; root -> left = newNode ( 3 ) ; root -> left -> left = newNode ( 1 ) ; root -> left -> right = newNode ( 6 ) ; root -> left -> right -> left = newNode ( 4 ) ; root -> left -> right -> right = newNode ( 7 ) ; root -> right = newNode ( 10 ) ; root -> right -> right = newNode ( 14 ) ; root -> right -> right -> left = newNode ( 13 ) ; if ( isSumSame ( root ) ) printf ( " Sum ▁ of ▁ covered ▁ and ▁ uncovered ▁ is ▁ same STRNEWLINE " ) ; else printf ( " Sum ▁ of ▁ covered ▁ and ▁ uncovered ▁ is ▁ not ▁ same STRNEWLINE " ) ; }
Check if a number is Bleak | An efficient C ++ program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; A function to return ceiling of log x in base 2. For example , it returns 3 for 8 and 4 for 9. ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBits ( int x ) { unsigned int count = 0 ; while ( x ) { x &= ( x - 1 ) ; count ++ ; } return count ; } int ceilLog2 ( int x ) { int count = 0 ; x -- ; while ( x > 0 ) { x = x >> 1 ; count ++ ; } return count ; } bool isBleak ( int n ) { for ( int x = n - ceilLog2 ( n ) ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } int main ( ) { isBleak ( 3 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; isBleak ( 4 ) ? cout << " Yes STRNEWLINE " : cout << " No STRNEWLINE " ; return 0 ; }
Count strings with consecutive 1 's | C ++ program to count all distinct binary strings with two consecutive 1 's ; Returns count of n length binary strings with consecutive 1 's ; Count binary strings without consecutive 1 's.  See the approach discussed on be  ( http:goo.gl/p8A3sW ) ; Subtract a [ n - 1 ] + b [ n - 1 ] from 2 ^ n ; Driver code
#include <iostream> NEW_LINE using namespace std ; int countStrings ( int n ) { int a [ n ] , b [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ; } int main ( ) { cout << countStrings ( 5 ) << endl ; return 0 ; }
How to swap two bits in a given integer ? | C ++ code for swapping given bits of a number ; left - shift 1 p1 and p2 times and using XOR ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int swapBits ( int n , int p1 , int p2 ) { n ^= 1 << p1 ; n ^= 1 << p2 ; return n ; } int main ( ) { cout << " Result ▁ = ▁ " << swapBits ( 28 , 0 , 3 ) ; return 0 ; }
Maximum length sub | C ++ implementation of the approach ; Function to return the maximum length of the required sub - array ; To store the maximum length for a valid subarray ; To store the count of contiguous similar elements for previous group and the current group ; If current element is equal to the previous element then it is a part of the same group ; Else update the previous group and start counting elements for the new group ; Update the maximum possible length for a group ; Return the maximum length of the valid subarray ; Driver code
#include <iostream> NEW_LINE using namespace std ; int maxLength ( int a [ ] , int n ) { int maxLen = 0 ; int prev_cnt = 0 , curr_cnt = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) curr_cnt ++ ; else { prev_cnt = curr_cnt ; curr_cnt = 1 ; } maxLen = max ( maxLen , min ( prev_cnt , curr_cnt ) ) ; } return ( 2 * maxLen ) ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxLength ( arr , n ) ; return 0 ; }
Check if all leaves are at same level | C ++ program to check if all leaves are at same level ; A binary tree node ; A utility function to allocate a new tree node ; Recursive function which checks whether all leaves are at same level ; Base case ; If a leaf node is encountered ; When a leaf node is found first time ; Set first found leaf 's level ; If this is not first leaf node , compare its level with first leaf 's level ; If this node is not leaf , recursively check left and right subtrees ; The main function to check if all leafs are at same level . It mainly uses checkUtil ( ) ; Driver Code ; Let us create tree shown in third example
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } bool checkUtil ( struct Node * root , int level , int * leafLevel ) { if ( root == NULL ) return true ; if ( root -> left == NULL && root -> right == NULL ) { if ( * leafLevel == 0 ) { * leafLevel = level ; return true ; } return ( level == * leafLevel ) ; } return checkUtil ( root -> left , level + 1 , leafLevel ) && checkUtil ( root -> right , level + 1 , leafLevel ) ; } bool check ( struct Node * root ) { int level = 0 , leafLevel = 0 ; return checkUtil ( root , level , & leafLevel ) ; } int main ( ) { struct Node * root = newNode ( 12 ) ; root -> left = newNode ( 5 ) ; root -> left -> left = newNode ( 3 ) ; root -> left -> right = newNode ( 9 ) ; root -> left -> left -> left = newNode ( 1 ) ; root -> left -> right -> left = newNode ( 1 ) ; if ( check ( root ) ) cout << " Leaves ▁ are ▁ at ▁ same ▁ level STRNEWLINE " ; else cout << " Leaves ▁ are ▁ not ▁ at ▁ same ▁ level STRNEWLINE " ; getchar ( ) ; return 0 ; }
Number of siblings of a given Node in n | C ++ program to find number of siblings of a given node ; Represents a node of an n - ary tree ; Function to calculate number of siblings of a given node ; Creating a queue and pushing the root ; Dequeue an item from queue and check if it is equal to x If YES , then return number of children ; Enqueue all children of the dequeued item ; If the value of children is equal to x , then return the number of siblings ; Driver program ; Creating a generic tree as shown in above figure ; Node whose number of siblings is to be calculated ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int key ; vector < Node * > child ; Node ( int data ) { key = data ; } } ; int numberOfSiblings ( Node * root , int x ) { if ( root == NULL ) return 0 ; queue < Node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { Node * p = q . front ( ) ; q . pop ( ) ; for ( int i = 0 ; i < p -> child . size ( ) ; i ++ ) { if ( p -> child [ i ] -> key == x ) return p -> child . size ( ) - 1 ; q . push ( p -> child [ i ] ) ; } } } int main ( ) { Node * root = new Node ( 50 ) ; ( root -> child ) . push_back ( new Node ( 2 ) ) ; ( root -> child ) . push_back ( new Node ( 30 ) ) ; ( root -> child ) . push_back ( new Node ( 14 ) ) ; ( root -> child ) . push_back ( new Node ( 60 ) ) ; ( root -> child [ 0 ] -> child ) . push_back ( new Node ( 15 ) ) ; ( root -> child [ 0 ] -> child ) . push_back ( new Node ( 25 ) ) ; ( root -> child [ 0 ] -> child [ 1 ] -> child ) . push_back ( new Node ( 70 ) ) ; ( root -> child [ 0 ] -> child [ 1 ] -> child ) . push_back ( new Node ( 100 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( new Node ( 6 ) ) ; ( root -> child [ 1 ] -> child ) . push_back ( new Node ( 1 ) ) ; ( root -> child [ 2 ] -> child ) . push_back ( new Node ( 7 ) ) ; ( root -> child [ 2 ] -> child [ 0 ] -> child ) . push_back ( new Node ( 17 ) ) ; ( root -> child [ 2 ] -> child [ 0 ] -> child ) . push_back ( new Node ( 99 ) ) ; ( root -> child [ 2 ] -> child [ 0 ] -> child ) . push_back ( new Node ( 27 ) ) ; ( root -> child [ 3 ] -> child ) . push_back ( new Node ( 16 ) ) ; int x = 100 ; cout << numberOfSiblings ( root , x ) << endl ; return 0 ; }
Check if all leaves are at same level | C ++ program to check if all leaf nodes are at same level of binary tree ; tree node ; returns a new tree Node ; return true if all leaf nodes are at same level , else false ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check for left child ; if its leaf node ; if it 's first leaf node, then update result ; if it 's not first leaf node, then compare  the level with level of previous leaf node ; check for right child ; if it 's leaf node ; if it 's first leaf node till now,  then update the result ; if it is not the first leaf node , then compare the level with level of previous leaf node ; driver program ; construct a tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * newNode ( int data ) { Node * temp = new Node ( ) ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int checkLevelLeafNode ( Node * root ) { if ( ! root ) return 1 ; queue < Node * > q ; q . push ( root ) ; int result = INT_MAX ; int level = 0 ; while ( ! q . empty ( ) ) { int size = q . size ( ) ; level += 1 ; while ( size > 0 ) { Node * temp = q . front ( ) ; q . pop ( ) ; if ( temp -> left ) { q . push ( temp -> left ) ; if ( ! temp -> left -> right && ! temp -> left -> left ) { if ( result == INT_MAX ) result = level ; else if ( result != level ) return 0 ; } } if ( temp -> right ) { q . push ( temp -> right ) ; if ( ! temp -> right -> left && ! temp -> right -> right ) if ( result == INT_MAX ) result = level ; else if ( result != level ) return 0 ; } size -= 1 ; } } return 1 ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> right = newNode ( 4 ) ; root -> right -> left = newNode ( 5 ) ; root -> right -> right = newNode ( 6 ) ; int result = checkLevelLeafNode ( root ) ; if ( result ) cout << " All ▁ leaf ▁ nodes ▁ are ▁ at ▁ same ▁ level STRNEWLINE " ; else cout << " Leaf ▁ nodes ▁ not ▁ at ▁ same ▁ level STRNEWLINE " ; return 0 ; }
FIFO ( First | C ++ program to demonstrate working of FIFO using Queue interface in C ++ ; print the elements of queue ; Driver code ; Adds elements { 0 , 1 , 2 , 3 , 4 } to queue ; Display contents of the queue . ; To remove the head of queue . In this the oldest element '0' will be removed ; To view the head of queue ; Rest all methods of collection interface , Like size and contains can be used with this implementation .
#include <bits/stdc++.h> NEW_LINE using namespace std ; void print_queue ( queue < int > q ) { while ( ! q . empty ( ) ) { cout << q . front ( ) << " ▁ " ; q . pop ( ) ; } cout << endl ; } int main ( ) { queue < int > q ; for ( int i = 0 ; i < 5 ; i ++ ) q . push ( i ) ; cout << " Elements ▁ of ▁ queue - " ; print_queue ( q ) ; int removedele = q . front ( ) ; q . pop ( ) ; cout << " removed ▁ element - " << removedele << endl ; print_queue ( q ) ; int head = q . front ( ) ; cout << " head ▁ of ▁ queue - " << head << endl ; int size = q . size ( ) ; cout << " Size ▁ of ▁ queue - " << size ; return 0 ; }
LIFO ( Last | C ++ program to demonstrate working of LIFO using stack in C ++ ; Pushing element on the top of the stack ; Popping element from the top of the stack ; Displaying element on the top of the stack ; Searching element in the stack ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; stack < int > stack_push ( stack < int > stack ) { for ( int i = 0 ; i < 5 ; i ++ ) { stack . push ( i ) ; } return stack ; } stack < int > stack_pop ( stack < int > stack ) { cout << " Pop ▁ : " ; for ( int i = 0 ; i < 5 ; i ++ ) { int y = ( int ) stack . top ( ) ; stack . pop ( ) ; cout << ( y ) << endl ; } return stack ; } void stack_peek ( stack < int > stack ) { int element = ( int ) stack . top ( ) ; cout << " Element ▁ on ▁ stack ▁ top ▁ : ▁ " << element << endl ; } void stack_search ( stack < int > stack , int element ) { int pos = -1 , co = 0 ; while ( stack . size ( ) > 0 ) { co ++ ; if ( stack . top ( ) == element ) { pos = co ; break ; } stack . pop ( ) ; } if ( pos == -1 ) cout << " Element ▁ not ▁ found " << endl ; else cout << " Element ▁ is ▁ found ▁ at ▁ position ▁ " << pos << endl ; } int main ( ) { stack < int > stack ; stack = stack_push ( stack ) ; stack = stack_pop ( stack ) ; stack = stack_push ( stack ) ; stack_peek ( stack ) ; stack_search ( stack , 2 ) ; stack_search ( stack , 6 ) ; return 0 ; }
Reversing a queue using recursion | C ++ code for reversing a queue ; Utility function to print the queue ; Recursive function to reverse the queue ; Base case ; Dequeue current item ( from front ) ; Reverse remaining queue ; Enqueue current item ( to rear ) ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printQueue ( queue < long long int > Queue ) { while ( ! Queue . empty ( ) ) { cout << Queue . front ( ) << " ▁ " ; Queue . pop ( ) ; } } void reverseQueue ( queue < long long int > & q ) { if ( q . empty ( ) ) return ; long long int data = q . front ( ) ; q . pop ( ) ; reverseQueue ( q ) ; q . push ( data ) ; } int main ( ) { queue < long long int > Queue ; Queue . push ( 56 ) ; Queue . push ( 27 ) ; Queue . push ( 30 ) ; Queue . push ( 45 ) ; Queue . push ( 85 ) ; Queue . push ( 92 ) ; Queue . push ( 58 ) ; Queue . push ( 80 ) ; Queue . push ( 90 ) ; Queue . push ( 100 ) ; reverseQueue ( Queue ) ; printQueue ( Queue ) ; }
Sorting a Queue without extra space | C ++ program to implement sorting a queue data structure ; Queue elements after sortedIndex are already sorted . This function returns index of minimum element from front to sortedIndex ; This is dequeue ( ) in C ++ STL ; we add the condition i <= sortedIndex because we don 't want to traverse  on the sorted part of the queue,  which is the right part. ; This is enqueue ( ) in C ++ STL ; Moves given minimum element to rear of queue ; SortQueue Function ; driver code ; Sort queue ; Print sorted queue
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minIndex ( queue < int > & q , int sortedIndex ) { int min_index = -1 ; int min_val = INT_MAX ; int n = q . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int curr = q . front ( ) ; q . pop ( ) ; if ( curr <= min_val && i <= sortedIndex ) { min_index = i ; min_val = curr ; } q . push ( curr ) ; } return min_index ; } void insertMinToRear ( queue < int > & q , int min_index ) { int min_val ; int n = q . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int curr = q . front ( ) ; q . pop ( ) ; if ( i != min_index ) q . push ( curr ) ; else min_val = curr ; } q . push ( min_val ) ; } void sortQueue ( queue < int > & q ) { for ( int i = 1 ; i <= q . size ( ) ; i ++ ) { int min_index = minIndex ( q , q . size ( ) - i ) ; insertMinToRear ( q , min_index ) ; } } int main ( ) { queue < int > q ; q . push ( 30 ) ; q . push ( 11 ) ; q . push ( 15 ) ; q . push ( 4 ) ; sortQueue ( q ) ; while ( q . empty ( ) == false ) { cout << q . front ( ) << " ▁ " ; q . pop ( ) ; } cout << endl ; return 0 ; }
Check if removing an edge can divide a Binary Tree in two halves | C ++ program to check if there exist an edge whose removal creates two trees of same size ; utility function to create a new node ; To calculate size of tree with given root ; This function returns true if there is an edge whose removal can divide the tree in two halves n is size of tree ; Base cases ; Check for root ; Check for rest of the nodes ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Now recursively check all nodes ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int x ) { struct Node * temp = new Node ; temp -> data = x ; temp -> left = temp -> right = NULL ; return temp ; } ; int count ( Node * root ) { if ( root == NULL ) return 0 ; return count ( root -> left ) + count ( root -> right ) + 1 ; } bool checkRec ( Node * root , int n ) { if ( root == NULL ) return false ; if ( count ( root ) == n - count ( root ) ) return true ; return checkRec ( root -> left , n ) || checkRec ( root -> right , n ) ; } bool check ( Node * root ) { int n = count ( root ) ; return checkRec ( root , n ) ; } int main ( ) { struct Node * root = newNode ( 5 ) ; root -> left = newNode ( 1 ) ; root -> right = newNode ( 6 ) ; root -> left -> left = newNode ( 3 ) ; root -> right -> left = newNode ( 7 ) ; root -> right -> right = newNode ( 4 ) ; check ( root ) ? printf ( " YES " ) : printf ( " NO " ) ; return 0 ; }
Sliding Window Maximum ( Maximum of all subarrays of size k ) | CPP program for the above approach ; A Dequeue ( Double ended queue ) based method for printing maximum element of all subarrays of size k ; Create a Double Ended Queue , Qi that will store indexes of array elements The queue will store indexes of useful elements in every window and it will maintain decreasing order of values from front to rear in Qi , i . e . , arr [ Qi . front [ ] ] to arr [ Qi . rear ( ) ] are sorted in decreasing order ; Process first k ( or first window ) elements of array ; For every element , the previous smaller elements are useless so remove them from Qi ; Remove from rear ; Add new element at rear of queue ; Process rest of the elements , i . e . , from arr [ k ] to arr [ n - 1 ] ; The element at the front of the queue is the largest element of previous window , so print it ; Remove the elements which are out of this window ; Remove from front of queue ; Remove all elements smaller than the currently being added element ( remove useless elements ) ; Add current element at the rear of Qi ; Print the maximum element of last window ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printKMax ( int arr [ ] , int n , int k ) { std :: deque < int > Qi ( k ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( ( ! Qi . empty ( ) ) && arr [ i ] >= arr [ Qi . back ( ) ] ) Qi . pop_back ( ) ; Qi . push_back ( i ) ; } for ( ; i < n ; ++ i ) { cout << arr [ Qi . front ( ) ] << " ▁ " ; while ( ( ! Qi . empty ( ) ) && Qi . front ( ) <= i - k ) Qi . pop_front ( ) ; while ( ( ! Qi . empty ( ) ) && arr [ i ] >= arr [ Qi . back ( ) ] ) Qi . pop_back ( ) ; Qi . push_back ( i ) ; } cout << arr [ Qi . front ( ) ] ; } int main ( ) { int arr [ ] = { 12 , 1 , 78 , 90 , 57 , 89 , 56 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; printKMax ( arr , n , k ) ; return 0 ; }
Sum of minimum and maximum elements of all subarrays of size k . | C ++ program to find sum of all minimum and maximum elements Of Sub - array Size k . ; Returns sum of min and max element of all subarrays of size k ; Initialize result ; The queue will store indexes of useful elements in every window In deque ' G ' we maintain decreasing order of values from front to rear In deque ' S ' we maintain increasing order of values from front to rear ; Process first window of size K ; Remove all previous greater elements that are useless . ; Remove from rear ; Remove all previous smaller that are elements are useless . ; Remove from rear ; Add current element at rear of both deque ; Process rest of the Array elements ; Element at the front of the deque ' G ' & ' S ' is the largest and smallest element of previous window respectively ; Remove all elements which are out of this window ; remove all previous greater element that are useless ; Remove from rear ; remove all previous smaller that are elements are useless ; Remove from rear ; Add current element at rear of both deque ; Sum of minimum and maximum element of last window ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int SumOfKsubArray ( int arr [ ] , int n , int k ) { int sum = 0 ; deque < int > S ( k ) , G ( k ) ; int i = 0 ; for ( i = 0 ; i < k ; i ++ ) { while ( ( ! S . empty ( ) ) && arr [ S . back ( ) ] >= arr [ i ] ) S . pop_back ( ) ; while ( ( ! G . empty ( ) ) && arr [ G . back ( ) ] <= arr [ i ] ) G . pop_back ( ) ; G . push_back ( i ) ; S . push_back ( i ) ; } for ( ; i < n ; i ++ ) { sum += arr [ S . front ( ) ] + arr [ G . front ( ) ] ; while ( ! S . empty ( ) && S . front ( ) <= i - k ) S . pop_front ( ) ; while ( ! G . empty ( ) && G . front ( ) <= i - k ) G . pop_front ( ) ; while ( ( ! S . empty ( ) ) && arr [ S . back ( ) ] >= arr [ i ] ) S . pop_back ( ) ; while ( ( ! G . empty ( ) ) && arr [ G . back ( ) ] <= arr [ i ] ) G . pop_back ( ) ; G . push_back ( i ) ; S . push_back ( i ) ; } sum += arr [ S . front ( ) ] + arr [ G . front ( ) ] ; return sum ; } int main ( ) { int arr [ ] = { 2 , 5 , -1 , 7 , -3 , -1 , -2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << SumOfKsubArray ( arr , n , k ) ; return 0 ; }