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 | Java 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
import java . util . * ; class GFG { static void findMaximumZeros ( String str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str . charAt ( i ) == '0' ) c0 ++ ; } if ( c0 == n ) { System . out . print ( n ) ; return ; } int mx = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) cnt ++ ; else { mx = Math . max ( mx , cnt ) ; cnt = 0 ; } } mx = Math . max ( mx , cnt ) ; int start = 0 , end = n - 1 ; cnt = 0 ; while ( str . charAt ( start ) != '1' && start < n ) { cnt ++ ; start ++ ; } while ( str . charAt ( end ) != '1' && end >= 0 ) { cnt ++ ; end -- ; } mx = Math . max ( mx , cnt ) ; System . out . println ( mx ) ; } public static void main ( String [ ] args ) { String s = "1001" ; int n = s . length ( ) ; findMaximumZeros ( s , n ) ; } }
Modify a matrix by rotating ith row exactly i times in clockwise direction | java program for the above approach ; Function to reverse arr [ ] from start to end ; 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
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void reverse ( int arr [ ] , int start , int end ) { while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } static void rotateMatrix ( int mat [ ] [ ] ) { int i = 0 ; for ( int rows [ ] : mat ) { reverse ( rows , 0 , rows . length - 1 ) ; reverse ( rows , 0 , i - 1 ) ; reverse ( rows , i , rows . length - 1 ) ; i ++ ; } for ( int rows [ ] : mat ) { for ( int cols : rows ) { System . out . print ( cols + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; rotateMatrix ( mat ) ; } }
Modify a string by performing given shift operations | Java 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
import java . io . * ; class GFG { static void stringShift ( String s , int [ ] [ ] shift ) { int val = 0 ; for ( int i = 0 ; i < shift . length ; ++ i ) if ( shift [ i ] [ 0 ] == 0 ) val -= shift [ i ] [ 1 ] ; else val += shift [ i ] [ 1 ] ; int len = s . length ( ) ; val = val % len ; String result = " " ; if ( val > 0 ) result = s . substring ( len - val , ( len - val ) + val ) + s . substring ( 0 , len - val ) ; else result = s . substring ( - val , len + val ) + s . substring ( 0 , - val ) ; System . out . println ( result ) ; } public static void main ( String [ ] args ) { String s = " abc " ; int [ ] [ ] shift = new int [ ] [ ] { { 0 , 1 } , { 1 , 2 } } ; stringShift ( s , shift ) ; } }
Modify given array to a non | Java 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
import java . util . * ; class GFG { static void rotateArray ( int [ ] arr , int N ) { int [ ] v = arr ; Arrays . sort ( v ) ; for ( int i = 1 ; i <= N ; ++ i ) { int x = arr [ N - 1 ] ; i = N - 1 ; while ( i > 0 ) { arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = x ; i -= 1 ; } if ( arr == v ) { System . out . print ( " YES " ) ; return ; } } System . out . print ( " NO " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 5 , 1 , 2 } ; int N = arr . length ; rotateArray ( arr , N ) ; } }
Maximum value possible by rotating digits of a given number | Java 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
import java . util . * ; class GFG { static void findLargestRotation ( int num ) { int ans = num ; int len = ( int ) Math . floor ( ( ( int ) Math . log10 ( num ) ) + 1 ) ; int x = ( int ) Math . 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 ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 657 ; findLargestRotation ( N ) ; } }
Rotate digits of a given number by K | Java 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
import java . io . * ; class GFG { static int numberOfDigit ( int N ) { int digit = 0 ; while ( N > 0 ) { digit ++ ; N /= 10 ; } return digit ; } static void rotateNumberByK ( int N , int K ) { int X = numberOfDigit ( N ) ; K = ( ( K % X ) + X ) % X ; int left_no = N / ( int ) ( Math . pow ( 10 , X - K ) ) ; N = N % ( int ) ( Math . pow ( 10 , X - K ) ) ; int left_digit = numberOfDigit ( left_no ) ; N = ( N * ( int ) ( Math . pow ( 10 , left_digit ) ) ) + left_no ; System . out . println ( N ) ; } public static void main ( String args [ ] ) { int N = 12345 , K = 7 ; rotateNumberByK ( N , K ) ; } }
Count rotations required to sort given array in non | Java 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
import java . util . * ; class GFG { static void minMovesToSort ( int arr [ ] , int N ) { int count = 0 ; int index = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { count ++ ; index = i ; } } if ( count == 0 ) { System . out . print ( "0" ) ; } else if ( count == N - 1 ) { System . out . print ( N - 1 ) ; } else if ( count == 1 && arr [ 0 ] <= arr [ N - 1 ] ) { System . out . print ( index + 1 ) ; } else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 5 , 4 , 2 } ; int N = arr . length ; minMovesToSort ( arr , N ) ; } }
Maximize sum of diagonal of a matrix by rotating all rows or all columns | Java 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
import java . util . * ; class GFG { static int N = 3 ; static int findMaximumDiagonalSumOMatrixf ( int A [ ] [ ] ) { int maxDiagonalSum = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int curr = 0 ; for ( int j = 0 ; j < N ; j ++ ) { curr += A [ j ] [ ( i + j ) % N ] ; } maxDiagonalSum = Math . 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 = Math . max ( maxDiagonalSum , curr ) ; } return maxDiagonalSum ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 1 , 2 } , { 2 , 1 , 2 } , { 1 , 2 , 2 } } ; System . out . println ( findMaximumDiagonalSumOMatrixf ( mat ) ) ; } }
Generate a matrix having sum of secondary diagonal equal to a perfect square | Java 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
class GFG { static void diagonalSumPerfectSquare ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { System . out . print ( arr [ ( j + i ) % 7 ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] srgs ) { int N = 7 ; int [ ] arr = new int [ 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 | Java 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
class GFG { static int MaxSum ( 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 ; } static int gcd ( int n1 , int n2 ) { if ( n2 == 0 ) { return n1 ; } else { return gcd ( n2 , n1 % n2 ) ; } } static int [ ] RotateArr ( 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 ; } static void performQuery ( int [ ] arr , int Q [ ] [ ] , int q ) { int N = arr . length ; for ( int i = 0 ; i < q ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { arr = RotateArr ( arr , N , Q [ i ] [ 1 ] ) ; for ( int t : arr ) { System . out . print ( t + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } else { System . out . print ( MaxSum ( arr , N , Q [ i ] [ 1 ] ) + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int q = 5 ; int Q [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 1 , 3 } , { 1 , 1 } , { 2 , 4 } } ; performQuery ( arr , Q , q ) ; } }
Minimize characters to be changed to make the left and right rotation of a string same | Java 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
class GFG { public static int getMinimumRemoval ( String str ) { int n = str . length ( ) ; int ans = n ; if ( n % 2 == 0 ) { int [ ] freqEven = new int [ 128 ] ; int [ ] freqOdd = new int [ 128 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { freqEven [ str . charAt ( i ) ] ++ ; } else { freqOdd [ str . charAt ( i ) ] ++ ; } } int evenMax = 0 , oddMax = 0 ; for ( char chr = ' a ' ; chr <= ' z ' ; chr ++ ) { evenMax = Math . max ( evenMax , freqEven [ chr ] ) ; oddMax = Math . max ( oddMax , freqOdd [ chr ] ) ; } ans = ans - evenMax - oddMax ; } else { int [ ] freq = new int [ 128 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str . charAt ( i ) ] ++ ; } int strMax = 0 ; for ( char chr = ' a ' ; chr <= ' z ' ; chr ++ ) { strMax = Math . max ( strMax , freq [ chr ] ) ; } ans = ans - strMax ; } return ans ; } public static void main ( String [ ] args ) { String str = " geeksgeeks " ; System . out . print ( getMinimumRemoval ( str ) ) ; } }
Longest subsequence of a number having same left and right rotation | Java 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
import java . util . * ; class GFG { static int findAltSubSeq ( String s ) { int n = s . length ( ) , ans = Integer . MIN_VALUE ; 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 && s . charAt ( k ) - '0' == i ) { f = 1 ; cur ++ ; } else if ( f == 1 && s . charAt ( k ) - '0' == j ) { f = 0 ; cur ++ ; } } if ( i != j && cur % 2 == 1 ) cur -- ; ans = Math . max ( cur , ans ) ; } } return ans ; } public static void main ( String [ ] args ) { String s = "100210601" ; System . out . print ( findAltSubSeq ( s ) ) ; } }
Rotate matrix by 45 degrees | Java 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
import java . util . * ; class GFG { static void matrix ( int n , int m , int [ ] [ ] li ) { int ctr = 0 ; while ( ctr < 2 * n - 1 ) { for ( int i = 0 ; i < Math . abs ( n - ctr - 1 ) ; i ++ ) { System . out . print ( " ▁ " ) ; } Vector < Integer > lst = new Vector < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i + j == ctr ) { lst . add ( li [ i ] [ j ] ) ; } } } for ( int i = lst . size ( ) - 1 ; i >= 0 ; i -- ) { System . out . print ( lst . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; ctr += 1 ; } } public static void main ( String [ ] args ) { int n = 8 ; int m = n ; 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 ) ; } }
Mth element after K Right Rotations of an Array | Java 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
class GFG { static 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 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = 5 ; int K = 3 , M = 2 ; System . out . println ( getFirstElement ( a , N , K , M ) ) ; } }
Find the Mth element of the Array after K left rotations | Java 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
import java . util . * ; class GFG { public static int getFirstElement ( int [ ] a , int N , int K , int M ) { K %= N ; int index = ( K + M - 1 ) % N ; int result = a [ index ] ; return result ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 4 , 5 , 23 } ; int N = a . length ; int K = 2 , M = 1 ; System . out . println ( getFirstElement ( a , N , K , M ) ) ; } }
Rotate all odd numbers right and all even numbers left in an Array of 1 to N | Java program to implement the above approach ; function to left rotate ; function to right rotate ; Function to rotate the array ; Driver code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void left_rotate ( int [ ] arr ) { int last = arr [ 1 ] ; for ( int i = 3 ; i < arr . length ; i = i + 2 ) { arr [ i - 2 ] = arr [ i ] ; } arr [ arr . length - 1 ] = last ; } static void right_rotate ( int [ ] arr ) { int start = arr [ arr . length - 2 ] ; for ( int i = arr . length - 4 ; i >= 0 ; i = i - 2 ) { arr [ i + 2 ] = arr [ i ] ; } arr [ 0 ] = start ; } public static void rotate ( int arr [ ] ) { left_rotate ( arr ) ; right_rotate ( arr ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; rotate ( arr ) ; } }
Maximize count of corresponding same elements in given permutations using cyclic rotations | Java 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
import java . util . * ; class GFG { static int maximumMatchingPairs ( int perm1 [ ] , int perm2 [ ] , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; HashMap < Integer , Integer > mp1 = new HashMap < > ( ) ; HashMap < Integer , Integer > mp2 = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp1 . put ( perm1 [ i ] , i ) ; } for ( int j = 0 ; j < n ; j ++ ) { mp2 . put ( perm2 [ j ] , j ) ; } for ( int i = 0 ; i < n ; i ++ ) { int idx2 = mp2 . get ( 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 ) ) ; } } HashMap < Integer , Integer > freq1 = new HashMap < > ( ) ; HashMap < Integer , Integer > freq2 = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq1 . containsKey ( left [ i ] ) ) freq1 . put ( left [ i ] , freq1 . get ( left [ i ] ) + 1 ) ; else freq1 . put ( left [ i ] , 1 ) ; if ( freq2 . containsKey ( right [ i ] ) ) freq2 . put ( right [ i ] , freq2 . get ( right [ i ] ) + 1 ) ; else freq2 . put ( right [ i ] , 1 ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , Math . max ( freq1 . get ( left [ i ] ) , freq2 . get ( right [ i ] ) ) ) ; } return ans ; } public static void main ( String [ ] args ) { int P1 [ ] = { 5 , 4 , 3 , 2 , 1 } ; int P2 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = P1 . length ; System . out . print ( maximumMatchingPairs ( P1 , P2 , n ) ) ; } }
Count of rotations required to generate a sorted array | Java 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
public class GFG { public static int countRotation ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { return i ; } } return 0 ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 4 , 5 , 1 , 2 , 3 } ; System . out . println ( countRotation ( arr1 , arr1 . length ) ) ; } }
Count of rotations required to generate a sorted array | Java 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 Program
public class GFG { public static 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 ; } } public static void main ( String [ ] args ) { int [ ] arr1 = { 4 , 5 , 1 , 2 , 3 } ; System . out . println ( countRotation ( arr1 , 0 , arr1 . length - 1 ) ) ; } }