codeparrot/xlcost-text-to-code huggingface dataset:
Count number of 1 s in the array after N moves | Java implementation of the above approach ; Function to count number of perfect squares ; Counting number of perfect squares between a and b ; Function to count number of 1 s in array after N moves ; Driver Code ; Initialize array size ; Initialize all elements to 0
import java . io . * ; class GFG { static double perfectSquares ( int a , int b ) { return ( Math . floor ( Math . sqrt ( b ) ) - Math . ceil ( Math . sqrt ( a ) ) + 1 ) ; } static double countOnes ( int arr [ ] , int n ) { return perfectSquares ( 1 , n ) ; } public static void main ( String [ ] args ) { int N = 10 ; int arr [ ] = { 0 } ; System . out . println ( countOnes ( arr , N ) ) ; } }
Find the position of box which occupies the given ball | Java implementation of the approach ; Function to print the position of each boxes where a ball has to be placed ; Find the cumulative sum of array A [ ] ; Find the position of box for each ball ; Row number ; Column ( position of box in particular row ) ; Row + 1 denotes row if indexing of array start from 1 ; Driver code
class GFG { static void printPosition ( int A [ ] , int B [ ] , int sizeOfA , int sizeOfB ) { for ( int i = 1 ; i < sizeOfA ; i ++ ) { A [ i ] += A [ i - 1 ] ; } for ( int i = 0 ; i < sizeOfB ; i ++ ) { int row = lower_bound ( A , 0 , A . length , B [ i ] ) ; int boxNumber = ( row >= 1 ) ? B [ i ] - A [ row - 1 ] : B [ i ] ; System . out . print ( row + 1 + " , ▁ " + boxNumber + "NEW_LINE"); } } private static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 2 , 2 , 2 } ; int B [ ] = { 1 , 2 , 3 , 4 } ; int sizeOfA = A . length ; int sizeOfB = B . length ; printPosition ( A , B , sizeOfA , sizeOfB ) ; } }
Highest power of a number that divides other number | Java program to implement the above approach ; Function to get the prime factors and its count of times it divides ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , count i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to return the highest power ; Initialize two arrays ; Get the prime factors of n and m ; Iterate and find the maximum power ; If i not a prime factor of n and m ; If i is a prime factor of n and m If count of i dividing m is more than i dividing n , then power will be 0 ; If i is a prime factor of M ; get the maximum power ; Drivers code
class GFG { static void primeFactors ( int n , int freq [ ] ) { int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } freq [ 2 ] = cnt ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { cnt = 0 ; while ( n % i == 0 ) { cnt ++ ; n = n / i ; } freq [ i ] = cnt ; } if ( n > 2 ) freq [ n ] = 1 ; } static int getMaximumPower ( int n , int m ) { int freq1 [ ] = new int [ n + 1 ] , freq2 [ ] = new int [ m + 1 ] ; primeFactors ( n , freq1 ) ; primeFactors ( m , freq2 ) ; int maxi = 0 ; for ( int i = 2 ; i <= m ; i ++ ) { if ( freq1 [ i ] == 0 && freq2 [ i ] == 0 ) continue ; if ( freq2 [ i ] > freq1 [ i ] ) return 0 ; if ( freq2 [ i ] != 0 ) { maxi = Math . max ( maxi , freq1 [ i ] / freq2 [ i ] ) ; } } return maxi ; } public static void main ( String [ ] args ) { int n = 48 , m = 4 ; System . out . println ( getMaximumPower ( n , m ) ) ; } }
Find the number of divisors of all numbers in the range [ 1 , n ] | Java implementation of the approach ; Function to find the number of divisors of all numbers in the range [ 1 , n ] ; Array to store the count of divisors ; For every number from 1 to n ; Increase divisors count for every number divisible by i ; Print the divisors ; Driver code
class GFG { static void findDivisors ( int n ) { int [ ] div = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j * i <= n ; j ++ ) div [ i * j ] ++ ; } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( div [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 ; findDivisors ( n ) ; } }
Predict the winner of the game on the basis of absolute difference of sum by selecting numbers | Java implementation of the approach ; Function to decide the winner ; Iterate for all numbers in the array ; If mod gives 0 ; If mod gives 1 ; If mod gives 2 ; If mod gives 3 ; Check the winning condition for X ; Driver code
class GFG { static int decideWinner ( int [ ] a , int n ) { int count0 = 0 ; int count1 = 0 ; int count2 = 0 ; int count3 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 4 == 0 ) count0 ++ ; else if ( a [ i ] % 4 == 1 ) count1 ++ ; else if ( a [ i ] % 4 == 2 ) count2 ++ ; else if ( a [ i ] % 4 == 3 ) count3 ++ ; } if ( count0 % 2 == 0 && count1 % 2 == 0 && count2 % 2 == 0 && count3 == 0 ) return 1 ; else return 2 ; } public static void main ( String args [ ] ) { int [ ] a = { 4 , 8 , 5 , 9 } ; int n = a . length ; if ( decideWinner ( a , n ) == 1 ) System . out . print ( " X ▁ wins " ) ; else System . out . print ( " Y ▁ wins " ) ; } }
Count all prefixes of the given binary array which are divisible by x | Java implementation of the approach ; Function to return the count of total binary prefix which are divisible by x ; Initialize with zero ; Instead of converting all prefixes to decimal , take reminder with x ; If number is divisible by x then reminder = 0 ; Driver code
class GFG { public static int CntDivbyX ( int arr [ ] , int n , int x ) { int number = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { number = ( number * 2 + arr [ i ] ) % x ; if ( number == 0 ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int n = 7 ; int x = 2 ; System . out . print ( CntDivbyX ( arr , n , x ) ) ; } }
Length of the smallest number which is divisible by K and formed by using 1 's only | Java implementation of the approach ; Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Instead of generating all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's  Take remainder with K ; If number is divisible by k then remainder will be 0 ; Driver code
class GFG { public static int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) return - 1 ; int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = ( number * 10 + 1 ) % K ; if ( number == 0 ) return len ; } return - 1 ; } public static void main ( String [ ] args ) { int K = 7 ; System . out . print ( numLen ( K ) ) ; } }
Sum of multiplication of triplet of divisors of a number | Java implementation of the approach ; Global array declaration ; Function to find the sum of multiplication of every triplet in the divisors of a number ; sum1 [ x ] represents the sum of all the divisors of x ; Adding i to sum1 [ j ] because i is a divisor of j ; sum2 [ x ] represents the sum of all the divisors of x ; Here i is divisor of j and sum1 [ j ] - i represents sum of all divisors of j which do not include i so we add i * ( sum1 [ j ] - i ) to sum2 [ j ] ; In the above implementation we have considered every pair two times so we have to divide every sum2 array element by 2 ; Here i is the divisor of j and we are trying to add the sum of multiplication of all triplets of divisors of j such that one of the divisors is i ; In the above implementation we have considered every triplet three times so we have to divide every sum3 array element by 3 ; Print the results ; Driver code ; Precomputing
class GFGq { static int max_Element = ( int ) ( 1e6 + 5 ) ; static int sum1 [ ] = new int [ max_Element ] , sum2 [ ] = new int [ max_Element ] , sum3 [ ] = new int [ max_Element ] ; static void precomputation ( int arr [ ] , int n ) { for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum1 [ j ] += i ; for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum2 [ j ] += ( sum1 [ j ] - i ) * i ; for ( int i = 1 ; i < max_Element ; i ++ ) sum2 [ i ] /= 2 ; for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum3 [ j ] += i * ( sum2 [ j ] - i * ( sum1 [ j ] - i ) ) ; for ( int i = 1 ; i < max_Element ; i ++ ) sum3 [ i ] /= 3 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( sum3 [ arr [ i ] ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 5 , 6 } ; int n = arr . length ; precomputation ( arr , n ) ; } }
Sum of Fibonacci Numbers in a range | Java implementation of the approach ; Function to return the nth Fibonacci number ; Function to return the required sum ; Using our deduced result ; Driver code
class GFG { static int fib ( int n ) { double phi = ( 1 + Math . sqrt ( 5 ) ) / 2 ; return ( int ) Math . round ( Math . pow ( phi , n ) / Math . sqrt ( 5 ) ) ; } static int calculateSum ( int l , int r ) { int sum = fib ( r + 2 ) - fib ( l + 1 ) ; return sum ; } public static void main ( String [ ] args ) { int l = 4 , r = 8 ; System . out . println ( calculateSum ( l , r ) ) ; } }
Print the balanced bracket expression using given brackets | Java implementation of the above approach ; Function to print balanced bracket expression if it is possible ; If the condition is met ; Print brackets of type - 1 ; Print brackets of type - 3 ; Print brackets of type - 4 ; Print brackets of type - 2 ; If the condition is not met ; Driver code
class GFG { static void printBalancedExpression ( int a , int b , int c , int d ) { if ( ( ( a == d ) && ( a != 0 ) ) || ( ( a == 0 ) && ( c == 0 ) && ( d == 0 ) ) ) { for ( int i = 1 ; i <= a ; i ++ ) System . out . print ( " ( ( " ) ; for ( int i = 1 ; i <= c ; i ++ ) System . out . print ( " ) ( " ) ; for ( int i = 1 ; i <= d ; i ++ ) System . out . print ( " ) ) " ) ; for ( int i = 1 ; i <= b ; i ++ ) System . out . print ( " ( ) " ) ; } else System . out . print ( - 1 ) ; } public static void main ( String args [ ] ) { int a = 3 , b = 1 , c = 4 , d = 3 ; printBalancedExpression ( a , b , c , d ) ; } }
Count numbers having N 0 ' s ▁ and ▁ and ▁ M ▁ 1' s with no leading zeros | Java implementation of the approach ; Function to return the factorial of a number ; Function to return the count of distinct ( N + M ) digit numbers having N 0 ' s ▁ and ▁ and ▁ M ▁ 1' s with no leading zeros ; Driver code
import java . io . * ; class GFG { static int factorial ( int f ) { int fact = 1 ; for ( int i = 2 ; i <= f ; i ++ ) fact *= ( int ) i ; return fact ; } static int findPermutation ( int N , int M ) { int permutation = factorial ( N + M - 1 ) / ( factorial ( N ) * factorial ( M - 1 ) ) ; return permutation ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; System . out . println ( findPermutation ( N , M ) ) ; } }
Maximum value of | arr [ 0 ] | Java implementation of the approach ; Function to return the maximum required value ; Driver code
class GFG { static int maxValue ( int n ) { if ( n == 1 ) return 0 ; return ( ( n * n / 2 ) - 1 ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . print ( maxValue ( n ) ) ; } }
Count of a , b & c after n seconds for given reproduction rate | Java implementation of the approach ; Function to print the count of a , b and c after n seconds ; Number of multiples of 60 below n ; Multiple of 60 nearest to n ; Change all a to b ; Change all b to c ; Change each c to two a ; Print the updated values of a , b and c ; Driver code
class GFG { static void findCount ( int n ) { long a = 1 , b = 0 , c = 0 ; int x = n / 60 ; a = ( long ) Math . pow ( 32 , x ) ; x = 60 * x ; for ( int i = x + 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { b += a ; a = 0 ; } if ( i % 5 == 0 ) { c += b ; b = 0 ; } if ( i % 12 == 0 ) { a += ( 2 * c ) ; c = 0 ; } } System . out . println ( " a ▁ = ▁ " + a + " , ▁ b ▁ = ▁ " + b + " , ▁ c ▁ = ▁ " + c ) ; } public static void main ( String [ ] args ) { int n = 72 ; findCount ( n ) ; } }
Find GCD of factorial of elements of given array | Java implementation of the above approach ; Implementation of factorial function ; Function to find GCD of factorial of elements from array ; find the minimum element of array ; return the factorial of minimum element ; Driver Code
class GFG { static int factorial ( int n ) { return ( n == 1 n == 0 ) ? 1 : factorial ( n - 1 ) * n ; } static int gcdOfFactorial ( int [ ] arr , int n ) { int minm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) minm = minm > arr [ i ] ? arr [ i ] : minm ; return factorial ( minm ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 12 , 122 , 34 , 15 } ; int n = arr . length ; System . out . println ( gcdOfFactorial ( arr , n ) ) ; } }
Sum of the series 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + ... . . + n ^ n using recursion | Java implementation of the approach ; Recursive function to return the sum of the given series ; 1 ^ 1 = 1 ; Recursive call ; Driver code
class GFG { static long sum ( int n ) { if ( n == 1 ) return 1 ; else return ( ( long ) Math . pow ( n , n ) + sum ( n - 1 ) ) ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( sum ( n ) ) ; } }
Count permutations that are first decreasing then increasing . | Java implementation of the above approach ; Function to compute a ^ n % mod ; Function to count permutations that are first decreasing and then increasing ; For n = 1 return 0 ; Calculate and return result ; Driver code
class GFG { static final int mod = 1000000007 ; static long power ( long a , long n ) { if ( n == 0 ) return 1 ; long p = power ( a , n / 2 ) % mod ; p = ( p * p ) % mod ; if ( ( n & 1 ) == 1 ) p = ( p * a ) % mod ; return p ; } static int countPermutations ( int n ) { if ( n == 1 ) { return 0 ; } return ( ( int ) power ( 2 , n - 1 ) - 2 ) % mod ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( countPermutations ( n ) ) ; } }
Find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . | Java program to find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . ; Function to find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . ; Driver code
class GFG { static long numbers ( int n ) { return ( long ) ( Math . pow ( 2 , n + 1 ) ) - 2 ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( numbers ( n ) ) ; } }
Ways to place 4 items in n ^ 2 positions such that no row / column contains more than one | Java implementation of the approach ; Function to return the number of ways to place 4 items in n ^ 2 positions ; Driver code
class GFG { static long NumberofWays ( int n ) { long x = ( 1l * ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) ) / ( 4 * 3 * 2 * 1 ) ; long y = ( 1l * ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) ) ; return ( 1l * x * y ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( NumberofWays ( n ) ) ; } }
Find Nth term of the series 1 , 6 , 18 , 40 , 75 , ... . | Java code to generate ' Nth ' term of this sequence ; Function to generate a fixed number ; ( N ^ 2 * ( N + 1 ) ) / 2 ; Driver Method
class GFG { public static int nthTerm ( int N ) { int nth = 0 ; nth = ( N * N * ( N + 1 ) ) / 2 ; return nth ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( nthTerm ( N ) ) ; } }
Print n numbers such that their sum is a perfect square | Java implementation of the approach ; Function to print n numbers such that their sum is a perfect square ; Print ith odd number ; Driver code
class GFG { static void findNumbers ( int n ) { int i = 1 ; while ( i <= n ) { System . out . print ( ( ( 2 * i ) - 1 ) + " ▁ " ) ; i ++ ; } } public static void main ( String args [ ] ) { int n = 3 ; findNumbers ( n ) ; } }
Missing even and odd elements from the given arrays | Java implementation of the approach ; Function to find the missing numbers ; To store the minimum and the maximum odd and even elements from the arrays ; To store the sum of the array elements ; Get the minimum and the maximum even elements from the array ; Get the minimum and the maximum odd elements from the array ; To store the total terms in the series and the required sum of the array ; Total terms from 2 to minEven ; Sum of all even numbers from 2 to minEven ; Total terms from 2 to maxEven ; Sum of all even numbers from 2 to maxEven ; Required sum for the even array ; Missing even number ; Total terms from 1 to minOdd ; Sum of all odd numbers from 1 to minOdd ; Total terms from 1 to maxOdd ; Sum of all odd numbers from 1 to maxOdd ; Required sum for the odd array ; Missing odd number ; Driver code
class GFG { static void findMissingNums ( int even [ ] , int sizeEven , int odd [ ] , int sizeOdd ) { int minEven = Integer . MAX_VALUE ; int maxEven = Integer . MIN_VALUE ; int minOdd = Integer . MAX_VALUE ; int maxOdd = Integer . MIN_VALUE ; int sumEvenArr = 0 , sumOddArr = 0 ; for ( int i = 0 ; i < sizeEven ; i ++ ) { minEven = Math . min ( minEven , even [ i ] ) ; maxEven = Math . max ( maxEven , even [ i ] ) ; sumEvenArr += even [ i ] ; } for ( int i = 0 ; i < sizeOdd ; i ++ ) { minOdd = Math . min ( minOdd , odd [ i ] ) ; maxOdd = Math . max ( maxOdd , odd [ i ] ) ; sumOddArr += odd [ i ] ; } int totalTerms = 0 , reqSum = 0 ; totalTerms = minEven / 2 ; int evenSumMin = ( totalTerms * ( totalTerms + 1 ) ) ; totalTerms = maxEven / 2 ; int evenSumMax = ( totalTerms * ( totalTerms + 1 ) ) ; reqSum = evenSumMax - evenSumMin + minEven ; System . out . println ( " Even ▁ = ▁ " + ( reqSum - sumEvenArr ) ) ; totalTerms = ( minOdd / 2 ) + 1 ; int oddSumMin = totalTerms * totalTerms ; totalTerms = ( maxOdd / 2 ) + 1 ; int oddSumMax = totalTerms * totalTerms ; reqSum = oddSumMax - oddSumMin + minOdd ; System . out . println ( " Odd ▁ = ▁ " + ( reqSum - sumOddArr ) ) ; } public static void main ( String [ ] args ) { int even [ ] = { 6 , 4 , 8 , 14 , 10 } ; int sizeEven = even . length ; int odd [ ] = { 7 , 5 , 3 , 11 , 13 } ; int sizeOdd = odd . length ; findMissingNums ( even , sizeEven , odd , sizeOdd ) ; } }
Minimum matches the team needs to win to qualify | Java implementation of the approach ; Function to return the minimum number of matches to win to qualify for next round ; Do a binary search to find ; Find mid element ; Check for condition to qualify for next round ; Driver Code
import java . io . * ; class GFG { static int findMinimum ( int x , int y ) { int low = 0 , high = y ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( ( mid * 2 + ( y - mid ) ) >= x ) high = mid - 1 ; else low = mid + 1 ; } return low ; } public static void main ( String [ ] args ) { int x = 6 , y = 5 ; System . out . println ( findMinimum ( x , y ) ) ; } }
Check if product of digits of a number at even and odd places is equal | Java implementation of the approach ; To store the respective product ; Converting integer to String ; Traversing the String ; Driver code
import java . util . * ; class GFG { static void getResult ( int n ) { int proOdd = 1 ; int proEven = 1 ; String num = String . valueOf ( n ) ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) if ( i % 2 == 0 ) proOdd = proOdd * ( num . charAt ( i ) - '0' ) ; else proEven = proEven * ( num . charAt ( i ) - '0' ) ; if ( proOdd == proEven ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int n = 4324 ; getResult ( n ) ; } }
Count of all even numbers in the range [ L , R ] whose sum of digits is divisible by 3 | Java implementation of the approach ; Function to return the sum of digits of x ; Function to return the count of required numbers ; If i is divisible by 2 and sum of digits of i is divisible by 3 ; Return the required count ; Driver code
class GFG { static int sumOfDigits ( int x ) { int sum = 0 ; while ( x != 0 ) { sum += x % 10 ; x = x / 10 ; } return sum ; } static int countNumbers ( int l , int r ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( i % 2 == 0 && sumOfDigits ( i ) % 3 == 0 ) count ++ ; } return count ; } public static void main ( String args [ ] ) { int l = 1000 , r = 6000 ; System . out . println ( countNumbers ( l , r ) ) ; } }
Sum of minimum element of all subarrays of a sorted array | Java implementation of the above approach ; Function to find the sum of minimum of all subarrays ; Driver code
class GfG { static int findMinSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] * ( n - i ) ; return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 , 8 } ; int n = arr . length ; System . out . println ( findMinSum ( arr , n ) ) ; } }
Longest Sub | Java implementation of the approach ; Function to return the max length of the sub - array that have the maximum average ( average value of the elements ) ; Finding the maximum value ; If consecutive maximum found ; Find the max length of consecutive max ; Driver code
class GFG { static int maxLenSubArr ( int a [ ] , int n ) { int count , j ; int cm = 1 , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; } for ( int i = 0 ; i < n - 1 ; ) { count = 1 ; if ( a [ i ] == a [ i + 1 ] && a [ i ] == max ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] == max ) { count ++ ; i ++ ; } else break ; } if ( count > cm ) cm = count ; } else i ++ ; } return cm ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 1 , 6 , 6 , 0 } ; int n = arr . length ; System . out . println ( maxLenSubArr ( arr , n ) ) ; } }
Minimum possible sum of array elements after performing the given operation | Java implementation of the approach ; Function to return the minimized sum ; To store the largest element from the array which is divisible by x ; Sum of array elements before performing any operation ; If current element is divisible by x and it is maximum so far ; Update the minimum element ; If no element can be reduced then there 's no point in performing the  operation as we will end up increasing  the sum when an element is multiplied by x ; Subtract the chosen elements from the sum and then add their updated values ; Return the minimized sum ; Driver code
class GFG { static int minSum ( int arr [ ] , int n , int x ) { int sum = 0 ; int largestDivisible = - 1 , minimum = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] % x == 0 && largestDivisible < arr [ i ] ) largestDivisible = arr [ i ] ; if ( arr [ i ] < minimum ) minimum = arr [ i ] ; } if ( largestDivisible == - 1 ) return sum ; int sumAfterOperation = sum - minimum - largestDivisible + ( x * minimum ) + ( largestDivisible / x ) ; return Math . min ( sum , sumAfterOperation ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 5 , 5 , 5 , 6 } ; int n = arr . length ; int x = 3 ; System . out . println ( minSum ( arr , n , x ) ) ; } }
Maximum Bitwise AND pair from given range | Java implementation of the approach ; Function to return the maximum bitwise AND possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; If there are only two values in the range [ L , R ] ; Driver code
class GfG { static int maxAND ( int L , int R ) { if ( L == R ) return L ; else if ( ( R - L ) == 1 ) return ( R & L ) ; else { if ( ( ( R - 1 ) & R ) > ( ( R - 2 ) & ( R - 1 ) ) ) return ( ( R - 1 ) & R ) ; else return ( ( R - 2 ) & ( R - 1 ) ) ; } } public static void main ( String [ ] args ) { int L = 1 , R = 632 ; System . out . println ( maxAND ( L , R ) ) ; } }