codeparrot/xlcost-text-to-code huggingface dataset:
Calculate absolute difference between minimum and maximum sum of pairs in an array | Java program for the above approach ; Function to find the difference between the maximum and minimum sum of a pair ( arr [ i ] , arr [ j ] ) from the array such that i < j and arr [ i ] < arr [ j ] ; Stores the maximum from the suffix of the array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Stores the maximum sum of any pair ; Calculate the maximum sum ; Stores the maximum sum of any pair ; Stores the minimum of suffixes from the given array ; Set the last element ; Traverse the remaining array ; Update the maximum from suffix for the remaining indices ; Calculate the minimum sum ; Return the resultant difference ; Driver Code ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int GetDiff ( int A [ ] , int N ) { int SuffMaxArr [ ] = new int [ N ] ; SuffMaxArr [ N - 1 ] = A [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; -- i ) { SuffMaxArr [ i ] = Math . max ( SuffMaxArr [ i + 1 ] , A [ i + 1 ] ) ; } int MaximumSum = Integer . MIN_VALUE ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] < SuffMaxArr [ i ] ) MaximumSum = Math . max ( MaximumSum , A [ i ] + SuffMaxArr [ i ] ) ; } int MinimumSum = Integer . MAX_VALUE ; int SuffMinArr [ ] = new int [ N ] ; SuffMinArr [ N - 1 ] = Integer . MAX_VALUE ; for ( int i = N - 2 ; i >= 0 ; -- i ) { SuffMinArr [ i ] = Math . min ( SuffMinArr [ i + 1 ] , A [ i + 1 ] ) ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] < SuffMinArr [ i ] ) { MinimumSum = Math . min ( MinimumSum , A [ i ] + SuffMinArr [ i ] ) ; } } return Math . abs ( MaximumSum - MinimumSum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 1 , 3 , 7 , 5 , 6 } ; int N = arr . length ; System . out . println ( GetDiff ( arr , N ) ) ; } }
Minimum number of swaps required to make parity of array elements same as their indices | Java program for the above approach ; Function to count the minimum number of swaps required to make the parity of array elements same as their indices ; Stores count of even and odd array elements ; Traverse the array ; Check if indices and array elements are not of the same parity ; If index is even ; Update even ; Update odd ; Condition for not possible ; Otherwise ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minimumSwaps ( int arr [ ] , int N ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != i % 2 ) { if ( i % 2 == 0 ) { even ++ ; } else { odd ++ ; } } } if ( even != odd ) { System . out . println ( - 1 ) ; } else { System . out . println ( even ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 7 , 6 } ; int N = arr . length ; minimumSwaps ( arr , N ) ; } }
Smallest substring occurring only once in a given string | Java program for the above approach ; Function to find the smallest substring occurring only once ; Stores all occurences ; Generate all the substrings ; Avoid multiple occurences ; Append all substrings ; Take into account all the substrings ; Iterate over all unique substrings ; If frequency is 1 ; Append into fresh list ; Initialize a dictionary ; Append the keys ; Traverse the dictionary ; Return the minimum of dictionary ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int smallestSubstring ( String a ) { ArrayList < String > a1 = new ArrayList < > ( ) ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) { for ( int j = i + 1 ; j <= a . length ( ) ; j ++ ) { if ( i != j ) a1 . add ( a . substring ( i , j ) ) ; } } TreeMap < String , Integer > a2 = new TreeMap < > ( ) ; for ( String s : a1 ) a2 . put ( s , a2 . getOrDefault ( s , 0 ) + 1 ) ; ArrayList < String > freshlist = new ArrayList < > ( ) ; for ( String s : a2 . keySet ( ) ) { if ( a2 . get ( s ) == 1 ) freshlist . add ( s ) ; } TreeMap < String , Integer > dictionary = new TreeMap < > ( ) ; for ( String s : freshlist ) { dictionary . put ( s , s . length ( ) ) ; } ArrayList < Integer > newlist = new ArrayList < > ( ) ; for ( String s : dictionary . keySet ( ) ) newlist . add ( dictionary . get ( s ) ) ; int ans = Integer . MAX_VALUE ; for ( int i : newlist ) ans = Math . min ( ans , i ) ; return ans == Integer . MAX_VALUE ? 0 : ans ; } public static void main ( String [ ] args ) { String S = " ababaabba " ; System . out . println ( smallestSubstring ( S ) ) ; } }
Minimize insertions required to make ratio of maximum and minimum of all pairs of adjacent array elements at most K | Java program to implement the above approach ; Function to count the minimum number of insertions required ; Stores the number of insertions required ; Traverse the array ; Store the minimum array element ; Store the maximum array element ; Checking condition ; Increase current count ; Return the count of insertions ; Driver Code
import java . util . * ; class GFG { static int mininsert ( int arr [ ] , int K , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = Math . min ( arr [ i ] , arr [ i + 1 ] ) ; int b = Math . max ( arr [ i ] , arr [ i + 1 ] ) ; while ( K * a < b ) { a *= K ; ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 10 , 25 , 21 } ; int K = 2 ; int N = arr . length ; System . out . print ( mininsert ( arr , K , N ) ) ; } }
Sum of numbers formed by consecutive digits present in a given string | Java program to implement the above approach ; Function to calculate the sum of numbers formed by consecutive sequences of digits present in the string ; Stores consecutive digits present in the string ; Stores the sum ; Iterate over characters of the input string ; If current character is a digit ; Append current digit to curr ; Add curr to sum ; Reset curr ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int sumOfDigits ( String s ) { int curr = 0 ; int ret = 0 ; for ( char ch : s . toCharArray ( ) ) { if ( ch >= 48 && ch <= 57 ) { curr = curr * 10 + ch - '0' ; } else { ret += curr ; curr = 0 ; } } ret += curr ; return ret ; } public static void main ( String [ ] args ) { String S = "11aa32bbb5" ; System . out . print ( sumOfDigits ( S ) ) ; } }
Find a triplet ( i , j , k ) from an array such that i < j < k and arr [ i ] < arr [ j ] > arr [ k ] | Java program for the above approach ; Function to find a triplet such that i < j < k and arr [ i ] < arr [ j ] and arr [ j ] > arr [ k ] ; Traverse the array ; Condition to satisfy for the resultant triplet ; Otherwise , triplet doesn 't exist ; Driver code
import java . util . * ; class GFG { static void print_triplet ( int arr [ ] , int n ) { for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { System . out . print ( i - 1 + " ▁ " + i + " ▁ " + ( i + 1 ) ) ; return ; } } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 5 , 2 , 1 , 6 } ; int N = arr . length ; print_triplet ( arr , N ) ; } }
Check if an array element is concatenation of two elements from another array | Java program for the above approach ; Function to find elements present in the array b [ ] which are concatenation of any pair of elements in the array a [ ] ; Stores if there doesn 't any such  element in the array brr[] ; Stored the size of both the arrays ; Store the presence of an element of array a [ ] ; Traverse the array ; Traverse the array b [ ] ; Traverse over all possible concatenations of b [ i ] ; Update right and left parts ; Check if both left and right parts are present in a [ ] ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void findConcatenatedNumbers ( int [ ] a , int [ ] b ) { boolean ans = true ; int n1 = a . length ; int n2 = b . length ; int cnt [ ] = new int [ 100000 ] ; for ( int i = 0 ; i < n1 ; i ++ ) { cnt [ a [ i ] ] = 1 ; } for ( int i = 0 ; i < n2 ; i ++ ) { int left = b [ i ] ; int right = 0 ; int mul = 1 ; while ( left > 9 ) { right += ( left % 10 ) * mul ; left /= 10 ; mul *= 10 ; if ( cnt [ left ] == 1 && cnt [ right ] == 1 ) { ans = false ; System . out . print ( b [ i ] + " ▁ " ) ; } } } if ( ans ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 34 , 4 , 5 } ; int [ ] b = { 26 , 24 , 345 , 4 , 22 } ; findConcatenatedNumbers ( a , b ) ; } }
Number of subarrays having even product | Java implementation of the above approach ; Function to count subarrays with even product ; Stores count of subarrays with even product ; Traverse the array ; Initialize product ; Update product of the subarray ; Print total count of subarrays ; Driver Code ; Input ; Length of an array ; Function call to count subarrays with even product
import java . io . * ; class GFG { static void evenproduct ( int arr [ ] , int length ) { int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { int product = 1 ; for ( int j = i ; j < arr . length ; j ++ ) { product *= arr [ j ] ; if ( product % 2 == 0 ) ++ count ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 5 , 4 , 9 } ; int length = arr . length - 1 ; evenproduct ( arr , length ) ; } }
Count pairs of nodes having minimum distance between them equal to the difference of their distances from root | Java program for the above approach ; Stores the count of pairs ; Store the adjacency list of the connecting vertex ; Function for theto perform DFS traversal of the given tree ; Traverse the adjacency list of the current node u ; If the current node is the parent node ; Add number of ancestors , which is same as depth of the node ; Function for DFS traversal of the given tree ; Print the result ; Function to find the count of pairs such that the minimum distance between them is equal to the difference between distance of the nodes from root node ; Add edges to adj [ ] ; Driver Code
import java . util . * ; class GFG { static int ans = 0 ; static ArrayList < Integer > [ ] adj = new ArrayList [ ( int ) ( 1e5 ) + 1 ] ; static { for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new ArrayList < > ( ) ; } static void dfsUtil ( int u , int par , int depth ) { for ( int it : adj [ u ] ) { if ( it != par ) { dfsUtil ( it , u , depth + 1 ) ; } } ans += depth ; } static void dfs ( int u , int par , int depth ) { dfsUtil ( u , par , depth ) ; System . out . print ( ans + "NEW_LINE"); } static void countPairs ( int [ ] [ ] edges ) { for ( int i = 0 ; i < edges . length ; i ++ ) { int u = edges [ i ] [ 0 ] ; int v = edges [ i ] [ 1 ] ; adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } dfs ( 1 , 1 , 1 ) ; } public static void main ( String [ ] args ) { int [ ] [ ] edges = { { 1 , 2 } , { 1 , 3 } , { 2 , 4 } } ; countPairs ( edges ) ; } }
Sum of decimal equivalents of binary node values in each level of a Binary Tree | Java program for the above approach ; Structure of a Tree Node ; Function to convert binary number to its equivalent decimal value ; Function to calculate sum of decimal equivalent of binary numbers of node values present at each level ; Push root node into queue ; Connect nodes at the same level to form a binary number ; Append the value of the current node to eachLvl ; Insert the Left child to queue , if its not NULL ; Insert the Right child to queue , if its not NULL ; Decrement length by one ; Stores the front element of the queue ; Add decimal equivalent of the binary number formed on the current level to ans ; Finally print ans ; Driver Code ; Given Tree ; Function Call
import java . util . ArrayList ; import java . util . LinkedList ; import java . util . Queue ; class GFG { static class TreeNode { int val ; TreeNode left , right ; public TreeNode ( int key ) { val = key ; left = right = null ; } } static int convertBinaryToDecimal ( ArrayList < Integer > arr ) { int ans = 0 ; for ( int i : arr ) ans = ( ans << 1 ) | i ; return ans ; } static void decimalEquilvalentAtEachLevel ( TreeNode root ) { int ans = 0 ; Queue < TreeNode > que = new LinkedList < > ( ) ; que . add ( root ) ; while ( true ) { int length = que . size ( ) ; if ( length == 0 ) break ; ArrayList < Integer > eachLvl = new ArrayList < > ( ) ; while ( length > 0 ) { TreeNode temp = que . poll ( ) ; eachLvl . add ( temp . val ) ; if ( temp . left != null ) que . add ( temp . left ) ; if ( temp . right != null ) que . add ( temp . right ) ; length -= 1 ; } ans += convertBinaryToDecimal ( eachLvl ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { TreeNode root = new TreeNode ( 0 ) ; root . left = new TreeNode ( 1 ) ; root . right = new TreeNode ( 0 ) ; root . left . left = new TreeNode ( 0 ) ; root . left . right = new TreeNode ( 1 ) ; root . right . left = new TreeNode ( 1 ) ; root . right . right = new TreeNode ( 1 ) ; decimalEquilvalentAtEachLevel ( root ) ; } }
Minimize increments required to make differences between all pairs of array elements even | Java program for the above approach ; Function to find the minimum increments required to difference between all pairs of array elements even ; Store the count of odd and even numbers ; Traverse the array ; Increment evenCnt by 1 ; Increment oddCnt by 1 ; Print the minimum of oddCnt and eveCnt ; Driver code
public class GFG { static void minimumOperations ( int [ ] arr , int N ) { int oddCnt = 0 , evenCnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { evenCnt ++ ; } else { oddCnt ++ ; } } System . out . print ( Math . min ( oddCnt , evenCnt ) ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 1 , 2 } ; int N = arr . length ; minimumOperations ( arr , N ) ; } }
Minimum increments required to make array elements alternately even and odd | Java program for the above approach ; Function to find the minimum number of increments required to make the array even - odd alternately or vice - versa ; Store the minimum number of increments required ; Traverse the array arr [ ] ; Increment forEven if even element is present at an odd index ; Increment forEven if odd element is present at an even index ; Return the minimum number of increments ; Driver Code
class GFG { static int minIncr ( int [ ] arr , int n ) { int forEven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 1 ) { if ( ( arr [ i ] % 2 ) == 0 ) forEven += 1 ; } else { if ( arr [ i ] % 2 == 1 ) forEven += 1 ; } } return Math . min ( forEven , n - forEven ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 8 , 9 , 5 } ; int n = arr . length ; System . out . println ( minIncr ( 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 element from either ends till each value Q [ i ] ; Stores the prefix sum from both the ends of the array ; Traverse the array from front ; Insert it into the map m1 ; Traverse the array in reverse ; Insert it into the map m2 ; Traverse the query array ; Print the minimum of the two values as the answer ; Driver code ; Function Call
import java . util . * ; class GFG { static void minOperations ( int [ ] arr , int N , int [ ] Q , int M ) { Map < Integer , Integer > m1 = new HashMap < Integer , Integer > ( ) ; Map < Integer , Integer > m2 = new HashMap < Integer , Integer > ( ) ; int front = 0 , rear = 0 ; for ( int i = 0 ; i < N ; i ++ ) { front += arr [ i ] ; m1 . put ( arr [ i ] , front ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { rear += arr [ i ] ; m2 . put ( arr [ i ] , rear ) ; } for ( int i = 0 ; i < M ; i ++ ) { System . out . print ( Math . min ( m1 . get ( Q [ i ] ) , m2 . get ( Q [ i ] ) ) + " ▁ " ) ; } } 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 ; minOperations ( arr , N , Q , M ) ; } }
Check if a string contains an anagram of another string as its substring | Java program for the above approach ; Function to check if string s2 contains anagram of the string s1 as its substring ; Stores frequencies of characters in substrings of s2 ; Stores frequencies of characters in s1 ; If length of s2 exceeds length of s1 ; Store frequencies of characters in first substring of length s1len in string s2 ; Perform Sliding Window technique ; If hashmaps are found to be identical for any substring ; Driver Code
import java . io . * ; import java . util . * ; class GFG { public static boolean checkAnagram ( String s1 , String s2 ) { int s2hash [ ] = new int [ 26 ] ; int s1hash [ ] = new int [ 26 ] ; int s1len = s1 . length ( ) ; int s2len = s2 . length ( ) ; if ( s1len > s2len ) return false ; int left = 0 , right = 0 ; while ( right < s1len ) { s1hash [ s1 . charAt ( right ) - ' a ' ] += 1 ; s2hash [ s2 . charAt ( right ) - ' a ' ] += 1 ; right ++ ; } right -= 1 ; while ( right < s2len ) { if ( Arrays . equals ( s1hash , s2hash ) ) return true ; right ++ ; if ( right != s2len ) s2hash [ s2 . charAt ( right ) - ' a ' ] += 1 ; s2hash [ s2 . charAt ( left ) - ' a ' ] -= 1 ; left ++ ; } return false ; } public static void main ( String [ ] args ) { String s1 = " ab " ; String s2 = " bbpobac " ; if ( checkAnagram ( s1 , s2 ) ) System . out . println ( " YES " ) ; else System . out . println ( " No " ) ; } }
Check if K '0' s can be flipped such that Binary String contains no pair of adjacent '1' s | package whatever import java . io . * ; class GFG { Function to check if k '0' s can be flipped such that the string does not contain any pair of adjacent '1' s ; Store the count of flips ; Variable to iterate the string ; Iterate over characters of the string ; If the current character is '1' , increment i by 2 ; Otherwise , 3 cases arises ; If the current index is the starting index ; If next character is '0' ; Increment i by 1 ; If the current index is the last index ; If previous character is '0' ; For remaining characters ; If both the adjacent characters are '0' ; If cnt is at least K , print " Yes " ; Otherwise , print " No " ; Driver code
public static void canPlace ( String s , int n , int k ) { int cnt = 0 ; int i = 0 ; while ( i < n ) { if ( s . charAt ( i ) == '1' ) { i += 2 ; } else { if ( i == 0 ) { if ( s . charAt ( i + 1 ) == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else if ( i == n - 1 ) { if ( s . charAt ( i - 1 ) == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else { if ( s . charAt ( i + 1 ) == '0' && s . charAt ( i - 1 ) == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } } } if ( cnt >= k ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String S = "10001" ; int K = 1 ; int N = 5 ; canPlace ( S , N , K ) ; } }
Maximum number of intervals that an interval can intersect | Java implementation of the approach ; Function to count the maximum number of intervals that an interval can intersect ; Store the required answer ; Traverse all the intervals ; Store the number of intersecting intervals ; Iterate in the range [ 0 , n - 1 ] ; Check if jth interval lies outside the ith interval ; Update the overall maximum ; Print the answer ; Driver code ; Function Call
import java . util . * ; class GFG { static class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findMaxIntervals ( ArrayList < Pair > v , int n ) { int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int c = n ; for ( int j = 0 ; j < n ; j ++ ) { if ( v . get ( i ) . second < v . get ( j ) . first || v . get ( i ) . first > v . get ( j ) . second ) { c -- ; } } maxi = Math . max ( c , maxi ) ; } System . out . print ( maxi ) ; } public static void main ( String [ ] args ) { ArrayList < Pair > arr = new ArrayList < > ( ) ; arr . add ( new Pair ( 1 , 2 ) ) ; arr . add ( new Pair ( 3 , 4 ) ) ; arr . add ( new Pair ( 2 , 5 ) ) ; int N = arr . size ( ) ; findMaxIntervals ( arr , N ) ; } }
Check if all disks can be placed at a single rod based on given conditions | Java program to implement the above approach ; Function to check if it is possible to move all disks to a single rod ; Stores if it is possible to move all disks to a single rod ; Traverse the array ; If i - th element is smaller than both its adjacent elements ; If flag is true ; Otherwise ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static boolean check ( int a [ ] , int n ) { boolean flag = false ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] && a [ i ] < a [ i - 1 ] ) flag = true ; } if ( flag ) return false ; else return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 2 } ; int N = arr . length ; if ( check ( arr , N ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
Smallest positive integer that does not divide any elements of the given array | Java program for the above approach ; Function to find the smallest number which doesn 't divides any integer in the given array arr[] ; Traverse the array arr [ ] ; Maximum array element ; Initialize variable ; Traverse from 2 to max ; Stores if any such integer is found or not ; If any array element is divisible by j ; Smallest integer ; Print the answer ; Driver Code ; Function Call
class GFG { static void smallestNumber ( int arr [ ] , int len ) { int maxi = 0 ; for ( int i = 0 ; i < len ; i ++ ) { maxi = Math . max ( maxi , arr [ i ] ) ; } int ans = - 1 ; for ( int i = 2 ; i < maxi + 2 ; i ++ ) { boolean flag = true ; for ( int j = 0 ; j < len ; j ++ ) { if ( arr [ j ] % i == 0 ) { flag = false ; break ; } } if ( flag ) { ans = i ; break ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 6 , 9 , 2 } ; int N = arr . length ; smallestNumber ( arr , N ) ; } }
Find the duplicate characters in a string in O ( 1 ) space | Java program for the above approach ; Function to find duplicate characters in string without using any additional data structure ; Check if ( i + ' a ' ) is present in str at least once or not . ; Check if ( i + ' a ' ) is present in str at least twice or not . ; Iterate over the characters of the string str ; If str [ i ] has already occurred in str ; Set ( str [ i ] - ' a ' ) - th bit of second ; Set ( str [ i ] - ' a ' ) - th bit of second ; Iterate over the range [ 0 , 25 ] ; If i - th bit of both first and second is Set ; Driver Code
public class GFG { static void findDuplicate ( String str , int N ) { int first = 0 ; int second = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( first & ( 1 << ( str . charAt ( i ) - ' a ' ) ) ) != 0 ) { second = second | ( 1 << ( str . charAt ( i ) - ' a ' ) ) ; } else { first = first | ( 1 << ( str . charAt ( i ) - ' a ' ) ) ; } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( ( ( first & ( 1 << i ) ) & ( second & ( 1 << i ) ) ) != 0 ) { System . out . print ( ( char ) ( i + ' a ' ) + " ▁ " ) ; } } } static public void main ( String args [ ] ) { String str = " geeksforgeeks " ; int N = str . length ( ) ; findDuplicate ( str , N ) ; } }
Minimize insertions required to make all characters of a given string equal | Java program to implement the above approach ; Function to calculate the minimum number of operations required to make all characters of the string same ; Stores count of operations ; Traverse the string ; Check if adjacent characters are same or not ; Increment count ; Print the count obtained ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void minOperations ( String S ) { int count = 0 ; for ( int i = 1 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( i - 1 ) ) { count += 1 ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { String S = "0101010101" ; minOperations ( S ) ; } }