codeparrot/xlcost-text-to-code huggingface dataset:
Average of even numbers till a given even number | Program to find average of even numbers till a given even number . ; Function to calculate the average of even numbers ; count even numbers ; store the sum of even numbers ; driver function
import java . io . * ; class GFG { static int averageEven ( int n ) { if ( n % 2 != 0 ) { System . out . println ( " Invalid ▁ Input " ) ; return - 1 ; } int sum = 0 , count = 0 ; while ( n >= 2 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } public static void main ( String args [ ] ) { int n = 16 ; System . out . println ( averageEven ( n ) ) ; } }
Average of even numbers till a given even number | Program to find average of even numbers till a given even number . ; Function to calculate the average of even numbers ; driver function
import java . io . * ; class GFG { static int averageEven ( int n ) { if ( n % 2 != 0 ) { System . out . println ( " Invalid ▁ Input " ) ; return - 1 ; } return ( n + 2 ) / 2 ; } public static void main ( String args [ ] ) { int n = 16 ; System . out . println ( averageEven ( n ) ) ; } }
Largest number that divides x and is co | java program to find the Largest Coprime Divisor ; Recursive function to return gcd of a and b ; Everything divides 0 ; base case ; a is greater ; function to find largest coprime divisor ; divisor code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } static int cpFact ( int x , int y ) { while ( gcd ( x , y ) != 1 ) { x = x / gcd ( x , y ) ; } return x ; } public static void main ( String [ ] args ) { int x = 15 ; int y = 3 ; System . out . println ( cpFact ( x , y ) ) ; x = 14 ; y = 28 ; System . out . println ( cpFact ( x , y ) ) ; x = 7 ; y = 3 ; System . out . println ( cpFact ( x , y ) ) ; } }
Count numbers with unit digit k in given range | Simple Java program to count numbers with last digit as k in given range . ; Returns count of numbers with k as last digit . ; driver function
import java . util . * ; import java . lang . * ; public class GfG { public static int counLastDigitK ( int low , int high , int k ) { int count = 0 ; for ( int i = low ; i <= high ; i ++ ) if ( i % 10 == k ) count ++ ; return count ; } public static void main ( String args [ ] ) { int low = 3 , high = 35 , k = 3 ; System . out . println ( counLastDigitK ( low , high , k ) ) ; } }
Taxicab Numbers | JAVA Code for Taxicab Numbers ; Starting from 1 , check every number if it is Taxicab until count reaches N . ; Try all possible pairs ( j , k ) whose cube sums can be i . ; Taxicab ( 2 ) found ; Driver program to test above function
import java . util . * ; class GFG { public static void printTaxicab2 ( int N ) { int i = 1 , count = 0 ; while ( count < N ) { int int_count = 0 ; for ( int j = 1 ; j <= Math . pow ( i , 1.0 / 3 ) ; j ++ ) for ( int k = j + 1 ; k <= Math . pow ( i , 1.0 / 3 ) ; k ++ ) if ( j * j * j + k * k * k == i ) int_count ++ ; if ( int_count == 2 ) { count ++ ; System . out . println ( count + " ▁ " + i ) ; } i ++ ; } } public static void main ( String [ ] args ) { int N = 5 ; printTaxicab2 ( N ) ; } }
Composite Number | / An optimized method based Java program to check if a number is Composite or not . ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Program to test above function
import java . io . * ; class Composite { static boolean isComposite ( int n ) { if ( n <= 1 ) System . out . println ( " False " ) ; if ( n <= 3 ) System . out . println ( " False " ) ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } public static void main ( String args [ ] ) { System . out . println ( isComposite ( 11 ) ? " true " : " false " ) ; System . out . println ( isComposite ( 15 ) ? " true " : " false " ) ; } }
Insert minimum number in array so that sum of array becomes prime | Java program to find minimum number to insert in array so their sum is prime ; function to check if a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Find prime number greater than a number ; find prime greater than n ; check if num is prime ; increment num ; To find number to be added so sum of array is prime ; To find sum of array elements ; if sum is already prime return 0 ; To find prime number greater than sum ; Return difference of sum and num ; Driver Code
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int findPrime ( int n ) { int num = n + 1 ; while ( num > 0 ) { if ( isPrime ( num ) ) return num ; num = num + 1 ; } return 0 ; } static int minNumber ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( isPrime ( sum ) ) return 0 ; int num = findPrime ( sum ) ; return num - sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 12 } ; int n = arr . length ; System . out . println ( minNumber ( arr , n ) ) ; } }
Sum of divisors of factorial of a number | Java program to find sum of proper divisor of factorial of a number ; function to calculate factorial ; function to calculate sum of divisor ; Returns sum of divisors of n ! ; driver program
import java . io . * ; import java . util . * ; public class Division { static int fact ( int n ) { if ( n == 0 ) return 1 ; return n * fact ( n - 1 ) ; } static int div ( int x ) { int ans = 0 ; for ( int i = 1 ; i <= x ; i ++ ) if ( x % i == 0 ) ans += i ; return ans ; } static int sumFactDiv ( int n ) { return div ( fact ( n ) ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( sumFactDiv ( n ) ) ; } }
Sum of divisors of factorial of a number | Java program to find sum of divisors in n ! ; allPrimes [ ] stores all prime numbers less than or equal to n . ; Fills above vector allPrimes [ ] for a given n ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; Loop to update prime [ ] ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store primes in the vector allPrimes ; Function to find all result of factorial number ; Initialize result ; find exponents of all primes which divides n and less than n ; Current divisor ; Find the highest power ( stored in exp ) ' ▁ ▁ of ▁ allPrimes [ i ] ▁ that ▁ divides ▁ n ▁ using ▁ ▁ Legendre ' s formula . ; Using the divisor function to calculate the sum ; return total divisors ; Driver program to run the cases
import java . util . * ; class GFG { static ArrayList < Integer > allPrimes = new ArrayList < Integer > ( ) ; static void sieve ( int n ) { boolean [ ] prime = new boolean [ n + 1 ] ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = true ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] == false ) allPrimes . add ( p ) ; } static int factorialDivisors ( int n ) { int result = 1 ; for ( int i = 0 ; i < allPrimes . size ( ) ; i ++ ) { int p = allPrimes . get ( i ) ; int exp = 0 ; while ( p <= n ) { exp = exp + ( n / p ) ; p = p * allPrimes . get ( i ) ; } result = result * ( ( int ) Math . pow ( allPrimes . get ( i ) , exp + 1 ) - 1 ) / ( allPrimes . get ( i ) - 1 ) ; } return result ; } public static void main ( String [ ] args ) { System . out . println ( factorialDivisors ( 4 ) ) ; } }
Pandigital number in a given base | Java program to check if a number is pandigital in given base . ; Return true if n is pandigit else return false . ; Checking length is less than base ; Traversing each digit of the number . ; If digit is integer ; If digit is alphabet ; Checking hash array , if any index is unmarked . ; Driver code
import java . util . * ; class GFG { static boolean checkPandigital ( int b , String n ) { if ( n . length ( ) < b ) return false ; boolean hash [ ] = new boolean [ b ] ; Arrays . fill ( hash , false ) ; for ( int i = 0 ; i < n . length ( ) ; i ++ ) { if ( n . charAt ( i ) >= '0' && n . charAt ( i ) <= '9' ) hash [ n . charAt ( i ) - '0' ] = true ; else if ( n . charAt ( i ) - ' A ' <= b - 11 ) hash [ n . charAt ( i ) - ' A ' + 10 ] = true ; } for ( int i = 0 ; i < b ; i ++ ) if ( hash [ i ] == false ) return false ; return true ; } public static void main ( String [ ] args ) { int b = 13 ; String n = "1298450376ABC " ; if ( checkPandigital ( b , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Convert a number m to n using minimum number of given operations | Java implementation to convert a number m to n using minimum number of given operations ; function to find minimum number of given operations to convert m to n ; only way is to do - 1 ( m - n ) times ; not possible ; n is greater and n is odd ; perform ' - 1' on m ( or + 1 on n ) ; n is even ; perform ' * 2' on m ( or n / 2 on n ) ; Driver Code
class ConvertNum { static int convert ( int m , int n ) { if ( m == n ) return 0 ; if ( m > n ) return m - n ; if ( m <= 0 && n > 0 ) return - 1 ; if ( n % 2 == 1 ) return 1 + convert ( m , n + 1 ) ; else return 1 + convert ( m , n / 2 ) ; } public static void main ( String [ ] args ) { int m = 3 , n = 11 ; System . out . println ( " Minimum ▁ number ▁ of ▁ " + " operations ▁ : ▁ " + convert ( m , n ) ) ; } }
Seeds ( Or Seed Roots ) of a number | Java program to find Seed of a number ; Stores product of digits of x in prodDig [ x ] ; If x has single digit ; If digit product is already computed ; If digit product is not computed before . ; Prints all seeds of n ; Find all seeds using prodDig [ ] ; If there was no seed ; Print seeds ; Driver code
import java . util . * ; class GFg { static int MAX = 10000 ; static int [ ] prodDig = new int [ MAX ] ; static int getDigitProduct ( int x ) { if ( x < 10 ) return x ; if ( prodDig [ x ] != 0 ) return prodDig [ x ] ; int prod = ( x % 10 ) * getDigitProduct ( x / 10 ) ; return ( prodDig [ x ] = prod ) ; } static void findSeed ( int n ) { List < Integer > res = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i <= n / 2 ; i ++ ) if ( i * getDigitProduct ( i ) == n ) res . add ( i ) ; if ( res . size ( ) == 0 ) { System . out . println ( " NO ▁ seed ▁ exists " ) ; return ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 138 ; findSeed ( n ) ; } }
Number with maximum number of prime factors | Java program to find integer having maximum number of prime factor in first N natural numbers . ; Return smallest number having maximum prime factors . ; Sieve of eratosthenes method to count number of prime factors . ; Finding number having maximum number of prime factor . ; Driver program
import java . util . Arrays ; public class GFG { static int maxPrimefactorNum ( int N ) { int arr [ ] = new int [ N + 5 ] ; Arrays . fill ( arr , 0 ) ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) { for ( int j = 2 * i ; j <= N ; j += i ) { arr [ j ] ++ ; } } arr [ i ] = 1 ; } int maxval = 0 , maxint = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > maxval ) { maxval = arr [ i ] ; maxint = i ; } } return maxint ; } public static void main ( String [ ] args ) { int N = 40 ; System . out . println ( maxPrimefactorNum ( N ) ) ; } }
Sum of all Subarrays | Set 1 | Efficient Java program to compute sum of subarray elements ; function compute sum all sub - array ; computing sum of subarray using formula ; return all subarray sum ; Driver program to test above function
class GFG { public static long SubArraySum ( int arr [ ] , int n ) { long result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( arr [ i ] * ( i + 1 ) * ( n - i ) ) ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( " Sum ▁ of ▁ SubArray ▁ " + SubArraySum ( arr , n ) ) ; } }
Highest power of 2 less than or equal to given number | Java program to find highest power of 2 smaller than or equal to n . ; If i is a power of 2 ; Driver code
class GFG { static int highestPowerof2 ( int n ) { int res = 0 ; for ( int i = n ; i >= 1 ; i -- ) { if ( ( i & ( i - 1 ) ) == 0 ) { res = i ; break ; } } return res ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . print ( highestPowerof2 ( n ) ) ; } }
Find Cube Pairs | Set 2 ( A n ^ ( 1 / 3 ) Solution ) | Java program to find pairs that can represent the given number as sum of two cubes ; Function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create a array of size of size ' cubeRoot ' ; for index i , cube [ i ] will contain i ^ 3 ; Find all pairs in above sorted array cube [ ] whose sum is equal to n ; Driver Code
import java . io . * ; class GFG { static void findPairs ( int n ) { int cubeRoot = ( int ) Math . pow ( n , 1.0 / 3.0 ) ; int cube [ ] = new int [ cubeRoot + 1 ] ; for ( int i = 1 ; i <= cubeRoot ; i ++ ) cube [ i ] = i * i * i ; int l = 1 ; int r = cubeRoot ; while ( l < r ) { if ( cube [ l ] + cube [ r ] < n ) l ++ ; else if ( cube [ l ] + cube [ r ] > n ) r -- ; else { System . out . println ( " ( " + l + " , ▁ " + r + " ) " ) ; l ++ ; r -- ; } } } public static void main ( String [ ] args ) { int n = 20683 ; findPairs ( n ) ; } }
Find Cube Pairs | Set 1 ( A n ^ ( 2 / 3 ) Solution ) | Java program to find pairs that can represent the given number as sum of two cubes ; Function to find pairs that can represent the given number as sum of two cubes ; find cube root of n ; create an empty map ; Consider all pairs such with values less than cuberoot ; find sum of current pair ( x , y ) ; do nothing if sum is not equal to given number ; if sum is seen before , we found two pairs ; if sum is seen for the first time ; Driver code
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 findPairs ( int n ) { int cubeRoot = ( int ) Math . pow ( n , 1.0 / 3.0 ) ; HashMap < Integer , pair > s = new HashMap < Integer , pair > ( ) ; for ( int x = 1 ; x < cubeRoot ; x ++ ) { for ( int y = x + 1 ; y <= cubeRoot ; y ++ ) { int sum = x * x * x + y * y * y ; if ( sum != n ) continue ; if ( s . containsKey ( sum ) ) { System . out . print ( " ( " + s . get ( sum ) . first + " , ▁ " + s . get ( sum ) . second + " ) ▁ and ▁ ( " + x + " , ▁ " + y + " ) " + "NEW_LINE"); } else s . put ( sum , new pair ( x , y ) ) ; } } } public static void main ( String [ ] args ) { int n = 13832 ; findPairs ( n ) ; } }
Find the minimum difference between Shifted tables of two numbers | Java program to find the minimum difference between any two terms of two tables ; Utility function to find GCD of a and b ; Returns minimum difference between any two terms of shifted tables of ' a ' and ' b ' . ' x ' is shift in table of ' a ' and ' y ' is shift in table of ' b ' . ; Calculate gcd of a nd b ; Calculate difference between x and y ; Driver Code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { while ( b != 0 ) { int t = b ; b = a % b ; a = t ; } return a ; } static int findMinDiff ( int a , int b , int x , int y ) { int g = gcd ( a , b ) ; int diff = Math . abs ( x - y ) % g ; return Math . min ( diff , g - diff ) ; } public static void main ( String [ ] args ) { int a = 20 , b = 52 , x = 5 , y = 7 ; System . out . println ( findMinDiff ( a , b , x , y ) ) ; } }
Find all divisors of a natural number | Set 2 | A O ( sqrt ( n ) ) java program that prints all divisors in sorted order ; method to print the divisors ; Vector to store half of the divisors ; check if divisors are equal ; push the second divisor in the vector ; The vector will be printed in reverse ; Driver method
import java . util . Vector ; class Test { static void printDivisors ( int n ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) System . out . printf ( " % d ▁ " , i ) ; else { System . out . printf ( " % d ▁ " , i ) ; v . add ( n / i ) ; } } } for ( int i = v . size ( ) - 1 ; i >= 0 ; i -- ) System . out . printf ( " % d ▁ " , v . get ( i ) ) ; } public static void main ( String args [ ] ) { System . out . println ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ " ) ; printDivisors ( 100 ) ; } }
Find all divisors of a natural number | Set 2 | A O ( sqrt ( n ) ) program that prints all divisors in sorted order ; Function to print the divisors ; Driver code
import java . lang . Math ; class GFG { public static void printDivisors ( int n ) { int i ; for ( i = 1 ; i * i < n ; i ++ ) { if ( n % i == 0 ) System . out . print ( i + " ▁ " ) ; } if ( i - ( n / i ) == 1 ) { i -- ; } for ( ; i >= 1 ; i -- ) { if ( n % i == 0 ) System . out . print ( n / i + " ▁ " ) ; } } public static void main ( String [ ] args ) { System . out . println ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ " ) ; printDivisors ( 100 ) ; } }
Find all factors of a natural number | Set 1 | Java implementation of Naive method to print all divisors ; method to print the divisors ; Driver method
class Test { static void printDivisors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) if ( n % i == 0 ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { System . out . println ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ " ) ; printDivisors ( 100 ) ; ; } }
Find all factors of a natural number | Set 1 | A Better ( than Naive ) Solution to find all divisors ; method to print the divisors ; Note that this loop runs till square root ; If divisors are equal , print only one ; else Otherwise print both ; Driver method
class Test { static void printDivisors ( int n ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) System . out . print ( " ▁ " + i ) ; System . out . print ( i + " ▁ " + n / i + " ▁ " ) ; } } } public static void main ( String args [ ] ) { System . out . println ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ " ) ; printDivisors ( 100 ) ; ; } }
Sieve of Atkin | Java program for implementation of Sieve of Atkin ; 2 and 3 are known to be prime ; Initialise the sieve array with false values ; Mark sieve [ n ] is true if one of the following is true : a ) n = ( 4 * x * x ) + ( y * y ) has odd number of solutions , i . e . , there exist odd number of distinct pairs ( x , y ) that satisfy the equation and n % 12 = 1 or n % 12 = 5. b ) n = ( 3 * x * x ) + ( y * y ) has odd number of solutions and n % 12 = 7 c ) n = ( 3 * x * x ) - ( y * y ) has odd number of solutions , x > y and n % 12 = 11 ; Main part of Sieve of Atkin ; Mark all multiples of squares as non - prime ; Print primes using sieve [ ] ; Driver code
class GFG { static int SieveOfAtkin ( int limit ) { if ( limit > 2 ) System . out . print ( 2 + " ▁ " ) ; if ( limit > 3 ) System . out . print ( 3 + " ▁ " ) ; boolean sieve [ ] = new boolean [ limit ] ; for ( int i = 0 ; i < limit ; i ++ ) sieve [ i ] = false ; for ( int x = 1 ; x * x < limit ; x ++ ) { for ( int y = 1 ; y * y < limit ; y ++ ) { int n = ( 4 * x * x ) + ( y * y ) ; if ( n <= limit && ( n % 12 == 1 n % 12 == 5 ) ) sieve [ n ] ^= true ; n = ( 3 * x * x ) + ( y * y ) ; if ( n <= limit && n % 12 == 7 ) sieve [ n ] ^= true ; n = ( 3 * x * x ) - ( y * y ) ; if ( x > y && n <= limit && n % 12 == 11 ) sieve [ n ] ^= true ; } } for ( int r = 5 ; r * r < limit ; r ++ ) { if ( sieve [ r ] ) { for ( int i = r * r ; i < limit ; i += r * r ) sieve [ i ] = false ; } } for ( int a = 5 ; a < limit ; a ++ ) if ( sieve [ a ] ) System . out . print ( a + " ▁ " ) ; return 0 ; } public static void main ( String [ ] args ) { int limit = 20 ; SieveOfAtkin ( limit ) ; } }
Find if a point lies inside a Circle | Java program to check if a point lies inside a circle or not ; Compare radius of circle with distance of its center from given point ; Driver Program to test above function
class GFG { static boolean isInside ( int circle_x , int circle_y , int rad , int x , int y ) { if ( ( x - circle_x ) * ( x - circle_x ) + ( y - circle_y ) * ( y - circle_y ) <= rad * rad ) return true ; else return false ; } public static void main ( String arg [ ] ) { int x = 1 , y = 1 ; int circle_x = 0 , circle_y = 1 , rad = 2 ; if ( isInside ( circle_x , circle_y , rad , x , y ) ) System . out . print ( " Inside " ) ; else System . out . print ( " Outside " ) ; } }
Find all possible outcomes of a given expression | Java program to evaluate all possible values of a expression ; Utility function to evaluate a simple expression with one operator only . ; This function evaluates all possible values and returns a list of evaluated values . ; To store result ( all possible evaluations of given expression ' expr ' ) ; If there is only one character , it must be a digit ( or operand ) , return it . ; If there are only three characters , middle one must be operator and corner ones must be operand ; every i refers to an operator ; l refers to all the possible values in the left of operator ' expr [ i ] ' ; r refers to all the possible values in the right of operator ' expr [ i ] ' ; Take above evaluated all possible values in left side of ' i ' ; Take above evaluated all possible values in right side of ' i ' ; Calculate value for every pair and add the value to result . ; Driver program
import java . util . * ; class GFG { static int eval ( int a , char op , int b ) { if ( op == ' + ' ) { return a + b ; } if ( op == ' - ' ) { return a - b ; } if ( op == ' * ' ) { return a * b ; } return Integer . MAX_VALUE ; } static Vector < Integer > evaluateAll ( String expr , int low , int high ) { Vector < Integer > res = new Vector < Integer > ( ) ; if ( low == high ) { res . add ( expr . charAt ( low ) - '0' ) ; return res ; } if ( low == ( high - 2 ) ) { int num = eval ( expr . charAt ( low ) - '0' , expr . charAt ( low + 1 ) , expr . charAt ( low + 2 ) - '0' ) ; res . add ( num ) ; return res ; } for ( int i = low + 1 ; i <= high ; i += 2 ) { Vector < Integer > l = evaluateAll ( expr , low , i - 1 ) ; Vector < Integer > r = evaluateAll ( expr , i + 1 , high ) ; for ( int s1 = 0 ; s1 < l . size ( ) ; s1 ++ ) { for ( int s2 = 0 ; s2 < r . size ( ) ; s2 ++ ) { int val = eval ( l . get ( s1 ) , expr . charAt ( i ) , r . get ( s2 ) ) ; res . add ( val ) ; } } } return res ; } public static void main ( String [ ] args ) { String expr = "1*2 + 3*4" ; int len = expr . length ( ) ; Vector < Integer > ans = evaluateAll ( expr , 0 , len - 1 ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . println ( ans . get ( i ) ) ; } } }
Program to check if a given number is Lucky ( all digits are different ) | Java program to check if a given number is lucky ; This function returns true if n is lucky ; Create an array of size 10 and initialize all elements as false . This array is used to check if a digit is already seen or not . ; Traverse through all digits of given number ; Find the last digit ; If digit is already seen , return false ; Mark this digit as seen ; Remove the last digit from number ; Driver code
class GFG { static boolean isLucky ( int n ) { boolean arr [ ] = new boolean [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) arr [ i ] = false ; while ( n > 0 ) { int digit = n % 10 ; if ( arr [ digit ] ) return false ; arr [ digit ] = true ; n = n / 10 ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1291 , 897 , 4566 , 1232 , 80 , 700 } ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) if ( isLucky ( arr [ i ] ) ) System . out . print ( arr [ i ] + " is Lucky NEW_LINE"); else System . out . print ( arr [ i ] + " is not Lucky NEW_LINE"); } }