codeparrot/xlcost-text-to-code huggingface dataset:
Count of distinct permutation of a String obtained by swapping only unequal characters | Importing HashMap class ; Function to calculate total number of valid permutations ; Creating count which is equal to the Total number of characters present and ans that will store the number of unique permutations ; Storing frequency of each character present in the string ; Adding count of characters by excluding characters equal to current char ; Reduce the frequency of the current character and count by 1 , so that it cannot interfere with the calculations of the same elements present to the right of it . ; Return ans + 1 ( Because the given string is also a unique permutation )
import java . util . HashMap ; class GFG { static int validPermutations ( String str ) { HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; int count = str . length ( ) , ans = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { m . put ( str . charAt ( i ) , m . getOrDefault ( str . charAt ( i ) , 0 ) + 1 ) ; } for ( int i = 0 ; i < str . length ( ) ; i ++ ) { ans += count - m . get ( str . charAt ( i ) ) ; m . put ( str . charAt ( i ) , m . get ( str . charAt ( i ) ) - 1 ) ; count -- ; } return ans + 1 ; } public static void main ( String [ ] args ) { String str = " sstt " ; System . out . println ( validPermutations ( str ) ) ; } }
Counts paths from a point to reach Origin | Java program to count total number of paths from a point to origin ; DP based function to count number of paths ; Fill entries in bottommost row and leftmost columns ; Fill DP in bottom up manner ; Driver Code
import java . io . * ; class GFG { static int countPaths ( int n , int m ) { int dp [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= m ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ; return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int n = 3 , m = 2 ; System . out . println ( " ▁ Number ▁ of ▁ Paths ▁ " + countPaths ( n , m ) ) ; } }
Coin Change | DP | Recursive JAVA program for coin change problem . ; Returns the count of ways we can sum S [ 0. . . m - 1 ] coins to get sum n ; If n is 0 then there is 1 solution ( do not include any coin ) ; If n is less than 0 then no solution exists ; If there are no coins and n is greater than 0 , then no solution exist ; count is sum of solutions ( i ) including S [ m - 1 ] ( ii ) excluding S [ m - 1 ] ; Driver code
import java . util . * ; class GFG { static int count ( int S [ ] , int m , int n ) { if ( n == 0 ) return 1 ; if ( n < 0 ) return 0 ; if ( m <= 0 && n >= 1 ) return 0 ; return count ( S , m - 1 , n ) + count ( S , m , n - S [ m - 1 ] ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 } ; int m = arr . length ; System . out . println ( count ( arr , m , 4 ) ) ; } }
Coin Change | DP | Java program for the above approach ; Either Pick this coin or not ; else We have no option but to leave this coin ; Driver code
import java . util . * ; class GFG { static int coinchange ( int [ ] a , int v , int n , int [ ] [ ] dp ) { if ( v == 0 ) return dp [ n ] [ v ] = 1 ; if ( n == 0 ) return 0 ; if ( dp [ n ] [ v ] != - 1 ) return dp [ n ] [ v ] ; if ( a [ n - 1 ] <= v ) { return dp [ n ] [ v ] = coinchange ( a , v - a [ n - 1 ] , n , dp ) + coinchange ( a , v , n - 1 , dp ) ; } return dp [ n ] [ v ] = coinchange ( a , v , n - 1 , dp ) ; } public static void main ( String [ ] args ) { int tc = 1 ; while ( tc != 0 ) { int n , v ; n = 3 ; v = 4 ; int [ ] a = { 1 , 2 , 3 } ; int [ ] [ ] dp = new int [ n + 1 ] [ v + 1 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , - 1 ) ; int res = coinchange ( a , v , n , dp ) ; System . out . println ( res ) ; tc -- ; } } }
Check if two strings are same ignoring their cases | Function to compare two Strings ignoring their cases ; Convert to lowercase using toUpperCase function ; Comparing both using inbuilt function ; if Strings are equal , return true otherwise false ; Function to print the same or not same if Strings are equal or not equal ; Driver Code
class GFG { static boolean equalIgnoreCase ( String str1 , String str2 ) { int i = 0 ; str1 = str1 . toUpperCase ( ) ; str2 = str2 . toUpperCase ( ) ; int x = str1 . compareTo ( str2 ) ; if ( x != 0 ) { return false ; } else { return true ; } } static void equalIgnoreCaseUtil ( String str1 , String str2 ) { boolean res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) { System . out . println ( " Same " ) ; } else { System . out . println ( " Not ▁ Same " ) ; } } public static void main ( String [ ] args ) { String str1 , str2 ; str1 = " Geeks " ; str2 = " geeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " Geek " ; str2 = " geeksforgeeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; } }
Replace every consonant sequence with its length in the given string | Java implementation of the approach ; Function to return the converted string after replacing every consonant sequence with its length ; To store the resultant string ; Checking each character for consonant sequence ; Count the length of consonants sequence ; Add the length in the string ; Add the vowel ; Check for the last consonant sequence in the string ; Return the resultant string ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static String replaceConsonants ( String str ) { String res = " " ; int i = 0 , count = 0 ; while ( i < str . length ( ) ) { if ( str . charAt ( i ) != ' a ' && str . charAt ( i ) != ' e ' && str . charAt ( i ) != ' i ' && str . charAt ( i ) != ' o ' && str . charAt ( i ) != ' u ' ) { i ++ ; count ++ ; } else { if ( count > 0 ) res += count ; res += str . charAt ( i ) ; i ++ ; count = 0 ; } } if ( count > 0 ) res += count ; return res ; } public static void main ( String [ ] args ) { String str = " abcdeiop " ; System . out . println ( replaceConsonants ( str ) ) ; } }
Encrypt string with product of number of vowels and consonants in substring of size k | Java Program to Encrypt String with product of number of vowels and consonants in every substring of size k ; isVowel ( ) is a function that returns true for a vowel and false otherwise . ; function to Encrypt the dtring ; for each substring ; substring of size k ; counting number of vowels and consonants ; append product to answer . ; Driven Program
class GFG { static boolean isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static String encryptString ( String s , int n , int k ) { int countVowels = 0 ; int countConsonants = 0 ; String ans = " " ; for ( int l = 0 ; l <= n - k ; l ++ ) { countVowels = 0 ; countConsonants = 0 ; for ( int r = l ; r <= l + k - 1 ; r ++ ) { if ( isVowel ( s . charAt ( r ) ) == true ) { countVowels ++ ; } else { countConsonants ++ ; } } ans += String . valueOf ( countVowels * countConsonants ) ; } return ans ; } static public void main ( String [ ] args ) { String s = " hello " ; int n = s . length ( ) ; int k = 2 ; System . out . println ( encryptString ( s , n , k ) ) ; } }
String containing first letter of every word in a given string with spaces | Java implementation of the above approach ; we are splitting the input based on spaces ( s ) + : this regular expression will handle scenarios where we have words separated by multiple spaces ; charAt ( 0 ) will pick only the first character from the string and append to buffer ; main function
class GFG { private static StringBuilder charBuffer = new StringBuilder ( ) ; public static String processWords ( String input ) { String s [ ] = input . split ( " ( \\ s ) + " ) ; for ( String values : s ) { charBuffer . append ( values . charAt ( 0 ) ) ; } return charBuffer . toString ( ) ; } public static void main ( String [ ] args ) { String input = " geeks ▁ for TABSYMBOL geeks ▁ geeks TABSYMBOL for ▁ geeks " ; System . out . println ( processWords ( input ) ) ; } }
Generate all binary strings without consecutive 1 's | Java program to Generate all binary string without consecutive 1 's of size K ; Array conversion to String -- ; Base Condition when we reached at the end of Array * * ; Printing the Generated String * * Return to the previous case * ; If the first Character is Zero then adding * * ; If the Character is One then add Zero to next * * ; Calling Recursively for the next value of Array ; Initializing first character to Zero ; Generating Strings starting with Zero -- ; Initialized first Character to one -- ; Calling function fun with argument k ; This code is Contributed by Praveen Tiwari
import java . util . * ; import java . lang . * ; public class BinaryS { public static String toString ( char [ ] a ) { String string = new String ( a ) ; return string ; } static void generate ( int k , char [ ] ch , int n ) { if ( n == k ) { System . out . print ( toString ( ch ) + " ▁ " ) ; return ; } if ( ch [ n - 1 ] == '0' ) { ch [ n ] = '0' ; generate ( k , ch , n + 1 ) ; ch [ n ] = '1' ; generate ( k , ch , n + 1 ) ; } if ( ch [ n - 1 ] == '1' ) { ch [ n ] = '0' ; generate ( k , ch , n + 1 ) ; } } static void fun ( int k ) { if ( k <= 0 ) { return ; } char [ ] ch = new char [ k ] ; ch [ 0 ] = '0' ; generate ( k , ch , 1 ) ; ch [ 0 ] = '1' ; generate ( k , ch , 1 ) ; } public static void main ( String args [ ] ) { int k = 3 ; fun ( k ) ; } }
Largest right circular cylinder within a cube | Java Program to find the biggest right circular cylinder that can be fit within a cube ; Function to find the biggest right circular cylinder ; side cannot be negative ; radius of right circular cylinder ; height of right circular cylinder ; volume of right circular cylinder ; Driver code
import java . io . * ; class GFG { static float findVolume ( float a ) { if ( a < 0 ) return - 1 ; float r = a / 2 ; float h = a ; float V = ( float ) ( 3.14 * Math . pow ( r , 2 ) * h ) ; return V ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . print ( findVolume ( a ) ) ; } }
Program for volume of Pyramid | Java Program for volume of Pyramid . ; Function to find the volume of triangular pyramid ; Function to find the volume of square pyramid ; Function to find the volume of pentagonal pyramid ; Function to find the volume of hexagonal pyramid ; Driver Code
import java . util . * ; import java . lang . * ; class GfG { public static float volumeTriangular ( int a , int b , int h ) { float vol = ( float ) ( 0.1666 ) * a * b * h ; return vol ; } public static float volumeSquare ( int b , int h ) { float vol = ( float ) ( 0.33 ) * b * b * h ; return vol ; } public static float volumePentagonal ( int a , int b , int h ) { float vol = ( float ) ( 0.83 ) * a * b * h ; return vol ; } public static float volumeHexagonal ( int a , int b , int h ) { float vol = ( float ) a * b * h ; return vol ; } public static void main ( String argc [ ] ) { int b = 4 , h = 9 , a = 4 ; System . out . println ( " Volume ▁ of ▁ triangular " + " ▁ base ▁ pyramid ▁ is ▁ " + volumeTriangular ( a , b , h ) ) ; System . out . println ( " Volume ▁ of ▁ square ▁ base " + " ▁ pyramid ▁ is ▁ " + volumeSquare ( b , h ) ) ; System . out . println ( " Volume ▁ of ▁ pentagonal " + " ▁ base ▁ pyramid ▁ is ▁ " + volumePentagonal ( a , b , h ) ) ; System . out . println ( " Volume ▁ of ▁ Hexagonal " + " ▁ base ▁ pyramid ▁ is ▁ " + volumeHexagonal ( a , b , h ) ) ; } }
Program to find area of a Trapezoid | Java program to calculate area of a trapezoid ; Function for the area ; Driver Code
import java . io . * ; class GFG { static double Area ( int b1 , int b2 , int h ) { return ( ( b1 + b2 ) / 2 ) * h ; } public static void main ( String [ ] args ) { int base1 = 8 , base2 = 10 , height = 6 ; double area = Area ( base1 , base2 , height ) ; System . out . println ( " Area ▁ is : ▁ " + area ) ; } }
Find number of diagonals in n sided convex polygon | Java function to find number of diagonals in n sided convex polygon ; driver code to test above function
public class Diagonals { static int numberOfDiagonals ( int n ) { return n * ( n - 3 ) / 2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( n + " ▁ sided ▁ convex ▁ polygon ▁ have ▁ " ) ; System . out . println ( numberOfDiagonals ( n ) + " ▁ diagonals " ) ; } }
Area of the largest Rectangle without a given point | Java implementation to find area of largest Rectangle without hole within a given Rectangle ; Function to find the maximum area such that it does not contains any hole ; Area for all the possible positions of the cut ; Find the maximum area among the above rectangles ; Driver Code ; Function call
import java . util . * ; class GFG { static void maximumArea ( int l , int b , int x , int y ) { int left , right , above , below ; left = x * b ; right = ( l - x - 1 ) * b ; above = l * y ; below = ( b - y - 1 ) * l ; System . out . print ( Math . max ( Math . max ( left , right ) , Math . max ( above , below ) ) ) ; } public static void main ( String [ ] args ) { int L = 8 , B = 8 ; int X = 0 , Y = 0 ; maximumArea ( L , B , X , Y ) ; } }
Minimize cost of removals required to make all remaining characters of the string unique | Java program for the above approach ; Function to find the minimum cost of removing characters to make the string unique ; Store the minimum cost required ; Create a dictionary to store the maximum cost of removal a character ; Create a dictionary to store the total deletion cost of a character ; Traverse the string , S ; Keep track of maximum cost of each character ; Update the maximum deletion cost ; Keep track of the total cost of each character ; Update the total deletion cost ; Traverse through all the unique characters ; Keep the maximum cost character and delete the rest ; Return the answer ; Driver code ; Given string ; Given cost array ; Function Call
import java . util . * ; public class GFG { static int delCost ( String s , int [ ] cost ) { int ans = 0 ; HashMap < Character , Integer > forMax = new HashMap < > ( ) ; HashMap < Character , Integer > forTot = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! forMax . containsKey ( s . charAt ( i ) ) ) { forMax . put ( s . charAt ( i ) , cost [ i ] ) ; } else { forMax . put ( s . charAt ( i ) , Math . max ( cost [ i ] , forMax . get ( s . charAt ( i ) ) ) ) ; } if ( ! forTot . containsKey ( s . charAt ( i ) ) ) { forTot . put ( s . charAt ( i ) , cost [ i ] ) ; } else { forTot . put ( s . charAt ( i ) , forTot . get ( s . charAt ( i ) ) + cost [ i ] ) ; } } for ( Map . Entry < Character , Integer > i : forMax . entrySet ( ) ) { ans += forTot . get ( i . getKey ( ) ) - i . getValue ( ) ; } return ans ; } public static void main ( String [ ] args ) { String s = " AAABBB " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 , 6 } ; System . out . println ( delCost ( s , cost ) ) ; } }
Smallest divisor of N closest to X | Java program for the above approach ; Define macros ; Stores divisors for all numbers in the vector divisors ; i is the divisor and j is the multiple ; Function to compare the closeness of the given target ; Function to find the element closest to target in divisors vector ; Corner cases ; Perform binary search ; Check if target is less than the array element then search in left half ; Check if target is greater than previous to mid , return closest of two ; Repeat for left half ; Check if target is greater than mid ; Update i ; Only single element left after search ; Function to print the divisor of N closest to X ; Function call to calculate and stores divisors of all numbers in a vector ; Stores the closest value to target ; Print the answer ; Driver Code ; Given N & X ; Function Call
import java . util . * ; class GFG { static final int MAX = 10000 ; static Vector < Integer > [ ] divisors = new Vector [ MAX + 1 ] ; static void computeDivisors ( ) { for ( int i = 1 ; i <= MAX ; i ++ ) { for ( int j = i ; j <= MAX ; j += i ) { divisors [ j ] . add ( i ) ; } } } static int getClosest ( int val1 , int val2 , int target ) { if ( target - val1 >= val2 - target ) return val2 ; else return val1 ; } static int findClosest ( Vector < Integer > array , int n , int target ) { Integer [ ] arr = array . toArray ( new Integer [ array . size ( ) ] ) ; if ( target <= arr [ 0 ] ) return arr [ 0 ] ; if ( target >= arr [ n - 1 ] ) return arr [ n - 1 ] ; int i = 0 , j = n , mid = 0 ; while ( i < j ) { mid = ( i + j ) / 2 ; if ( arr [ mid ] == target ) return arr [ mid ] ; if ( target < arr [ mid ] ) { if ( mid > 0 && target > arr [ mid - 1 ] ) return getClosest ( arr [ mid - 1 ] , arr [ mid ] , target ) ; j = mid ; } else { if ( mid < n - 1 && target < arr [ mid + 1 ] ) return getClosest ( arr [ mid ] , arr [ mid + 1 ] , target ) ; i = mid + 1 ; } } return arr [ mid ] ; } static void printClosest ( int N , int X ) { computeDivisors ( ) ; int ans = findClosest ( divisors [ N ] , divisors [ N ] . size ( ) , X ) ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 16 , X = 5 ; for ( int i = 0 ; i < divisors . length ; i ++ ) divisors [ i ] = new Vector < Integer > ( ) ; printClosest ( N , X ) ; } }
Count elements of same value placed at same indices of two given arrays | Java program for the above approach ; Function to count maximum matched elements from the arrays A [ ] and B [ ] ; Stores position of elements of array A [ ] in the array B [ ] ; Keep track of difference between the indices ; Traverse the array A [ ] ; Traverse the array B [ ] ; If difference is negative , add N to it ; Keep track of the number of shifts required to place elements at same indices ; Return the max matches ; Driver Code ; Returns the count of matched elements
import java . io . Console ; import java . util . HashMap ; import java . util . Map ; class GFG { static int maxMatch ( int [ ] A , int [ ] B ) { HashMap < Integer , Integer > Aindex = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > diff = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < A . length ; i ++ ) { Aindex . put ( A [ i ] , i ) ; } for ( int i = 0 ; i < B . length ; i ++ ) { if ( i - Aindex . get ( B [ i ] ) < 0 ) { if ( ! diff . containsKey ( A . length + i - Aindex . get ( B [ i ] ) ) ) { diff . put ( A . length + i - Aindex . get ( B [ i ] ) , 1 ) ; } else { diff . put ( A . length + i - Aindex . get ( B [ i ] ) , diff . get ( A . length + i - Aindex . get ( B [ i ] ) ) + 1 ) ; } } else { if ( ! diff . containsKey ( i - Aindex . get ( B [ i ] ) ) ) { diff . put ( i - Aindex . get ( B [ i ] ) , 1 ) ; } else { diff . put ( i - Aindex . get ( B [ i ] ) , diff . get ( i - Aindex . get ( B [ i ] ) ) + 1 ) ; } } } int max = 0 ; for ( Map . Entry < Integer , Integer > ele : diff . entrySet ( ) ) { if ( ele . getValue ( ) > max ) { max = ele . getValue ( ) ; } } return max ; } public static void main ( String [ ] args ) { int [ ] A = { 5 , 3 , 7 , 9 , 8 } ; int [ ] B = { 8 , 7 , 3 , 5 , 9 } ; System . out . println ( maxMatch ( A , B ) ) ; } }
Check if given Sudoku solution is valid or not | Java program to implement the above approach ; Function to check if all elements of the board [ ] [ ] array store value in the range [ 1 , 9 ] ; Traverse board [ ] [ ] array ; Check if board [ i ] [ j ] lies in the range ; Function to check if the solution of sudoku puzzle is valid or not ; Check if all elements of board [ ] [ ] stores value in the range [ 1 , 9 ] ; Stores unique value from 1 to N ; Traverse each row of the given array ; Initialize unique [ ] array to false ; Traverse each column of current row ; Stores the value of board [ i ] [ j ] ; Check if current row stores duplicate value ; Traverse each column of the given array ; Initialize unique [ ] array to false ; Traverse each row of current column ; Stores the value of board [ j ] [ i ] ; Check if current column stores duplicate value ; Traverse each block of size 3 * 3 in board [ ] [ ] array ; j stores first column of each 3 * 3 block ; Initialize unique [ ] array to false ; Traverse current block ; Stores row number of current block ; Stores column number of current block ; Stores the value of board [ X ] [ Y ] ; Check if current block stores duplicate value ; If all conditions satisfied ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int N = 9 ; static boolean isinRange ( int [ ] [ ] board ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( board [ i ] [ j ] <= 0 board [ i ] [ j ] > 9 ) { return false ; } } } return true ; } static boolean isValidSudoku ( int board [ ] [ ] ) { if ( isinRange ( board ) == false ) { return false ; } boolean [ ] unique = new boolean [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { Arrays . fill ( unique , false ) ; for ( int j = 0 ; j < N ; j ++ ) { int Z = board [ i ] [ j ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } for ( int i = 0 ; i < N ; i ++ ) { Arrays . fill ( unique , false ) ; for ( int j = 0 ; j < N ; j ++ ) { int Z = board [ j ] [ i ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } for ( int i = 0 ; i < N - 2 ; i += 3 ) { for ( int j = 0 ; j < N - 2 ; j += 3 ) { Arrays . fill ( unique , false ) ; for ( int k = 0 ; k < 3 ; k ++ ) { for ( int l = 0 ; l < 3 ; l ++ ) { int X = i + k ; int Y = j + l ; int Z = board [ X ] [ Y ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } } } return true ; } public static void main ( String [ ] args ) { int [ ] [ ] board = { { 7 , 9 , 2 , 1 , 5 , 4 , 3 , 8 , 6 } , { 6 , 4 , 3 , 8 , 2 , 7 , 1 , 5 , 9 } , { 8 , 5 , 1 , 3 , 9 , 6 , 7 , 2 , 4 } , { 2 , 6 , 5 , 9 , 7 , 3 , 8 , 4 , 1 } , { 4 , 8 , 9 , 5 , 6 , 1 , 2 , 7 , 3 } , { 3 , 1 , 7 , 4 , 8 , 2 , 9 , 6 , 5 } , { 1 , 3 , 6 , 7 , 4 , 8 , 5 , 9 , 2 } , { 9 , 7 , 4 , 2 , 1 , 5 , 6 , 3 , 8 } , { 5 , 2 , 8 , 6 , 3 , 9 , 4 , 1 , 7 } } ; if ( isValidSudoku ( board ) ) { System . out . println ( " Valid " ) ; } else { System . out . println ( " Not ▁ Valid " ) ; } } }
Subarray of length K whose concatenation forms a palindrome | Java program for the above approach ; Function to check if a number is Palindrome or not here i is the starting index and j is the last index of the subarray ; If the integer at i is not equal to j then the subarray is not palindrome ; Otherwise ; all a [ i ] is equal to a [ j ] then the subarray is palindrome ; Function to find a subarray whose concatenation forms a palindrome and return its starting index ; Iterating over subarray of length k and checking if that subarray is palindrome ; If no subarray is palindrome ; Driver code
import java . io . * ; class GFG { public static boolean palindrome ( int [ ] a , int i , int j ) { while ( i < j ) { if ( a [ i ] != a [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int findSubArray ( int [ ] arr , int k ) { int n = arr . length ; for ( int i = 0 ; i <= n - k ; i ++ ) { if ( palindrome ( arr , i , i + k - 1 ) ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 1 , 3 } ; int k = 4 ; int ans = findSubArray ( arr , k ) ; if ( ans == - 1 ) System . out . print ( - 1 + "NEW_LINE"); else { for ( int i = ans ; i < ans + k ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } } }