codeparrot/xlcost-text-to-code huggingface dataset:
Smallest subsequence having GCD equal to GCD of given array | Java program to implement the above approach ; Function to calculate gcd of two numbers ; Function to print the smallest subsequence that satisfies the condition ; Stores gcd of the array . ; Traverse the given array ; Update gcdArr ; Traverse the given array . ; If current element equal to gcd of array . ; Generate all possible pairs . ; If gcd of current pair equal to gcdArr ; Print current pair of the array ; Driver Code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void printSmallSub ( int [ ] arr , int N ) { int gcdArr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { gcdArr = gcd ( gcdArr , arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == gcdArr ) { System . out . print ( arr [ i ] + " ▁ " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( gcd ( arr [ i ] , arr [ j ] ) == gcdArr ) { System . out . print ( arr [ i ] + " ▁ " + arr [ j ] ) ; return ; } } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 12 } ; int N = arr . length ; printSmallSub ( arr , N ) ; } }
Count pairs in an array whose product is composite number | Java program to implement the above approach ; Function to get all the prime numbers in the range [ 1 , X ] ; Stores the boolean value to check if a number is prime or not ; Mark all non prime numbers as false ; If i is prime number ; Mark j as a composite number ; Function to get the count of pairs whose product is a composite number ; Stores the boolean value to check if a number is prime or not ; Stores the count of 1 s ; Stores the count of prime numbers ; Traverse the given array . ; Stores count of pairs whose product is not a composite number ; Stores the count of pairs whose product is composite number ; Driver code
import java . util . * ; class GFG { public static int X = 1000000 ; public static boolean [ ] getPrimeNum ( ) { boolean isPrime [ ] = new boolean [ X ] ; Arrays . fill ( isPrime , true ) ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= X ; i ++ ) { if ( isPrime [ i ] == true ) { for ( int j = i * i ; j < X ; j += i ) { isPrime [ j ] = false ; } } } return isPrime ; } public static int cntPairs ( int arr [ ] , int N ) { boolean isPrime [ ] = getPrimeNum ( ) ; int cntOne = 0 ; int cntPrime = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOne += 1 ; } else if ( isPrime [ i ] ) { cntPrime += 1 ; } } int cntNonComp = 0 ; cntNonComp = cntPrime * cntOne + cntOne * ( cntOne - 1 ) / 2 ; int res = 0 ; res = N * ( N - 1 ) / 2 - cntNonComp ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 8 } ; int N = arr . length ; System . out . println ( cntPairs ( arr , N ) ) ; } }
Find the greater number closest to N having at most one non | Java program to implement the above approach ; Function to calculate X ^ n in log ( n ) ; Stores the value of X ^ n ; If N is odd ; Function to find the closest number > N having at most 1 non - zero digit ; Stores the count of digits in N ; Stores the power of 10 ^ ( n - 1 ) ; Stores the last ( n - 1 ) digits ; Store the answer ; Driver Code ; Function call
import java . io . * ; class GFG { static int power ( int X , int n ) { int res = 1 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) res = res * X ; X = X * X ; n = n >> 1 ; } return res ; } static int closestgtNum ( int N ) { int n = ( int ) Math . log10 ( N ) + 1 ; int P = power ( 10 , n - 1 ) ; int Y = N % P ; int res = N + ( P - Y ) ; return res ; } public static void main ( String [ ] args ) { int N = 120 ; System . out . print ( closestgtNum ( N ) ) ; } }
Rearrange an array to make similar indexed elements different from that of another array | Java program for the above approach ; Function to find the arrangement of array B [ ] such that element at each index of A [ ] and B [ ] are not equal ; Length of array ; Print not possible , if arrays only have single equal element ; Reverse array B ; Traverse over arrays to check if there is any index where A [ i ] and B [ i ] are equal ; Swap B [ i ] with B [ i - 1 ] ; Break the loop ; Print required arrangement of array B ; Driver Code ; Given arrays A [ ] and B [ ] ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static void RearrangeB ( int [ ] A , int [ ] B ) { int n = A . length ; if ( n == 1 && A [ 0 ] == B [ 0 ] ) { System . out . println ( " - 1" ) ; return ; } for ( int i = 0 ; i < n / 2 ; i ++ ) { int t = B [ i ] ; B [ i ] = B [ n - i - 1 ] ; B [ n - i - 1 ] = t ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] == A [ i ] ) { int t = B [ i + 1 ] ; B [ i + 1 ] = B [ i ] ; B [ i ] = t ; break ; } } for ( int k : B ) System . out . print ( k + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] A = { 2 , 4 , 5 , 8 } ; int [ ] B = { 2 , 4 , 5 , 8 } ; RearrangeB ( A , B ) ; } }
Swap the elements between any two given quadrants of a Matrix | Java program for the above approach ; Function to iterate over the X quadrant and swap its element with Y quadrant ; Iterate over X quadrant ; Swap operations ; Function to swap the elements of the two given quadrants ; For Swapping 1 st and 2 nd Quadrant ; For Swapping 1 st and 3 rd Quadrant ; For Swapping 1 st and 4 th Quadrant ; For Swapping 2 nd and 3 rd Quadrant ; For Swapping 2 nd and 4 th Quadrant ; For Swapping 3 rd and 4 th Quadrant ; Print the resultant matrix ; Function to print the matrix ; Iterate over the rows ; Iterate over the cols ; Driver Code ; Given matrix ; Given quadrants ; Function Call
public class Main { static void swap ( int N , int M , int mat [ ] [ ] , int startx_X , int starty_X , int startx_Y , int starty_Y ) { int row = 0 ; int col = 0 ; for ( int i = startx_X ; ; i ++ ) { col = 0 ; for ( int j = startx_X ; ; j ++ ) { int temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ startx_Y + row ] [ starty_Y + col ] ; mat [ startx_Y + row ] [ starty_Y + col ] = temp ; col ++ ; if ( col >= M / 2 ) break ; } row ++ ; if ( row >= N / 2 ) break ; } } static void swapQuadOfMatrix ( int N , int M , int mat [ ] [ ] , int X , int Y ) { if ( X == 1 && Y == 2 ) { swap ( N , M , mat , 0 , 0 , 0 , M / 2 ) ; } else if ( X == 1 && Y == 3 ) { swap ( N , M , mat , 0 , 0 , N / 2 , 0 ) ; } else if ( X == 1 && Y == 4 ) { swap ( N , M , mat , 0 , 0 , N / 2 , M / 2 ) ; } else if ( X == 2 && Y == 3 ) { swap ( N , M , mat , 0 , M / 2 , N / 2 , 0 ) ; } else if ( X == 2 && Y == 4 ) { swap ( N , M , mat , 0 , M / 2 , N / 2 , M / 2 ) ; } else if ( X == 3 && Y == 4 ) { swap ( N , M , mat , N / 2 , 0 , N / 2 , M / 2 ) ; } printMat ( N , M , mat ) ; } static void printMat ( int N , int M , int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 6 , M = 6 ; int [ ] [ ] mat = { { 1 , 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 , 17 , 18 } , { 19 , 20 , 21 , 22 , 23 , 24 } , { 25 , 26 , 27 , 28 , 29 , 30 } , { 31 , 32 , 33 , 34 , 35 , 36 } } ; int X = 1 , Y = 4 ; swapQuadOfMatrix ( N , M , mat , X , Y ) ; } }
Bottom View of a Binary Tree | Java Program to print Bottom View of Binary Tree ; Tree node class ; data of the node ; horizontal distance of the node ; left and right references ; Constructor of tree node ; Tree class ; root node of tree ; Default constructor ; Parameterized tree constructor ; Method that prints the bottom view . ; Initialize a variable ' hd ' with 0 for the root element . ; TreeMap which stores key value pair sorted on key value ; Queue to store tree nodes in level order traversal ; Assign initialized horizontal distance value to root node and add it to the queue . ; In STL , push ( ) is used enqueue an item ; Loop until the queue is empty ( standard level order loop ) ; Extract the horizontal distance value from the dequeued tree node . ; Put the dequeued tree node to TreeMap having key as horizontal distance . Every time we find a node having same horizontal distance we need to replace the data in the map . ; If the dequeued node has a left child add it to the queue with a horizontal distance hd - 1. ; If the dequeued node has a right child add it to the queue with a horizontal distance hd + 1. ; Extract the entries of map into a set to traverse an iterator over that . ; Make an iterator ; Traverse the map elements using the iterator . ; Main driver class
import java . util . * ; import java . util . Map . Entry ; class Node { int data ; int hd ; Node left , right ; public Node ( int key ) { data = key ; hd = Integer . MAX_VALUE ; left = right = null ; } } class Tree { Node root ; public Tree ( ) { } public Tree ( Node node ) { root = node ; } public void bottomView ( ) { if ( root == null ) return ; int hd = 0 ; Map < Integer , Integer > map = new TreeMap < > ( ) ; Queue < Node > queue = new LinkedList < Node > ( ) ; root . hd = hd ; queue . add ( root ) ; while ( ! queue . isEmpty ( ) ) { Node temp = queue . remove ( ) ; hd = temp . hd ; map . put ( hd , temp . data ) ; if ( temp . left != null ) { temp . left . hd = hd - 1 ; queue . add ( temp . left ) ; } if ( temp . right != null ) { temp . right . hd = hd + 1 ; queue . add ( temp . right ) ; } } Set < Entry < Integer , Integer > > set = map . entrySet ( ) ; Iterator < Entry < Integer , Integer > > iterator = set . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry < Integer , Integer > me = iterator . next ( ) ; System . out . print ( me . getValue ( ) + " ▁ " ) ; } } } public class BottomView { public static void main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 25 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; Tree tree = new Tree ( root ) ; System . out . println ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree : " ) ; tree . bottomView ( ) ; } }
Minimum increments by index value required to obtain at least two equal Array elements | Java program to implement the above approach ; Function to calculate the minimum number of steps required ; Stores minimum difference ; Driver code
import java . util . * ; class GFG { static void incrementCount ( int arr [ ] , int N ) { int mini = arr [ 0 ] - arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { mini = Math . min ( mini , arr [ i - 1 ] - arr [ i ] ) ; } System . out . println ( mini ) ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; } }
Minimum operations required to convert all characters of a String to a given Character | Java program to implement the above approach ; Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the String less than maximum number of characters that can be changed in an operation ; Set the last index as the index for the operation ; Otherwise ; If size of the String is equal to the maximum number of characters in an operation ; Find the number of operations required ; Find the starting position ; Print i - th index ; Shift to next index ; Otherwise ; Find the number of operations required ; If n % div exceeds k ; Print i - th index ; Shift to next index ; Driver Code
class GFG { static void countOperations ( int n , int k ) { int div = 2 * k + 1 ; if ( n / 2 <= k ) { System . out . print ( 1 + "NEW_LINE"); if ( n > k ) System . out . print ( k + 1 ) ; else System . out . print ( n ) ; } else { if ( n % div == 0 ) { int oprn = n / div ; System . out . print ( oprn + "NEW_LINE"); int pos = k + 1 ; System . out . print ( pos + " ▁ " ) ; for ( int i = 1 ; i <= oprn ; i ++ ) { System . out . print ( pos + " ▁ " ) ; pos += div ; } } else { int oprn = n / div + 1 ; System . out . print ( oprn + "NEW_LINE"); int pos = n % div ; if ( n % div > k ) pos -= k ; for ( int i = 1 ; i <= oprn ; i ++ ) { System . out . print ( pos + " ▁ " ) ; pos += div ; } } } } public static void main ( String [ ] args ) { String str = " edfreqwsazxet " ; char ch = ' $ ' ; int n = str . length ( ) ; int k = 4 ; countOperations ( n , k ) ; } }
Length of largest subsequence consisting of a pair of alternating digits | Java program for the above approach ; Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Variable initialization ; Nested loops for iteration ; Check if i is not equal to j ; Initialize length as 0 ; Iterate from 0 till the size of the String ; Increment length ; Increment length ; Update maxi ; Check if maxi is not equal to 1 the print it otherwise print 0 ; Driver Code ; Given String ; Function call
import java . util . * ; class GFG { static void largestSubsequence ( char [ ] s ) { int maxi = 0 ; char prev1 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { if ( i != j ) { int len = 0 ; prev1 = ( char ) ( j + '0' ) ; for ( int k = 0 ; k < s . length ; k ++ ) { if ( s [ k ] == i + '0' && prev1 == j + '0' ) { prev1 = s [ k ] ; len ++ ; } else if ( s [ k ] == j + '0' && prev1 == i + '0' ) { prev1 = s [ k ] ; len ++ ; } } maxi = Math . max ( len , maxi ) ; } } } if ( maxi != 1 ) System . out . print ( maxi + "NEW_LINE"); else System . out . print ( 0 + "NEW_LINE"); } public static void main ( String [ ] args ) { String s = "1542745249842" ; largestSubsequence ( s . toCharArray ( ) ) ; } }
Bottom View of a Binary Tree | Java program to print Bottom View of Binary Tree ; Tree node class ; Data of the node ; Horizontal distance of the node ; Left and right references ; Constructor of tree node ; printBottomViewUtil function ; Base case ; If node for a particular horizontal distance is not present , add to the map . ; Compare height for already present node at similar horizontal distance ; Recur for left subtree ; Recur for right subtree ; printBottomView function ; Map to store Horizontal Distance , Height and Data . ; Prints the values stored by printBottomViewUtil ( ) ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Node { int data ; int hd ; Node left , right ; public Node ( int key ) { data = key ; hd = Integer . MAX_VALUE ; left = right = null ; } } static void printBottomViewUtil ( Node root , int curr , int hd , TreeMap < Integer , int [ ] > m ) { if ( root == null ) return ; if ( ! m . containsKey ( hd ) ) { m . put ( hd , new int [ ] { root . data , curr } ) ; } else { int [ ] p = m . get ( hd ) ; if ( p [ 1 ] <= curr ) { p [ 1 ] = curr ; p [ 0 ] = root . data ; } m . put ( hd , p ) ; } printBottomViewUtil ( root . left , curr + 1 , hd - 1 , m ) ; printBottomViewUtil ( root . right , curr + 1 , hd + 1 , m ) ; } static void printBottomView ( Node root ) { TreeMap < Integer , int [ ] > m = new TreeMap < > ( ) ; printBottomViewUtil ( root , 0 , 0 , m ) ; for ( int val [ ] : m . values ( ) ) { System . out . print ( val [ 0 ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 25 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; System . out . println ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree : " ) ; printBottomView ( root ) ; } }
Find maximum GCD value from root to leaf in a Binary tree | Java program for the above approach ; Initialise to update the maximum gcd value from all the path ; Node structure ; Left & right child of the node ; Initialize constructor ; Function to find gcd of a and b ; Function to find the gcd of a path ; Function to find the maximum value of gcd from root to leaf in a Binary tree ; Check if root is not null ; Find the maximum gcd of path value and store in global maxm variable ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver Code ; Given Tree ; Function call ; Print the maximum AND value
import java . util . * ; class GFG { static int maxm = 0 ; static class Node { int val ; Node left , right ; Node ( int x ) { val = x ; left = null ; right = null ; } } ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int find_gcd ( Vector < Integer > arr ) { if ( arr . size ( ) == 1 ) return arr . get ( 0 ) ; int g = arr . get ( 0 ) ; for ( int i = 1 ; i < arr . size ( ) ; i ++ ) { g = gcd ( g , arr . get ( 1 ) ) ; } return g ; } static void maxm_gcd ( Node root , Vector < Integer > ans ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { ans . add ( root . val ) ; maxm = Math . max ( find_gcd ( ans ) , maxm ) ; return ; } ans . add ( root . val ) ; maxm_gcd ( root . left , ans ) ; maxm_gcd ( root . right , ans ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 15 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 7 ) ; root . left . left = new Node ( 15 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 31 ) ; root . right . right = new Node ( 9 ) ; maxm_gcd ( root , new Vector < > ( ) ) ; System . out . print ( maxm + "NEW_LINE"); } }
Count of pairs with sum N from first N natural numbers | Java program for the above approach ; Function to calculate the value of count ; Stores the count of pairs ; Set the two pointers ; Check if the sum of pairs is equal to N ; Increase the count of pairs ; Move to the next pair ; Driver code
import java . io . * ; class GFG { public static int numberOfPairs ( int n ) { int count = 0 ; int i = 1 , j = n - 1 ; while ( i < j ) { if ( i + j == n ) { count ++ ; } i ++ ; j -- ; } return count ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( numberOfPairs ( n ) ) ; } }
Count of ways to generate a Matrix with product of each row and column as 1 or | Java implementation of the above approach ; Function to return the number of possible ways ; Check if product can be - 1 ; Driver code
import java . util . Arrays ; class GFG { static void Solve ( int N , int M ) { int temp = ( N - 1 ) * ( M - 1 ) ; int ans = ( int ) ( Math . pow ( 2 , temp ) ) ; if ( ( N + M ) % 2 != 0 ) System . out . print ( ans ) ; else System . out . print ( 2 * ans ) ; } public static void main ( String [ ] args ) { int N = 3 ; int M = 3 ; Solve ( N , M ) ; } }
Program to count leaf nodes in a binary tree | Class containing left and right child of current node and key value ; Root of the Binary Tree ; Function to get the count of leaf nodes in a binary tree ; Driver program to test above functions ; create a tree ; get leaf count of the abve tree
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; int getLeafCount ( ) { return getLeafCount ( root ) ; } int getLeafCount ( Node node ) { if ( node == null ) return 0 ; if ( node . left == null && node . right == null ) return 1 ; else return getLeafCount ( node . left ) + getLeafCount ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ leaf ▁ count ▁ of ▁ binary ▁ tree ▁ is ▁ : ▁ " + tree . getLeafCount ( ) ) ; } }
Make a palindromic string from given string | Java implementation to find which player can form a palindromic string first in a game ; Function to find winner of the game ; Array to maintain frequency of the characters in S ; Initialise freq array with 0 ; Maintain count of all distinct characters ; Finding frequency of each character ; Count unique duplicate characters ; Loop to count the unique duplicate characters ; Condition for Player - 1 to be winner ; Else Player - 2 is always winner ; Driver Code ; Function call
import java . util . * ; class GFG { static int palindromeWinner ( String S ) { int freq [ ] = new int [ 26 ] ; Arrays . fill ( freq , 0 ) ; int count = 0 ; for ( int i = 0 ; i < ( int ) S . length ( ) ; ++ i ) { if ( freq [ S . charAt ( i ) - ' a ' ] == 0 ) count ++ ; freq [ S . charAt ( i ) - ' a ' ] ++ ; } int unique = 0 ; int duplicate = 0 ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 1 ) unique ++ ; else if ( freq [ i ] >= 2 ) duplicate ++ ; } if ( unique == 1 && ( unique + duplicate ) == count ) return 1 ; return 2 ; } public static void main ( String s [ ] ) { String S = " abcbc " ; System . out . println ( " Player - " + palindromeWinner ( S ) ) ; } }
Number of substrings with length divisible by the number of 1 's in it | Java program to count number of subString under given condition ; Function return count of such substring ; Selection of adequate x value ; Store where 1 's are located ; If there are no ones , then answer is 0 ; For ease of implementation ; Count storage ; Iterate on all k values less than fixed x ; Keeps a count of 1 's occured  during String traversal ; Iterate on String and modify the totCount ; If this character is 1 ; Add to the final sum / count ; Increase totCount at exterior position ; Reduce totCount at index + k * n ; Slightly modified prefix sum storage ; Number of 1 's till i-1 ; Traversing over String considering each position and finding bounds and count using the inequalities ; Calculating bounds for l and r ; If valid then add to answer ; Driver code
import java . util . * ; class GFG { static int countOfSubStrings ( String s ) { int n = s . length ( ) ; int x = ( int ) Math . sqrt ( n ) ; Vector < Integer > ones = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) ones . add ( i ) ; } if ( ones . size ( ) == 0 ) return 0 ; ones . add ( n ) ; int [ ] totCount = new int [ n * x + n ] ; int sum = 0 ; for ( int k = 0 ; k <= x ; k ++ ) { int now = 0 ; totCount [ k * n ] ++ ; for ( int j = 1 ; j <= n ; j ++ ) { if ( s . charAt ( j - 1 ) == '1' ) now ++ ; int index = j - k * now ; sum += totCount [ index + k * n ] ; totCount [ index + k * n ] ++ ; } now = 0 ; totCount [ k * n ] -- ; for ( int j = 1 ; j <= n ; j ++ ) { if ( s . charAt ( j - 1 ) == '1' ) now ++ ; int index = j - k * now ; totCount [ index + k * n ] -- ; } } int [ ] prefix_sum = new int [ n ] ; Arrays . fill ( prefix_sum , - 1 ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ i ] = cnt ; if ( s . charAt ( i ) == '1' ) cnt ++ ; } for ( int k = 0 ; k < n ; k ++ ) { for ( int j = 1 ; j <= ( n / x ) && prefix_sum [ k ] + j <= cnt ; j ++ ) { int l = ones . get ( prefix_sum [ k ] + j - 1 ) - k + 1 ; int r = ones . get ( prefix_sum [ k ] + j ) - k ; l = Math . max ( l , j * ( x + 1 ) ) ; if ( l <= r ) { sum += r / j - ( l - 1 ) / j ; } } } return sum ; } public static void main ( String [ ] args ) { String S = "1111100000" ; System . out . print ( countOfSubStrings ( S ) ) ; } }
Find the largest number smaller than integer N with maximum number of set bits | Java implementation to Find the largest number smaller than integer N with maximum number of set bits ; Function to get no of set bits in binary representation of positive integer n ; Function to return the largest number less than N ; Iterate through all the numbers ; Find the number of set bits for the current number ; Check if this number has the highest set bits ; Return the result ; Driver code
class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int largestNum ( int n ) { int num = 0 ; int max_setBits = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { int setBits = countSetBits ( i ) ; if ( setBits >= max_setBits ) { num = i ; max_setBits = setBits ; } } return num ; } public static void main ( String [ ] args ) { int N = 345 ; System . out . println ( largestNum ( N ) ) ; } }