codeparrot/xlcost-text-to-code huggingface dataset:
Maximize shortest path between given vertices by adding a single edge | C ++ program for the above approach ; To store graph as adjacency list ; To store the shortest path ; Function that performs BFS Traversal ; Fill initially each distance as INF ; Perform BFS ; Traverse the current edges ; Update the distance ; Insert in queue ; Function that maximizes the shortest path between source and destination vertex by adding a single edge between given selected nodes ; To update the shortest distance between node 1 to other vertices ; To update the shortest distance between node N to other vertices ; Store the values x [ i ] - y [ i ] ; Sort all the vectors of pairs ; Traverse data [ ] ; Maximize x [ a ] - y [ b ] ; Print minimum cost ; Driver Code ; Given nodes and edges ; Sort the selected nodes ; Given edges ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int INF = 1e9 + 7 ; int N , M ; vector < int > edges [ 200005 ] ; int dist [ 2 ] [ 200000 ] ; void bfs ( int * dist , int s ) { int q [ 200000 ] ; fill ( dist , dist + N , INF ) ; int qh = 0 , qt = 0 ; q [ qh ++ ] = s ; dist [ s ] = 0 ; while ( qt < qh ) { int x = q [ qt ++ ] ; for ( int y : edges [ x ] ) { if ( dist [ y ] == INF ) { dist [ y ] = dist [ x ] + 1 ; q [ qh ++ ] = y ; } } } } void shortestPathCost ( int selected [ ] , int K ) { vector < pair < int , int > > data ; bfs ( dist [ 0 ] , 0 ) ; bfs ( dist [ 1 ] , N - 1 ) ; for ( int i = 0 ; i < K ; i ++ ) { data . emplace_back ( dist [ 0 ] [ selected [ i ] ] - dist [ 1 ] [ selected [ i ] ] , selected [ i ] ) ; } sort ( data . begin ( ) , data . end ( ) ) ; int best = 0 ; int MAX = - INF ; for ( auto it : data ) { int a = it . second ; best = max ( best , MAX + dist [ 1 ] [ a ] ) ; MAX = max ( MAX , dist [ 0 ] [ a ] ) ; } printf ( " % d STRNEWLINE " , min ( dist [ 0 ] [ N - 1 ] , best + 1 ) ) ; } int main ( ) { N = 5 , M = 4 ; int K = 2 ; int selected [ ] = { 1 , 3 } ; sort ( selected , selected + K ) ; edges [ 0 ] . push_back ( 1 ) ; edges [ 1 ] . push_back ( 0 ) ; edges [ 1 ] . push_back ( 2 ) ; edges [ 2 ] . push_back ( 1 ) ; edges [ 2 ] . push_back ( 3 ) ; edges [ 3 ] . push_back ( 2 ) ; edges [ 3 ] . push_back ( 4 ) ; edges [ 4 ] . push_back ( 3 ) ; shortestPathCost ( selected , K ) ; return 0 ; }
Program to cyclically rotate an array by one |  ; i and j pointing to first and last element respectively ; Driver code
#include <iostream> NEW_LINE using namespace std ; void rotate ( int arr [ ] , int n ) { int i = 0 , j = n - 1 ; while ( i != j ) { swap ( arr [ i ] , arr [ j ] ) ; i ++ ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } , i ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Given ▁ array ▁ is ▁ STRNEWLINE " ; for ( i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; rotate ( arr , n ) ; cout << " Rotated array is " ; for ( i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Minimum cost to empty Array where cost of removing an element is 2 ^ ( removed_count ) * arr [ i ] | C ++ implementation to find the minimum cost of removing all elements from the array ; Function to find the minimum cost of removing elements from the array ; Sorting in Increasing order ; Loop to find the minimum cost of removing elements ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE int removeElements ( ll arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; ll ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += arr [ i ] * pow ( 2 , i ) ; } return ans ; } int main ( ) { int n = 4 ; ll arr [ n ] = { 3 , 1 , 2 , 3 } ; cout << removeElements ( arr , n ) ; }
Sorting boundary elements of a matrix | C ++ program for the above approach ; Appending border elements ; Sorting the list ; Printing first row with first N elements from A ; Printing N - 2 rows ; Print elements from last ; Print middle elements from original matrix ; Print elements from front ; Printing last row ; Driver Code ; Dimensions of a Matrix ; Given Matrix ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMatrix ( int grid [ ] [ 5 ] , int m , int n ) { vector < int > A ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == n - 1 || ( i == m - 1 ) j == 0 i == 0 ) A . push_back ( grid [ i ] [ j ] ) ; } } sort ( A . begin ( ) , A . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) cout << A [ i ] << " ▁ " ; cout << endl ; for ( int i = 0 ; i < m - 2 ; i ++ ) { cout << A [ A . size ( ) - i - 1 ] << " ▁ " ; for ( int j = 1 ; j < n - 1 ; j ++ ) cout << grid [ i + 1 ] [ j ] << " ▁ " ; cout << ( A [ n + i ] ) << endl ; } reverse ( A . begin ( ) + n + m - 2 , A . begin ( ) + n + m + n - 2 ) ; for ( int i = n + m - 2 ; i < n + m - 2 + n ; i ++ ) cout << A [ i ] << " ▁ " ; [ n + m - 2 : n + m - 2 + n ] << endl ; } int main ( ) { int m = 4 , n = 5 ; int grid [ ] [ 5 ] = { { 1 , 2 , 3 , 4 , 0 } , { 1 , 1 , 1 , 1 , 2 } , { 1 , 2 , 2 , 2 , 4 } , { 1 , 9 , 3 , 1 , 7 } } ; printMatrix ( grid , m , n ) ; return 0 ; }
Flattening a linked list | Set 2 | C ++ program for Flattening a linked list using Heaps ; Structure of given Linked list ; Function to print the linked list ; Function that compares the value pointed by node and returns true if first data is greater ; Function which returns the root of the flattened linked list ; Min Heap which will return smallest element currently present in heap ; Push the head nodes of each downward linked list ; This loop will execute till the map becomes empty ; Pop out the node that contains element currently in heap ; Push the next node pointed by the current node into heap if it is not null ; Create new linked list that is to be returned ; Pointer to head node in the linked list ; Create and push new nodes ; Driver Code ; Given Linked List ; Flatten the list ; Print the flattened linked list
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * right ; struct Node * down ; Node ( int x ) { data = x ; right = NULL ; down = NULL ; } } ; void printList ( Node * Node ) { while ( Node != NULL ) { printf ( " % d ▁ " , Node -> data ) ; Node = Node -> down ; } } struct compare { bool operator() ( Node * a , Node * b ) { return a -> data > b -> data ; } } ; Node * flatten ( Node * root ) { Node * ptr = root ; Node * head = NULL ; priority_queue < Node * , vector < Node * > , compare > pq ; while ( ptr != NULL ) { pq . push ( ptr ) ; ptr = ptr -> right ; } while ( ! pq . empty ( ) ) { Node * temp = pq . top ( ) ; pq . pop ( ) ; if ( temp -> down != NULL ) { pq . push ( temp -> down ) ; } if ( head == NULL ) { head = temp ; ptr = temp ; ptr -> right = NULL ; } else { ptr -> down = temp ; ptr = temp ; ptr -> right = NULL ; } } return head ; } void push ( Node * * head_ref , int new_data ) { Node * new_node = ( Node * ) malloc ( sizeof ( Node ) ) ; new_node -> right = NULL ; new_node -> data = new_data ; new_node -> down = ( * head_ref ) ; ( * head_ref ) = new_node ; } int main ( ) { Node * root = NULL ; push ( & root , 30 ) ; push ( & root , 8 ) ; push ( & root , 7 ) ; push ( & root , 5 ) ; push ( & ( root -> right ) , 20 ) ; push ( & ( root -> right ) , 10 ) ; push ( & ( root -> right -> right ) , 50 ) ; push ( & ( root -> right -> right ) , 22 ) ; push ( & ( root -> right -> right ) , 19 ) ; push ( & ( root -> right -> right -> right ) , 45 ) ; push ( & ( root -> right -> right -> right ) , 40 ) ; push ( & ( root -> right -> right -> right ) , 35 ) ; push ( & ( root -> right -> right -> right ) , 20 ) ; root = flatten ( root ) ; printList ( root ) ; return 0 ; }
Lexicographically largest string possible in one swap | C ++ implementation to find the lexicographically largest string by atmost at most one swap ; Function to return the lexicographically largest string possible by swapping at most one character ; Stores last occurrence of every character ; Initialize with - 1 for every character ; Keep updating the last occurrence of each character ; If a previously unvisited character occurs ; Stores the sorted string ; Character to replace ; Find the last occurrence of this character ; Swap this with the last occurrence ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; string findLargest ( string s ) { int len = s . size ( ) ; int loccur [ 26 ] ; memset ( loccur , -1 , sizeof ( loccur ) ) ; for ( int i = len - 1 ; i >= 0 ; -- i ) { int chI = s [ i ] - ' a ' ; if ( loccur [ chI ] == -1 ) { loccur [ chI ] = i ; } } string sorted_s = s ; sort ( sorted_s . begin ( ) , sorted_s . end ( ) , greater < int > ( ) ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { int chI = sorted_s [ i ] - ' a ' ; int last_occ = loccur [ chI ] ; swap ( s [ i ] , s [ last_occ ] ) ; break ; } } return s ; } int main ( ) { string s = " yrstvw " ; cout << findLargest ( s ) ; return 0 ; }
Given a sorted and rotated array , find if there is a pair with a given sum | C ++ program to find a pair with a given sum in a sorted and rotated array ; This function returns true if arr [ 0. . n - 1 ] has a pair with sum equals to x . ; Find the pivot element ; l is now index of smallest element ; r is now index of largest element ; Keep moving either l or r till they meet ; If we find a pair with sum x , we return true ; If current pair sum is less , move to the higher sum ; Move to the lower sum side ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; bool pairInSortedRotated ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; int l = ( i + 1 ) % n ; int r = i ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) return true ; if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return false ; } int main ( ) { int arr [ ] = { 11 , 15 , 6 , 8 , 9 , 10 } ; int sum = 16 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( pairInSortedRotated ( arr , n , sum ) ) cout << " Array ▁ has ▁ two ▁ elements ▁ with ▁ sum ▁ 16" ; else cout << " Array ▁ doesn ' t ▁ have ▁ two ▁ elements ▁ with ▁ sum ▁ 16 ▁ " ; return 0 ; }
Given a sorted and rotated array , find if there is a pair with a given sum | C ++ program to find number of pairs with a given sum in a sorted and rotated array . ; This function returns count of number of pairs with sum equals to x . ; Find the pivot element . Pivot element is largest element of array . ; l is index of smallest element . ; r is index of largest element . ; Variable to store count of number of pairs . ; Find sum of pair formed by arr [ l ] and and arr [ r ] and update l , r and cnt accordingly . ; If we find a pair with sum x , then increment cnt , move l and r to next element . ; This condition is required to be checked , otherwise l and r will cross each other and loop will never terminate . ; If current pair sum is less , move to the higher sum side . ; If current pair sum is greater , move to the lower sum side . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pairsInSortedRotated ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; int l = ( i + 1 ) % n ; int r = i ; int cnt = 0 ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) { cnt ++ ; if ( l == ( r - 1 + n ) % n ) { return cnt ; } l = ( l + 1 ) % n ; r = ( r - 1 + n ) % n ; } else if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return cnt ; } int main ( ) { int arr [ ] = { 11 , 15 , 6 , 7 , 9 , 10 } ; int sum = 16 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << pairsInSortedRotated ( arr , n , sum ) ; return 0 ; }
Find maximum value of Sum ( i * arr [ i ] ) with only rotations on given array allowed | C ++ program to find max value of i * arr [ i ] ; Returns max possible value of i * arr [ i ] ; Find array sum and i * arr [ i ] with no rotation Stores sum of arr [ i ] ; Stores sum of i * arr [ i ] ; Initialize result as 0 rotation sum ; Try all rotations one by one and find the maximum rotation sum . ; Return result ; Driver program
#include <iostream> NEW_LINE using namespace std ; int maxSum ( int arr [ ] , int n ) { int arrSum = 0 ; int currVal = 0 ; for ( int i = 0 ; i < n ; i ++ ) { arrSum = arrSum + arr [ i ] ; currVal = currVal + ( i * arr [ i ] ) ; } int maxVal = currVal ; for ( int j = 1 ; j < n ; j ++ ) { currVal = currVal + arrSum - n * arr [ n - j ] ; if ( currVal > maxVal ) maxVal = currVal ; } return maxVal ; } int main ( void ) { int arr [ ] = { 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Max sum is " return 0 ; }
Print the nodes at odd levels of a tree | Recursive C ++ program to print odd level nodes ; If empty tree ; If current node is of odd level ; Recur for children with isOdd switched . ; Utility method to create a node ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; void printOddNodes ( Node * root , bool isOdd = true ) { if ( root == NULL ) return ; if ( isOdd ) cout << root -> data << " ▁ " ; printOddNodes ( root -> left , ! isOdd ) ; printOddNodes ( root -> right , ! isOdd ) ; } struct Node * newNode ( int data ) { struct Node * node = new Node ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; printOddNodes ( root ) ; return 0 ; }
Maximum sum of i * arr [ i ] among all rotations of a given array | A Naive C ++ program to find maximum sum rotation ; Returns maximum value of i * arr [ i ] ; Initialize result ; Consider rotation beginning with i for all possible values of i . ; Initialize sum of current rotation ; Compute sum of all values . We don 't  acutally rotation the array, but compute  sum by finding ndexes when arr[i] is  first element ; Update result if required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int arr [ ] , int n ) { int res = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = max ( res , curr_sum ) ; } return res ; } int main ( ) { int arr [ ] = { 8 , 3 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSum ( arr , n ) << endl ; return 0 ; }
Maximum sum of i * arr [ i ] among all rotations of a given array | An efficient C ++ program to compute maximum sum of i * arr [ i ] ; Compute sum of all array elements ; Compute sum of i * arr [ i ] for initial configuration . ; Initialize result ; Compute values for other iterations ; Compute next value using previous value in O ( 1 ) time ; Update current value ; Update result if required ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int arr [ ] , int n ) { int cum_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) cum_sum += arr [ i ] ; int curr_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) curr_val += i * arr [ i ] ; int res = curr_val ; for ( int i = 1 ; i < n ; i ++ ) { int next_val = curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 ) ; curr_val = next_val ; res = max ( res , next_val ) ; } return res ; } int main ( ) { int arr [ ] = { 8 , 3 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSum ( arr , n ) << endl ; return 0 ; }
Minimum time required to cover a Binary Array | CPP implementation to find the Minimum time required to cover a Binary Array ; function to calculate the time ; Map to mark or store the binary values ; Firstly fill the boolean array with all zeroes ; Mark the 1 s ; Number of 0 s until first '1' occurs ; Maximum Number of 0 s in between 2 '1' s . ; Number of 0 s from right until first '1' occurs ; Return maximum from left and right segment ; check if count is odd ; check ifcount is even ; return the time ; driver code ; initialise N ; array initialisation
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( vector < int > arr , int n ) { int k = arr . size ( ) ; bool mp [ n + 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } int leftSegment = arr [ 0 ] - 1 ; for ( int i = 1 ; i < k ; i ++ ) { leftSegment = max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } int rightSegment = n - arr [ k - 1 ] ; int maxSegment = max ( leftSegment , rightSegment ) ; int tim ; if ( maxSegment & 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } int main ( ) { int N = 5 ; vector < int > arr = { 1 , 4 } ; cout << solve ( arr , N ) ; }
Find the Rotation Count in Rotated Sorted array | C ++ program to find number of rotations in a sorted and rotated array . ; Returns count of rotations for an array which is first sorted in ascending order , then rotated ; We basically find index of minimum element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countRotations ( int arr [ ] , int n ) { int min = arr [ 0 ] , min_index ; for ( int i = 0 ; i < n ; i ++ ) { if ( min > arr [ i ] ) { min = arr [ i ] ; min_index = i ; } } return min_index ; } int main ( ) { int arr [ ] = { 15 , 18 , 2 , 3 , 6 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countRotations ( arr , n ) ; return 0 ; }
Find the Rotation Count in Rotated Sorted array | Binary Search based C ++ program to find number of rotations in a sorted and rotated array . ; Returns count of rotations for an array which is first sorted in ascending order , then rotated ; This condition is needed to handle the case when the array is not rotated at all ; If there is only one element left ; Find mid ; Check if element ( mid + 1 ) is minimum element . Consider the cases like { 3 , 4 , 5 , 1 , 2 } ; Check if mid itself is minimum element ; Decide whether we need to go to left half or right half ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countRotations ( int arr [ ] , int low , int high ) { if ( high < low ) return 0 ; if ( high == low ) return low ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ; if ( arr [ high ] > arr [ mid ] ) return countRotations ( arr , low , mid - 1 ) ; return countRotations ( arr , mid + 1 , high ) ; } int main ( ) { int arr [ ] = { 15 , 18 , 2 , 3 , 6 , 12 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countRotations ( arr , 0 , n - 1 ) ; return 0 ; }
Quickly find multiple left rotations of an array | Set 1 | CPP implementation of left rotation of an array K number of times ; Fills temp [ ] with two copies of arr [ ] ; Store arr [ ] elements at i and i + n ; Function to left rotate an array k times ; Starting position of array after k rotations in temp [ ] will be k % n ; Print array after k rotations ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void preprocess ( int arr [ ] , int n , int temp [ ] ) { for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = temp [ i + n ] = arr [ i ] ; } void leftRotate ( int arr [ ] , int n , int k , int temp [ ] ) { int start = k % n ; for ( int i = start ; i < start + n ; i ++ ) cout << temp [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int temp [ 2 * n ] ; preprocess ( arr , n , temp ) ; int k = 2 ; leftRotate ( arr , n , k , temp ) ; k = 3 ; leftRotate ( arr , n , k , temp ) ; k = 4 ; leftRotate ( arr , n , k , temp ) ; return 0 ; }
Arrange the array such that upon performing given operations an increasing order is obtained | C ++ program to find the desired output after performing given operations ; Function to arrange array in such a way that after performing given operation We get increasing sorted array ; Size of given array ; Sort the given array ; Start erasing last element and place it at ith index ; While we reach at starting ; Store last element ; Shift all elements by 1 position in right ; insert last element at ith position ; print desired Array ; Driver Code ; Given Array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Desired_Array ( vector < int > & v ) { int n = v . size ( ) ; sort ( v . begin ( ) , v . end ( ) ) ; int i = n - 1 ; while ( i > 0 ) { int p = v [ n - 1 ] ; for ( int j = n - 1 ; j >= i ; j -- ) { v [ j + 1 ] = v [ j ] ; } v [ i ] = p ; i -- ; } for ( auto x : v ) cout << x << " ▁ " ; cout << " STRNEWLINE " ; } int main ( ) { vector < int > v = { 1 , 2 , 3 , 4 , 5 } ; Desired_Array ( v ) ; vector < int > v1 = { 1 , 12 , 2 , 10 , 4 , 16 , 6 } ; Desired_Array ( v1 ) ; return 0 ; }
Quickly find multiple left rotations of an array | Set 1 | CPP implementation of left rotation of an array K number of times ; Function to left rotate an array k times ; Print array after k rotations ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void leftRotate ( int arr [ ] , int n , int k ) { for ( int i = k ; i < k + n ; i ++ ) cout << arr [ i % n ] << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; leftRotate ( arr , n , k ) ; cout << endl ; k = 3 ; leftRotate ( arr , n , k ) ; cout << endl ; k = 4 ; leftRotate ( arr , n , k ) ; cout << endl ; return 0 ; }
How to sort an array in a single loop ? | C ++ code to sort an array of integers with the help of single loop ; Function for Sorting the array using a single loop ; Sorting using a single loop ; Checking the condition for two simultaneous elements of the array ; Swapping the elements . ; updating the value of j = - 1 so after getting updated for j ++ in the loop it becomes 0 and the loop begins from the start . ; Driver code ; Declaring an integer array of size 11. ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
#include <bits/stdc++.h> NEW_LINE using namespace std ; int * sortArrays ( int arr [ ] , int length ) { for ( int j = 0 ; j < length - 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { int temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = -1 ; } } return arr ; } int main ( ) { int arr [ ] = { 1 , 2 , 99 , 9 , 8 , 7 , 6 , 0 , 5 , 4 , 3 } ; int length = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; string str ; for ( int i : arr ) { str += to_string ( i ) + " ▁ " ; } cout << " Original ▁ array : ▁ [ " << str << " ] " << endl ; int * arr1 ; arr1 = sortArrays ( arr , length ) ; string str1 ; for ( int i = 0 ; i < length ; i ++ ) { str1 += to_string ( arr1 [ i ] ) + " ▁ " ; } cout << " Sorted ▁ array : ▁ [ " << ( str1 ) << " ] " ; }
How to sort an array in a single loop ? | C ++ code to sort an array of Strings with the help of single loop ; Function for Sorting the array using a single loop ; Sorting using a single loop ; Type Conversion of char to int . ; Comparing the ascii code . ; Swapping of the characters ; Driver code ; Declaring a String ; declaring character array ; copying the contents of the string to char array ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
#include <bits/stdc++.h> NEW_LINE using namespace std ; char * sortArrays ( char arr [ ] , int length ) { for ( int j = 0 ; j < length - 1 ; j ++ ) { int d1 = arr [ j ] ; int d2 = arr [ j + 1 ] ; if ( d1 > d2 ) { char temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = -1 ; } } return arr ; } int main ( ) { string geeks = " GEEKSFORGEEKS " ; int n = geeks . length ( ) ; char arr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = geeks [ i ] ; } cout << " Original ▁ array : ▁ [ " ; for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] ; if ( i + 1 != n ) cout << " , ▁ " ; } cout << " ] " << endl ; char * ansarr ; ansarr = sortArrays ( arr , n ) ; cout << " Sorted ▁ array : ▁ [ " ; for ( int i = 0 ; i < n ; i ++ ) { cout << ansarr [ i ] ; if ( i + 1 != n ) cout << " , ▁ " ; } cout << " ] " << endl ; }