codeparrot/xlcost-text-to-code huggingface dataset:
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices | Java program to Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices ; function to find total number of ways ; No of odd indices in n - digit number ; No of even indices in n - digit number ; No of ways of arranging prime number digits in odd indices ; No of ways of arranging odd number digits in even indices ; returning the total number of ways ; Driver Code
import java . util . * ; class GFG { static int find_Numb_ways ( int n ) { int odd_indices = n / 2 ; int even_indices = ( n / 2 ) + ( n % 2 ) ; int arr_odd = ( int ) Math . pow ( 4 , odd_indices ) ; int arr_even = ( int ) Math . pow ( 5 , even_indices ) ; return arr_odd * arr_even ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( find_Numb_ways ( n ) ) ; } }
Check if a given array is sorted in Spiral manner or not | Java program to implement the above approach ; Function to check if the array is spirally sorted or not ; Stores start index of the array ; Stores end index of an array ; If arr [ start ] greater than arr [ end ] ; Update start ; If arr [ end ] greater than arr [ start ] ; Update end ; Driver code ; Function Call
import java . util . * ; class GFG { static boolean isSpiralSorted ( int [ ] arr , int n ) { int start = 0 ; int end = n - 1 ; while ( start < end ) { if ( arr [ start ] > arr [ end ] ) { return false ; } start ++ ; if ( arr [ end ] > arr [ start ] ) { return false ; } end -- ; } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 10 , 14 , 20 , 18 , 12 , 5 } ; int N = arr . length ; if ( isSpiralSorted ( arr , N ) != false ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
Print all strings from given array that can be typed using keys from a single row of a QWERTY keyboard | Java program to implement the above approach ; Function to print all strings that can be typed using keys of a single row in a QWERTY Keyboard ; Stores row number of all possible character of the strings ; Traverse the array ; If current string is not an empty string ; Sets true / false if a string can be typed using keys of a single row or not ; Stores row number of the first character of current string ; Stores length of word ; Traverse current string ; If current character can 't be  typed using keys of rowNum only ; Update flag ; If current string can be typed using keys from rowNum only ; Print the string ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void findWordsSameRow ( List < String > arr ) { Map < Character , Integer > mp = new HashMap < Character , Integer > ( ) ; mp . put ( ' q ' , 1 ) ; mp . put ( ' w ' , 1 ) ; mp . put ( ' e ' , 1 ) ; mp . put ( ' r ' , 1 ) ; mp . put ( ' t ' , 1 ) ; mp . put ( ' y ' , 1 ) ; mp . put ( ' u ' , 1 ) ; mp . put ( ' i ' , 1 ) ; mp . put ( ' o ' , 1 ) ; mp . put ( ' p ' , 1 ) ; mp . put ( ' a ' , 2 ) ; mp . put ( ' s ' , 2 ) ; mp . put ( ' d ' , 2 ) ; mp . put ( ' f ' , 2 ) ; mp . put ( ' g ' , 2 ) ; mp . put ( ' h ' , 2 ) ; mp . put ( ' j ' , 2 ) ; mp . put ( ' k ' , 2 ) ; mp . put ( ' l ' , 2 ) ; mp . put ( ' z ' , 3 ) ; mp . put ( ' x ' , 3 ) ; mp . put ( ' c ' , 3 ) ; mp . put ( ' v ' , 3 ) ; mp . put ( ' b ' , 3 ) ; mp . put ( ' n ' , 3 ) ; mp . put ( ' m ' , 3 ) ; for ( String word : arr ) { if ( word . length ( ) != 0 ) { boolean flag = true ; int rowNum = mp . get ( Character . toLowerCase ( word . charAt ( 0 ) ) ) ; int M = word . length ( ) ; for ( int i = 1 ; i < M ; i ++ ) { if ( mp . get ( Character . toLowerCase ( word . charAt ( i ) ) ) != rowNum ) { flag = false ; break ; } } if ( flag ) { System . out . print ( word + " ▁ " ) ; } } } } public static void main ( String [ ] args ) { List < String > words = Arrays . asList ( " Yeti " , " Had " , " GFG " , " comment " ) ; findWordsSameRow ( words ) ; } }
Count quadruples of given type from given array | Java program of the above approach ; Function to find the count of the subsequence of given type ; Stores the count of quadruples ; Generate all possible combinations of quadruples ; Check if 1 st element is equal to 3 rd element ; Check if 2 nd element is equal to 4 th element ; Driver code
import java . util . * ; class GFG { static int countSubsequece ( int a [ ] , int n ) { int i , j , k , l ; int answer = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { for ( k = j + 1 ; k < n ; k ++ ) { for ( l = k + 1 ; l < n ; l ++ ) { if ( a [ j ] == a [ l ] && a [ i ] == a [ k ] ) { answer ++ ; } } } } } return answer ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , 2 , 1 , 3 , 2 } ; System . out . print ( countSubsequece ( a , 7 ) ) ; } }
Smallest character in a string having minimum sum of distances between consecutive repetitions | Java program for the above approach ; Function to find the character repeats with minimum distance ; Stores the first and last index ; Initialize with - 1 ; Get the values of last and first occurence ; Update the first index ; Update the last index ; Initialize min ; Get the minimum ; Values must not be same ; Update the minimum distance ; return ans ; Driver Code ; Function Call
import java . util . * ; class GFG { static char minDistChar ( char [ ] s ) { int n = s . length ; int [ ] first = new int [ 26 ] ; int [ ] last = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( first [ s [ i ] - ' a ' ] == - 1 ) { first [ s [ i ] - ' a ' ] = i ; } last [ s [ i ] - ' a ' ] = i ; } int min = Integer . MAX_VALUE ; char ans = '1' ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( last [ i ] == first [ i ] ) continue ; if ( min > last [ i ] - first [ i ] ) { min = last [ i ] - first [ i ] ; ans = ( char ) ( i + ' a ' ) ; } } return ans ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( minDistChar ( str . toCharArray ( ) ) ) ; } }
Minimum steps required to reach the end of a matrix | Set 2 | Java implementation of the approach ; Function to return the minimum steps required to reach the end of the matrix ; Array to determine whether a cell has been visited before ; Queue for bfs ; Initializing queue ; To store the depth of search ; BFS algorithm ; Current queue size ; Top - most element of queue ; To store index of cell for simplicity ; Base case ; If we reach ( n - 1 , n - 1 ) ; Marking the cell visited ; Pushing the adjacent cells in the queue that can be visited from the current cell ; Driver code
import java . util . * ; class GFG { static int n = 3 ; static class Pair { int first , second ; Pair ( int a , int b ) { first = a ; second = b ; } } static int minSteps ( int arr [ ] [ ] ) { boolean v [ ] [ ] = new boolean [ n ] [ n ] ; Queue < Pair > q = new LinkedList < Pair > ( ) ; q . add ( new Pair ( 0 , 0 ) ) ; int depth = 0 ; while ( q . size ( ) != 0 ) { int x = q . size ( ) ; while ( x -- > 0 ) { Pair y = q . peek ( ) ; int i = y . first , j = y . second ; q . remove ( ) ; if ( v [ i ] [ j ] ) continue ; if ( i == n - 1 && j == n - 1 ) return depth ; v [ i ] [ j ] = true ; if ( i + arr [ i ] [ j ] < n ) q . add ( new Pair ( i + arr [ i ] [ j ] , j ) ) ; if ( j + arr [ i ] [ j ] < n ) q . add ( new Pair ( i , j + arr [ i ] [ j ] ) ) ; } depth ++ ; } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; System . out . println ( minSteps ( arr ) ) ; } }
Largest gap in an array | A Java program to find largest gap between two elements in an array . ; function to solve the given problem ; Driver Code
import java . io . * ; class GFG { static int solve ( int [ ] a , int n ) { int max1 = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( Math . abs ( a [ i ] - a [ j ] ) > max1 ) { max1 = Math . abs ( a [ i ] - a [ j ] ) ; } } } return max1 ; } static public void main ( String [ ] args ) { int [ ] arr = { - 1 , 2 , 3 , - 4 , - 10 , 22 } ; int size = arr . length ; System . out . println ( " Largest ▁ gap ▁ is ▁ : ▁ " + solve ( arr , size ) ) ; } }
Largest gap in an array | A Java program to find largest gap between two elements in an array . ; function to solve the given problem ; finding maximum and minimum of an array ; Driver code
import java . io . * ; class GFG { static int solve ( int a [ ] , int n ) { int min1 = a [ 0 ] ; int max1 = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max1 ) max1 = a [ i ] ; if ( a [ i ] < min1 ) min1 = a [ i ] ; } return Math . abs ( min1 - max1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { - 1 , 2 , 3 , 4 , - 10 } ; int size = arr . length ; System . out . println ( " Largest ▁ gap ▁ is ▁ : ▁ " + solve ( arr , size ) ) ; } }
Print reverse string after removing vowels | Java Program for removing characters from reversed string where vowels are present in original string ; Function for replacing the string ; initialize a string of length n ; Traverse through all characters of string ; assign the value to string r from last index of string s ; if s [ i ] is a consonant then print r [ i ] ; Driver function
class GFG { static void replaceOriginal ( String s , int n ) { char r [ ] = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { r [ i ] = s . charAt ( n - 1 - i ) ; if ( s . charAt ( i ) != ' a ' && s . charAt ( i ) != ' e ' && s . charAt ( i ) != ' i ' && s . charAt ( i ) != ' o ' && s . charAt ( i ) != ' u ' ) { System . out . print ( r [ i ] ) ; } } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . length ( ) ; replaceOriginal ( s , n ) ; } }
Check if a string can be made equal to another string by swapping or replacement of characters | Java program for the above approach ; Function to find if given Strings are same or not ; Base Condition ; Stores frequency of characters of the String str1 and str2 ; Traverse Strings str1 & str2 and store frequencies in a [ ] and b [ ] ; Check if both Strings have same characters or not ; If a character is present in one String and is not in another String , return false ; Sort the array a [ ] and b [ ] ; Check arrays a and b contain the same frequency or not ; If the frequencies are not the same after sorting ; At this point , str1 can be converted to str2 ; Driver Code
import java . util . * ; class GFG { static boolean sameStrings ( String str1 , String str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; if ( N != M ) { return false ; } int [ ] a = new int [ 256 ] ; int [ ] b = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { a [ str1 . charAt ( i ) - ' a ' ] ++ ; b [ str2 . charAt ( i ) - ' a ' ] ++ ; } int i = 0 ; while ( i < 256 ) { if ( ( a [ i ] == 0 && b [ i ] == 0 ) || ( a [ i ] != 0 && b [ i ] != 0 ) ) { i ++ ; } else { return false ; } } Arrays . sort ( a ) ; Arrays . sort ( b ) ; for ( i = 0 ; i < 256 ; i ++ ) { if ( a [ i ] != b [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { String S1 = " cabbba " , S2 = " abbccc " ; if ( sameStrings ( S1 , S2 ) ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " ▁ NO " + "NEW_LINE"); } }
Reduce given three Numbers by decrementing in Pairs | Java program for the above approach ; Function to find the minimum number operations ; Insert the three numbers in array ; Sort the array ; Case 2 ; Case 1 ; Driver Code ; Given A , B , C ; Function call
import java . util . * ; class GFG { public static int solution ( int A , int B , int C ) { int arr [ ] = new int [ 3 ] ; arr [ 0 ] = A ; arr [ 1 ] = B ; arr [ 2 ] = C ; Arrays . sort ( arr ) ; if ( arr [ 2 ] < arr [ 0 ] + arr [ 1 ] ) return ( ( arr [ 0 ] + arr [ 1 ] + arr [ 2 ] ) / 2 ) ; else return ( arr [ 0 ] + arr [ 1 ] ) ; } public static void main ( String [ ] args ) { int A = 8 , B = 1 , C = 5 ; System . out . println ( solution ( A , B , C ) ) ; } }
Search an element in a sorted and rotated array with duplicates | Java implementation of the approach ; Function to return the index of the key in arr [ l . . h ] if the key is present otherwise return - 1 ; The tricky case , just update left and right ; If arr [ l ... mid ] is sorted ; As this subarray is sorted , we can quickly check if key lies in any of the halves ; If key does not lie in the first half subarray then divide the other half into two subarrays such that we can quickly check if key lies in the other half ; If arr [ l . . mid ] first subarray is not sorted then arr [ mid ... h ] must be sorted subarray ; Driver code
class GFG { static int search ( int arr [ ] , int l , int h , int key ) { if ( l > h ) return - 1 ; int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( ( arr [ l ] == arr [ mid ] ) && ( arr [ h ] == arr [ mid ] ) ) { l ++ ; h -- ; return search ( arr , l , h , key ) ; } else if ( arr [ l ] <= arr [ mid ] ) { if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ; else return search ( arr , mid + 1 , h , key ) ; } else if ( key >= arr [ mid ] && key <= arr [ h ] ) return search ( arr , mid + 1 , h , key ) ; return search ( arr , l , mid - 1 , key ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 1 , 2 , 3 , 3 } ; int n = arr . length ; int key = 3 ; System . out . println ( search ( arr , 0 , n - 1 , key ) ) ; } }
Case | Java implementation of the approach ; Function to return the sorted string ; Vectors to store the lowercase and uppercase characters ; Sort both the vectors ; If current character is lowercase then pick the lowercase character from the sorted list ; Else pick the uppercase character ; Return the sorted string ; Driver code
import java . util . Collections ; import java . util . Vector ; class GFG { public static String getSortedString ( StringBuilder s , int n ) { Vector < Character > v1 = new Vector < > ( ) ; Vector < Character > v2 = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) >= ' a ' && s . charAt ( i ) <= ' z ' ) v1 . add ( s . charAt ( i ) ) ; if ( s . charAt ( i ) >= ' A ' && s . charAt ( i ) <= ' z ' ) v2 . add ( s . charAt ( i ) ) ; } Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; int i = 0 , j = 0 ; for ( int k = 0 ; k < n ; k ++ ) { if ( s . charAt ( k ) > = ' a ' && s . charAt ( k ) <= ' z ' ) { s . setCharAt ( k , v1 . elementAt ( i ) ) ; ++ i ; } else if ( s . charAt ( k ) > = ' A ' && s . charAt ( k ) <= ' Z ' ) { s . setCharAt ( k , v2 . elementAt ( j ) ) ; ++ j ; } } return s . toString ( ) ; } public static void main ( String [ ] args ) { StringBuilder s = new StringBuilder ( " gEeksfOrgEEkS " ) ; int n = s . length ( ) ; System . out . println ( getSortedString ( s , n ) ) ; } }
Check if the string contains consecutive letters and each letter occurs exactly once | Java program to implement the above approach ; Function to check if the condition holds ; Get the length of the string ; sort the given string ; Iterate for every index and check for the condition ; If are not consecutive ; Driver code ; 1 st example ; 2 nd example
import java . util . * ; class GfG { static boolean check ( char s [ ] ) { int l = s . length ; Arrays . sort ( s ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( s [ i ] - s [ i - 1 ] != 1 ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " dcef " ; if ( check ( str . toCharArray ( ) ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String str1 = " xyza " ; if ( check ( str1 . toCharArray ( ) ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Smallest subset with sum greater than all other elements | Java code to find minimum number of elements such that their sum is greater than sum of remaining elements of the array . ; Function to find minimum elements needed ; Calculating HALF of array sum ; Sort the array in ascending order and start traversing array from the ascending sort in descending order . ; Current sum greater than sum ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int minElements ( int arr [ ] , int n ) { int halfSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) halfSum = halfSum + arr [ i ] ; halfSum = halfSum / 2 ; Arrays . sort ( arr ) ; int res = 0 , curr_sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { curr_sum += arr [ i ] ; res ++ ; if ( curr_sum > halfSum ) return res ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 7 , 1 } ; int n = arr . length ; System . out . println ( minElements ( arr , n ) ) ; } }
Minimum increment and decrement by K of each pair elements required to make all array elements equal | Java program for the above approach ; Function to check if its possible to make all array elements equal or not ; Stores the sum of the array ; Traverse the array ; If sum is divisible by N ; Otherwise , not possible to make all array elements equal ; Driver Code ; Given array ; Size of the array
import java . util . * ; class GFG { static void arrayElementEqual ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum % N == 0 ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 6 , 4 } ; int N = arr . length ; arrayElementEqual ( arr , N ) ; } }
Rearrange array to maximize sum of GCD of array elements with their respective indices | Java program to implement the above approach ; Function to find the maximum sum of GCD ( arr [ i ] , i ) by rearranging the array ; Stores maximum sum of GCD ( arr [ i ] , i ) by rearranging the array elements ; Update res ; Driver Code
import java . util . * ; class GFG { static int findMaxValByRearrArr ( int arr [ ] , int N ) { int res = 0 ; res = ( N * ( N + 1 ) ) / 2 ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 } ; int N = arr . length ; System . out . print ( findMaxValByRearrArr ( arr , N ) ) ; } }
Polygon with maximum sides that can be inscribed in an N | Java program for the above approach ; Function to find the maximum sided polygon that can be inscribed ; Base Case ; Return n / 2 if n is even Otherwise , return - 1 ; Driver Code ; Given N ; Function Call
import java . util . * ; class GFG { static int MaximumSides ( int n ) { if ( n < 4 ) return - 1 ; return n % 2 == 0 ? n / 2 : - 1 ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . print ( MaximumSides ( N ) ) ; } }
Mean of array generated by products of all pairs of the given array | Java program for the above approach ; Function to find the mean of pair product array of arr [ ] ; Initializing suffix sum array ; Build suffix sum array ; Size of pairProductArray ; Stores sum of pairProductArray ; Store the mean ; Find mean of pairProductArray ; Return the resultant mean ; Driver Code ; Given array arr [ ] ; Function call
import java . io . * ; import java . util . * ; class GFG { static float pairProductMean ( int arr [ ] , int N ) { int suffixSumArray [ ] = new int [ N ] ; suffixSumArray [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { suffixSumArray [ i ] = suffixSumArray [ i + 1 ] + arr [ i ] ; } int length = ( N * ( N - 1 ) ) / 2 ; float res = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { res += arr [ i ] * suffixSumArray [ i + 1 ] ; } float mean ; if ( length != 0 ) mean = res / length ; else mean = 0 ; return mean ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 } ; int N = arr . length ; System . out . format ( " % .2f " , pairProductMean ( arr , N ) ) ; } }
Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1 s | Java Program to implement the above approach ; Function to return the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; Function to find the minimum count of paths from top left to bottom right by placing K 1 s in the matrix ; Count of ways without 1 s ; Count of paths from starting point to mid point ; Count of paths from mid point to end point ; Driver Code
import java . util . * ; class GFG { static int ncr ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int countPath ( int N , int M , int K ) { int answer ; if ( K >= 2 ) answer = 0 ; else if ( K == 0 ) answer = ncr ( N + M - 2 , N - 1 ) ; else { answer = ncr ( N + M - 2 , N - 1 ) ; int X = ( N - 1 ) / 2 + ( M - 1 ) / 2 ; int Y = ( N - 1 ) / 2 ; int midCount = ncr ( X , Y ) ; X = ( ( N - 1 ) - ( N - 1 ) / 2 ) + ( ( M - 1 ) - ( M - 1 ) / 2 ) ; Y = ( ( N - 1 ) - ( N - 1 ) / 2 ) ; midCount *= ncr ( X , Y ) ; answer -= midCount ; } return answer ; } public static void main ( String [ ] args ) { int N = 3 ; int M = 3 ; int K = 1 ; System . out . print ( countPath ( N , M , K ) ) ; } }
Maximum number of operations required such that no pairs from a Matrix overlap | Java program to implement the above approach ; Function to find maximum count of operations ; Initialize count by 0 ; Iterate over remaining pairs ; Check if first operation is applicable ; Check if 2 nd operation is applicable ; Otherwise ; Return the count of operations ; Driver Code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int find_max ( Vector < pair > v , int n ) { int count = 0 ; if ( n >= 2 ) count = 2 ; else count = 1 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( v . get ( i - 1 ) . first < ( v . get ( i ) . first - v . get ( i ) . second ) ) count ++ ; else if ( v . get ( i + 1 ) . first > ( v . get ( i ) . first + v . get ( i ) . second ) ) { count ++ ; v . get ( i ) . first = v . get ( i ) . first + v . get ( i ) . second ; } else continue ; } return count ; } public static void main ( String [ ] args ) { int n = 3 ; Vector < pair > v = new Vector < > ( ) ; v . add ( new pair ( 10 , 20 ) ) ; v . add ( new pair ( 15 , 10 ) ) ; v . add ( new pair ( 20 , 16 ) ) ; System . out . print ( find_max ( v , n ) ) ; } }
Count of substrings formed using a given set of characters only | Java program for the above approach ; Function to find the number of substrings that can be formed using given characters ; Boolean array for storing the available characters ; Mark indices of all available characters as 1 ; Initialize lastPos as - 1 ; Initialize ans with the total no of possible substrings ; Traverse the string from left to right ; If the current character is not present in B ; Subtract the total possible substrings ; Update the value of lastpos to current index ; Print the final answer ; Driver Code ; Given String ; Given character array ; Function Call
import java . util . Arrays ; class GFG { public static void numberofsubstrings ( String str , int k , char charArray [ ] ) { int N = str . length ( ) ; int available [ ] = new int [ 26 ] ; Arrays . fill ( available , 0 ) ; for ( int i = 0 ; i < k ; i ++ ) { available [ charArray [ i ] - ' a ' ] = 1 ; } int lastPos = - 1 ; int ans = ( N * ( N + 1 ) ) / 2 ; for ( int i = 0 ; i < N ; i ++ ) { if ( available [ str . charAt ( i ) - ' a ' ] == 0 ) { ans -= ( ( i - lastPos ) * ( N - i ) ) ; lastPos = i ; } } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { String str = " abcb " ; int k = 2 ; char [ ] charArray = { ' a ' , ' b ' } ; numberofsubstrings ( str , k , charArray ) ; } }