codeparrot/xlcost-text-to-code huggingface dataset:
Program to print an array in Pendulum Arrangement with constant space | C ++ implementation of the approach ; Function to print the Pendulum arrangement of the given array ; Sort the array ; pos stores the index of the last element of the array ; odd stores the last odd index in the array ; Move all odd index positioned elements to the right ; Shift the elements by one position from odd to pos ; Reverse the element from 0 to ( n - 1 ) / 2 ; Printing the pendulum arrangement ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pendulumArrangement ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int odd , temp , in , pos ; pos = n - 1 ; if ( n % 2 == 0 ) odd = n - 1 ; else odd = n - 2 ; while ( odd > 0 ) { temp = arr [ odd ] ; in = odd ; while ( in != pos ) { arr [ in ] = arr [ in + 1 ] ; in ++ ; } arr [ in ] = temp ; odd = odd - 2 ; pos = pos - 1 ; } int start = 0 , end = ( n - 1 ) / 2 ; for ( ; start < end ; start ++ , end -- ) { temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 11 , 2 , 4 , 55 , 6 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pendulumArrangement ( arr , n ) ; return 0 ; }
Find all the pairs with given sum in a BST | Set 2 | C ++ implementation of the above approach ; A binary tree node ; Function to add a node to the BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Function to find the target pairs ; LeftList which stores the left side values ; RightList which stores the right side values ; curr_left pointer is used for left side execution and curr_right pointer is used for right side execution ; Storing the left side values into LeftList till leaf node not found ; Storing the right side values into RightList till leaf node not found ; Last node of LeftList ; Last node of RightList ; To prevent repetition like 2 , 6 and 6 , 2 ; Delete the last value of LeftList and make the execution to the right side ; Delete the last value of RightList and make the execution to the left side ; ( left value + right value ) = target then print the left value and right value Delete the last value of left and right list and make the left execution to right side and right side execution to left side ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right , * root ; Node ( int data ) { this -> data = data ; left = NULL ; right = NULL ; root = NULL ; } } ; Node * AddNode ( Node * root , int data ) { if ( root == NULL ) { root = new Node ( data ) ; return root ; } if ( root -> data < data ) root -> right = AddNode ( root -> right , data ) ; else if ( root -> data > data ) root -> left = AddNode ( root -> left , data ) ; return root ; } void TargetPair ( Node * node , int tar ) { vector < Node * > LeftList ; vector < Node * > RightList ; Node * curr_left = node ; Node * curr_right = node ; while ( curr_left != NULL || curr_right != NULL || LeftList . size ( ) > 0 && RightList . size ( ) > 0 ) { while ( curr_left != NULL ) { LeftList . push_back ( curr_left ) ; curr_left = curr_left -> left ; } while ( curr_right != NULL ) { RightList . push_back ( curr_right ) ; curr_right = curr_right -> right ; } Node * LeftNode = LeftList [ LeftList . size ( ) - 1 ] ; Node * RightNode = RightList [ RightList . size ( ) - 1 ] ; int leftVal = LeftNode -> data ; int rightVal = RightNode -> data ; if ( leftVal >= rightVal ) break ; if ( leftVal + rightVal < tar ) { LeftList . pop_back ( ) ; curr_left = LeftNode -> right ; } else if ( leftVal + rightVal > tar ) { RightList . pop_back ( ) ; curr_right = RightNode -> left ; } else { cout << LeftNode -> data << " ▁ " << RightNode -> data << endl ; RightList . pop_back ( ) ; LeftList . pop_back ( ) ; curr_left = LeftNode -> right ; curr_right = RightNode -> left ; } } } int main ( ) { Node * root = NULL ; root = AddNode ( root , 2 ) ; root = AddNode ( root , 6 ) ; root = AddNode ( root , 5 ) ; root = AddNode ( root , 3 ) ; root = AddNode ( root , 4 ) ; root = AddNode ( root , 1 ) ; root = AddNode ( root , 7 ) ; int sum = 8 ; TargetPair ( root , sum ) ; }
Minimum number greater than the maximum of array which cannot be formed using the numbers in the array | C ++ implementation of the approach ; Function that returns the minimum number greater than maximum of the array that cannot be formed using the elements of the array ; Sort the given array ; Maximum number in the array ; table [ i ] will store the minimum number of elements from the array to form i ; Calculate the minimum number of elements from the array required to form the numbers from 1 to ( 2 * max ) ; If there exists a number greater than the maximum element of the array that can be formed using the numbers of array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int max = arr [ n - 1 ] ; int table [ ( 2 * max ) + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) table [ i ] = INT_MAX ; int ans = -1 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] <= i ) { int res = table [ i - arr [ j ] ] ; if ( res != INT_MAX && res + 1 < table [ i ] ) table [ i ] = res + 1 ; } } if ( i > arr [ n - 1 ] && table [ i ] == INT_MAX ) { ans = i ; break ; } } return ans ; } int main ( ) { int arr [ ] = { 6 , 7 , 15 } ; int n = ( sizeof ( arr ) / sizeof ( int ) ) ; cout << findNumber ( arr , n ) ; return 0 ; }
Product of all Subsequences of size K except the minimum and maximum Elements | C ++ program to find product of all Subsequences of size K except the minimum and maximum Elements ; 2D array to store value of combinations nCr ; Function to pre - calculate value of all combinations nCr ; Function to calculate product of all subsequences except the minimum and maximum elements ; Sorting array so that it becomes easy to calculate the number of times an element will come in first or last place ; An element will occur ' powa ' times in total of which ' powla ' times it will be last element and ' powfa ' times it will be first element ; In total it will come powe = powa - powla - powfa times ; Multiplying a [ i ] powe times using Fermat Little Theorem under MODulo MOD for fast exponentiation ; Driver Code ; pre - calculation of all combinations
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MOD  1000000007 NEW_LINE #define ll  long long NEW_LINE #define max  101 NEW_LINE ll C [ max - 1 ] [ max - 1 ] ; ll power ( ll x , unsigned ll y ) { unsigned ll res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y & 1 ) { res = ( res * x ) % MOD ; } y = y >> 1 ; x = ( x * x ) % MOD ; } return res % MOD ; } void combi ( int n , int k ) { int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] % MOD + C [ i - 1 ] [ j ] % MOD ) % MOD ; } } } unsigned ll product ( ll a [ ] , int n , int k ) { unsigned ll ans = 1 ; sort ( a , a + n ) ; ll powa = C [ n - 1 ] [ k - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { ll powla = C [ i ] [ k - 1 ] ; ll powfa = C [ n - i - 1 ] [ k - 1 ] ; ll powe = ( ( powa % MOD ) - ( powla + powfa ) % MOD + MOD ) % MOD ; unsigned ll mul = power ( a [ i ] , powe ) % MOD ; ans = ( ( ans % MOD ) * ( mul % MOD ) ) % MOD ; } return ans % MOD ; } int main ( ) { combi ( 100 , 100 ) ; ll arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof arr [ 0 ] ; int k = 3 ; unsigned ll ans = product ( arr , n , k ) ; cout << ans << endl ; return 0 ; }
Bitwise AND of N binary strings | C ++ implementation of the above approach ; Helper method : given two unequal sized bit strings , converts them to same length by adding leading 0 s in the smaller string . Returns the the new length ; Return len_b which is highest . No need to proceed further ! ; Return len_a which is greater or equal to len_b ; The main function that performs AND operation of two - bit sequences and returns the resultant string ; Make both strings of same length with the maximum length of s1 & s2 . ; Initialize res as NULL string ; We start from left to right as we have made both strings of same length . ; Convert s1 [ i ] and s2 [ i ] to int and perform bitwise AND operation , append to " res " string ; Driver Code ; Check corner case : If there is just one binary string , then print it and return .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int makeEqualLength ( string & a , string & b ) { int len_a = a . length ( ) ; int len_b = b . length ( ) ; int num_zeros = abs ( len_a - len_b ) ; if ( len_a < len_b ) { for ( int i = 0 ; i < num_zeros ; i ++ ) { a = '0' + a ; } return len_b ; } else { for ( int i = 0 ; i < num_zeros ; i ++ ) { b = '0' + b ; } } return len_a ; } string andOperationBitwise ( string s1 , string s2 ) { int length = makeEqualLength ( s1 , s2 ) ; string res = " " ; for ( int i = 0 ; i < length ; i ++ ) { res = res + ( char ) ( ( s1 [ i ] - '0' & s2 [ i ] - '0' ) + '0' ) ; } return res ; } int main ( ) { string arr [ ] = { "101" , "110110" , "111" } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string result ; if ( n < 2 ) { cout << arr [ n - 1 ] << endl ; return 0 ; } result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { result = andOperationBitwise ( result , arr [ i ] ) ; } cout << result << endl ; }
Maximum number of segments that can contain the given points | C ++ implementation of the approach ; Function to return the maximum number of segments ; Sort both the vectors ; Initially pointing to the first element of b [ ] ; Try to find a match in b [ ] ; The segment ends before b [ j ] ; The point lies within the segment ; The segment starts after b [ j ] ; Return the required count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPoints ( int n , int m , vector < int > a , vector < int > b , int x , int y ) { sort ( a . begin ( ) , a . end ( ) ) ; sort ( b . begin ( ) , b . end ( ) ) ; int j = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( a [ i ] + y < b [ j ] ) break ; if ( b [ j ] >= a [ i ] - x && b [ j ] <= a [ i ] + y ) { count ++ ; j ++ ; break ; } else j ++ ; } } return count ; } int main ( ) { int x = 1 , y = 4 ; vector < int > a = { 1 , 5 } ; int n = a . size ( ) ; vector < int > b = { 1 , 1 , 2 } ; int m = a . size ( ) ; cout << countPoints ( n , m , a , b , x , y ) ; return 0 ; }
Merge K sorted arrays | Set 3 ( Using Divide and Conquer Approach ) | C ++ program to merge K sorted arrays ; Merge and sort k arrays ; Put the elements in sorted array . ; Sort the output array ; Driver Function ; Input 2D - array ; Number of arrays ; Output array ; Print merged array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  4 NEW_LINE void merge_and_sort ( int * output , int arr [ ] [ N ] , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { output [ i * n + j ] = arr [ i ] [ j ] ; } } sort ( output , output + n * k ) ; } int main ( ) { int arr [ ] [ N ] = { { 5 , 7 , 15 , 18 } , { 1 , 8 , 9 , 17 } , { 1 , 4 , 7 , 7 } } ; int n = N ; int k = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int * output = new int [ n * k ] ; merge_and_sort ( output , arr , n , k ) ; for ( int i = 0 ; i < n * k ; i ++ ) cout << output [ i ] << " ▁ " ; return 0 ; }
Minimum operations of given type to make all elements of a matrix equal | C ++ implementation of the approach ; Function to return the minimum number of operations required ; Create another array to store the elements of matrix ; will not work for negative elements , so . . adding this ; adding this to handle negative elements too . ; Sort the array to get median ; To count the minimum operations ; If there are even elements , then there are two medians . We consider the best of two as answer . ; changed here as in case of even elements there will be 2 medians ; Return minimum operations required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int n , int m , int k , vector < vector < int > > & matrix ) { vector < int > arr ; int mod ; if ( matrix [ 0 ] [ 0 ] < 0 ) { mod = k - ( abs ( matrix [ 0 ] [ 0 ] ) % k ) ; } else { mod = matrix [ 0 ] [ 0 ] % k ; } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr . push_back ( matrix [ i ] [ j ] ) ; int val = matrix [ i ] [ j ] ; if ( val < 0 ) { int res = k - ( abs ( val ) % k ) ; if ( res != mod ) { return -1 ; } } else { int foo = matrix [ i ] [ j ] ; if ( foo % k != mod ) { return -1 ; } } } } sort ( arr . begin ( ) , arr . end ( ) ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += abs ( arr [ i ] - median2 ) / k ; minOperations = min ( minOperations , minOperations2 ) ; } return minOperations ; } int main ( ) { vector < vector < int > > matrix = { { 2 , 4 , 6 } , { 8 , 10 , 12 } , { 14 , 16 , 18 } , { 20 , 22 , 24 } } ; int n = matrix . size ( ) ; int m = matrix [ 0 ] . size ( ) ; int k = 2 ; cout << minOperations ( n , m , k , matrix ) ; return 0 ; }
Smallest subarray containing minimum and maximum values | C ++ implementation of above approach ; Function to return length of smallest subarray containing both maximum and minimum value ; find maximum and minimum values in the array ; iterate over the array and set answer to smallest difference between position of maximum and position of minimum value ; last occurrence of minValue ; last occurrence of maxValue ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSubarray ( int A [ ] , int n ) { int minValue = * min_element ( A , A + n ) ; int maxValue = * max_element ( A , A + n ) ; int pos_min = -1 , pos_max = -1 , ans = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == minValue ) pos_min = i ; if ( A [ i ] == maxValue ) pos_max = i ; if ( pos_max != -1 and pos_min != -1 ) ans = min ( ans , abs ( pos_min - pos_max ) + 1 ) ; } return ans ; } int main ( ) { int A [ ] = { 1 , 5 , 9 , 7 , 1 , 9 , 4 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minSubarray ( A , n ) ; return 0 ; }
Minimum number of consecutive sequences that can be formed in an array | C ++ program find the minimum number of consecutive sequences in an array ; Driver program ; function call to print required answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSequences ( int arr [ ] , int n ) { int count = 1 ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; return count ; } int main ( ) { int arr [ ] = { 1 , 7 , 3 , 5 , 10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSequences ( arr , n ) ; return 0 ; }
Minimum number of increment / decrement operations such that array contains all elements from 1 to N | C ++ implementation of the above approach ; Function to find the minimum operations ; Sort the given array ; Count operations by assigning a [ i ] = i + 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long minimumMoves ( int a [ ] , int n ) { long long operations = 0 ; sort ( a , a + n ) ; for ( int i = 0 ; i < n ; i ++ ) operations += abs ( a [ i ] - ( i + 1 ) ) ; return operations ; } int main ( ) { int arr [ ] = { 5 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumMoves ( arr , n ) ; return 0 ; }
Print a case where the given sorting algorithm fails | C ++ program to find a case where the given algorithm fails ; Function to print a case where the given sorting algorithm fails ; only case where it fails ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCase ( int n ) { if ( n <= 2 ) { cout << -1 ; return ; } for ( int i = n ; i >= 1 ; i -- ) cout << i << " ▁ " ; } int main ( ) { int n = 3 ; printCase ( n ) ; return 0 ; }
Find the missing elements from 1 to M in given N ranges | C ++ program to find missing elements from given Ranges ; Function to find missing elements from given Ranges ; First of all sort all the given ranges ; store ans in a different vector ; prev is use to store end of last range ; j is used as a counter for ranges ; for last segment ; finally print all answer ; Driver code ; Store ranges in vector of pair
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMissingNumber ( vector < pair < int , int > > ranges , int m ) { sort ( ranges . begin ( ) , ranges . end ( ) ) ; vector < int > ans ; int prev = 0 ; for ( int j = 0 ; j < ranges . size ( ) ; j ++ ) { int start = ranges [ j ] . first ; int end = ranges [ j ] . second ; for ( int i = prev + 1 ; i < start ; i ++ ) ans . push_back ( i ) ; prev = end ; } for ( int i = prev + 1 ; i <= m ; i ++ ) ans . push_back ( i ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { if ( ans [ i ] <= m ) cout << ans [ i ] << " ▁ " ; } } int main ( ) { int N = 2 , M = 6 ; vector < pair < int , int > > ranges ; ranges . push_back ( { 1 , 2 } ) ; ranges . push_back ( { 4 , 5 } ) ; findMissingNumber ( ranges , M ) ; return 0 ; }
Check whether it is possible to make both arrays equal by modifying a single element | C ++ implementation of the above approach ; Function to check if both sequences can be made equal ; Sorting both the arrays ; Flag to tell if there are more than one mismatch ; To stores the index of mismatched element ; If there is more than one mismatch then return False ; If there is no mismatch or the difference between the mismatching elements is <= k then return true ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; static bool check ( int n , int k , int * a , int * b ) { sort ( a , a + n ) ; sort ( b , b + n ) ; bool fl = false ; int ind = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { if ( fl == true ) { return false ; } fl = true ; ind = i ; } } if ( ind == -1 | abs ( a [ ind ] - b [ ind ] ) <= k ) { return true ; } return false ; } int main ( ) { int n = 2 , k = 4 ; int a [ ] = { 1 , 5 } ; int b [ ] = { 1 , 1 } ; if ( check ( n , k , a , b ) ) { printf ( " Yes " ) ; } else { printf ( " No " ) ; } return 0 ; }
Sum of width ( max and min diff ) of all Subsequences | CPP implementation of above approach ; Function to return sum of width of all subsets ; Sort the array ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MOD  1000000007 NEW_LINE int SubseqWidths ( int A [ ] , int n ) { sort ( A , A + n ) ; int pow2 [ n ] ; pow2 [ 0 ] = 1 ; for ( int i = 1 ; i < n ; ++ i ) pow2 [ i ] = ( pow2 [ i - 1 ] * 2 ) % MOD ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) ans = ( ans + ( pow2 [ i ] - pow2 [ n - 1 - i ] ) * A [ i ] ) % MOD ; return ans ; } int main ( ) { int A [ ] = { 5 , 6 , 4 , 3 , 8 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << SubseqWidths ( A , n ) ; return 0 ; }
Covering maximum array elements with given value | C ++ implementation of the approach ; Function to find value for covering maximum array elements ; sort the students in ascending based on the candies ; To store the number of happy students ; To store the running sum ; If the current student can 't be made happy ; increment the count if we can make the ith student happy ; If the sum = x then answer is n ; If the count is equal to n then the answer is n - 1 ; Driver function
# include <bits/stdc++.h> NEW_LINE using namespace std ; int maxArrayCover ( vector < int > a , int n , int x ) { sort ( a . begin ( ) , a . end ( ) ) ; int cc = 0 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += a [ i ] ; if ( s > x ) { break ; } cc += 1 ; } if ( accumulate ( a . begin ( ) , a . end ( ) , 0 ) == x ) { return n ; } else { if ( cc == n ) { return n - 1 ; } else { return cc ; } } } int main ( ) { int n = 3 ; int x = 70 ; vector < int > a = { 10 , 20 , 30 } ; printf ( " % d STRNEWLINE " , maxArrayCover ( a , n , x ) ) ; return 0 ; }
Quotient | CPP program to implement Quotient - Remainder Sort ; max_element finds maximum element in an array ; min_element finds minimum element in an array ; Creating a ROW * COL matrix of all zeros ; Driver Code
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void QRsort ( int arr [ ] , int size ) { int MAX = * max_element ( arr , arr + size ) ; int MIN = * min_element ( arr , arr + size ) ; cout << " Maximum ▁ Element ▁ found ▁ is ▁ : ▁ " << MAX << endl ; cout << " Minimum ▁ Element ▁ found ▁ is ▁ : ▁ " << MIN << endl ; int COL = MIN ; int ROW = MAX / MIN + 1 ; int matrix [ ROW ] [ COL ] = { 0 } ; for ( int i = 0 ; i < size ; i ++ ) { int quotient = arr [ i ] / MIN ; int remainder = arr [ i ] % MIN ; matrix [ quotient ] [ remainder + 1 ] = arr [ i ] ; } int k = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( matrix [ i ] [ j ] != 0 ) { arr [ k ++ ] = matrix [ i ] [ j ] ; } } } } void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 5 , 3 , 7 , 4 , 8 , 2 , 6 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Initial ▁ Array ▁ : ▁ " << endl ; printArray ( arr , size ) ; QRsort ( arr , size ) ; cout << " Array ▁ after ▁ sorting ▁ : ▁ " << endl ; printArray ( arr , size ) ; }
Check if a Linked List is Pairwise Sorted | C ++ program to check if linked list is pairwise sorted ; A linked list node ; Function to check if linked list is pairwise sorted ; Traverse further only if there are at - least two nodes left ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver program to test above function ; The constructed linked list is : 10 -> 15 -> 9 -> 9 -> 1 -> 5
#include <iostream> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next ; } ; bool isPairWiseSorted ( struct Node * head ) { bool flag = true ; struct Node * temp = head ; while ( temp != NULL && temp -> next != NULL ) { if ( temp -> data > temp -> next -> data ) { flag = false ; break ; } temp = temp -> next -> next ; } return flag ; } void push ( struct Node * * head_ref , int new_data ) { struct Node * new_node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int main ( ) { struct Node * start = NULL ; push ( & start , 5 ) ; push ( & start , 1 ) ; push ( & start , 9 ) ; push ( & start , 9 ) ; push ( & start , 15 ) ; push ( & start , 10 ) ; if ( isPairWiseSorted ( start ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Maximum Sum of Products of Two Arrays | CPP program to calculate maximum sum of products of two arrays ; Function that calculates maximum sum of products of two arrays ; Variable to store the sum of products of array elements ; length of the arrays ; Sorting both the arrays ; Traversing both the arrays and calculating sum of product ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSOP ( int * a , int * b ) { int sop = 0 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; sort ( a , a + n + 1 ) ; sort ( b , b + n + 1 ) ; for ( int i = 0 ; i <= n ; i ++ ) { sop += a [ i ] * b [ i ] ; } return sop ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 4 , 5 , 1 } ; cout << maximumSOP ( A , B ) ; return 0 ; }