codeparrot/xlcost-text-to-code huggingface dataset:
K Centers Problem | Set 1 ( Greedy Approximate Algorithm ) | Java program for the above approach ; Index of city having the maximum distance to it 's  closest center ; Updating the distance of the cities to their closest centers ; Updating the index of the city with the maximum distance to it 's closest center ; Printing the maximum distance of a city to a center that is our answer ; Printing the cities that were chosen to be made centers ; Driver Code ; Function Call
import java . util . * ; class GFG { static int maxindex ( int [ ] dist , int n ) { int mi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dist [ i ] > dist [ mi ] ) mi = i ; } return mi ; } static void selectKcities ( int n , int weights [ ] [ ] , int k ) { int [ ] dist = new int [ n ] ; ArrayList < Integer > centers = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { dist [ i ] = Integer . MAX_VALUE ; } int max = 0 ; for ( int i = 0 ; i < k ; i ++ ) { centers . add ( max ) ; for ( int j = 0 ; j < n ; j ++ ) { dist [ j ] = Math . min ( dist [ j ] , weights [ max ] [ j ] ) ; } max = maxindex ( dist , n ) ; } System . out . println ( dist [ max ] ) ; for ( int i = 0 ; i < centers . size ( ) ; i ++ ) { System . out . print ( centers . get ( i ) + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 4 ; int [ ] [ ] weights = new int [ ] [ ] { { 0 , 4 , 8 , 5 } , { 4 , 0 , 10 , 7 } , { 8 , 10 , 0 , 9 } , { 5 , 7 , 9 , 0 } } ; int k = 2 ; selectKcities ( n , weights , k ) ; } }
Perfect Binary Tree Specific Level Order Traversal | Set 2 | Java program for special order traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Given a perfect binary tree , print its nodes in specific level order ; for level order traversal ; Stack to print reverse ; vector to store the level ; considering size of the level ; push data of the node of a particular level to vector ; push vector containing a level in Stack ; print the Stack ; Finally pop all Nodes from Stack and prints them . ; finally print root ; ; Driver code
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; Node ( int value ) { data = value ; left = null ; right = null ; } } ; static void specific_level_order_traversal ( Node root ) { Queue < Node > q = new LinkedList < > ( ) ; Stack < Vector < Integer > > s = new Stack < Vector < Integer > > ( ) ; q . add ( root ) ; int sz ; while ( q . size ( ) > 0 ) { Vector < Integer > v = new Vector < Integer > ( ) ; sz = q . size ( ) ; for ( int i = 0 ; i < sz ; ++ i ) { Node temp = q . peek ( ) ; q . remove ( ) ; v . add ( temp . data ) ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } s . push ( v ) ; } while ( s . size ( ) > 0 ) { Vector < Integer > v = s . peek ( ) ; s . pop ( ) ; for ( int i = 0 , j = v . size ( ) - 1 ; i < j ; ++ i ) { System . out . print ( v . get ( i ) + " ▁ " + v . get ( j ) + " ▁ " ) ; j -- ; } } System . out . println ( root . data ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; System . out . println ( " Specific ▁ Level ▁ Order ▁ traversal " + " ▁ of ▁ binary ▁ tree ▁ is " ) ; specific_level_order_traversal ( root ) ; } }
Number of Triangles in an Undirected Graph | Java program to find number of triangles in an Undirected Graph . The program is for adjacency matrix representation of the graph ; Number of vertices in the graph ; Utility function for matrix multiplication ; Utility function to calculate trace of a matrix ( sum of diagnonal elements ) ; Utility function for calculating number of triangles in graph ; To Store graph ^ 2 ; To Store graph ^ 3 ; Initialising aux matrices with 0 ; aux2 is graph ^ 2 now printMatrix ( aux2 ) ; after this multiplication aux3 is graph ^ 3 printMatrix ( aux3 ) ; Driver code
import java . io . * ; class Directed { int V = 4 ; void multiply ( int A [ ] [ ] , int B [ ] [ ] , int C [ ] [ ] ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { C [ i ] [ j ] = 0 ; for ( int k = 0 ; k < V ; k ++ ) { C [ i ] [ j ] += A [ i ] [ k ] * B [ k ] [ j ] ; } } } } int getTrace ( int graph [ ] [ ] ) { int trace = 0 ; for ( int i = 0 ; i < V ; i ++ ) { trace += graph [ i ] [ i ] ; } return trace ; } int triangleInGraph ( int graph [ ] [ ] ) { int [ ] [ ] aux2 = new int [ V ] [ V ] ; int [ ] [ ] aux3 = new int [ V ] [ V ] ; for ( int i = 0 ; i < V ; ++ i ) { for ( int j = 0 ; j < V ; ++ j ) { aux2 [ i ] [ j ] = aux3 [ i ] [ j ] = 0 ; } } multiply ( graph , graph , aux2 ) ; multiply ( graph , aux2 , aux3 ) ; int trace = getTrace ( aux3 ) ; return trace / 6 ; } public static void main ( String args [ ] ) { Directed obj = new Directed ( ) ; int graph [ ] [ ] = { { 0 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 0 } } ; System . out . println ( " Total ▁ number ▁ of ▁ Triangle ▁ in ▁ Graph ▁ : ▁ " + obj . triangleInGraph ( graph ) ) ; } }
Count subsequences in first string which are anagrams of the second string | Java implementation to count subsequences in first string which are anagrams of the second string ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; function to count subsequences in first string which are anagrams of the second string ; hash tables to store frequencies of each character ; store frequency of each character of ' str1' ; store frequency of each character of ' str2' ; to store the total count of subsequences ; if character ( i + ' a ' ) exists in ' str2' ; if this character ' s ▁ frequency ▁ ▁ in ▁ ' str2 ' ▁ in ▁ less ▁ than ▁ or ▁ ▁ equal ▁ to ▁ its ▁ frequency ▁ in ▁ ▁ ' str1 ' ▁ then ▁ accumulate ▁ its ▁ ▁ contribution ▁ to ▁ the ▁ count ▁ ▁ of ▁ subsequences . ▁ If ▁ its ▁ ▁ frequency ▁ in ▁ ' str1 ' ▁ is ▁ ' n ' ▁ ▁ and ▁ in ▁ ' str2 ' ▁ is ▁ ' r ', then  its contribution will be nCr,  where C is the binomial  coefficient. ; else return 0 as there could be no subsequence which is an anagram of ' str2' ; required count of subsequences ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public final static int SIZE = 26 ; public static int binomialCoeff ( 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 ; } public static int countSubsequences ( String str , String str3 ) { int [ ] freq1 = new int [ SIZE ] ; int [ ] freq2 = new int [ SIZE ] ; char [ ] str1 = str . toCharArray ( ) ; char [ ] str2 = str3 . toCharArray ( ) ; int n1 = str . length ( ) ; int n2 = str3 . length ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) freq1 [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < n2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] ++ ; int count = 1 ; for ( int i = 0 ; i < SIZE ; i ++ ) if ( freq2 [ i ] != 0 ) { if ( freq2 [ i ] <= freq1 [ i ] ) count = count * binomialCoeff ( freq1 [ i ] , freq2 [ i ] ) ; else return 0 ; } return count ; } public static void main ( String argc [ ] ) { String str1 = " abacd " ; String str2 = " abc " ; System . out . println ( " Count ▁ = ▁ " + countSubsequences ( str1 , str2 ) ) ; } }
Subsequence queries after removing substrings | Java program for answering queries to check whether a String subsequence or not after removing a substring . ; arrays to store results of preprocessing ; function to preprocess the strings ; initialize it as 0. ; store subsequence count in forward direction ; store number of matches till now ; store subsequence count in backward direction ; store number of matches till now ; function that gives the output ; length of remaining String A is less than B 's length ; Driver Code ; two queries
class GFG { static int [ ] fwd = new int [ 100 ] ; static int [ ] bwd = new int [ 100 ] ; static void preProcess ( String a , String b ) { int n = a . length ( ) ; int j = 0 ; for ( int i = 1 ; i <= a . length ( ) ; i ++ ) { if ( j < b . length ( ) && a . charAt ( i - 1 ) == b . charAt ( j ) ) { j ++ ; } fwd [ i ] = j ; } j = 0 ; for ( int i = a . length ( ) ; i >= 1 ; i -- ) { if ( j < b . length ( ) && a . charAt ( i - 1 ) == b . charAt ( b . length ( ) - j - 1 ) ) { j ++ ; } bwd [ i ] = j ; } } static void query ( String a , String b , int x , int y ) { if ( ( x - 1 + a . length ( ) - y ) < b . length ( ) ) { System . out . print ( "NoNEW_LINE"); return ; } if ( fwd [ x - 1 ] + bwd [ y + 1 ] >= b . length ( ) ) { System . out . print ( "YesNEW_LINE"); } else { System . out . print ( "NoNEW_LINE"); } } public static void main ( String [ ] args ) { String a = " abcabcxy " , b = " acy " ; preProcess ( a , b ) ; int x = 2 , y = 5 ; query ( a , b , x , y ) ; x = 3 ; y = 6 ; query ( a , b , x , y ) ; } }
Count subsequence of length three in a given string | Java program to find number of occurrences of a subsequence of length 3 ; Function to find number of occurrences of a subsequence of length three in a string ; variable to store no of occurrences ; loop to find first character ; loop to find 2 nd character ; loop to find 3 rd character ; increment count if subsequence is found ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int findOccurrences ( String str1 , String substr1 ) { int counter = 0 ; char [ ] str = str1 . toCharArray ( ) ; char [ ] substr = substr1 . toCharArray ( ) ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str [ i ] == substr [ 0 ] ) { for ( int j = i + 1 ; j < str1 . length ( ) ; j ++ ) { if ( str [ j ] == substr [ 1 ] ) { for ( int k = j + 1 ; k < str1 . length ( ) ; k ++ ) { if ( str [ k ] == substr [ 2 ] ) counter ++ ; } } } } } return counter ; } public static void main ( String argc [ ] ) { String str = " GFGFGYSYIOIWIN " ; String substr = " GFG " ; System . out . println ( findOccurrences ( str , substr ) ) ; } }
Count subsequence of length three in a given string | Java program to find number of occurrences of a subsequence of length 3 ; Function to find number of occurrences of a subsequence of length three in a string ; calculate length of string ; auxiliary array to store occurrences of first character ; auxiliary array to store occurrences of third character ; calculate occurrences of first character upto ith index from left ; calculate occurrences of third character upto ith index from right ; variable to store total number of occurrences ; loop to find the occurrences of middle element ; if middle character of subsequence is found in the string ; multiply the total occurrences of first character before middle character with the total occurrences of third character after middle character ; Driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int findOccurrences ( String str1 , String substr1 ) { int n = str1 . length ( ) ; char [ ] str = str1 . toCharArray ( ) ; char [ ] substr = substr1 . toCharArray ( ) ; int [ ] preLeft = new int [ n ] ; int [ ] preRight = new int [ n ] ; if ( str [ 0 ] == substr [ 0 ] ) preLeft [ 0 ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == substr [ 0 ] ) preLeft [ i ] = preLeft [ i - 1 ] + 1 ; else preLeft [ i ] = preLeft [ i - 1 ] ; } if ( str [ n - 1 ] == substr [ 2 ] ) preRight [ n - 1 ] ++ ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( str [ i ] == substr [ 2 ] ) preRight [ i ] = preRight [ i + 1 ] + 1 ; else preRight [ i ] = preRight [ i + 1 ] ; } int counter = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ 1 ] ) { int total = preLeft [ i - 1 ] * preRight [ i + 1 ] ; counter += total ; } } return counter ; } public static void main ( String argc [ ] ) { String str = " GFGFGYSYIOIWIN " ; String substr = " GFG " ; System . out . println ( findOccurrences ( str , substr ) ) ; } }
Form the largest palindromic number using atmost two swaps | Java implementation to form the largest palindromic number using atmost two swaps ; function to form the largest palindromic number using atmost two swaps ; if length of number is less than '3' then no higher palindromic number can be formed ; find the index of last digit in the 1 st half of ' num ' ; as only the first half of ' num [ ] ' is being considered , therefore for the rightmost digit in the first half of ' num [ ] ' , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element in the first half of ' num [ ] ' up to the left element ; if ' num [ i ] ' is less than the greatest digit encountered so far ; there is no greater right digit for ' num [ i ] ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and also perform the required swap operation in the right halft of ' num [ ] ' to maintain palindromic property , then break ; performing the required swap operations ;  ; Driver code ; required largest palindromic number
class GFG { static void largestPalin ( char num [ ] , int n ) { if ( n <= 3 ) return ; int mid = n / 2 - 1 ; int [ ] rightMax = new int [ mid + 1 ] ; int right ; rightMax [ mid ] = - 1 ; right = mid ; for ( int i = mid - 1 ; i >= 0 ; i -- ) { if ( num [ i ] < num [ right ] ) rightMax [ i ] = right ; else { rightMax [ i ] = - 1 ; right = i ; } } for ( int i = 0 ; i <= mid ; i ++ ) { if ( rightMax [ i ] != - 1 ) { swap ( num , i , rightMax [ i ] ) ; swap ( num , n - i - 1 , n - rightMax [ i ] - 1 ) ; break ; } } } static char [ ] swap ( char [ ] arr , int i , int j ) { char temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { char num [ ] = "4697557964" . toCharArray ( ) ; int n = num . length ; largestPalin ( num , n ) ; System . out . println ( " Largest ▁ Palindrome : ▁ " + String . valueOf ( num ) ) ; } }
Mirror characters of a string | Java code to find the reverse alphabetical order from a given position ; Function which take the given string and the position from which the reversing shall be done and returns the modified string ; Creating a string having reversed alphabetical order ; The string up to the point specified in the question , the string remains unchanged and from the point up to the length of the string , we reverse the alphabetical order ; Driver function
import java . io . * ; class GeeksforGeeks { static String compute ( String str , int n ) { String reverseAlphabet = " zyxwvutsrqponmlkjihgfedcba " ; int l = str . length ( ) ; String answer = " " ; for ( int i = 0 ; i < n ; i ++ ) answer = answer + str . charAt ( i ) ; for ( int i = n ; i < l ; i ++ ) answer = answer + reverseAlphabet . charAt ( str . charAt ( i ) - ' a ' ) ; return answer ; } public static void main ( String args [ ] ) { String str = " pneumonia " ; int n = 4 ; System . out . print ( compute ( str , n - 1 ) ) ; } }
Check whether a given graph is Bipartite or not |  ;  ; vector to store colour of vertex assiging all to - 1 i . e . uncoloured colours are either 0 or 1 for understanding take 0 as red and 1 as blue ; queue for BFS storing { vertex , colour } ; loop incase graph is not connected ; if not coloured ; colouring with 0 i . e . red ; current vertex ; colour of current vertex ; traversing vertexes connected to current vertex ; if already coloured with parent vertex color then bipartite graph is not possible ; if uncooloured ; colouring with opposite color to that of parent ; if all vertexes are coloured such that no two connected vertex have same colours ; Driver Code Starts . ; adjacency list for storing graph ; returns 1 if bipatite graph is possible ; returns 0 if bipartite graph is not possible
import java . util . * ; public class GFG { static class Pair { int first , second ; Pair ( int f , int s ) { first = f ; second = s ; } } static boolean isBipartite ( int V , ArrayList < ArrayList < Integer > > adj ) { int col [ ] = new int [ V ] ; Arrays . fill ( col , - 1 ) ; Queue < Pair > q = new LinkedList < Pair > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { if ( col [ i ] == - 1 ) { q . add ( new Pair ( i , 0 ) ) ; col [ i ] = 0 ; while ( ! q . isEmpty ( ) ) { Pair p = q . peek ( ) ; q . poll ( ) ; int v = p . first ; int c = p . second ; for ( int j : adj . get ( v ) ) { if ( col [ j ] == c ) return false ; if ( col [ j ] == - 1 ) { col [ j ] = ( c == 1 ) ? 0 : 1 ; q . add ( new Pair ( j , col [ j ] ) ) ; } } } } } return true ; } public static void main ( String args [ ] ) { int V , E ; V = 4 ; E = 8 ; ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { adj . add ( new ArrayList < Integer > ( ) ) ; } adj . get ( 0 ) . add ( 1 ) ; adj . get ( 0 ) . add ( 3 ) ; adj . get ( 1 ) . add ( 0 ) ; adj . get ( 1 ) . add ( 2 ) ; adj . get ( 2 ) . add ( 1 ) ; adj . get ( 2 ) . add ( 3 ) ; adj . get ( 3 ) . add ( 0 ) ; adj . get ( 3 ) . add ( 2 ) ; boolean ans = isBipartite ( V , adj ) ; if ( ans ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Lexicographically smallest string whose hamming distance from given string is exactly K | Java program to find Lexicographically smallest string whose hamming distance from given string is exactly K ; function to find Lexicographically smallest string with hamming distance k ; If k is 0 , output input string ; Copying input string into output string ; Traverse all the character of the string ; If current character is not ' a ' ; copy character ' a ' to output string ; str2 [ i ] = ' a ' ; ; If hamming distance became k , break ; ; If k is less than p ; Traversing string in reverse order ; Driven Program
class GFG { static void findString ( String str , int n , int k ) { if ( k == 0 ) { System . out . println ( str ) ; ; return ; } String str2 = str ; int p = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str2 . charAt ( i ) != ' a ' ) { str2 = str2 . substring ( 0 , i ) + ' a ' + str2 . substring ( i + 1 ) ; p ++ ; if ( p == k ) break ; } } if ( p < k ) { for ( int i = n - 1 ; i >= 0 ; i -- ) if ( str . charAt ( i ) == ' a ' ) { str2 = str2 . substring ( 0 , i ) + ' b ' + str2 . substring ( i + 1 ) ; p ++ ; if ( p == k ) break ; } } System . out . println ( str2 ) ; } public static void main ( String [ ] args ) { String str = " pqrs " ; int n = str . length ( ) ; int k = 2 ; findString ( str , n , k ) ; } }
Lexicographically first alternate vowel and consonant string | Java implementation of lexicographically first alternate vowel and consonant String ; ' ch ' is vowel or not ; create alternate vowel and consonant String str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required lexicographically first alternate vowel and consonant String ; hash table to store frequencies of each character in ' str ' ; initialize all elements of char_freq [ ] to 0 ; count vowels ; count consonants ; update frequency of ' ch ' in char_freq [ ] ; no such String can be formed ; form the vowel String ' vstr ' and consonant String ' cstr ' which contains characters in lexicographical order ; remove first character of vowel String then create alternate String with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant String then create alternate String with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating String with consonant ; start creating String with vowel ; Driver code
import java . util . Arrays ; class GFG { static final int SIZE = 26 ; static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) { return true ; } return false ; } static String createAltStr ( String str1 , String str2 , int start , int l ) { String finalStr = " " ; for ( int i = 0 , j = start ; j < l ; i ++ , j ++ ) { finalStr = ( finalStr + str1 . charAt ( i ) ) + str2 . charAt ( j ) ; } return finalStr ; } static String findAltStr ( String str ) { int char_freq [ ] = new int [ SIZE ] ; Arrays . fill ( char_freq , 0 ) ; int nv = 0 , nc = 0 ; String vstr = " " , cstr = " " ; int l = str . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { char ch = str . charAt ( i ) ; if ( isVowel ( ch ) ) { nv ++ ; } else { nc ++ ; } char_freq [ ch - 97 ] ++ ; } if ( Math . abs ( nv - nc ) >= 2 ) { return " no ▁ such ▁ String " ; } for ( int i = 0 ; i < SIZE ; i ++ ) { char ch = ( char ) ( i + 97 ) ; for ( int j = 1 ; j <= char_freq [ i ] ; j ++ ) { if ( isVowel ( ch ) ) { vstr += ch ; } else { cstr += ch ; } } } if ( nv > nc ) { return ( vstr . charAt ( 0 ) + createAltStr ( cstr , vstr , 1 , nv ) ) ; } if ( nc > nv ) { return ( cstr . charAt ( 0 ) + createAltStr ( vstr , cstr , 1 , nc ) ) ; } if ( cstr . charAt ( 0 ) < vstr . charAt ( 0 ) ) { return createAltStr ( cstr , vstr , 0 , nv ) ; } return createAltStr ( vstr , cstr , 0 , nc ) ; } public static void main ( String [ ] args ) { String str = " aeroplane " ; System . out . println ( findAltStr ( str ) ) ; } }
Decode an Encoded Base 64 String to ASCII String | Java Program to decode a base64 Encoded String back to ASCII String ; char_set = " ABCDEFGHIJKLMNOPQRSTUVWXYZ ▁ abcdefghijklmnopqrstuvwxyz0123456789 + / " ; stores the bitstream . ; count_bits stores current number of bits in num . ; selects 4 characters from encoded String at a time . find the position of each encoded character in char_set and stores in num . ; make space for 6 bits . ; encoded [ i + j ] = ' E ' , ' E ' - ' A ' = 5 ' E ' has 5 th position in char_set . ; encoded [ i + j ] = ' e ' , ' e ' - ' a ' = 5 , 5 + 26 = 31 , ' e ' has 31 st position in char_set . ; encoded [ i + j ] = '8' , '8' - '0' = 8 8 + 52 = 60 , '8' has 60 th position in char_set . ; ' + ' occurs in 62 nd position in char_set . ; ' / ' occurs in 63 rd position in char_set . ; ( str [ i + j ] == ' = ' ) remove 2 bits to delete appended bits during encoding . ; 255 in binary is 11111111 ; Driver code ; Do not count last null character .
class GFG { static final int SIZE = 100 ; static String base64Decoder ( char encoded [ ] , int len_str ) { char [ ] decoded_String ; decoded_String = new char [ SIZE ] ; int i , j , k = 0 ; int num = 0 ; int count_bits = 0 ; for ( i = 0 ; i < len_str ; i += 4 ) { num = 0 ; count_bits = 0 ; for ( j = 0 ; j < 4 ; j ++ ) { if ( encoded [ i + j ] != ' = ' ) { num = num << 6 ; count_bits += 6 ; } if ( encoded [ i + j ] >= ' A ' && encoded [ i + j ] <= ' Z ' ) num = num | ( encoded [ i + j ] - ' A ' ) ; else if ( encoded [ i + j ] >= ' a ' && encoded [ i + j ] <= ' z ' ) num = num | ( encoded [ i + j ] - ' a ' + 26 ) ; else if ( encoded [ i + j ] >= '0' && encoded [ i + j ] <= '9' ) num = num | ( encoded [ i + j ] - '0' + 52 ) ; else if ( encoded [ i + j ] == ' + ' ) num = num | 62 ; else if ( encoded [ i + j ] == ' / ' ) num = num | 63 ; else { num = num >> 2 ; count_bits -= 2 ; } } while ( count_bits != 0 ) { count_bits -= 8 ; decoded_String [ k ++ ] = ( char ) ( ( num >> count_bits ) & 255 ) ; } } return String . valueOf ( decoded_String ) ; } public static void main ( String [ ] args ) { char encoded_String [ ] = " TUVOT04 = " . toCharArray ( ) ; int len_str = encoded_String . length ; len_str -= 1 ; System . out . printf ( "Encoded String : %sNEW_LINE", String . valueOf ( encoded_String ) ) ; System . out . printf ( "Decoded_String : %sNEW_LINE", base64Decoder ( encoded_String , len_str ) ) ; } }