codeparrot/xlcost-text-to-code huggingface dataset:
Largest number having both positive and negative values present in the array | Java program for the above approach ; Function to find the largest number k such that both k and - k are present in the array ; Stores the array elements ; Initialize a variable res as 0 to store maximum element while traversing the array ; Iterate through array arr ; Add the current element into the set ; Check if the negative of this element is also present in the set or not ; Return the resultant element ; Drive Code
import java . io . * ; import java . util . * ; class GFG { public static int largestNum ( int [ ] arr ) { Set < Integer > set = new HashSet < > ( ) ; int res = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { set . add ( arr [ i ] ) ; if ( set . contains ( - 1 * arr [ i ] ) ) { res = Math . max ( res , Math . abs ( arr [ i ] ) ) ; } } return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , - 2 , 5 , - 3 } ; System . out . println ( largestNum ( arr ) ) ; } }
Check if an element is present in an array using at most floor ( N / 2 ) + 2 comparisons | Java program for the above approach ; Function to check whether X is present in the array A [ ] ; Initialise a pointer ; Store the number of comparisons ; Variable to store product ; Check is N is odd ; Update i and T ; Traverse the array ; Check if i < N ; Update T ; Check if T is equal to 0 ; Driver code ; Given Input Given Input ; Function Call
public class GFG { static void findElement ( int [ ] A , int N , int X ) { int i = 0 ; int Comparisons = 0 ; int T = 1 ; Comparisons ++ ; if ( N % 2 == 1 ) { i = 1 ; T *= ( A [ 0 ] - X ) ; } for ( ; i < N ; i += 2 ) { Comparisons += 1 ; T *= ( A [ i ] - X ) ; T *= ( A [ i + 1 ] - X ) ; } Comparisons += 1 ; if ( T == 0 ) { System . out . println ( " Yes ▁ " + Comparisons ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] A = { - 3 , 5 , 11 , 3 , 100 , 2 , 88 , 22 , 7 , 900 , 23 , 4 , 1 } ; int N = A . length ; int X = 1 ; findElement ( A , N , X ) ; } }
Search an element in a sorted array formed by reversing subarrays from a random index | Java program for the above approach ; Function to search an element in a sorted array formed by reversing subarrays from a random index ; Set the boundaries for binary search ; Apply binary search ; Initialize the middle element ; If element found ; Random point is on right side of mid ; From l to mid arr is reverse sorted ; Random point is on the left side of mid ; From mid to h arr is reverse sorted ; Return Not Found ; Drive Code ; Given Input ; Function Call
import java . util . * ; public class GFG { public static int find ( Vector < Integer > arr , int N , int key ) { int l = 0 ; int h = N - 1 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( arr . get ( mid ) == key ) return mid ; if ( arr . get ( l ) >= arr . get ( mid ) ) { if ( arr . get ( l ) >= key && key >= arr . get ( mid ) ) h = mid - 1 ; else l = mid + 1 ; } else { if ( arr . get ( mid ) >= key && key >= arr . get ( h ) ) l = mid + 1 ; else h = mid - 1 ; } } return - 1 ; } public static void main ( String args [ ] ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( 10 ) ; arr . add ( 8 ) ; arr . add ( 6 ) ; arr . add ( 5 ) ; arr . add ( 2 ) ; arr . add ( 1 ) ; arr . add ( 13 ) ; arr . add ( 12 ) ; int N = arr . size ( ) ; int key = 8 ; int ans = find ( arr , N , key ) ; System . out . println ( ans ) ; } }
Maximum elements that can be removed from front of two arrays such that their sum is at most K | Java program for the above approach ; Function to find the maximum number of items that can be removed from both the arrays ; Stores the maximum item count ; Stores the prefix sum of the cost of items ; Insert the item cost 0 at the front of the arrays ; Build the prefix sum for the array A [ ] ; Update the value of A [ i ] ; Build the prefix sum for the array B [ ] ; Update the value of B [ i ] ; Iterate through each item of the array A [ ] ; If A [ i ] exceeds K ; Store the remaining amount after taking top i elements from the array A ; Store the number of items possible to take from the array B [ ] ; Store low and high bounds for binary search ; Binary search to find number of item that can be taken from stack B in rem amount ; Calculate the mid value ; Update the value of j and lo ; Update the value of the hi ; Store the maximum of total item count ; Print the result ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void maxItems ( int n , int m , int a [ ] , int b [ ] , int K ) { int count = 0 ; int A [ ] = new int [ n + 1 ] ; int B [ ] = new int [ m + 1 ] ; A [ 0 ] = 0 ; B [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { A [ i ] = a [ i - 1 ] + A [ i - 1 ] ; } for ( int i = 1 ; i <= m ; i ++ ) { B [ i ] = b [ i - 1 ] + B [ i - 1 ] ; } for ( int i = 0 ; i <= n ; i ++ ) { if ( A [ i ] > K ) break ; int rem = K - A [ i ] ; int j = 0 ; int lo = 0 , hi = m ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; if ( B [ mid ] <= rem ) { j = mid ; lo = mid + 1 ; } else { hi = mid - 1 ; } } count = Math . max ( j + i , count ) ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 , K = 7 ; int A [ ] = { 2 , 4 , 7 , 3 } ; int B [ ] = { 1 , 9 , 3 , 4 , 5 } ; maxItems ( n , m , A , B , K ) ; } }
Numbers of pairs from an array whose average is also present in the array | Java program for the above approach ; Function to count the total count of pairs having sum S ; Store the total count of all elements in map mp ; Initialize value to 0 , if key not found ; Stores the total count of total pairs ; Iterate through each element and increment the count ; If the value ( S - arr [ i ] ) exists in the map hm ; Update the twice count ; Return the half of twice_count ; Function to count of pairs having whose average exists in the array ; Stores the total count of pairs ; Use set to remove duplicates ; Insert all the element in the set S ; For every sum find the getCountPairs ; Return the total count of pairs ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int getCountPairs ( int arr [ ] , int N , int S ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! mp . containsKey ( arr [ i ] ) ) mp . put ( arr [ i ] , 0 ) ; mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } int twice_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . get ( S - arr [ i ] ) != null ) { twice_count += mp . get ( S - arr [ i ] ) ; } if ( S - arr [ i ] == arr [ i ] ) twice_count -- ; } return twice_count / 2 ; } public static int countPairs ( int arr [ ] , int N ) { int count = 0 ; HashSet < Integer > S = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) S . add ( arr [ i ] ) ; for ( int ele : S ) { int sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = { 4 , 2 , 5 , 1 , 3 , 5 } ; System . out . println ( countPairs ( arr , N ) ) ; } }
Number which is co | Java program for the above approach ; Function to check whether the given number N is prime or not ; Base Case ; If N has more than one factor , then return false ; Otherwise , return true ; Function to find X which is co - prime with the integers from the range [ L , R ] ; Store the resultant number ; Check for prime integers greater than R ; If the current number is prime , then update coPrime and break out of loop ; Print the resultant number ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean isPrime ( int N ) { if ( N == 1 ) return false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } static int findCoPrime ( int L , int R ) { int coPrime ; for ( int i = R + 1 ; ; i ++ ) { if ( isPrime ( i ) ) { coPrime = i ; break ; } } return coPrime ; } public static void main ( String [ ] args ) { int L = 16 , R = 17 ; System . out . println ( findCoPrime ( L , R ) ) ; } }
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | Java program for the above approach ; Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the required result ; Traverse over all the pairs ( i , j ) ; Calculate the value of the expression and update the overall maximum value ; Print the result ; Driver Code
class GFG { static void maximumValue ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { ans = Math . max ( ans , arr [ i ] + arr [ j ] + i - j ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . length ; maximumValue ( arr , N ) ; } }
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | Java program for the above approach ; Function to find the maximum value of ( arr [ i ] + arr [ j ] + i - j ) possible for a pair in the array ; Stores the maximum value of ( arr [ i ] + i ) ; Stores the required result ; Traverse the array arr [ ] ; Calculate for current pair and update maximum value ; Update maxValue if ( arr [ i ] + I ) is greater than maxValue ; Print the result ; Driver Code
class GFG { static void maximumValue ( int arr [ ] , int n ) { int maxvalue = arr [ 0 ] ; int result = 0 ; for ( int i = 1 ; i < n ; i ++ ) { result = Math . max ( result , maxvalue + arr [ i ] - i ) ; maxvalue = Math . max ( maxvalue , arr [ i ] + i ) ; } System . out . println ( result ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . length ; maximumValue ( arr , N ) ; } }
Minimum sum of absolute differences of pairs in a triplet from three arrays | Java program for the above approach ; Lower_bound function ; Function to find the value closest to K in the array A [ ] ; Initialize close value as the end element ; Find lower bound of the array ; If lower_bound is found ; If lower_bound is not the first array element ; If * ( it - 1 ) is closer to k ; Return closest value of k ; Function to find the minimum sum of abs ( arr [ i ] - brr [ j ] ) and abs ( brr [ j ] - crr [ k ] ) ; Sort the vectors arr and crr ; Initialize minimum as INT_MAX ; Traverse the array brr [ ] ; Stores the element closest to val from the array arr [ ] ; Stores the element closest to val from the array crr [ ] ; If sum of differences is minimum ; Update the minimum ; Print the minimum absolute difference possible ; Driver Code ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { public static int lower_bound ( int arr [ ] , int key ) { int low = 0 ; int high = arr . length - 1 ; while ( low < high ) { int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] >= key ) { high = mid ; } else { low = mid + 1 ; } } return low ; } static int closestValue ( int A [ ] , int k ) { int close = A [ A . length - 1 ] ; int it = lower_bound ( A , k ) ; if ( it != A . length ) { close = A [ it ] ; if ( it != 0 ) { if ( ( k - A [ it - 1 ] ) < ( close - k ) ) { close = A [ it - 1 ] ; } } } return close ; } static void minPossible ( int arr [ ] , int brr [ ] , int crr [ ] ) { Arrays . sort ( arr ) ; Arrays . sort ( crr ) ; int minimum = Integer . MAX_VALUE ; for ( int val : brr ) { int arr_close = closestValue ( arr , val ) ; int crr_close = closestValue ( crr , val ) ; if ( Math . abs ( val - arr_close ) + Math . abs ( val - crr_close ) < minimum ) minimum = Math . abs ( val - arr_close ) + Math . abs ( val - crr_close ) ; } System . out . println ( minimum ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 8 , 5 } ; int b [ ] = { 2 , 9 } ; int c [ ] = { 5 , 4 } ; minPossible ( a , b , c ) ; } }
Maximum time required for all patients to get infected | Java program for the above approach ; Direction arrays ; Function to find the maximum time required for all patients to get infected ; Stores the number of rows ; Stores the number of columns ; Stores whether particular index ( i , j ) is visited or not ; Stores index and time of infection of infected persons ; Stores uninfected patients count ; Stores time at which last person got infected ; Traverse the matrix ; If the current patient is already infected ; Push the index of current patient and mark it as visited ; If current patient is uninfected increment uninfected count ; Iterate until queue becomes empty ; Stores the front element of queue ; Pop out the front element ; Check for all four adjacent indices ; Find the index of the adjacent cell ; If the current adjacent cell is invalid or it contains an infected patient ; Continue to the next neighbouring cell ; Push the infected neighbour into queue ; If an uninfected patient is present ; Return the final result ; Driver Code
import java . util . * ; public class GFG { static class pair { int first , second , third ; pair ( int first , int second , int third ) { this . first = first ; this . second = second ; this . third = third ; } } static int direction [ ] [ ] = { { 1 , 0 } , { 0 , - 1 } , { - 1 , 0 } , { 0 , 1 } } ; static int maximumTime ( int arr [ ] [ ] ) { int n = arr . length ; int m = arr [ 0 ] . length ; boolean visited [ ] [ ] = new boolean [ n ] [ m ] ; Queue < pair > q = new LinkedList < > ( ) ; int uninfected_count = 0 ; int time = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ i ] [ j ] == 2 ) { q . add ( new pair ( i , j , 0 ) ) ; visited [ i ] [ j ] = true ; } if ( arr [ i ] [ j ] == 1 ) { uninfected_count ++ ; } } } while ( ! q . isEmpty ( ) ) { pair current = q . peek ( ) ; time = current . third ; q . poll ( ) ; for ( int [ ] it : direction ) { int i = current . first + it [ 0 ] ; int j = current . second + it [ 1 ] ; if ( i < 0 j < 0 i >= n j >= m arr [ i ] [ j ] != 1 visited [ i ] [ j ] ) { continue ; } q . add ( new pair ( i , j , time + 1 ) ) ; visited [ i ] [ j ] = true ; uninfected_count -- ; } } if ( uninfected_count != 0 ) return - 1 ; return time ; } public static void main ( String args [ ] ) { int arr [ ] [ ] = { { 2 , 1 , 0 , 2 , 1 } , { 1 , 0 , 1 , 2 , 1 } , { 1 , 0 , 0 , 2 , 1 } } ; System . out . println ( maximumTime ( arr ) ) ; } }
Minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively | Java program for the above approach ; Function to find the minimum number of swaps required to make the first and the last elements the largest and smallest element in the array ; Stores the count of swaps ; Stores the maximum element ; Stores the minimum element ; If the array contains a single distinct element ; Stores the indices of the maximum and minimum elements ; If the first index of the maximum element is found ; If current index has the minimum element ; Update the count of operations to place largest element at the first ; Update the count of operations to place largest element at the last ; If smallest element is present before the largest element initially ; Driver Code
import java . util . Arrays ; import java . util . Collections ; class GFG { static int minimum_swaps ( Integer arr [ ] , int n ) { int count = 0 ; int max_el = Collections . max ( Arrays . asList ( arr ) ) ; int min_el = Collections . min ( Arrays . asList ( arr ) ) ; if ( min_el == max_el ) return 0 ; int index_max = - 1 ; int index_min = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == max_el && index_max == - 1 ) { index_max = i ; } if ( arr [ i ] == min_el ) { index_min = i ; } } count += index_max ; count += ( n - 1 - index_min ) ; if ( index_min < index_max ) count -= 1 ; return count ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 2 , 4 , 1 , 6 , 5 } ; int N = arr . length ; System . out . println ( minimum_swaps ( arr , N ) ) ; } }
Maximum difference between node and its ancestor in a Directed Acyclic Graph ( DAG ) | Java program for the above approach ; Function to perform DFS Traversal on the given graph ; Update the value of ans ; Update the currentMin and currentMax ; Traverse the adjacency list of the node src ; Recursively call for the child node ; Function to calculate maximum absolute difference between a node and its ancestor ; Stores the adjacency list of graph ; Create Adjacency list ; Add a directed edge ; Perform DFS Traversal ; Print the maximum absolute difference ; Driver code
import java . util . * ; class GFG { static int ans ; static void DFS ( int src , ArrayList < ArrayList < Integer > > Adj , int arr [ ] , int currentMin , int currentMax ) { ans = Math . max ( ans , Math . max ( Math . abs ( currentMax - arr [ src - 1 ] ) , Math . abs ( currentMin - arr [ src - 1 ] ) ) ) ; currentMin = Math . min ( currentMin , arr [ src - 1 ] ) ; currentMax = Math . min ( currentMax , arr [ src - 1 ] ) ; for ( Integer child : Adj . get ( src ) ) { DFS ( child , Adj , arr , currentMin , currentMax ) ; } } static void getMaximumDifference ( int Edges [ ] [ ] , int arr [ ] , int N , int M ) { ans = 0 ; ArrayList < ArrayList < Integer > > Adj = new ArrayList < > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) Adj . add ( new ArrayList < > ( ) ) ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; Adj . get ( u ) . add ( v ) ; } DFS ( 1 , Adj , arr , arr [ 0 ] , arr [ 0 ] ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 , M = 4 ; int Edges [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 4 , 5 } , { 1 , 3 } } ; int arr [ ] = { 13 , 8 , 3 , 15 , 18 } ; getMaximumDifference ( Edges , arr , N , M ) ; } }
Check if a given string is a comment or not | Java program for the above approach ; Function to check if the given string is a comment or not ; If two continuous slashes preceeds the comment ; Driver Code ; Given string ; Function call to check whether then given string is a comment or not
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void isComment ( String S ) { char line [ ] = S . toCharArray ( ) ; if ( line [ 0 ] == ' / ' && line [ 1 ] == ' / ' ) { System . out . println ( " It ▁ is ▁ a ▁ single - line ▁ comment " ) ; return ; } if ( line [ 0 ] == ' / ' && line [ 1 ] == ' * ' && line [ line . length - 2 ] == ' * ' && line [ line . length - 1 ] == ' / ' ) { System . out . println ( " It ▁ is ▁ a ▁ multi - line ▁ comment " ) ; return ; } System . out . println ( " It ▁ is ▁ not ▁ a ▁ comment " ) ; } public static void main ( String [ ] args ) { String line = " GeeksForGeeks ▁ GeeksForGeeks " ; isComment ( line ) ; } }
Print digits for each array element that does not divide any digit of that element | Java program for the above approach ; Function to find digits for each array element that doesn 't divide any digit  of the that element ; Traverse the array arr [ ] ; Iterate over the range [ 2 , 9 ] ; Stores if there exists any digit in arr [ i ] which is divisible by j ; If any digit of the number is divisible by j ; If the digit j doesn 't  divide any digit of arr[i] ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void indivisibleDigits ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " : ▁ " ) ; for ( int j = 2 ; j < 10 ; j ++ ) { int temp = arr [ i ] ; boolean flag = true ; while ( temp > 0 ) { if ( ( temp % 10 ) != 0 && ( temp % 10 ) % j == 0 ) { flag = false ; break ; } temp /= 10 ; } if ( flag ) { System . out . print ( j + " ▁ " ) ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 4162 , 1152 , 99842 } ; int N = arr . length ; indivisibleDigits ( arr , N ) ; } }
Length of longest non | Java program for the above approach ; Function to find the longest non - decreasing subsequence with difference between adjacent elements exactly equal to 1 ; Base case ; Sort the array in ascending order ; Stores the maximum length ; Traverse the array ; If difference between current pair of adjacent elements is 1 or 0 ; Extend the current sequence Update len and max_len ; Otherwise , start a new subsequence ; Print the maximum length ; Driver Code ; Given array ; Size of the array ; Function call to find the longest subsequence
import java . io . * ; import java . util . * ; class GFG { static void longestSequence ( int arr [ ] , int N ) { if ( N == 0 ) { System . out . println ( 0 ) ; return ; } Arrays . sort ( arr ) ; int maxLen = 1 ; int len = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i - 1 ] + 1 ) { len ++ ; maxLen = Math . max ( maxLen , len ) ; } else { len = 1 ; } } System . out . println ( maxLen ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 5 , 4 , 8 , 4 } ; int N = arr . length ; longestSequence ( arr , N ) ; } }
Queries to find the minimum array sum possible by removing elements from either end | Java program for the above approach ; Function to find the minimum sum for each query after removing elements from either ends ; Traverse the query array ; Traverse the array from the front ; If element equals val , then break out of loop ; Traverse the array from rear ; If element equals val , break ; Print the minimum of the two as the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static void minSum ( int arr [ ] , int N , int Q [ ] , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int val = Q [ i ] ; int front = 0 , rear = 0 ; for ( int j = 0 ; j < N ; j ++ ) { front += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } for ( int j = N - 1 ; j >= 0 ; j -- ) { rear += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } System . out . print ( Math . min ( front , rear ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 6 , 7 , 4 , 5 , 1 } ; int N = arr . length ; int Q [ ] = { 7 , 6 } ; int M = Q . length ; minSum ( arr , N , Q , M ) ; } }
Frequency of lexicographically Kth smallest character in the a string | Java program for the above approach ; Function to find the frequency of the lexicographically Kth smallest character ; Convert the string to array of characters ; Sort the array in ascending order ; Store the Kth character ; Store the frequency of the K - th character ; Count the frequency of the K - th character ; Print the count ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static void KthCharacter ( String S , int N , int K ) { char strarray [ ] = S . toCharArray ( ) ; Arrays . sort ( strarray ) ; char ch = strarray [ K - 1 ] ; int count = 0 ; for ( char c : strarray ) { if ( c == ch ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = S . length ( ) ; int K = 3 ; KthCharacter ( S , N , K ) ; } }
Longest substring with no pair of adjacent characters are adjacent English alphabets | Java program for the above approach ; Function to find the longest substring satisfying the given condition ; Stores all temporary substrings ; Stores the longest substring ; Stores the length of the substring T ; Stores the first character of string S ; Traverse the string ; If the absolute difference is 1 ; Update the length of substring T ; Update the longest substring ; Otherwise , stores the current character ; Again checking for longest substring and update accordingly ; Print the longest substring ; Driver Code ; Given string ; Function call to find the longest substring satisfying given condition
import java . io . * ; class GFG { static void findSubstring ( String S ) { String T = " " ; String ans = " " ; int l = 0 ; T += S . charAt ( 0 ) ; for ( int i = 1 ; i < S . length ( ) ; i ++ ) { if ( Math . abs ( S . charAt ( i ) - S . charAt ( i - 1 ) ) == 1 ) { l = T . length ( ) ; if ( l > ans . length ( ) ) { ans = T ; } T = " " ; T += S . charAt ( i ) ; } else { T += S . charAt ( i ) ; } } l = T . length ( ) ; if ( l > ans . length ( ) ) { ans = T ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " aabdml " ; findSubstring ( S ) ; } }