codeparrot/xlcost-text-to-code huggingface dataset:
Maximum subarray sum in an array created after repeated concatenation | Set | Java program for the above approach ; Function to find contiguous subarray with maximum sum if array is repeated K times ; Store the sum of the array arr [ ] ; Traverse the array and find sum ; Store the answer ; If K = 1 ; Apply Kadane algorithm to find sum ; Return the answer ; Stores the twice repeated array ; Traverse the range [ 0 , 2 * N ] ; Stores the maximum suffix sum ; Stores the maximum prefix sum ; Apply Kadane algorithm for 2 repetition of the array ; If the sum of the array is greater than 0 ; Return the answer ; Driver Code ; Given Input ; Function Call
import java . util . * ; class GFG { static int maxSubArraySumRepeated ( int [ ] arr , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; int curr = arr [ 0 ] ; int ans = arr [ 0 ] ; if ( K == 1 ) { for ( int i = 1 ; i < N ; i ++ ) { curr = Math . max ( arr [ i ] , curr + arr [ i ] ) ; ans = Math . max ( ans , curr ) ; } return ans ; } ArrayList < Integer > V = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < 2 * N ; i ++ ) { V . add ( arr [ i % N ] ) ; } int maxSuf = V . get ( 0 ) ; int maxPref = V . get ( 2 * N - 1 ) ; curr = V . get ( 0 ) ; for ( int i = 1 ; i < 2 * N ; i ++ ) { curr += V . get ( i ) ; maxPref = Math . max ( maxPref , curr ) ; } curr = V . get ( 2 * N - 1 ) ; for ( int i = 2 * N - 2 ; i >= 0 ; i -- ) { curr += V . get ( i ) ; maxSuf = Math . max ( maxSuf , curr ) ; } curr = V . get ( 0 ) ; for ( int i = 1 ; i < 2 * N ; i ++ ) { curr = Math . max ( V . get ( i ) , curr + V . get ( i ) ) ; ans = Math . max ( ans , curr ) ; } if ( sum > 0 ) { int temp = sum * ( K - 2 ) ; ans = Math . max ( ans , Math . max ( temp + maxPref , temp + maxSuf ) ) ; } return ans ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 20 , - 30 , - 1 , 40 } ; int N = arr . length ; int K = 10 ; System . out . print ( maxSubArraySumRepeated ( arr , N , K ) ) ; } }
Count of subarrays with largest element at least twice the largest of remaining elements | Java program for the above approach ; Function to find count of subarrays which have max element greater than twice maximum of all other elements ; Stores the count of subarrays ; Generate all possible subarrays ; Stores the maximum element of the subarray ; Stores the maximum of all other elements ; Find the maximum element in the subarray [ i , j ] ; Find the maximum of all other elements ; If the maximum of subarray is greater than twice the maximum of other elements ; Print the maximum value obtained ; Driver code
import java . io . * ; class GFG { public static void countSubarray ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int mxSubarray = 0 ; int mxOther = 0 ; for ( int k = i ; k <= j ; k ++ ) { mxSubarray = Math . max ( mxSubarray , arr [ k ] ) ; } for ( int k = 0 ; k < i ; k ++ ) { mxOther = Math . max ( mxOther , arr [ k ] ) ; } for ( int k = j + 1 ; k < n ; k ++ ) { mxOther = Math . max ( mxOther , arr [ k ] ) ; } if ( mxSubarray > ( 2 * mxOther ) ) count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 10 , 9 , 7 , 3 } ; int N = arr . length ; countSubarray ( arr , N ) ; } }
Count of subarrays with largest element at least twice the largest of remaining elements | Java program for the above approach ; Function to find count of subarrays which have max element greater than twice maximum of all other elements ; Stores the maximum element of the array ; Traverse the given array ; If the value of 2 * arr [ i ] is greater than mx ; Update the value of L and break out of loop ; If the value 2 * arr [ i ] is greater than mx ; Update the value of R and break out of loop ; Print the final answer ; Driver Code
import java . util . * ; class GFG { static void countSubarray ( int [ ] arr , int n ) { int L = 0 , R = 0 ; int mx = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) mx = Math . max ( mx , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] * 2 > mx ) { L = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] * 2 > mx ) { R = i ; break ; } } System . out . println ( ( L + 1 ) * ( n - R ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 6 , 10 , 9 , 7 , 3 } ; int N = arr . length ; countSubarray ( arr , N ) ; } }
Find Prime number just less than and just greater each element of given Array | Java program for the above approach ; Utility function to check for primality of a number X by checking whether X has any factors other than 1 and itself . ; Factor found ; Function to print primes just less than and just greater than of each element in an array ; Traverse the array ; Traverse for finding prime just less than A [ i ] ; Prime just less than A [ i ] found ; Traverse for finding prime just greater than A [ i ] ; Prime just greater than A [ i ] found ; Driver code ; Input ; Function call
import java . io . * ; class GFG { static boolean isPrime ( int X ) { for ( int i = 2 ; i * i <= X ; i ++ ) if ( X % i == 0 ) return false ; return true ; } static void printPrimes ( int A [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] - 1 ; ; j -- ) { if ( isPrime ( j ) ) { System . out . print ( j + " ▁ " ) ; break ; } } for ( int j = A [ i ] + 1 ; ; j ++ ) { if ( isPrime ( j ) ) { System . out . print ( j + " ▁ " ) ; break ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 17 , 28 } ; int N = A . length ; printPrimes ( A , N ) ; } }
Kth smallest element in an array that contains A [ i ] exactly B [ i ] times | Java program for the above approach ; Function to find the Kth smallest element that contains A [ i ] exactly B [ i ] times ; Traverse the given array ; Stores the frequency of every elements ; Traverse the given array ; Initialize a variable to store the prefix sums ; Iterate over the range [ 0 , M ] ; Increment sum by freq [ i ] ; If sum is greater than or equal to K ; Return the current element as answer ; Return - 1 ; Driver code ; { Given Input ; Function call
public class GFG_JAVA { static int KthSmallest ( int A [ ] , int B [ ] , int N , int K ) { int M = 0 ; for ( int i = 0 ; i < N ; i ++ ) { M = Math . max ( A [ i ] , M ) ; } int freq [ ] = new int [ M + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ A [ i ] ] += B [ i ] ; } int sum = 0 ; for ( int i = 0 ; i <= M ; i ++ ) { sum += freq [ i ] ; if ( sum >= K ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) int A [ ] = { 3 , 4 , 5 } ; int B [ ] = { 2 , 1 , 3 } ; int N = A . length ; int K = 4 ; System . out . println ( KthSmallest ( A , B , N , K ) ) ; } }
Bitwise OR of Bitwise AND of all subarrays of an array | Java program for the above approach ; Function to find the Bitwise OR of Bitwise AND of all subarrays ; Stores the required result ; Generate all the subarrays ; Store the current element ; Find the Bitwise OR ; Update the result ; Print the result ; Driver code
public class GFG { static void findbitwiseOR ( int [ ] a , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sub_array = a [ i ] ; res = res | curr_sub_array ; for ( int j = i ; j < n ; j ++ ) { curr_sub_array = curr_sub_array & a [ j ] ; res = res | curr_sub_array ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int N = A . length ; findbitwiseOR ( A , N ) ; } }
Bitwise OR of Bitwise AND of all subarrays of an array | Java program for the above approach ; Function to find the Bitwise OR of Bitwise AND of all consecutive subsets of the array ; Stores the required result ; Traverse the given array ; Print the result ; Driver Code
import java . io . * ; class GFG { static void findbitwiseOR ( int [ ] a , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = res | a [ i ] ; System . out . println ( res ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 } ; int N = A . length ; findbitwiseOR ( A , N ) ; } }
Check if sum of digits of a number exceeds the product of digits of that number | Java program for the above approach ; Function to check if the sum of the digits of N is strictly greater than the product of the digits of N or not ; Stores the sum and the product of the digits of N ; Stores the last digit if N ; Increment the value of sumOfDigits ; Update the prodOfDigit ; Divide N by 10 ; Print the result ; Driver code
class GFG { static void check ( int n ) { int sumOfDigit = 0 ; int prodOfDigit = 1 ; while ( n > 0 ) { int rem ; rem = n % 10 ; sumOfDigit += rem ; prodOfDigit *= rem ; n /= 10 ; } if ( sumOfDigit > prodOfDigit ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 1234 ; check ( N ) ; } }
Print all distinct even and odd prefix Bitwise XORs of first N natural numbers | Java approach for the above approach ; Print all distinct even & odd prefix Bitwise XORs from 1 to N ; Print the even number ; Print the odd number ; Driver Code
class GFG { static void evenOddBitwiseXOR ( int N ) { System . out . print ( " Even : ▁ " + 0 + " ▁ " ) ; for ( int i = 4 ; i <= N ; i = i + 4 ) { System . out . print ( i + " ▁ " ) ; } System . out . print ( "NEW_LINE"); System . out . print ( " Odd : ▁ " + 1 + " ▁ " ) ; for ( int i = 4 ; i <= N ; i = i + 4 ) { System . out . print ( i - 1 + " ▁ " ) ; } if ( N % 4 == 2 ) System . out . print ( N + 1 ) ; else if ( N % 4 == 3 ) System . out . print ( N ) ; } public static void main ( String [ ] args ) { int N = 6 ; evenOddBitwiseXOR ( N ) ; } }
Lexicographically largest permutation possible by a swap that is smaller than a given array | java program for the above approach ; Function to lexicographic largest permutation possible by a swap that is smaller than given array ; Find the index of first element such that arr [ i ] > arr [ i + 1 ] ; If the array is sorted in increasing order ; Find the index of first element which is smaller than arr [ i ] ; If arr [ j ] = = arr [ j - 1 ] ; Decrement j ; Swap the element ; Print the array arr [ ] ; Driver code
import java . util . * ; class GFG { static void findPermutation ( int [ ] arr ) { int N = arr . length ; int i = N - 2 ; while ( i >= 0 && arr [ i ] <= arr [ i + 1 ] ) i -- ; if ( i == - 1 ) { System . out . print ( " - 1" ) ; return ; } int j = N - 1 ; while ( j > i && arr [ j ] >= arr [ i ] ) j -- ; while ( j > i && arr [ j ] == arr [ j - 1 ] ) { j -- ; } int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; for ( int it : arr ) { System . out . print ( it + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 6 } ; findPermutation ( arr ) ; } }
Absolute difference between the count of odd and even factors of N | Java program for the above approach ; Function to find the smallest prime factor of all the numbers using Sieve Of Eratosthenes ; Stores whether any number is prime or not ; Initialize smallest factor as 2 for all the even numbers ; Iterate over the range [ 3 , N ] ; If i is prime ; Iterate all multiples of i ; i is the smallest prime factor of i * j ; Function to find the absolute difference between the count of odd and even factors of N ; Stores the smallest prime factor of i ; Fill values in s [ ] using sieve of eratosthenes ; Stores the total number of factors and the total number of odd and even factors ; Store the current prime factor of the number N ; Store the power of current prime factor ; Loop while N is greater than 1 ; If N also has smallest prime factor as curr , then increment cnt by 1 ; Update only total number of factors if curr is 2 ; Update total number of factors and total number of odd factors ; Update current prime factor as s [ N ] and count as 1 ; Calculate the number of even factors ; Print the difference ; Driver Code
import java . util . * ; class GFG { static void sieveOfEratosthenes ( int N , int s [ ] ) { boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= N ; j += 2 ) { if ( ! prime [ i * j ] ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } static void findDifference ( int N ) { int [ ] s = new int [ N + 1 ] ; sieveOfEratosthenes ( N , s ) ; int total = 1 , odd = 1 , even = 0 ; int curr = s [ N ] ; int cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } if ( curr == 2 ) { total = total * ( cnt + 1 ) ; } else { total = total * ( cnt + 1 ) ; odd = odd * ( cnt + 1 ) ; } curr = s [ N ] ; cnt = 1 ; } even = total - odd ; System . out . print ( Math . abs ( even - odd ) ) ; } public static void main ( String [ ] args ) { int N = 12 ; findDifference ( N ) ; } }
Calculate Median from given values of Mean and Mode | Java program for the above approach ; Function to find the median of a group of data with given mean and mode ; Calculate the median ; Print the median ; Driver code
import java . util . * ; class GFG { static void findMedian ( int Mean , int Mode ) { double Median = ( 2 * Mean + Mode ) / 3.0 ; System . out . print ( ( int ) Median ) ; } public static void main ( String [ ] args ) { int mode = 6 , mean = 3 ; findMedian ( mean , mode ) ; } }
Program to find the Magnitude of a Vector | Java program for the above approach ; Function to calculate magnitude of a 3 dimensional vector ; Stores the sum of squares of coordinates of a vector ; Return the magnitude ; Driver code
class GFG { private static double vectorMagnitude ( int x , int y , int z ) { int sum = x * x + y * y + z * z ; return Math . sqrt ( sum ) ; } public static void main ( String [ ] args ) { int x = 1 ; int y = 2 ; int z = 3 ; System . out . print ( vectorMagnitude ( x , y , z ) ) ; } }
Program to find the product of a number with a Mersenne Number | Java program to implement the above approach ; Function to find prodcut of a Mersenne number with another number ; Stores the power of 2 of integer M + 1 ; Return the product ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static long multiplyByMersenne ( long N , long M ) { long x = ( int ) ( Math . log ( M + 1 ) / Math . log ( 2 ) ) ; return ( ( N << x ) - N ) ; } public static void main ( String [ ] args ) { long N = 4 ; long M = 15 ; System . out . print ( multiplyByMersenne ( N , M ) ) ; } }
Nearest power of 2 of nearest perfect squares of non | Java program for the above approach ; Function to find nearest perfect square of num ; Calculate square root of num ; Calculate perfect square ; Find the nearest perfect square ; Function to find the power of 2 nearest to the number num ; Calculate log base 2 of num ; Highest power of 2 which is <= num ; Function to find the nearest perfect square and the nearest power of 2 of every array element whose occurrence is 1 ; Stores frequency of array elements ; Traverse the array and update frequency of current array element ; Traverse the map freq ; If the frequency is 1 ; Find nearest perfect square ; Print the nearest power of 2 ; If the any does not contain any non - repeating elements ; Driver Code
import java . util . * ; class GFG { static int perfectSquare ( int num ) { int sr = ( int ) ( Math . sqrt ( num ) ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( num - a ) < ( b - num ) ) { return a ; } else { return b ; } } static int powerOfTwo ( int num ) { int lg = ( int ) ( Math . log ( num ) / Math . log ( 2 ) ) ; int p = ( int ) ( Math . pow ( 2 , lg ) ) ; return p ; } static void uniqueElement ( int arr [ ] , int N ) { boolean ans = true ; HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > el : freq . entrySet ( ) ) { if ( el . getValue ( ) == 1 ) { ans = false ; int ps = perfectSquare ( el . getKey ( ) ) ; System . out . print ( powerOfTwo ( ps ) + " ▁ " ) ; } } if ( ans ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 11 , 4 , 3 , 4 } ; int N = arr . length ; uniqueElement ( arr , N ) ; } }
Partition array into two subarrays with every element in the right subarray strictly greater than every element in left subarray | Java program of the above approach ; Function to partition the array into two non - empty subarrays which satisfies the given condition ; Stores the suffix min array ; Stores the minimum of a suffix ; Traverse the array in reverse ; Update minimum ; Store the minimum ; Stores the maximum value of a prefix ; Stores the index of the partition ; Update max ; If max is less than min [ i + 1 ] ; Store the index of partition ; break ; If ind is not - 1 ; Print the first subarray ; Print the second subarray ; Otherwise ; Driver Code
import java . util . * ; class GFG { static void partitionArray ( int a [ ] , int n ) { int min [ ] = new int [ n ] ; int mini = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { mini = Math . min ( mini , a [ i ] ) ; min [ i ] = mini ; } int maxi = Integer . MIN_VALUE ; int ind = - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { maxi = Math . max ( maxi , a [ i ] ) ; if ( maxi < min [ i + 1 ] ) { ind = i ; break ; } } if ( ind != - 1 ) { for ( int i = 0 ; i <= ind ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; System . out . println ( ) ; for ( int i = ind + 1 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } else System . out . println ( " Impossible " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 2 , 7 , 9 } ; int N = arr . length ; partitionArray ( arr , N ) ; } }
Check if a number can be represented as sum of K positive integers out of which at least K | Java program for the above approach ; Function to count all prime factors of a given number ; Count the number of 2 s that divides n ; Since n is odd at this point , skip one element ; While i divides n , count i and divide n ; If n is a prime number greater than 2 ; Function to find the sum of first n nearly prime numbers ; Store the required sum ; Add this number if it is satisfies the condition ; Increment count of nearly prime numbers ; Function to check if N can be represented as sum of K different positive integers out of which at least K - 1 of them are nearly prime ; Store the sum of first K - 1 nearly prime numbers ; If sum is greater than or equal to n ; Otherwise , print Yes ; Driver Code
import java . util . * ; class GFG { static int countPrimeFactors ( int n ) { int count = 0 ; while ( n % 2 == 0 ) { n = n / 2 ; count ++ ; } for ( int i = 3 ; i <= ( int ) Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == 2 ) { sum += num ; i ++ ; } } return sum ; } static void check ( int n , int k ) { int s = findSum ( k - 1 ) ; if ( s >= n ) System . out . print ( " No " ) ; else System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int n = 100 , k = 6 ; check ( n , k ) ; } }
Count ways to represent an integer as an exponent | Java program for the above approach ; Function to calculate GCD of a and b using Euclidean Algorithm ; Iterate until b is non - zero ; Return the GCD ; Function to count the number of ways N can be expressed as x ^ y ; Base Case ; Stores the gcd of powers ; Calculate the degree of 2 in N ; Calculate the degree of prime numbers in N ; Calculate the degree of prime ' i ' in N ; If N is a prime , g becomes 1. ; Stores the number of ways to represent N as x ^ y ; Find the number of Factors of g ; Update the count of ways ; Iterate to find rest of the prime numbers ; Find the power of i ; Update the count of ways ; If g is prime ; Return the total number of ways ; Driver Code
import java . util . * ; class GFG { static int gcd ( int a , int b ) { while ( b > 0 ) { int rem = a % b ; a = b ; b = rem ; } return a ; } static int countNumberOfWays ( int n ) { if ( n == 1 ) return - 1 ; int g = 0 ; int power = 0 ; while ( n % 2 == 0 ) { power ++ ; n /= 2 ; } g = gcd ( g , power ) ; for ( int i = 3 ; i <= ( int ) Math . sqrt ( n ) ; i += 2 ) { power = 0 ; while ( n % i == 0 ) { power ++ ; n /= i ; } g = gcd ( g , power ) ; } if ( n > 2 ) g = gcd ( g , 1 ) ; int ways = 1 ; power = 0 ; while ( g % 2 == 0 ) { g /= 2 ; power ++ ; } ways *= ( power + 1 ) ; for ( int i = 3 ; i <= ( int ) Math . sqrt ( g ) ; i += 2 ) { power = 0 ; while ( g % i == 0 ) { power ++ ; g /= i ; } ways *= ( power + 1 ) ; } if ( g > 2 ) ways *= 2 ; return ways ; } public static void main ( String [ ] args ) { int N = 64 ; System . out . print ( countNumberOfWays ( N ) ) ; } }
Highest power of 2 less than or equal to given Integer | Java implementation of the above approach ; Function to return the lowest power of 2 close to given positive number ; Floor function is used to determine the value close to the number ; Function to return the lowest power of 2 close to given negative number ; Ceil function is used for negative numbers as - 1 > - 4. It would be opposite to positive numbers where 1 < 4 ; Function to find the highest power of 2 ; To check if the given number is positive or negative ; If the number is negative , then the ceil of the positive number is calculated and negative sign is added ; Driver code
class GFG { static int powOfPositive ( int n ) { int pos = ( int ) Math . floor ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ; return ( int ) Math . pow ( 2 , pos ) ; } static int powOfNegative ( int n ) { int pos = ( int ) Math . ceil ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ; return ( int ) ( - 1 * Math . pow ( 2 , pos ) ) ; } static void highestPowerOf2 ( int n ) { if ( n > 0 ) { System . out . println ( powOfPositive ( n ) ) ; } else { n = - n ; System . out . println ( powOfNegative ( n ) ) ; } } public static void main ( String [ ] args ) { int n = - 24 ; highestPowerOf2 ( n ) ; } }
Number of cards needed build a House of Cards of a given level N | Java implementation of the above approach ; Function to find number of cards needed ; Driver Code
import java . lang . * ; class GFG { public static int noOfCards ( int n ) { return n * ( 3 * n + 1 ) / 2 ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . print ( noOfCards ( n ) ) ; } }
Find smallest possible Number from a given large Number with same count of digits | Java implementation of the above approach ; Function for finding the smallest possible number after swapping the digits any number of times ; Variable to store the final answer ; Array to store the count of occurrence of each digit ; Loop to calculate the number of occurrences of every digit ; Loop to get smallest number ; Returning the answer ; Driver code
class GFG { static String smallestPoss ( String s , int n ) { String ans = " " ; int arr [ ] = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s . charAt ( i ) - 48 ] ++ ; } for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) ans = ans + String . valueOf ( i ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 15 ; String K = "325343273113434" ; System . out . print ( smallestPoss ( K , N ) ) ; } }