codeparrot/xlcost-text-to-code huggingface dataset:
Counting cross lines in an array | Java program to count cross line in array ; Merges two subarrays of arr [ ] . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] ; create temp arrays ; Copy data to temp arrays L [ ] and R [ ] ; i = 0 ; Initial index of first subarray j = 0 ; Initial index of second subarray k = l ; Initial index of merged subarray ; == == == == == == == == == == == == == == == == == == == == == = MAIN PORTION OF CODE == == == == == == == == == == == == == == == == == == == == = add all line which is cross by current element ; Copy the remaining elements of L [ ] , if there are any ; Copy the remaining elements of R [ ] , if there are any ; l is for left index and r is right index of the sub - array of arr to be sorted ; Same as ( l + r ) / 2 , but avoids overflow for large l and h ; Sort first and second halves ; function return count of cross line in an array ; Driver Code
import java . util . * ; class GFG { static int count_crossline ; static void merge ( int arr [ ] , int l , int m , int r ) { int i , j , k ; int n1 = m - l + 1 ; int n2 = r - m ; int [ ] L = new int [ n1 ] ; int [ ] R = new int [ n2 ] ; for ( i = 0 ; i < n1 ; i ++ ) { L [ i ] = arr [ l + i ] ; } for ( j = 0 ; j < n2 ; j ++ ) { R [ j ] = arr [ m + 1 + j ] ; } while ( i < n1 && j < n2 ) { if ( L [ i ] <= R [ j ] ) { arr [ k ] = L [ i ] ; i ++ ; } else { arr [ k ] = R [ j ] ; count_crossline += ( n1 - i ) ; j ++ ; } k ++ ; } while ( i < n1 ) { arr [ k ] = L [ i ] ; i ++ ; k ++ ; } while ( j < n2 ) { arr [ k ] = R [ j ] ; j ++ ; k ++ ; } } static void mergeSort ( int arr [ ] , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } static int countCrossLine ( int arr [ ] , int n ) { mergeSort ( arr , 0 , n - 1 ) ; return count_crossline ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; int n = arr . length ; System . out . println ( countCrossLine ( arr , n ) ) ; } }
Count number of elements between two given elements in array | Program to count number of elements between two given elements . ; Function to count number of elements occurs between the elements . ; Find num1 ; If num1 is not present or present at end ; Find num2 ; If num2 is not present ; return number of elements between the two elements . ; Driver program
import java . io . * ; class GFG { static int getCount ( int arr [ ] , int n , int num1 , int num2 ) { int i = 0 ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == num1 ) break ; if ( i >= n - 1 ) return 0 ; int j ; for ( j = n - 1 ; j >= i + 1 ; j -- ) if ( arr [ j ] == num2 ) break ; if ( j == i ) return 0 ; return ( j - i - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 } ; int n = arr . length ; int num1 = 5 , num2 = 4 ; System . out . println ( getCount ( arr , n , num1 , num2 ) ) ; } }
Best meeting point in 2D binary array | Java program to find best meeting point in 2D array ; Find all members home 's position ; Sort positions so we can find most beneficial point ; middle position will always beneficial for all group members but it will be sorted which we have already done ; Now find total distance from best meeting point ( x , y ) using Manhattan Distance formula ; Driver code
import java . util . * ; class GFG { static int ROW = 3 ; static int COL = 5 ; static int minTotalDistance ( int grid [ ] [ ] ) { if ( ROW == 0 COL == 0 ) return 0 ; Vector < Integer > vertical = new Vector < Integer > ( ) ; Vector < Integer > horizontal = new Vector < Integer > ( ) ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( grid [ i ] [ j ] == 1 ) { vertical . add ( i ) ; horizontal . add ( j ) ; } } } Collections . sort ( vertical ) ; Collections . sort ( horizontal ) ; int size = vertical . size ( ) / 2 ; int x = vertical . get ( size ) ; int y = horizontal . get ( size ) ; int distance = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( grid [ i ] [ j ] == 1 ) distance += Math . abs ( x - i ) + Math . abs ( y - j ) ; return distance ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 } } ; System . out . println ( minTotalDistance ( grid ) ) ; } }
Count numbers with difference between number and its digit sum greater than specific value | Java program to count total numbers which have difference with sum of digits greater than specific value ; Utility method to get sum of digits of K ; loop until K is not zero ; method returns count of numbers smaller than N , satisfying difference condition ; binary search while loop ; if difference between number and its sum of digit is smaller than given difference then smallest number will be on left side ; if difference between number and its sum of digit is greater than or equal to given difference then smallest number will be on right side ; return the difference between ' smallest ▁ number ▁ ▁ found ' and ' N ' as result ; Driver method
class Test { static int sumOfDigit ( int K ) { int sod = 0 ; while ( K != 0 ) { sod += K % 10 ; K /= 10 ; } return sod ; } static int totalNumbersWithSpecificDifference ( int N , int diff ) { int low = 1 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( mid - sumOfDigit ( mid ) < diff ) low = mid + 1 ; else high = mid - 1 ; } return ( N - high ) ; } public static void main ( String args [ ] ) { int N = 13 ; int diff = 2 ; System . out . println ( totalNumbersWithSpecificDifference ( N , diff ) ) ; } }
Randomized Binary Search Algorithm | Java program to implement iterative randomized algorithm . ; To generate random number between x and y ie . . [ x , y ] ; A iterative randomized binary search function . It returns location of x in given array arr [ l . . r ] if present , otherwise - 1 ; Here we have defined middle as random index between l and r ie . . [ l , r ] ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; if we reach here , then element was not present ; Driver code
class GFG { static int getRandom ( int x , int y ) { return ( int ) ( x + Math . random ( ) * 10 % ( y - x + 1 ) ) ; } static int randomizedBinarySearch ( int arr [ ] , int l , int r , int x ) { while ( l <= r ) { int m = getRandom ( l , r ) ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int x = 10 ; int result = randomizedBinarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) System . out . printf ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; else System . out . printf ( " Element ▁ is ▁ present ▁ at ▁ index ▁ % d " , result ) ; } }
Number of buildings facing the sun | Java program to count buildings that can see sunlight . ; Returns count buildings that can see sunlight ; Initialuze result ( Note that first building always sees sunlight ) ; Start traversing element ; If curr_element is maximum or current element is equal , update maximum and increment count ; Driver method
class Test { static int countBuildings ( int arr [ ] , int n ) { int count = 1 ; int curr_max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > curr_max arr [ i ] == curr_max ) { count ++ ; curr_max = arr [ i ] ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 4 , 8 , 2 , 9 } ; System . out . println ( countBuildings ( arr , arr . length ) ) ; } }
Find index of an extra element present in one sorted array | Java program to find an extra element present in arr1 [ ] ; Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Driver Code ; Solve is passed both arrays
class GFG { static int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ; return n ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arr2 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . length ; System . out . println ( findExtra ( arr1 , arr2 , n ) ) ; } }
Find index of an extra element present in one sorted array | Java program to find an extra element present in arr1 [ ] ; Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Initialize result ; left and right are end points denoting the current range . ; If middle element is same of both arrays , it means that extra element is after mid so we update left to mid + 1 ; If middle element is different of the arrays , it means that the index we are searching for is either mid , or before mid . Hence we update right to mid - 1. ; when right is greater than left , our search is complete . ; Driver Code ; Solve is passed both arrays
class GFG { static int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { int index = n ; int left = 0 , right = n - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr2 [ mid ] == arr1 [ mid ] ) left = mid + 1 ; else { index = mid ; right = mid - 1 ; } } return index ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arr2 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . length ; System . out . println ( findExtra ( arr1 , arr2 , n ) ) ; } }
Make all array elements equal with minimum cost | JAVA Code for Make all array elements equal with minimum cost ; Utility method to compute cost , when all values of array are made equal to X ; Method to find minimum cost to make all elements equal ; setting limits for ternary search by smallest and largest element ; loop until difference between low and high become less than 3 , because after that mid1 and mid2 will start repeating ; mid1 and mid2 are representative array equal values of search space ; if mid2 point gives more total cost , skip third part ; if mid1 point gives more total cost , skip first part ; computeCost gets optimum cost by sending average of low and high as X ; Driver program to test above function
class GFG { public static int computeCost ( int arr [ ] , int N , int X ) { int cost = 0 ; for ( int i = 0 ; i < N ; i ++ ) cost += Math . abs ( arr [ i ] - X ) ; return cost ; } public static int minCostToMakeElementEqual ( int arr [ ] , int N ) { int low , high ; low = high = arr [ 0 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( low > arr [ i ] ) low = arr [ i ] ; if ( high < arr [ i ] ) high = arr [ i ] ; } while ( ( high - low ) > 2 ) { int mid1 = low + ( high - low ) / 3 ; int mid2 = high - ( high - low ) / 3 ; int cost1 = computeCost ( arr , N , mid1 ) ; int cost2 = computeCost ( arr , N , mid2 ) ; if ( cost1 < cost2 ) high = mid2 ; else low = mid1 ; } return computeCost ( arr , N , ( low + high ) / 2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 100 , 101 } ; int N = arr . length ; System . out . println ( minCostToMakeElementEqual ( arr , N ) ) ; } }
Minimum number of jumps required to sort numbers placed on a number line | Java program for the above approach ; Function to find the minimum number of jumps required to sort the array ; Base Case ; Store the required result ; Stores the current position of elements and their respective maximum jump ; Used to check if a position is already taken by another element ; Stores the sorted array a [ ] ; Traverse the array w [ ] & update positions jumps array a [ ] ; Sort the array a [ ] ; Traverse the array a [ ] over the range [ 1 , N - 1 ] ; Store the index of current element and its just smaller element in array w [ ] ; Iterate until current element position is at most its just smaller element position ; Update the position of the current element ; Print the result ; Driver Code
import java . util . * ; class GFG { static void minJumps ( int [ ] w , int [ ] l , int n ) { if ( n == 1 ) { System . out . print ( 0 ) ; return ; } int ans = 0 ; HashMap < Integer , Integer > pos = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > jump = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Boolean > filled = new HashMap < Integer , Boolean > ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( pos . containsKey ( w [ i ] ) ) pos . put ( w [ i ] , i ) ; else pos . put ( w [ i ] , i ) ; if ( filled . containsKey ( w [ i ] ) ) filled . put ( i , true ) ; else filled . put ( i , true ) ; if ( jump . containsKey ( w [ i ] ) ) jump . put ( w [ i ] , l [ i ] ) ; else jump . put ( w [ i ] , l [ i ] ) ; a [ i ] = w [ i ] ; } Arrays . sort ( a ) ; for ( int curr = 1 ; curr < n ; curr ++ ) { int currElementPos = pos . get ( a [ curr ] ) ; int prevElementPos = pos . get ( a [ curr - 1 ] ) ; if ( currElementPos > prevElementPos ) continue ; while ( currElementPos <= prevElementPos || filled . containsKey ( currElementPos ) && filled . containsKey ( currElementPos ) ) { currElementPos += jump . get ( a [ curr ] ) ; ans ++ ; } if ( pos . containsKey ( a [ curr ] ) ) pos . put ( a [ curr ] , currElementPos ) ; else pos . put ( a [ curr ] , currElementPos ) ; if ( filled . containsKey ( currElementPos ) ) filled . put ( currElementPos , true ) ; else filled . put ( currElementPos , true ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int [ ] W = { 2 , 1 , 4 , 3 } ; int [ ] L = { 4 , 1 , 2 , 4 } ; int N = W . length ; minJumps ( W , L , N ) ; } }
Make an array strictly increasing by repeatedly subtracting and adding arr [ i | Java program for the above approach ; Function to check if an array can be made strictly increasing ; Traverse the given array arr [ ] ; Update the value of p , arr [ i ] , and arr [ i - 1 ] ; Traverse the given array ; Check if the array arr [ ] is strictly increasing or not ; Otherwise , array is increasing order , print Yes ; Driver Code
import java . io . * ; class GFG { static void check ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= ( i - 1 ) ) { int p = arr [ i - 1 ] - ( i - 1 ) ; arr [ i ] += p ; arr [ i - 1 ] -= p ; } } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= arr [ i - 1 ] ) { System . out . println ( " No " ) ; return ; } } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 7 , 6 } ; int N = arr . length ; check ( arr , N ) ; } }
Rearrange array to make decimal equivalents of reversed binary representations of array elements sorted | Java program for the above approach ; Function to reverse the bits of a number ; Stores the reversed number ; Divide rev by 2 ; If the value of N is odd ; Update the value of N ; Return the final value of rev ; Function for rearranging the array element according to the given rules ; Stores the new array elements ; Function for rearranging the array ; Stores the new array ; Function to sort the array by reversing binary representation ; Creating a new array ; Sort the array with the key ; Get arr from newArr ; Print the sorted array ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int keyFunc ( int n ) { int rev = 0 ; while ( n > 0 ) { rev = rev << 1 ; if ( ( n & 1 ) == 1 ) rev = rev ^ 1 ; n = n >> 1 ; } return rev ; } static int [ ] [ ] getNew ( int arr [ ] ) { int ans [ ] [ ] = new int [ arr . length ] [ 2 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) ans [ i ] = new int [ ] { keyFunc ( arr [ i ] ) , arr [ i ] } ; return ans ; } static int [ ] getArr ( int [ ] [ ] arr ) { int ans [ ] = new int [ arr . length ] ; int idx = 0 ; for ( int i [ ] : arr ) ans [ idx ++ ] = i [ 1 ] ; return ans ; } static void sortArray ( int arr [ ] ) { int [ ] [ ] newArr = getNew ( arr ) ; Arrays . sort ( newArr , ( a , b ) -> { if ( Integer . compare ( a [ 0 ] , b [ 0 ] ) == 0 ) return Integer . compare ( a [ 1 ] , b [ 1 ] ) ; return Integer . compare ( a [ 0 ] , b [ 0 ] ) ; } ) ; arr = getArr ( newArr ) ; int n = arr . length ; System . out . print ( " [ " ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) System . out . print ( arr [ i ] + " , ▁ " ) ; System . out . print ( arr [ n - 1 ] + " ] " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 43 , 52 , 61 , 41 } ; sortArray ( arr ) ; } }
Minimize maximum array element by splitting array elements into powers of two at most K times | Java program for the above approach ; Function to find the minimum value of the maximum element of the array by splitting at most K array element into perfect powers of 2 ; Sort the array element in the ascending order ; Reverse the array ; If count of 0 is equal to N ; Otherwise , if K is greater than or equal to N ; Otherwise ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static void minimumSize ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; reverse ( arr ) ; if ( count ( arr , 0 ) == N ) System . out . println ( 0 ) ; else if ( K >= N ) System . out . println ( 1 ) ; else System . out . println ( arr [ K ] ) ; } static void reverse ( int [ ] a ) { int i , k , t ; int n = a . length ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static int count ( int [ ] a , int n ) { int freq = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] == n ) freq ++ ; } return freq ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 8 , 2 } ; int K = 2 ; int N = arr . length ; minimumSize ( arr , N , K ) ; } }
Check if removal of a subsequence of non | Java program for the above approach ; Function to check if it is possible to sort the array or not ; Stores the index if there are two consecutive 1 's in the array ; Traverse the given array ; Check adjacent same elements having values 1 s ; If there are no two consecutive 1 s , then always remove all the 1 s from array & make it sorted ; If two consecutive 0 ' s ▁ are ▁ ▁ present ▁ after ▁ two ▁ consecutive ▁ ▁ 1' s then array can 't be sorted ; Otherwise , print Yes ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void isPossibleToSort ( int arr [ ] , int N ) { int idx = - 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 1 && arr [ i - 1 ] == 1 ) { idx = i ; break ; } } if ( idx == - 1 ) { System . out . println ( " YES " ) ; return ; } for ( int i = idx + 1 ; i < N ; i ++ ) { if ( arr [ i ] == 0 && arr [ i - 1 ] == 0 ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int N = arr . length ; isPossibleToSort ( arr , N ) ; } }
Maximize ropes of consecutive length possible by connecting given ropes | Java program for the above approach ; Function to find maximized count of ropes of consecutive length ; Stores the maximum count of ropes of consecutive length ; Sort the ropes by their length ; Traverse the array ; If size of the current rope is less than or equal to current maximum possible size + 1 , update the range to curSize + ropes [ i ] ; If a rope of size ( curSize + 1 ) cannot be obtained ; Driver code ; Input ; Function Call
import java . util . * ; class GFG { static int maxConsecutiveRope ( int ropes [ ] , int N ) { int curSize = 0 ; Arrays . sort ( ropes ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ropes [ i ] <= curSize + 1 ) { curSize = curSize + ropes [ i ] ; } else break ; } return curSize ; } public static void main ( String [ ] args ) { int N = 5 ; int ropes [ ] = { 1 , 2 , 7 , 1 , 1 } ; System . out . println ( maxConsecutiveRope ( ropes , N ) ) ; } }
Maximum ranges that can be uniquely represented by any integer from the range | java program for the above approach ; Function to find the maximum number of ranges where each range can be uniquely represented by an integer ; Sort the ranges in ascending order ; Stores the count of ranges ; Stores previously assigned range ; Traverse the vector arr [ ] ; Skip the similar ranges of size 1 ; Find the range of integer available to be assigned ; Check if an integer is available to be assigned ; Update the previously assigned range ; Update the count of range ; Return the maximum count of ranges ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int maxRanges ( Integer arr [ ] [ ] , int N ) { Arrays . sort ( arr , ( a , b ) -> { if ( a [ 0 ] . equals ( b [ 0 ] ) ) return Integer . compare ( a [ 1 ] , b [ 1 ] ) ; return Integer . compare ( a [ 0 ] , b [ 0 ] ) ; } ) ; int count = 1 ; Integer prev [ ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { Integer last [ ] = arr [ i - 1 ] ; Integer current [ ] = arr [ i ] ; if ( last [ 0 ] == current [ 0 ] && last [ 1 ] == current [ 1 ] && current [ 1 ] == current [ 0 ] ) continue ; int start = Math . max ( prev [ 0 ] , current [ 0 ] - 1 ) ; int end = Math . max ( prev [ 1 ] , current [ 1 ] ) ; if ( end - start > 0 ) { prev [ 0 ] = 1 + start ; prev [ 1 ] = end ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { Integer range [ ] [ ] = { { 1 , 4 } , { 4 , 4 } , { 2 , 2 } , { 3 , 4 } , { 1 , 1 } } ; int N = range . length ; System . out . print ( maxRanges ( range , N ) ) ; } }
Sort a string lexicographically by reversing a substring | Java program for the above approach ; Function to find the substring in S required to be reversed ; Stores the size of the string ; Stores the starting point of the substring ; Iterate over the string S while i < N ; Increment the value of i ; Stores the ending index of the substring ; If start <= 0 or i >= N ; If start >= 1 and i <= N ; Return the boolean value ; If start >= 1 ; Return the boolean value ; If i < N ; Return true if S [ start ] is less than or equal to S [ i ] ; Otherwise ; Function to check if it is possible to sort the string or not ; Stores the starting and the ending index of substring ; Stores whether it is possible to sort the substring ; Traverse the range [ 1 , N ] ; If S [ i ] is less than S [ i - 1 ] ; If flag stores true ; If adjust ( S , i , start , end ) return false ; Print - 1 ; Unset the flag ; Otherwise ; Print - 1 ; If start is equal to - 1 ; Update start and end ; Print the value of start ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int i , start , end ; static boolean adjust ( String S ) { int N = S . length ( ) ; start = i - 1 ; while ( i < N && S . charAt ( i ) < S . charAt ( i - 1 ) ) { i ++ ; } end = i - 1 ; if ( start <= 0 && i >= N ) return true ; if ( start >= 1 && i <= N ) { return ( S . charAt ( end ) >= S . charAt ( start - 1 ) && S . charAt ( start ) <= S . charAt ( i ) ) ; } if ( start >= 1 ) { return S . charAt ( end ) >= S . charAt ( start - 1 ) ; } if ( i < N ) { return S . charAt ( start ) <= S . charAt ( i ) ; } return false ; } static void isPossible ( String S , int N ) { start = - 1 ; end = - 1 ; boolean flag = true ; for ( i = 1 ; i < N ; i ++ ) { if ( S . charAt ( i ) < S . charAt ( i - 1 ) ) { if ( flag ) { if ( adjust ( S ) == false ) { System . out . println ( - 1 ) ; return ; } flag = false ; } else { System . out . println ( - 1 ) ; return ; } } } if ( start == - 1 ) { start = end = 1 ; } System . out . println ( start + " ▁ " + end ) ; } public static void main ( String [ ] args ) { String S = " abcyxuz " ; int N = S . length ( ) ; isPossible ( S , N ) ; } }