codeparrot/xlcost-text-to-code huggingface dataset:
Maximum number of 0 s placed consecutively at the start and end in any rotation of a Binary String | C ++ program for the above approach ; Function to find the maximum sum of consecutive 0 s present at the start and end of any rotation of the string str ; Stores the count of 0 s ; If the frequency of '1' is 0 ; Print n as the result ; Stores the required sum ; Find the maximum consecutive length of 0 s present in the string ; Update the overall maximum sum ; Find the number of 0 s present at the start and end of the string ; Update the count of 0 s at the start ; Update the count of 0 s at the end ; Update the maximum sum ; Print the result ; Driver Code ; Given string ; Store the size of the string
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaximumZeros ( string str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str [ i ] == '0' ) c0 ++ ; } if ( c0 == n ) { cout << n ; return ; } int mx = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) cnt ++ ; else { mx = max ( mx , cnt ) ; cnt = 0 ; } } mx = max ( mx , cnt ) ; int start = 0 , end = n - 1 ; cnt = 0 ; while ( str [ start ] != '1' && start < n ) { cnt ++ ; start ++ ; } while ( str [ end ] != '1' && end >= 0 ) { cnt ++ ; end -- ; } mx = max ( mx , cnt ) ; cout << mx ; } int main ( ) { string s = "1001" ; int n = s . size ( ) ; findMaximumZeros ( s , n ) ; return 0 ; }
Modify a matrix by rotating ith row exactly i times in clockwise direction | C ++ program for the above approach ; Function to rotate every i - th row of the matrix i times ; Traverse the matrix row - wise ; Reverse the current row ; Reverse the first i elements ; Reverse the last ( N - i ) elements ; Increment count ; Print final matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rotateMatrix ( vector < vector < int > > & mat ) { int i = 0 ; for ( auto & it : mat ) { reverse ( it . begin ( ) , it . end ( ) ) ; reverse ( it . begin ( ) , it . begin ( ) + i ) ; reverse ( it . begin ( ) + i , it . end ( ) ) ; i ++ ; } for ( auto rows : mat ) { for ( auto cols : rows ) { cout << cols << " ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { vector < vector < int > > mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; rotateMatrix ( mat ) ; return 0 ; }
Modify a string by performing given shift operations | C ++ implementataion of above approach ; Function to find the string obtained after performing given shift operations ; If shift [ i ] [ 0 ] = 0 , then left shift Otherwise , right shift ; Stores length of the string ; Effective shift calculation ; Stores modified string ; Right rotation ; Left rotation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void stringShift ( string s , vector < vector < int > > & shift ) { int val = 0 ; for ( int i = 0 ; i < shift . size ( ) ; ++ i ) val += shift [ i ] [ 0 ] == 0 ? - shift [ i ] [ 1 ] : shift [ i ] [ 1 ] ; int len = s . length ( ) ; val = val % len ; string result = " " ; if ( val > 0 ) result = s . substr ( len - val , val ) + s . substr ( 0 , len - val ) ; else result = s . substr ( - val , len + val ) + s . substr ( 0 , - val ) ; cout << result ; } int main ( ) { string s = " abc " ; vector < vector < int > > shift = { { 0 , 1 } , { 1 , 2 } } ; stringShift ( s , shift ) ; return 0 ; }
Modify given array to a non | C ++ program for the above approach ; Function to check if a non - decreasing array can be obtained by rotating the original array ; Stores copy of original array ; Sort the given vector ; Traverse the array ; Rotate the array by 1 ; If array is sorted ; If it is not possible to sort the array ; Driver Code ; Given array ; Size of the array ; Function call to check if it is possible to make array non - decreasing by rotating
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rotateArray ( vector < int > & arr , int N ) { vector < int > v = arr ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 1 ; i <= N ; ++ i ) { rotate ( arr . begin ( ) , arr . begin ( ) + 1 , arr . end ( ) ) ; if ( arr == v ) { cout << " YES " << endl ; return ; } } cout << " NO " << endl ; } int main ( ) { vector < int > arr = { 3 , 4 , 5 , 1 , 2 } ; int N = arr . size ( ) ; rotateArray ( arr , N ) ; }
Maximum value possible by rotating digits of a given number | C ++ program for the above approach ; Function to find the maximum value possible by rotations of digits of N ; Store the required result ; Store the number of digits ; Iterate over the range [ 1 , len - 1 ] ; Store the unit 's digit ; Store the remaining number ; Find the next rotation ; If the current rotation is greater than the overall answer , then update answer ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findLargestRotation ( int num ) { int ans = num ; int len = floor ( log10 ( num ) + 1 ) ; int x = pow ( 10 , len - 1 ) ; for ( int i = 1 ; i < len ; i ++ ) { int lastDigit = num % 10 ; num = num / 10 ; num += ( lastDigit * x ) ; if ( num > ans ) { ans = num ; } } cout << ans ; } int main ( ) { int N = 657 ; findLargestRotation ( N ) ; return 0 ; }
Rotate digits of a given number by K | C ++ program to implement the above approach ; Function to find the count of digits in N ; Stores count of digits in N ; Calculate the count of digits in N ; Update digit ; Update N ; Function to rotate the digits of N by K ; Stores count of digits in N ; Update K so that only need to handle left rotation ; Stores first K digits of N ; Remove first K digits of N ; Stores count of digits in left_no ; Append left_no to the right of digits of N ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfDigit ( int N ) { int digit = 0 ; while ( N > 0 ) { digit ++ ; N /= 10 ; } return digit ; } void rotateNumberByK ( int N , int K ) { int X = numberOfDigit ( N ) ; K = ( ( K % X ) + X ) % X ; int left_no = N / ( int ) ( pow ( 10 , X - K ) ) ; N = N % ( int ) ( pow ( 10 , X - K ) ) ; int left_digit = numberOfDigit ( left_no ) ; N = ( N * ( int ) ( pow ( 10 , left_digit ) ) ) + left_no ; cout << N ; } int main ( ) { int N = 12345 , K = 7 ; rotateNumberByK ( N , K ) ; return 0 ; }
Count rotations required to sort given array in non | C ++ program for the above approach ; Function to count minimum anti - clockwise rotations required to sort the array in non - increasing order ; Stores count of arr [ i + 1 ] > arr [ i ] ; Store last index of arr [ i + 1 ] > arr [ i ] ; Traverse the given array ; If the adjacent elements are in increasing order ; Increment count ; Update index ; Print the result according to the following conditions ; Otherwise , it is not possible to sort the array ; Driver Code ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minMovesToSort ( int arr [ ] , int N ) { int count = 0 ; int index ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { count ++ ; index = i ; } } if ( count == 0 ) { cout << "0" ; } else if ( count == N - 1 ) { cout << N - 1 ; } else if ( count == 1 && arr [ 0 ] <= arr [ N - 1 ] ) { cout << index + 1 ; } else { cout << " - 1" ; } } int main ( ) { int arr [ ] = { 2 , 1 , 5 , 4 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minMovesToSort ( arr , N ) ; return 0 ; }
Maximize sum of diagonal of a matrix by rotating all rows or all columns | C ++ program to implement the above approach ; Function to find maximum sum of diagonal elements of matrix by rotating either rows or columns ; Stores maximum diagonal sum of elements of matrix by rotating rows or columns ; Rotate all the columns by an integer in the range [ 0 , N - 1 ] ; Stores sum of diagonal elements of the matrix ; Calculate sum of diagonal elements of the matrix ; Update curr ; Update maxDiagonalSum ; Rotate all the rows by an integer in the range [ 0 , N - 1 ] ; Stores sum of diagonal elements of the matrix ; Calculate sum of diagonal elements of the matrix ; Update curr ; Update maxDiagonalSum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  3 NEW_LINE int findMaximumDiagonalSumOMatrixf ( int A [ ] [ N ] ) { int maxDiagonalSum = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { int curr = 0 ; for ( int j = 0 ; j < N ; j ++ ) { curr += A [ j ] [ ( i + j ) % N ] ; } maxDiagonalSum = max ( maxDiagonalSum , curr ) ; } for ( int i = 0 ; i < N ; i ++ ) { int curr = 0 ; for ( int j = 0 ; j < N ; j ++ ) { curr += A [ ( i + j ) % N ] [ j ] ; } maxDiagonalSum = max ( maxDiagonalSum , curr ) ; } return maxDiagonalSum ; } int main ( ) { int mat [ N ] [ N ] = { { 1 , 1 , 2 } , { 2 , 1 , 2 } , { 1 , 2 , 2 } } ; cout << findMaximumDiagonalSumOMatrixf ( mat ) ; return 0 ; }
Generate a matrix having sum of secondary diagonal equal to a perfect square | C ++ program for the above approach ; Function to print the matrix whose sum of element in secondary diagonal is a perfect square ; Iterate for next N - 1 rows ; Print the current row after the left shift ; Driver Code ; Given N ; Fill the array with elements ranging from 1 to N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void diagonalSumPerfectSquare ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { cout << ( arr [ ( j + i ) % 7 ] ) << " ▁ " ; } cout << endl ; } } int main ( ) { int N = 7 ; int arr [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = i + 1 ; } diagonalSumPerfectSquare ( arr , N ) ; }
Queries to find maximum sum contiguous subarrays of given length in a rotating array | C ++ program for the above approach ; Function to calculate the maximum sum of length k ; Calculating the max sum for the first k elements ; Find subarray with maximum sum ; Update the sum ; Return maximum sum ; Function to calculate gcd of the two numbers n1 and n2 ; Base Case ; Recursively find the GCD ; Function to rotate the array by Y ; For handling k >= N ; Dividing the array into number of sets ; Rotate the array by Y ; Update arr [ j ] ; Return the rotated array ; Function that performs the queries on the given array ; Traverse each query ; If query of type X = 1 ; Print the array ; If query of type X = 2 ; Driver Code ; Given array arr [ ] ; Given Queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxSum ( vector < int > arr , int n , int k ) { int i , max_sum = 0 , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } max_sum = sum ; while ( i < n ) { sum = sum - arr [ i - k ] + arr [ i ] ; if ( max_sum < sum ) { max_sum = sum ; } i ++ ; } return max_sum ; } int gcd ( int n1 , int n2 ) { if ( n2 == 0 ) { return n1 ; } else { return gcd ( n2 , n1 % n2 ) ; } } vector < int > RotateArr ( vector < int > arr , int n , int d ) { int i = 0 , j = 0 ; d = d % n ; int no_of_sets = gcd ( d , n ) ; for ( i = 0 ; i < no_of_sets ; i ++ ) { int temp = arr [ i ] ; j = i ; while ( true ) { int k = j + d ; if ( k >= n ) k = k - n ; if ( k == i ) break ; arr [ j ] = arr [ k ] ; j = k ; } arr [ j ] = temp ; } return arr ; } void performQuery ( vector < int > & arr , int Q [ ] [ 2 ] , int q ) { int N = ( int ) arr . size ( ) ; for ( int i = 0 ; i < q ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { arr = RotateArr ( arr , N , Q [ i ] [ 1 ] ) ; for ( auto t : arr ) { cout << t << " ▁ " ; } cout << " STRNEWLINE " ; } else { cout << MaxSum ( arr , N , Q [ i ] [ 1 ] ) << " STRNEWLINE " ; } } } int main ( ) { vector < int > arr = { 1 , 2 , 3 , 4 , 5 } ; int q = 5 ; int Q [ ] [ 2 ] = { { 1 , 2 } , { 2 , 3 } , { 1 , 3 } , { 1 , 1 } , { 2 , 4 } } ; performQuery ( arr , Q , q ) ; return 0 ; }
Minimize characters to be changed to make the left and right rotation of a string same | C ++ Program of the above approach ; Function to find the minimum characters to be removed from the string ; Initialize answer by N ; If length is even ; Frequency array for odd and even indices ; Store the frequency of the characters at even and odd indices ; Stores the most occuring frequency for even and odd indices ; Update the answer ; If length is odd ; Stores the frequency of the characters of the string ; Stores the most occuring character in the string ; Update the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinimumRemoval ( string str ) { int n = str . length ( ) ; int ans = n ; if ( n % 2 == 0 ) { vector < int > freqEven ( 128 ) ; vector < int > freqOdd ( 128 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { freqEven [ str [ i ] ] ++ ; } else { freqOdd [ str [ i ] ] ++ ; } } int evenMax = 0 , oddMax = 0 ; for ( char chr = ' a ' ; chr <= ' z ' ; chr ++ ) { evenMax = max ( evenMax , freqEven [ chr ] ) ; oddMax = max ( oddMax , freqOdd [ chr ] ) ; } ans = ans - evenMax - oddMax ; } else { vector < int > freq ( 128 ) ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str [ i ] ] ++ ; } int strMax = 0 ; for ( char chr = ' a ' ; chr <= ' z ' ; chr ++ ) { strMax = max ( strMax , freq [ chr ] ) ; } ans = ans - strMax ; } return ans ; } int main ( ) { string str = " geeksgeeks " ; cout << getMinimumRemoval ( str ) ; }
Longest subsequence of a number having same left and right rotation | C ++ Program to implement the above approach ; Function to find the longest subsequence having equal left and right rotation ; Length of the string ; Iterate for all possible combinations of a two - digit numbers ; Check for alternate occurrence of current combination ; Increment the current value ; Increment the current value ; If alternating sequence is obtained of odd length ; Reduce to even length ; Update answer to store the maximum ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findAltSubSeq ( string s ) { int n = s . size ( ) , ans = INT_MIN ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { int cur = 0 , f = 0 ; for ( int k = 0 ; k < n ; k ++ ) { if ( f == 0 and s [ k ] - '0' == i ) { f = 1 ; cur ++ ; } else if ( f == 1 and s [ k ] - '0' == j ) { f = 0 ; cur ++ ; } } if ( i != j and cur % 2 == 1 ) cur -- ; ans = max ( cur , ans ) ; } } return ans ; } int main ( ) { string s = "100210601" ; cout << findAltSubSeq ( s ) ; return 0 ; }
Rotate matrix by 45 degrees | C ++ program for the above approach ; Function to rotate matrix by 45 degree ; Counter Variable ; Iterate [ 0 , m ] ; Iterate [ 0 , n ] ; Diagonal Elements Condition ; Appending the Diagonal Elements ; Printing reversed Diagonal Elements ; Driver code ; Dimensions of Matrix ; Given matrix ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void matrix ( int n , int m , vector < vector < int > > li ) { int ctr = 0 ; while ( ctr < 2 * n - 1 ) { for ( int i = 0 ; i < abs ( n - ctr - 1 ) ; i ++ ) { cout << " ▁ " ; } vector < int > lst ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i + j == ctr ) { lst . push_back ( li [ i ] [ j ] ) ; } } } for ( int i = lst . size ( ) - 1 ; i >= 0 ; i -- ) { cout << lst [ i ] << " ▁ " ; } cout << endl ; ctr += 1 ; } } int main ( ) { int n = 8 ; int m = n ; vector < vector < int > > li { { 4 , 5 , 6 , 9 , 8 , 7 , 1 , 4 } , { 1 , 5 , 9 , 7 , 5 , 3 , 1 , 6 } , { 7 , 5 , 3 , 1 , 5 , 9 , 8 , 0 } , { 6 , 5 , 4 , 7 , 8 , 9 , 3 , 7 } , { 3 , 5 , 6 , 4 , 8 , 9 , 2 , 1 } , { 3 , 1 , 6 , 4 , 7 , 9 , 5 , 0 } , { 8 , 0 , 7 , 2 , 3 , 1 , 0 , 8 } , { 7 , 5 , 3 , 1 , 5 , 9 , 8 , 5 } } ; matrix ( n , m , li ) ; return 0 ; }
Mth element after K Right Rotations of an Array | C ++ program to implement the above approach ; Function to return Mth element of array after k right rotations ; The array comes to original state after N rotations ; If K is greater or equal to M ; Mth element after k right rotations is ( N - K ) + ( M - 1 ) th element of the array ; Otherwise ; ( M - K - 1 ) th element of the array ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getFirstElement ( int a [ ] , int N , int K , int M ) { K %= N ; int index ; if ( K >= M ) index = ( N - K ) + ( M - 1 ) ; else index = ( M - K - 1 ) ; int result = a [ index ] ; return result ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int K = 3 , M = 2 ; cout << getFirstElement ( a , N , K , M ) ; return 0 ; }
Find the Mth element of the Array after K left rotations | C ++ program for the above approach ; Function to return Mth element of array after k left rotations ; The array comes to original state after N rotations ; Mth element after k left rotations is ( K + M - 1 ) % N th element of the original array ; Return the result ; Driver Code ; Array initialization ; Size of the array ; Given K rotation and Mth element to be found after K rotation ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getFirstElement ( int a [ ] , int N , int K , int M ) { K %= N ; int index = ( K + M - 1 ) % N ; int result = a [ index ] ; return result ; } int main ( ) { int a [ ] = { 3 , 4 , 5 , 23 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int K = 2 , M = 1 ; cout << getFirstElement ( a , N , K , M ) ; return 0 ; }
Rotate all odd numbers right and all even numbers left in an Array of 1 to N | C ++ program to implement the above approach ; function to left rotate ; function to right rotate ; Function to rotate the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void left_rotate ( int arr [ ] ) { int last = arr [ 1 ] ; for ( int i = 3 ; i < 6 ; i = i + 2 ) { arr [ i - 2 ] = arr [ i ] ; } arr [ 6 - 1 ] = last ; } void right_rotate ( int arr [ ] ) { int start = arr [ 6 - 2 ] ; for ( int i = 6 - 4 ; i >= 0 ; i = i - 2 ) { arr [ i + 2 ] = arr [ i ] ; } arr [ 0 ] = start ; } void rotate ( int arr [ ] ) { left_rotate ( arr ) ; right_rotate ( arr ) ; for ( int i = 0 ; i < 6 ; i ++ ) { cout << ( arr [ i ] ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; rotate ( arr ) ; }
Maximize count of corresponding same elements in given permutations using cyclic rotations | C ++ program for the above approach ; Function to maximize the matching pairs between two permutation using left and right rotation ; Left array store distance of element from left side and right array store distance of element from right side ; Map to store index of elements ; idx1 is index of element in first permutation idx2 is index of element in second permutation ; If element if present on same index on both permutations then distance is zero ; Calculate distance from left and right side ; Calculate distance from left and right side ; Maps to store frequencies of elements present in left and right arrays ; Find maximum frequency ; Return the result ; Driver Code ; Given permutations P1 and P2 ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumMatchingPairs ( int perm1 [ ] , int perm2 [ ] , int n ) { int left [ n ] , right [ n ] ; map < int , int > mp1 , mp2 ; for ( int i = 0 ; i < n ; i ++ ) { mp1 [ perm1 [ i ] ] = i ; } for ( int j = 0 ; j < n ; j ++ ) { mp2 [ perm2 [ j ] ] = j ; } for ( int i = 0 ; i < n ; i ++ ) { int idx2 = mp2 [ perm1 [ i ] ] ; int idx1 = i ; if ( idx1 == idx2 ) { left [ i ] = 0 ; right [ i ] = 0 ; } else if ( idx1 < idx2 ) { left [ i ] = ( n - ( idx2 - idx1 ) ) ; right [ i ] = ( idx2 - idx1 ) ; } else { left [ i ] = ( idx1 - idx2 ) ; right [ i ] = ( n - ( idx1 - idx2 ) ) ; } } map < int , int > freq1 , freq2 ; for ( int i = 0 ; i < n ; i ++ ) { freq1 [ left [ i ] ] ++ ; freq2 [ right [ i ] ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = max ( ans , max ( freq1 [ left [ i ] ] , freq2 [ right [ i ] ] ) ) ; } return ans ; } int main ( ) { int P1 [ ] = { 5 , 4 , 3 , 2 , 1 } ; int P2 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( P1 ) / sizeof ( P1 [ 0 ] ) ; cout << maximumMatchingPairs ( P1 , P2 , n ) ; return 0 ; }
Count of rotations required to generate a sorted array | C ++ program to find the count of rotations ; Function to return the count of rotations ; Find the smallest element ; Return its index ; If array is not rotated at all ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countRotation ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { return i ; } } return 0 ; } int main ( ) { int arr1 [ ] = { 4 , 5 , 1 , 2 , 3 } ; int n = sizeof ( arr1 ) / sizeof ( int ) ; cout << countRotation ( arr1 , n ) ; }
Count of rotations required to generate a sorted array | C ++ program to implement the above approach ; Function to return the count of rotations ; If array is not rotated ; Check if current element is greater than the next element ; The next element is the smallest ; Check if current element is smaller than it 's previous  element ; Current element is the smallest ; Check if current element is greater than lower bound ; The sequence is increasing so far Search for smallest element on the right subarray ; Smallest element lies on the left subarray ; Search for the smallest element on both subarrays ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countRotation ( int arr [ ] , int low , int high ) { if ( low > high ) { return 0 ; } int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid ] > arr [ mid + 1 ] ) { return mid + 1 ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return mid ; } if ( arr [ mid ] > arr [ low ] ) { return countRotation ( arr , mid + 1 , high ) ; } if ( arr [ mid ] < arr [ high ] ) { return countRotation ( arr , low , mid - 1 ) ; } else { int rightIndex = countRotation ( arr , mid + 1 , high ) ; int leftIndex = countRotation ( arr , low , mid - 1 ) ; if ( rightIndex == 0 ) { return leftIndex ; } return rightIndex ; } } int main ( ) { int arr1 [ ] = { 4 , 5 , 1 , 2 , 3 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << countRotation ( arr1 , 0 , N - 1 ) ; return 0 ; }
Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings | C ++ Program to count the minimum number of circular rotations required to obtain a given numeric strings avoiding a set of blocked strings ; If the starting string needs to be avoided ; If the final string needs to be avoided ; Variable to store count of rotations ; BFS Approach ; Store the current size of the queue ; Traverse the string ; Increase the current character ; Circular rotation ; If target is reached ; If the string formed is not one to be avoided ; Add it to the list of strings to be avoided to prevent visiting already visited states ; Decrease the current value by 1 and repeat the similar checkings ; Restore the original character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCircularRotations ( string target , vector < string > & blocked , int N ) { string start = " " ; for ( int i = 0 ; i < N ; i ++ ) { start += '0' ; } unordered_set < string > avoid ; for ( int i = 0 ; i < blocked . size ( ) ; i ++ ) avoid . insert ( blocked [ i ] ) ; if ( avoid . find ( start ) != avoid . end ( ) ) return -1 ; if ( avoid . find ( target ) != avoid . end ( ) ) return -1 ; queue < string > qu ; qu . push ( start ) ; int count = 0 ; while ( ! qu . empty ( ) ) { count ++ ; int size = qu . size ( ) ; for ( int j = 0 ; j < size ; j ++ ) { string st = qu . front ( ) ; qu . pop ( ) ; for ( int i = 0 ; i < N ; i ++ ) { char ch = st [ i ] ; st [ i ] ++ ; if ( st [ i ] > '9' ) st [ i ] = '0' ; if ( st == target ) return count ; if ( avoid . find ( st ) == avoid . end ( ) ) qu . push ( st ) ; avoid . insert ( st ) ; st [ i ] = ch - 1 ; if ( st [ i ] < '0' ) st [ i ] = '9' ; if ( st == target ) return count ; if ( avoid . find ( st ) == avoid . end ( ) ) qu . push ( st ) ; avoid . insert ( st ) ; st [ i ] = ch ; } } } return -1 ; } int main ( ) { int N = 4 ; string target = "7531" ; vector < string > blocked = { "1543" , "7434" , "7300" , "7321" , "2427" } ; cout << minCircularRotations ( target , blocked , N ) << endl ; return 0 ; }