codeparrot/xlcost-text-to-code huggingface dataset:
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | C ++ program for the above approach ; Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Stores the Bitwise XOR of X & Y ; Traverse all possible pairs ; If a pair exists ; If a pair is found ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findWinner ( int X , int Y ) { int playerA = ( X ^ Y ) ; bool flag = false ; for ( int i = 1 ; i <= X ; i ++ ) { for ( int j = 1 ; j <= Y ; j ++ ) { int val = ( i ^ j ) ; if ( val > playerA ) { flag = true ; break ; } } if ( flag ) { break ; } } if ( flag ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; return 0 ; }
Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | C ++ program for the above approach ; Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Check for the invalid condition ; Otherwise , ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findWinner ( int X , int Y ) { int first = ( X ^ Y ) ; int second = ( X + Y ) ; if ( first == second ) { cout << " No " ; } else { cout << " Yes " ; } } int main ( ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; return 0 ; }
Smallest number required to be added to M to make it divisible by N | C ++ program for the above approach ; Function to find the smallest number greater than or equal to N , that is divisible by k ; Function to find the smallest number required to be added to to M to make it divisible by N ; Stores the smallest multiple of N , greater than or equal to M ; Return the result ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } int findSmallest ( int M , int N ) { int x = findNum ( M , N ) ; return x - M ; } int main ( ) { int M = 100 , N = 28 ; cout << findSmallest ( M , N ) ; return 0 ; }
Prefix Factorials of a Prefix Sum Array | C ++ program for the above approach ; Function to find the factorial of prefix sum at every possible index ; Find the prefix sum array ; Stores the factorial of all the element till the sum of array ; Find the factorial array ; Find the factorials of each array element ; Print the resultant array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void prefixFactorialArray ( int A [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) { A [ i ] += A [ i - 1 ] ; } int fact [ A [ N - 1 ] + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= A [ N - 1 ] ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = fact [ A [ i ] ] ; } for ( int i = 0 ; i < N ; i ++ ) { cout << A [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; prefixFactorialArray ( arr , N ) ; return 0 ; }
Maximum frequency of any array element possible by at most K increments | C ++ program for the above approach ; Function to find the maximum possible frequency of a most frequent element after at most K increment operations ; Sort the input array ; Stores the sum of sliding window and the maximum possible frequency of any array element ; Traverse the array ; Add the current element to the window ; If it is not possible to make the array elements in the window equal ; Update the value of sum ; Increment the value of start ; Update the maximum possible frequency ; Print the frequency of the most frequent array element after K increments ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxFrequency ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int start = 0 , end = 0 ; int sum = 0 , res = 0 ; for ( end = 0 ; end < N ; end ++ ) { sum += arr [ end ] ; while ( ( end - start + 1 ) * arr [ end ] - sum > K ) { sum -= arr [ start ] ; start ++ ; } res = max ( res , end - start + 1 ) ; } cout << res << endl ; } int main ( ) { int arr [ ] = { 1 , 4 , 8 , 13 } ; int N = 4 ; int K = 5 ; maxFrequency ( arr , N , K ) ; return 0 ; }
Count pairs ( i , j ) from an array such that i < j and arr [ j ] | C ++ program for the above approach ; Function to count the number of pairs ( i , j ) such that i < j and arr [ i ] - arr [ j ] = X * ( j - i ) ; Stores the count of all such pairs that satisfies the condition . ; Stores count of distinct values of arr [ i ] - x * i ; Iterate over the Map ; Increase count of pairs ; Print the count of such pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int n , int x ) { int count = 0 ; map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] - x * i ] ++ ; } for ( auto x : mp ) { int n = x . second ; count += ( n * ( n - 1 ) ) / 2 ; } cout << count ; } int main ( ) { int n = 6 , x = 3 ; int arr [ ] = { 5 , 4 , 8 , 11 , 13 , 16 } ; countPairs ( arr , n , x ) ; return 0 ; }
Queries to find the sum of weights of all nodes with vertical width from given range in a Binary Tree | C ++ program for the above approach ; Structure of a node ; Function to create new node ; Function to pre - compute the sum of weights at each width position ; Base Case ; Update the current width position weight ; Recursive Call to its left ; Recursive Call to its right ; Function to find the sum of the weights of nodes whose vertical widths lies over the range [ L , R ] for Q queries ; Stores the weight sum of each width position ; Function Call to fill um ; Stores the sum of all previous nodes , while traversing Map ; Traverse the Map um ; Iterate over all queries ; Print the result for the current query [ l , r ] ; Driver Code ; Given Tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left ; Node * right ; } ; Node * newNode ( int d ) { Node * n = new Node ; n -> data = d ; n -> left = NULL ; n -> right = NULL ; return n ; } void findwt ( Node * root , int wt [ ] , map < int , int > & um , int width = 0 ) { if ( root == NULL ) { return ; } um [ width ] += wt [ root -> data ] ; findwt ( root -> left , wt , um , width - 1 ) ; findwt ( root -> right , wt , um , width + 1 ) ; } void solveQueries ( int wt [ ] , Node * root , vector < vector < int > > queries ) { map < int , int > um ; findwt ( root , wt , um ) ; int x = 0 ; for ( auto it = um . begin ( ) ; it != um . end ( ) ; it ++ ) { x += it -> second ; um [ it -> first ] = x ; } for ( int i = 0 ; i < queries . size ( ) ; i ++ ) { int l = queries [ i ] [ 0 ] ; int r = queries [ i ] [ 1 ] ; cout << um [ r ] - um [ l - 1 ] << " STRNEWLINE " ; } } int main ( ) { int N = 8 ; Node * root = newNode ( 1 ) ; root -> left = newNode ( 3 ) ; root -> left -> left = newNode ( 5 ) ; root -> left -> right = newNode ( 6 ) ; root -> right = newNode ( 2 ) ; root -> right -> right = newNode ( 4 ) ; root -> right -> right -> left = newNode ( 7 ) ; root -> right -> right -> right = newNode ( 0 ) ; int wt [ ] = { 8 , 6 , 4 , 5 , 1 , 2 , 9 , 1 } ; vector < vector < int > > queries { { -1 , 1 } , { -2 , -1 } , { 0 , 3 } } ; solveQueries ( wt , root , queries ) ; return 0 ; }
Check if a triplet of buildings can be selected such that the third building is taller than the first building and smaller than the second building | C ++ implementation of the above approach ; Function to check if it is possible to select three buildings that satisfy the given condition ; Stores prefix min array ; Iterate over the range [ 1 , N - 1 ] ; Stores the element from the ending in increasing order ; Iterate until j is greater than or equal to 0 ; If current array element is greater than the prefix min upto j ; Iterate while stack is not empty and top element is less than or equal to preMin [ j ] ; Remove the top element ; If stack is not empty and top element of the stack is less than the current element ; Push the arr [ j ] in stack ; If none of the above case satisfy then return " No " ; Driver code ; Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; string recreationalSpot ( int arr [ ] , int N ) { if ( N < 3 ) { return " No " ; } int preMin [ N ] ; preMin [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { preMin [ i ] = min ( preMin [ i - 1 ] , arr [ i ] ) ; } stack < int > stack ; for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( arr [ j ] > preMin [ j ] ) { while ( ! stack . empty ( ) && stack . top ( ) <= preMin [ j ] ) { stack . pop ( ) ; } if ( ! stack . empty ( ) && stack . top ( ) < arr [ j ] ) { return " Yes " ; } stack . push ( arr [ j ] ) ; } } return " No " ; } int main ( ) { int arr [ ] = { 4 , 7 , 11 , 5 , 13 , 2 } ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << recreationalSpot ( arr , size ) ; }
Check if a number N can be expressed as the sum of powers of X or not | C ++ program for the above approach ; Function to check if the number N can be expressed as the sum of different powers of X or not ; While n is a positive number ; Find the remainder ; If rem is at least 2 , then representation is impossible ; Divide the value of N by x ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool ToCheckPowerofX ( int n , int x ) { while ( n > 0 ) { int rem = n % x ; if ( rem >= 2 ) { return false ; } n = n / x ; } return true ; } int main ( ) { int N = 10 , X = 3 ; if ( ToCheckPowerofX ( N , X ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Find the maximum between N and the number formed by reversing 32 | C ++ program for tha above approach ; Function that obtains the number using said operations from N ; Stores the binary representation of the number N ; Find the binary representation of the number N ; Check for the set bits ; Reverse the string S ; Stores the obtained number ; Calculating the decimal value ; Check for set bits ; Function to find the maximum value between N and the obtained number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int reverseBin ( int N ) { string S = " " ; int i ; for ( i = 0 ; i < 32 ; i ++ ) { if ( N & ( 1LL << i ) ) S += '1' ; else S += '0' ; } reverse ( S . begin ( ) , S . end ( ) ) ; int M = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( S [ i ] == '1' ) M += ( 1LL << i ) ; } return M ; } int maximumOfTwo ( int N ) { int M = reverseBin ( N ) ; return max ( N , M ) ; } int main ( ) { int N = 6 ; cout << maximumOfTwo ( N ) ; return 0 ; }
Sum of Euler Totient Functions obtained for each divisor of N | C ++ program for the above approach ; Function to find the sum of Euler Totient Function of divisors of N ; Return the value of N ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int sumOfDivisors ( int N ) { return N ; } int main ( ) { int N = 5 ; cout << sumOfDivisors ( N ) ; return 0 ; }
Square root of two Complex Numbers | C ++ program for the above approach ; Function to find the square root of a complex number ; Stores all the square roots ; Stores the first square root ; Push the square root in the ans ; Stores the second square root ; If X2 is not 0 ; Push the square root in the array ans [ ] ; Stores the third square root ; If X3 is greater than 0 ; Push the square root in the array ans [ ] ; Stores the fourth square root ; Push the square root in the array ans [ ] ; Prints the square roots ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void complexRoot ( int A , int B ) { vector < pair < double , double > > ans ; double X1 = abs ( sqrt ( ( A + sqrt ( A * A + B * B ) ) / 2 ) ) ; double Y1 = B / ( 2 * X1 ) ; ans . push_back ( { X1 , Y1 } ) ; double X2 = -1 * X1 ; double Y2 = B / ( 2 * X2 ) ; if ( X2 != 0 ) { ans . push_back ( { X2 , Y2 } ) ; } double X3 = ( A - sqrt ( A * A + B * B ) ) / 2 ; if ( X3 > 0 ) { X3 = abs ( sqrt ( X3 ) ) ; double Y3 = B / ( 2 * X3 ) ; ans . push_back ( { X3 , Y3 } ) ; double X4 = -1 * X3 ; double Y4 = B / ( 2 * X4 ) ; if ( X4 != 0 ) { ans . push_back ( { X4 , Y4 } ) ; } } cout << " The ▁ Square ▁ roots ▁ are : ▁ " << endl ; for ( auto p : ans ) { cout << p . first ; if ( p . second > 0 ) cout << " + " ; if ( p . second ) cout << p . second << " * i " << endl ; else cout << endl ; } } int main ( ) { int A = 0 , B = 1 ; complexRoot ( A , B ) ; return 0 ; }
Minimum time required to schedule K processes | C ++ program for the above approach ; Function to find minimum required time to schedule all process ; Stores max element from A [ ] ; Find the maximum element ; Stores frequency of each element ; Stores minimum time required to schedule all process ; Count frequencies of elements ; Find the minimum time ; Decrease the value of K ; Increment tmp [ i / 2 ] ; Increment the count ; Return count , if all process are scheduled ; Increment count ; Return the count ; If it is not possible to schedule all process ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minTime ( int A [ ] , int n , int K ) { int max_ability = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { max_ability = max ( max_ability , A [ i ] ) ; } int tmp [ max_ability + 1 ] = { 0 } ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { tmp [ A [ i ] ] ++ ; } for ( int i = max_ability ; i >= 0 ; i -- ) { if ( tmp [ i ] != 0 ) { if ( tmp [ i ] * i < K ) { K -= ( i * tmp [ i ] ) ; tmp [ i / 2 ] += tmp [ i ] ; count += tmp [ i ] ; if ( K <= 0 ) { return count ; } } else { if ( K % i != 0 ) { count += ( K / i ) + 1 ; } else { count += ( K / i ) ; } return count ; } } } return -1 ; } int main ( ) { int arr [ ] = { 3 , 1 , 7 , 2 , 4 } ; int N = 5 ; int K = 15 ; cout << minTime ( arr , N , K ) ; return 0 ; }
Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts | C ++ program for the above approach ; Function to find GCD of two numbers a and b ; Base Case ; Find GCD recursively ; Function to find the LCM of the resultant array ; Initialize a variable ans as the first element ; Traverse the array ; Update LCM ; Return the minimum length of the rod ; Function to find the minimum length of the rod that can be divided into N equals parts and each part can be further divided into arr [ i ] equal parts ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int findlcm ( int arr [ ] , int n ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } void minimumRod ( int A [ ] , int N ) { cout << N * findlcm ( A , N ) ; } int main ( ) { int arr [ ] = { 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumRod ( arr , N ) ; return 0 ; }
Check if N can be represented as sum of positive integers containing digit D at least once | C ++ program for the above approach ; Function to check if N contains digit D in it ; Iterate until N is positive ; Find the last digit ; If the last digit is the same as digit D ; Return false ; Function to check if the value of N can be represented as sum of integers having digit d in it ; Iterate until N is positive ; Check if N contains digit D or not ; Subtracting D from N ; Return false ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool findDigit ( int N , int D ) { while ( N > 0 ) { int a = N % 10 ; if ( a == D ) { return true ; } N /= 10 ; } return false ; } bool check ( int N , int D ) { while ( N > 0 ) { if ( findDigit ( N , D ) == true ) { return true ; } N -= D ; } return false ; } int main ( ) { int N = 24 ; int D = 7 ; if ( check ( N , D ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }
Number of relations that are neither Reflexive nor Irreflexive on a Set | C ++ program for the above approach ; Function to calculate x ^ y modulo 10 ^ 9 + 7 in O ( log y ) ; Stores the result of ( x ^ y ) ; Update x , if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with res ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of relations that are neither reflexive nor irreflexive ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int mod = 1000000007 ; int power ( long long x , unsigned int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } void countRelations ( int N ) { cout << ( power ( 2 , N ) - 2 ) * power ( 2 , N * N - N ) ; } int main ( ) { int N = 2 ; countRelations ( N ) ; return 0 ; }
Minimum operations required to make all elements in an array of first N odd numbers equal | C ++ program for the above approach ; Function to find the minimum number of operations required to make the array elements equal ; Stores the array elements ; Stores the sum of the array ; Iterate over the range [ 0 , N ] ; Update the value arr [ i ] ; Increment the sum by the value arr [ i ] ; Stores the middle element ; If N is even ; Otherwise ; Stores the result ; Traverse the range [ 0 , N / 2 ] ; Update the value of ans ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int N ) { int arr [ N ] ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = ( 2 * i ) + 1 ; sum = sum + arr [ i ] ; } int mid = 0 ; if ( N % 2 == 0 ) { mid = sum / N ; } else { mid = arr [ N / 2 ] ; } int ans = 0 ; for ( int i = 0 ; i < N / 2 ; i ++ ) { ans += mid - arr [ i ] ; } return ans ; } int main ( ) { int N = 6 ; cout << minOperations ( N ) ; return 0 ; }
Minimum operations required to make all elements in an array of first N odd numbers equal | C ++ code for the above approach ; Function to find the minimum number of operations required to make the array elements equal ; If the value of N is even ; Return the value ; Otherwise , N is odd ; Return the value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( int N ) { if ( N % 2 == 0 ) { return ( N / 2 ) * ( N / 2 ) ; } int k = ( N - 1 ) / 2 ; return k * ( k + 1 ) ; } int main ( ) { int N = 6 ; cout << minOperation ( N ) ; return 0 ; }
Bitwise XOR of Bitwise AND of all pairs from two given arrays | C ++ program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores the result ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; Stores Bitwise AND of the pair { arr1 [ i ] , arr2 [ j ] } ; Update res ; Return the res ; Driver Code ; Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findXORS ( int arr1 [ ] , int arr2 [ ] , int N , int M ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int temp = arr1 [ i ] & arr2 [ j ] ; res ^= temp ; } } return res ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 6 , 5 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int M = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << findXORS ( arr1 , arr2 , N , M ) ; return 0 ; }
Bitwise XOR of Bitwise AND of all pairs from two given arrays | C ++ program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores XOR of array arr1 [ ] ; Stores XOR of array arr2 [ ] ; Traverse the array arr1 [ ] ; Traverse the array arr2 [ ] ; Return the result ; Driver Code ; Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findXORS ( int arr1 [ ] , int arr2 [ ] , int N , int M ) { int XORS1 = 0 ; int XORS2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { XORS1 ^= arr1 [ i ] ; } for ( int i = 0 ; i < M ; i ++ ) { XORS2 ^= arr2 [ i ] ; } return XORS1 and XORS2 ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 6 , 5 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int M = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << findXORS ( arr1 , arr2 , N , M ) ; return 0 ; }
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | C ++ program for the above approach ; Function to check if a number can be expressed as a power of 2 ; f N is power of two ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the resultant count of integers ; Iterate over the range [ 1 , N ] ; Check if i is power of 2 ; Increment the count if i is not power of 2 ; Print the value of count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerof2 ( unsigned int n ) { return ( ( n & ( n - 1 ) ) && n ) ; } void countNum ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { bool flag = isPowerof2 ( i ) ; if ( ! flag ) { count ++ ; } } cout << count << " STRNEWLINE " ; } int main ( ) { int N = 100 ; countNum ( N ) ; return 0 ; }
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | C ++ program for the above approach ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the count of such numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countNum ( int N ) { int ans = log2 ( N ) + 1 ; cout << ans << " STRNEWLINE " ; } int main ( ) { int N = 100 ; countNum ( N ) ; return 0 ; }
Partition a Linked List into K continuous groups with difference in their sizes at most 1 | C ++ program for the above approach ; Link List Node ; Function to insert a node into the Linked List ; Allocate a new dynamic node ; Update new_node -> val ; Stores the head_ref ; Update ( * head_ref ) ; Function to split the linked list in K groups ; Stores the K groups ; If head is NULL ; Iterate until K is non - zero ; Stores the length of the linked list ; Stores the head node of the linked list ; Iterate over the linked list ; Update p ; Update N ; Iterate over the linked list ; Stores the length of the current group ; Stores the current node ; Stores the previous node ; If rem is greater than 0 ; Update p ; Decrement rem by 1 ; Iterate until x is non - zero ; If the last is equal to p ; Otherwise ; Join the link between last and the current element ; Update the last node ; Update p node ; Assign NULL to last -> next ; Push the current linked list in ans ; Decrement K ; While K greater than 0 ; Update the value of ans ; Increment K ; Print the result ; Print the value ; Update ans [ i ] ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct ListNode { int val ; struct ListNode * next ; } ; void push ( ListNode * * head_ref , int node_val ) { ListNode * new_node = new ListNode ( ) ; new_node -> val = node_val ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } void splitListInParts ( ListNode * head , int K ) { vector < ListNode * > ans ; if ( ! head ) { while ( K -- ) ans . push_back ( NULL ) ; } int N = 0 ; ListNode * p = head ; while ( p ) { p = p -> next ; N ++ ; } int len = N / K ; int rem = N % K ; p = head ; while ( K > 0 && p ) { int x = len ; ListNode * curr_head = p ; ListNode * last = p ; if ( rem > 0 ) { p = p -> next ; rem -- ; } while ( x -- ) { if ( last == p ) p = p -> next ; else { last -> next = p ; last = p ; p = p -> next ; } } last -> next = NULL ; ans . push_back ( curr_head ) ; K -- ; } while ( K > 0 ) { ans . push_back ( NULL ) ; K -- ; } cout << " { " ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { cout << " { " ; while ( ans [ i ] ) { cout << ans [ i ] -> val << " ▁ " ; ans [ i ] = ans [ i ] -> next ; } cout << " } " ; if ( i != ans . size ( ) - 1 ) cout << " , ▁ " ; } cout << " } " ; } int main ( ) { ListNode * root = NULL ; push ( & root , 8 ) ; push ( & root , 7 ) ; push ( & root , 6 ) ; push ( & root , 5 ) ; push ( & root , 4 ) ; push ( & root , 3 ) ; push ( & root , 2 ) ; push ( & root , 1 ) ; int K = 3 ; splitListInParts ( root , K ) ; return 0 ; }