codeparrot/xlcost-text-to-code huggingface dataset:
Check if an array contains all elements of a given range | C ++ Code for Check if an array contains all elements of a given range ; Function to check the array for elements in given range ; Range is the no . of elements that are to be checked ; Traversing the array ; If an element is in range ; Checking whether elements in range 0 - range are negative ; Element from range is missing from array ; All range elements are present ; Driver code ; Defining Array and size ; A is lower limit and B is the upper limit of range ; True denotes all elements were present ; False denotes any element was not present
#include <iostream> NEW_LINE using namespace std ; bool check_elements ( int arr [ ] , int n , int A , int B ) { int range = B - A ; for ( int i = 0 ; i < n ; i ++ ) { if ( abs ( arr [ i ] ) >= A && abs ( arr [ i ] ) <= B ) { int z = abs ( arr [ i ] ) - A ; if ( arr [ z ] > 0 ) { arr [ z ] = arr [ z ] * -1 ; } } } int count = 0 ; for ( int i = 0 ; i <= range && i < n ; i ++ ) { if ( arr [ i ] > 0 ) return false ; else count ++ ; } if ( count != ( range + 1 ) ) return false ; return true ; } int main ( ) { int arr [ ] = { 1 , 4 , 5 , 2 , 7 , 8 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int A = 2 , B = 5 ; if ( check_elements ( arr , n , A , B ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Recursive Programs to find Minimum and Maximum elements of array | Recursive C ++ program to find minimum ; function to print Minimum element using recursion ; if size = 0 means whole array has been traversed ; driver code to test above function
#include <iostream> NEW_LINE using namespace std ; int findMinRec ( int A [ ] , int n ) { if ( n == 1 ) return A [ 0 ] ; return min ( A [ n - 1 ] , findMinRec ( A , n - 1 ) ) ; } int main ( ) { int A [ ] = { 1 , 4 , 45 , 6 , -50 , 10 , 2 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << findMinRec ( A , n ) ; return 0 ; }
Allocate minimum number of pages | C ++ program for optimal allocation of pages ; Utility function to check if current minimum value is feasible or not . ; iterate over all books ; check if current number of pages are greater than curr_min that means we will get the result after mid no . of pages ; count how many students are required to distribute curr_min pages ; increment student count ; update curr_sum ; if students required becomes greater than given no . of students , return false ; else update curr_sum ; function to find minimum pages ; return - 1 if no . of books is less than no . of students ; Count total number of pages ; initialize start as 0 pages and end as total pages ; traverse until start <= end ; check if it is possible to distribute books by using mid as current minimum ; update result to current distribution as it 's the best we have found till now. ; as we are finding minimum and books are sorted so reduce end = mid - 1 that means ; if not possible means pages should be increased so update start = mid + 1 ; at - last return minimum no . of pages ; Drivers code ; Number of pages in books ; int m = 2 ; No . of students
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossible ( int arr [ ] , int n , int m , int curr_min ) { int studentsRequired = 1 ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > curr_min ) return false ; if ( curr_sum + arr [ i ] > curr_min ) { studentsRequired ++ ; curr_sum = arr [ i ] ; if ( studentsRequired > m ) return false ; } else curr_sum += arr [ i ] ; } return true ; } int findPages ( int arr [ ] , int n , int m ) { long long sum = 0 ; if ( n < m ) return -1 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int start = 0 , end = sum ; int result = INT_MAX ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( isPossible ( arr , n , m , mid ) ) { result = mid ; end = mid - 1 ; } else start = mid + 1 ; } return result ; } int main ( ) { int arr [ ] = { 12 , 34 , 67 , 90 } ; int n = sizeof arr / sizeof arr [ 0 ] ; cout << " Minimum ▁ number ▁ of ▁ pages ▁ = ▁ " << findPages ( arr , n , m ) << endl ; return 0 ; }
Maximum number of plates that can be placed from top to bottom in increasing order of size | C ++ Program for the above approach ; Comparator function to sort plates in decreasing order of area ; Recursive function to count and return the max number of plates that can be placed ; If no plate remains ; If length and width of previous plate exceeds that of the current plate ; Calculate including the plate ; Calculate excluding the plate ; Otherwise ; Calculate only excluding the plate ; Driver code ; Sorting plates in decreasing order of area ; Assuming first plate to be of maximum size
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool comp ( vector < int > v1 , vector < int > v2 ) { return v1 [ 0 ] * v1 [ 1 ] > v2 [ 0 ] * v2 [ 1 ] ; } int countPlates ( vector < vector < int > > & plates , int lastLength , int lastWidth , int i , int n ) { if ( i == n ) return 0 ; int taken = 0 , notTaken = 0 ; if ( lastLength > plates [ i ] [ 0 ] && lastWidth > plates [ i ] [ 1 ] ) { taken = 1 + countPlates ( plates , plates [ i ] [ 0 ] , plates [ i ] [ 1 ] , i + 1 , n ) ; notTaken = countPlates ( plates , lastLength , lastWidth , i + 1 , n ) ; } else notTaken = countPlates ( plates , lastLength , lastWidth , i + 1 , n ) ; return max ( taken , notTaken ) ; } int main ( ) { vector < vector < int > > plates = { { 6 , 4 } , { 5 , 7 } , { 1 , 2 } , { 3 , 3 } , { 7 , 9 } } ; int n = plates . size ( ) ; sort ( plates . begin ( ) , plates . end ( ) , comp ) ; int lastLength = INT_MAX ; int lastWidth = INT_MAX ; cout << countPlates ( plates , lastLength , lastWidth , 0 , n ) ; return 0 ; }
Smallest element with K set bits such that sum of Bitwise AND of each array element with K is maximum | C ++ program for the above approach ; Comparator function to sort the vector of pairs ; Function to find the value of X such that Bitwise AND of all array elements with X is maximum ; Stores the count of set bit at each position ; Stores the resultant value of X ; Calculate the count of set bits at each position ; If the jth bit is set ; Stores the contribution of each set bit ; Store all bit and amount of contribution ; Find the total contribution ; Sort V [ ] in decreasing order of second parameter ; Choose exactly K set bits ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool comp ( pair < int , int > & a , pair < int , int > & b ) { if ( a . second != b . second ) return a . second > b . second ; return a . first < b . first ; } int maximizeSum ( int arr [ ] , int n , int k ) { vector < int > cnt ( 30 , 0 ) ; int X = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 30 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) cnt [ j ] ++ ; } } vector < pair < int , int > > v ; for ( int i = 0 ; i < 30 ; i ++ ) { int gain = cnt [ i ] * ( 1 << i ) ; v . push_back ( { i , gain } ) ; } sort ( v . begin ( ) , v . end ( ) , comp ) ; for ( int i = 0 ; i < k ; i ++ ) { X |= ( 1 << v [ i ] . first ) ; } cout << X ; } int main ( ) { int arr [ ] = { 3 , 4 , 5 , 1 } ; int K = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximizeSum ( arr , N , K ) ; return 0 ; }
Largest Roman Numeral possible by rearranging characters of a given string | C ++ implementation of the above approach ; Function to find decimal value of a roman character ; Function to convert string representing roman number to equivalent decimal value ; Stores the decimal value of the string S ; Stores the size of the S ; Update res ; Traverse the string ; Return res ; Function to convert decimal to equivalent roman numeral ; Stores the string ; Stores all the digit values of a roman digit ; Iterate while number is greater than 0 ; Return res ; Function to sort the string in descending order of values assigned to characters ; Function to find largest roman value possible by rearranging the characters of the string ; Stores all roman characters ; Traverse the string ; If X is not found ; Stores the decimal value of the roman number ; Find the roman value equivalent to the decimal value of N ; Return result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int charVal ( char c ) { if ( c == ' I ' ) return 1 ; if ( c == ' V ' ) return 5 ; if ( c == ' X ' ) return 10 ; if ( c == ' L ' ) return 50 ; if ( c == ' C ' ) return 100 ; if ( c == ' D ' ) return 500 ; if ( c == ' M ' ) return 1000 ; return -1 ; } int romanToDec ( string S ) { int res = 0 ; int n = S . size ( ) ; res = charVal ( S [ n - 1 ] ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( charVal ( S [ i ] ) < charVal ( S [ i + 1 ] ) ) res -= charVal ( S [ i ] ) ; else res += charVal ( S [ i ] ) ; } return res ; } string DecToRoman ( int number ) { string res = " " ; int num [ ] = { 1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 } ; string sym [ ] = { " I " , " IV " , " V " , " IX " , " X " , " XL " , " L " , " XC " , " C " , " CD " , " D " , " CM " , " M " } ; int i = 12 ; while ( number > 0 ) { int div = number / num [ i ] ; number = number % num [ i ] ; while ( div -- ) { res += sym [ i ] ; } i -- ; } return res ; } bool compare ( char x , char y ) { int val_x = charVal ( x ) ; int val_y = charVal ( y ) ; return ( val_x > val_y ) ; } string findLargest ( string S ) { set < char > st = { ' I ' , ' V ' , ' X ' , ' L ' , ' C ' , ' D ' , ' M ' } ; for ( auto x : S ) { if ( st . find ( x ) == st . end ( ) ) return " Invalid " ; } sort ( S . begin ( ) , S . end ( ) , compare ) ; int N = romanToDec ( S ) ; string R = DecToRoman ( N ) ; if ( S != R ) return " Invalid " ; return S ; } int main ( ) { string S = " MCMIV " ; cout << findLargest ( S ) ; }
Find Minimum Depth of a Binary Tree | C ++ program to find minimum depth of a given Binary Tree ; A BT Node ; Function to calculate the minimum depth of the tree ; Corner case . Should never be hit unless the code is called on root = NULL ; Base case : Leaf Node . This accounts for height = 1. ; If left subtree is not NULL , recur for left subtree ; If right subtree is not NULL , recur for right subtree ; Utility function to create new Node ; Driver program ; Let us construct the Tree shown in the above figure
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; int minDepth ( Node * root ) { if ( root == NULL ) return 0 ; if ( root -> left == NULL && root -> right == NULL ) return 1 ; int l = INT_MAX , r = INT_MAX ; if ( root -> left ) l = minDepth ( root -> left ) ; if ( root -> right ) r = minDepth ( root -> right ) ; return min ( l , r ) + 1 ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << " The ▁ minimum ▁ depth ▁ of ▁ binary ▁ tree ▁ is ▁ : ▁ " << minDepth ( root ) ; return 0 ; }
Remove an occurrence of most frequent array element exactly K times | C ++ program for the above approach ; Function to print the most frequent array element exactly K times ; Stores frequency array element ; Count frequency of array element ; Maximum array element ; Traverse the Map ; Find the element with maximum frequency ; If the frequency is maximum , store that number in element ; Print element as it contains the element having highest frequency ; Decrease the frequency of the maximum array element ; Reduce the number of operations ; Driver Code ; Given array ; Size of the array ; Given K
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxFreqElements ( int arr [ ] , int N , int K ) { map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } while ( K > 0 ) { int max = 0 ; int element ; for ( auto i : mp ) { if ( i . second > max ) { max = i . second ; element = i . first ; } } cout << element << " ▁ " ; mp [ element ] -- ; K -- ; } } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 1 , 4 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; maxFreqElements ( arr , N , K ) ; return 0 ; }
Maximize difference between maximum and minimum array elements after K operations | C ++ program to implement the above approach ; Function to find the maximum difference between the maximum and minimum in the array after K operations ; Stores maximum difference between largest and smallest array element ; Sort the array in descending order ; Traverse the array arr [ ] ; Update maxDiff ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDiffLargSmallOper ( int arr [ ] , int N , int K ) { int maxDiff = 0 ; sort ( arr , arr + N , greater < int > ( ) ) ; for ( int i = 0 ; i <= min ( K , N - 1 ) ; i ++ ) { maxDiff += arr [ i ] ; } return maxDiff ; } int main ( ) { int arr [ ] = { 7 , 7 , 7 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 1 ; cout << maxDiffLargSmallOper ( arr , N , K ) ; return 0 ; }
Minimize cost to convert all characters of a binary string to 0 s | C ++ program to implement the above approach ; Function to get the minimum Cost to convert all characters of given string to 0 s ; Stores the range of indexes of characters that need to be flipped ; Stores the number of times current character is flipped ; Stores minimum cost to get the required string ; Traverse the given string ; Remove all value from pq whose value is less than i ; Update flip ; If current character is flipped odd times ; If current character contains non - zero value ; Update flip ; Update cost ; Append R [ i ] into pq ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( string str , int N , int R [ ] , int C [ ] ) { priority_queue < int , vector < int > , greater < int > > pq ; int flip = 0 ; int cost = 0 ; for ( int i = 0 ; i < N ; i ++ ) { while ( pq . size ( ) > 0 and pq . top ( ) < i ) { pq . pop ( ) ; flip -- ; } if ( flip % 2 == 1 ) { str [ i ] = '1' - str [ i ] + '0' ; } if ( str [ i ] == '1' ) { flip ++ ; cost += C [ i ] ; pq . push ( R [ i ] ) ; } } return cost ; } int main ( ) { string str = "1010" ; int R [ ] = { 1 , 2 , 2 , 3 } ; int C [ ] = { 3 , 1 , 2 , 3 } ; int N = str . length ( ) ; cout << minCost ( str , N , R , C ) ; }
Find Minimum Depth of a Binary Tree | C ++ program to find minimum depth of a given Binary Tree ; A Binary Tree Node ; A queue item ( Stores pointer to node and an integer ) ; Iterative method to find minimum depth of Binary Tree ; Corner Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize depth as 1 ; Do level order traversal ; Remove the front queue item ; Get details of the remove item ; If this is the first leaf node seen so far Then return its depth as answer ; If left subtree is not NULL , add it to queue ; If right subtree is not NULL , add it to queue ; Utility function to create a new tree Node ; Driver program to test above functions ; Let us create binary tree shown in above diagram
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct qItem { Node * node ; int depth ; } ; int minDepth ( Node * root ) { if ( root == NULL ) return 0 ; queue < qItem > q ; qItem qi = { root , 1 } ; q . push ( qi ) ; while ( q . empty ( ) == false ) { qi = q . front ( ) ; q . pop ( ) ; Node * node = qi . node ; int depth = qi . depth ; if ( node -> left == NULL && node -> right == NULL ) return depth ; if ( node -> left != NULL ) { qi . node = node -> left ; qi . depth = depth + 1 ; q . push ( qi ) ; } if ( node -> right != NULL ) { qi . node = node -> right ; qi . depth = depth + 1 ; q . push ( qi ) ; } } return 0 ; } Node * newNode ( int data ) { Node * temp = new Node ; temp -> data = data ; temp -> left = temp -> right = NULL ; return temp ; } int main ( ) { Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; cout << minDepth ( root ) ; return 0 ; }
Maximum Manhattan distance between a distinct pair from N coordinates | C ++ program for the above approach ; Function to calculate the maximum Manhattan distance ; Vectors to store maximum and minimum of all the four forms ; Sorting both the vectors ; Driver Code ; Given Co - ordinates ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MaxDist ( vector < pair < int , int > > & A , int N ) { vector < int > V ( N ) , V1 ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { V [ i ] = A [ i ] . first + A [ i ] . second ; V1 [ i ] = A [ i ] . first - A [ i ] . second ; } sort ( V . begin ( ) , V . end ( ) ) ; sort ( V1 . begin ( ) , V1 . end ( ) ) ; int maximum = max ( V . back ( ) - V . front ( ) , V1 . back ( ) - V1 . front ( ) ) ; cout << maximum << endl ; } int main ( ) { int N = 3 ; vector < pair < int , int > > A = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; MaxDist ( A , N ) ; return 0 ; }
Maximum distance between two points in coordinate plane using Rotating Caliper 's Method |  ; Function calculates distance between two points ; Function to find the maximum distance between any two points ; Iterate over all possible pairs ; Update max ; Return actual distance ; Driver code ; Number of points ; Given points ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long dist ( pair < long , long > p1 , pair < long , long > p2 ) { long x0 = p1 . first - p2 . first ; long y0 = p1 . second - p2 . second ; return x0 * x0 + y0 * y0 ; } double maxDist ( pair < long , long > p [ ] , int n ) { double Max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { Max = max ( Max , ( double ) dist ( p [ i ] , p [ j ] ) ) ; } } return sqrt ( Max ) ; } int main ( ) { int n = 5 ; pair < long , long > p [ n ] ; p [ 0 ] . first = 4 , p [ 0 ] . second = 0 ; p [ 1 ] . first = 0 , p [ 1 ] . second = 2 ; p [ 2 ] . first = -1 , p [ 2 ] . second = -7 ; p [ 3 ] . first = 1 , p [ 3 ] . second = 10 ; p [ 4 ] . first = 2 , p [ 4 ] . second = -3 ; cout << fixed << setprecision ( 14 ) << maxDist ( p , n ) << endl ; return 0 ; }
Reorder an array such that sum of left half is not equal to sum of right half | C ++ program for the above approach ; Function to print the required reordering of array if possible ; Sort the array in increasing order ; If all elements are equal , then it is not possible ; Else print the sorted array arr [ ] ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; if ( arr [ 0 ] == arr [ n - 1 ] ) { cout << " No " << endl ; } else { cout << " Yes " << endl ; for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 1 , 3 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printArr ( arr , N ) ; return 0 ; }
Find Minimum Depth of a Binary Tree | C ++ implementation to find minimum depth of a given Binary tree ; Class containing left and right child of current Node and key value ; Function to calculate the minimum depth of the tree ; Driver program to test above functions
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; Node ( int k ) { data = k ; left = right = NULL ; } } ; int minimumDepth ( Node * root , int level ) { if ( root == NULL ) return level ; level ++ ; return min ( minimumDepth ( root -> left , level ) , minimumDepth ( root -> right , level ) ) ; } int main ( ) { Node * root = new Node ( 1 ) ; root -> left = new Node ( 2 ) ; root -> right = new Node ( 3 ) ; root -> left -> left = new Node ( 4 ) ; root -> left -> right = new Node ( 5 ) ; cout << minimumDepth ( root , 0 ) ; return 0 ; }
Maximize Array sum by swapping at most K elements with another array | C ++ implementation to find maximum sum of array A by swapping at most K elements with array B ; Function to find the maximum sum ; If element of array a is smaller than that of array b , swap them . ; Find sum of resultant array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSum ( int a [ ] , int b [ ] , int k , int n ) { int i , j ; sort ( a , a + n ) ; sort ( b , b + n ) ; for ( i = 0 , j = n - 1 ; i < k ; i ++ , j -- ) { if ( a [ i ] < b [ j ] ) swap ( a [ i ] , b [ j ] ) ; else break ; } int sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += a [ i ] ; cout << sum << endl ; } int main ( ) { int K = 1 ; int A [ ] = { 2 , 3 , 4 } ; int B [ ] = { 6 , 8 , 5 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; maximumSum ( A , B , K , N ) ; return 0 ; }
Maximize distinct elements by incrementing / decrementing an element or keeping it same | C ++ program to Maximize distinct elements by incrementing / decrementing an element or keeping it same ; Function that Maximize the count of distinct element ; sort thr array ; keeping track of previous change ; check the decremented value ; check the current value ; check the incremented value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_dist_ele ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int ans = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prev < ( arr [ i ] - 1 ) ) { ans ++ ; prev = arr [ i ] - 1 ; } else if ( prev < ( arr [ i ] ) ) { ans ++ ; prev = arr [ i ] ; } else if ( prev < ( arr [ i ] + 1 ) ) { ans ++ ; prev = arr [ i ] + 1 ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 8 , 8 , 8 , 9 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << max_dist_ele ( arr , n ) << endl ; return 0 ; }
Find if array can be sorted by swaps limited to multiples of k |  ; CheckSort function To check if array can be sorted ; sortarr is sorted array of arr ; if k = 1 then ( always possible to sort ) swapping can easily give sorted array ; comparing sortarray with array ; element at index j must be in j = i + l * k form where i = 0 , 1 , 2 , 3. . . where l = 0 , 1 , 2 , 3 , . . n - 1 ; if element is present then swapped ; if element of sorted array does not found in its sequence then flag remain zero that means arr can not be sort after swapping ; if flag is 0 Not possible else Possible ; Driver code ; size of step ; array initialized ; length of arr ; calling function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CheckSort ( vector < int > arr , int k , int n ) { vector < int > sortarr ( arr . begin ( ) , arr . end ( ) ) ; sort ( sortarr . begin ( ) , sortarr . end ( ) ) ; if ( k == 1 ) printf ( " yes " ) ; else { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { flag = 0 ; for ( int j = i ; j < n ; j += k ) { if ( sortarr [ i ] == arr [ j ] ) { swap ( arr [ i ] , arr [ j ] ) ; flag = 1 ; break ; } if ( j + k >= n ) break ; } if ( flag == 0 ) break ; } if ( flag == 0 ) printf ( " Not ▁ possible ▁ to ▁ sort " ) ; else printf ( " Possible ▁ to ▁ sort " ) ; } } int main ( ) { int k = 3 ; vector < int > arr = { 1 , 5 , 6 , 9 , 2 , 3 , 5 , 9 } ; int n = arr . size ( ) ; CheckSort ( arr , k , n ) ; return 0 ; }