codeparrot/xlcost-text-to-code huggingface dataset:
Find the Nth term of the series 14 , 28 , 20 , 40 , ... . . | Java program to find the Nth term of the series 14 , 28 , 20 , 40 , ; Function to find the N - th term ; initializing the 1 st number ; loop from 2 nd term to nth term ; if i is even , double the previous number ; if i is odd , subtract 8 from previous number ; Driver Code
import java . io . * ; class GFG { static int findNth ( int N ) { int b = 14 ; int i ; for ( i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) b = b * 2 ; else b = b - 8 ; } return b ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( findNth ( N ) ) ; } }
Problem of 8 Neighbours of an element in a 2 | Java implementation of the approach ; Dimension of Array ; Count of 1 s ; Counting all neighbouring 1 s ; Comparing the number of neighbouring 1 s with given ranges ; Copying changes to the main matrix ; Driver code ; Function call to calculate the resultant matrix after ' K ' iterations . ; Printing Result
public class GFG { final static int N = 4 ; static void predictMatrix ( int arr [ ] [ ] , int range1a , int range1b , int range0a , int range0b , int K , int b [ ] [ ] ) { int c = 0 ; while ( K != 0 ) { K -- ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { c = 0 ; if ( i > 0 && arr [ i - 1 ] [ j ] == 1 ) c ++ ; if ( j > 0 && arr [ i ] [ j - 1 ] == 1 ) c ++ ; if ( i > 0 && j > 0 && arr [ i - 1 ] [ j - 1 ] == 1 ) c ++ ; if ( i < N - 1 && arr [ i + 1 ] [ j ] == 1 ) c ++ ; if ( j < N - 1 && arr [ i ] [ j + 1 ] == 1 ) c ++ ; if ( i < N - 1 && j < N - 1 && arr [ i + 1 ] [ j + 1 ] == 1 ) c ++ ; if ( i < N - 1 && j > 0 && arr [ i + 1 ] [ j - 1 ] == 1 ) c ++ ; if ( i > 0 && j < N - 1 && arr [ i - 1 ] [ j + 1 ] == 1 ) c ++ ; if ( arr [ i ] [ j ] == 1 ) { if ( c >= range1a && c <= range1b ) b [ i ] [ j ] = 1 ; else b [ i ] [ j ] = 0 ; } if ( arr [ i ] [ j ] == 0 ) { if ( c >= range0a && c <= range0b ) b [ i ] [ j ] = 1 ; else b [ i ] [ j ] = 0 ; } } } for ( int k = 0 ; k < N ; k ++ ) for ( int m = 0 ; m < N ; m ++ ) arr [ k ] [ m ] = b [ k ] [ m ] ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 } } ; int range1a = 2 , range1b = 2 ; int range0a = 2 , range0b = 3 ; int K = 3 ; int b [ ] [ ] = new int [ N ] [ N ] ; predictMatrix ( arr , range1a , range1b , range0a , range0b , K , b ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . println ( ) ; for ( int j = 0 ; j < N ; j ++ ) System . out . print ( b [ i ] [ j ] + " ▁ " ) ; } } }
Number of moves required to guess a permutation . | Java implementation of the approach ; Function that returns the required moves ; Final move ; Driver Program to test above function
import java . io . * ; class GFG { static int countMoves ( int n ) { int ct = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ct += i * ( n - i ) ; ct += n ; return ct ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countMoves ( n ) ) ; } }
Count number of triplets ( a , b , c ) such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n | Java implementation of the approach ; Function to return an ArrayList containing all the perfect squares upto n ; while current perfect square is less than or equal to n ; Function to return the count of triplet ( a , b , c ) pairs such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n ; List of perfect squares upto n ^ 2 ; Since , a ^ 2 + b ^ 2 = c ^ 2 ; If c < a or bSquare is not a perfect square ; If triplet pair ( a , b , c ) satisfy the given condition ; Driver code
import java . util . * ; public class GFG { public static ArrayList < Integer > getPerfectSquares ( int n ) { ArrayList < Integer > perfectSquares = new ArrayList < > ( ) ; int current = 1 , i = 1 ; while ( current <= n ) { perfectSquares . add ( current ) ; current = ( int ) Math . pow ( ++ i , 2 ) ; } return perfectSquares ; } public static int countTriplets ( int n ) { ArrayList < Integer > perfectSquares = getPerfectSquares ( ( int ) Math . pow ( n , 2 ) ) ; int count = 0 ; for ( int a = 1 ; a <= n ; a ++ ) { int aSquare = ( int ) Math . pow ( a , 2 ) ; for ( int i = 0 ; i < perfectSquares . size ( ) ; i ++ ) { int cSquare = perfectSquares . get ( i ) ; int bSquare = cSquare - aSquare ; int b = ( int ) Math . sqrt ( bSquare ) ; int c = ( int ) Math . sqrt ( cSquare ) ; if ( c < a || ! perfectSquares . contains ( bSquare ) ) continue ; if ( ( b >= a ) && ( b <= c ) && ( aSquare + bSquare == cSquare ) ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( countTriplets ( n ) ) ; } }
Count Numbers with N digits which consists of even number of 0 ’ s | Java program to count numbers with N digits which consists of odd number of 0 's ; Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static double countNumbers ( int N ) { return ( Math . pow ( 10 , N ) - 1 ) - ( Math . pow ( 10 , N ) - Math . pow ( 8 , N ) ) / 2 ; } static public void main ( String args [ ] ) { int n = 2 ; System . out . println ( countNumbers ( n ) ) ; } }
Find determinant of matrix generated by array rotation | Java program for finding determinant of generated matrix ; Function to calculate determinant ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int N = 3 ; static double calcDeterminant ( int arr [ ] ) { double determinant = 0 ; for ( int i = 0 ; i < N ; i ++ ) { determinant += Math . pow ( arr [ i ] , 3 ) ; } determinant -= 3 * arr [ 0 ] * arr [ 1 ] * arr [ 2 ] ; return determinant ; } static public void main ( String args [ ] ) { int [ ] arr = { 4 , 5 , 3 } ; System . out . println ( calcDeterminant ( arr ) ) ; } }
Minimum elements to be added in a range so that count of elements is divisible by K | Java implementation of the approach ; Total elements in the range ; If total elements are already divisible by k ; Value that must be added to count in order to make it divisible by k ; Driver Program to test above function
import java . io . * ; class GFG { static int minimumMoves ( int k , int l , int r ) { int count = r - l + 1 ; if ( count % k == 0 ) return 0 ; return ( k - ( count % k ) ) ; } public static void main ( String [ ] args ) { int k = 3 , l = 10 , r = 10 ; System . out . print ( minimumMoves ( k , l , r ) ) ; } }
Sum of all even numbers in range L and R | Java program to print the sum of all even numbers in range L and R ; Function to return the sum of all natural numbers ; Function to return sum of even numbers in range L and R ; Driver Code
import java . io . * ; class GFG { static int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) ; return sum ; } static int sumEven ( int l , int r ) { return sumNatural ( r / 2 ) - sumNatural ( ( l - 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int l = 2 , r = 5 ; System . out . println ( " Sum ▁ of ▁ Natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " + sumEven ( l , r ) ) ; } }
Check if N is divisible by a number which is composed of the digits from the set { A , B } | Java program to find if number N is divisible by a number that contains only a and b as it 's digits ; Function to check whether n is divisible by a number whose digits are either a or b ; base condition ; recursive call ; Check for all numbers beginning with ' a ' or ' b ' ; Driver program
import java . util . * ; class solution { static boolean isDivisibleRec ( int x , int a , int b , int n ) { if ( x > n ) return false ; if ( n % x == 0 ) return true ; return ( isDivisibleRec ( x * 10 + a , a , b , n ) || isDivisibleRec ( x * 10 + b , a , b , n ) ) ; } static boolean isDivisible ( int a , int b , int n ) { return isDivisibleRec ( a , a , b , n ) || isDivisibleRec ( b , a , b , n ) ; } public static void main ( String args [ ] ) { int a = 3 , b = 5 , n = 53 ; if ( isDivisible ( a , b , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Minimum number of moves required to reach the destination by the king in a chess board | Java program to Find the minimum number of moves required to reach the destination by the king in a chess board ; function to Find the minimum number of moves required to reach the destination by the king in a chess board ; minimum number of steps ; while the king is not in the same row or column as the destination ; Go up ; Go down ; Go left ; Go right ; Driver code
import java . io . * ; class GFG { static void MinSteps ( int SourceX , int SourceY , int DestX , int DestY ) { System . out . println ( Math . max ( Math . abs ( SourceX - DestX ) , Math . abs ( SourceY - DestY ) ) ) ; while ( ( SourceX != DestX ) || ( SourceY != DestY ) ) { if ( SourceX < DestX ) { System . out . print ( ' U ' ) ; SourceX ++ ; } if ( SourceX > DestX ) { System . out . println ( ' D ' ) ; SourceX -- ; } if ( SourceY > DestY ) { System . out . print ( ' L ' ) ; SourceY -- ; } if ( SourceY < DestY ) { System . out . print ( ' R ' ) ; SourceY ++ ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int sourceX = 4 , sourceY = 4 ; int destinationX = 7 , destinationY = 0 ; MinSteps ( sourceX , sourceY , destinationX , destinationY ) ; } }
Count of pairs in an array whose sum is a perfect square | Java implementation of the approach ; Function to return an ArrayList containing all the perfect squares upto n ; while current perfect square is less than or equal to n ; Function to print the sum of maximum two elements from the array ; Function to return the count of numbers that when added with n give a perfect square ; temp > n is checked so that pairs ( x , y ) and ( y , x ) don 't get counted twice ; Function to count the pairs whose sum is a perfect square ; Sum of the maximum two elements from the array ; List of perfect squares upto max ; Contains all the array elements ; Add count of the elements that when added with arr [ i ] give a perfect square ; Driver code
import java . util . * ; public class GFG { public static ArrayList < Integer > getPerfectSquares ( int n ) { ArrayList < Integer > perfectSquares = new ArrayList < > ( ) ; int current = 1 , i = 1 ; while ( current <= n ) { perfectSquares . add ( current ) ; current = ( int ) Math . pow ( ++ i , 2 ) ; } return perfectSquares ; } public static int maxPairSum ( int arr [ ] ) { int n = arr . length ; int max , secondMax ; if ( arr [ 0 ] > arr [ 1 ] ) { max = arr [ 0 ] ; secondMax = arr [ 1 ] ; } else { max = arr [ 1 ] ; secondMax = arr [ 0 ] ; } for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } } return ( max + secondMax ) ; } public static int countPairsWith ( int n , ArrayList < Integer > perfectSquares , HashSet < Integer > nums ) { int count = 0 ; for ( int i = 0 ; i < perfectSquares . size ( ) ; i ++ ) { int temp = perfectSquares . get ( i ) - n ; if ( temp > n && nums . contains ( temp ) ) count ++ ; } return count ; } public static int countPairs ( int arr [ ] ) { int i , n = arr . length ; int max = maxPairSum ( arr ) ; ArrayList < Integer > perfectSquares = getPerfectSquares ( max ) ; HashSet < Integer > nums = new HashSet < > ( ) ; for ( i = 0 ; i < n ; i ++ ) nums . add ( arr [ i ] ) ; int count = 0 ; for ( i = 0 ; i < n ; i ++ ) { count += countPairsWith ( arr [ i ] , perfectSquares , nums ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 6 , 9 , 10 , 20 } ; System . out . println ( countPairs ( arr ) ) ; } }
Element equal to the sum of all the remaining elements | Java implementation of the above approach ; Function to find the element ; sum is use to store sum of all elements of array ; iterate over all elements ; Driver code
import java . io . * ; class GFG { static int findEle ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == sum - arr [ i ] ) return arr [ i ] ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 6 } ; int n = arr . length ; System . out . print ( findEle ( arr , n ) ) ; } }
Sum of all natural numbers in range L to R | Java program to print the sum of all numbers in range L and R ; Function to return the sum of all natural numbers ; Function to return the sum of all numbers in range L and R ; Driver Code
class GFG { static int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } static int suminRange ( int l , int r ) { return sumNatural ( r ) - sumNatural ( l - 1 ) ; } public static void main ( String [ ] args ) { int l = 2 , r = 5 ; System . out . println ( " Sum ▁ of ▁ Natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " + suminRange ( l , r ) ) ; } }
Check if a large number is divisible by 75 or not | Java implementation to check the number is divisible by 75 or not ; check divisibleBy3 ; to store sum of Digit ; traversing through each digit ; summing up Digit ; check if sumOfDigit is divisibleBy3 ; check divisibleBy25 ; if a single digit number ; length of the number ; taking the last two digit ; checking if the lastTwo digit is divisibleBy25 ; Function to check divisibleBy75 or not ; check if divisibleBy3 and divisibleBy25 ; Drivers code ; divisible ; if divisibleBy75
import java . io . * ; class GFG { static boolean divisibleBy3 ( String number ) { int sumOfDigit = 0 ; for ( int i = 0 ; i < number . length ( ) ; i ++ ) sumOfDigit += number . charAt ( i ) - '0' ; if ( sumOfDigit % 3 == 0 ) return true ; return false ; } static boolean divisibleBy25 ( String number ) { if ( number . length ( ) < 2 ) return false ; int length = number . length ( ) ; int lastTwo = ( number . charAt ( length - 2 ) - '0' ) * 10 + ( number . charAt ( length - 1 ) - '0' ) ; if ( lastTwo % 25 == 0 ) return true ; return false ; } static boolean divisibleBy75 ( String number ) { if ( divisibleBy3 ( number ) && divisibleBy25 ( number ) ) return true ; return false ; } public static void main ( String [ ] args ) { String number = "754586672150" ; boolean divisible = divisibleBy75 ( number ) ; if ( divisible ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the other number when LCM and HCF given | Java program to find other number from given HCF and LCM ; Function that will calculates the zeroes at the end ; Driver code ; Calling function .
class GFG { static int otherNumber ( int A , int Lcm , int Hcf ) { return ( Lcm * Hcf ) / A ; } public static void main ( String args [ ] ) { int A = 8 , Lcm = 8 , Hcf = 1 ; int result = otherNumber ( A , Lcm , Hcf ) ; System . out . println ( " B ▁ = ▁ " + result ) ; } }
Overall percentage change from successive changes | Java implementation of above approach ; Calculate successive change of 1 st 2 change ; Calculate successive change for rest of the value ; Driver code ; Calling function
import java . io . * ; class GFG { static float successiveChange ( int arr [ ] , int N ) { float var1 , var2 , result = 0 ; var1 = arr [ 0 ] ; var2 = arr [ 1 ] ; result = var1 + var2 + ( ( var1 * var2 ) / 100 ) ; for ( int i = 2 ; i < N ; i ++ ) result = result + arr [ i ] + ( ( result * arr [ i ] ) / 100 ) ; return result ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 30 , 10 } ; int N = arr . length ; float result = successiveChange ( arr , N ) ; System . out . println ( " Percentage ▁ change ▁ is ▁ = ▁ " + result + " ▁ % " ) ; } }
Minimum numbers ( smaller than or equal to N ) with sum S | Java program to find the minimum numbers required to get to S ; Function to find the minimum numbers required to get to S ; Driver Code
import java . io . * ; class GFG { static int minimumNumbers ( int n , int s ) { if ( ( s % n ) > 0 ) return s / n + 1 ; else return s / n ; } public static void main ( String [ ] args ) { int n = 5 ; int s = 11 ; System . out . println ( minimumNumbers ( n , s ) ) ; } }
Sum of multiples of A and B less than N | Java program to find the sum of all multiples of A and B below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of A and B below N ; Since , we need the sum of multiples less than N ; common factors of A and B ; Driver code
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int sumAP ( int n , int d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } static int sumMultiples ( int A , int B , int n ) { n -- ; int common = ( A * B ) / __gcd ( A , B ) ; return sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ; } public static void main ( String [ ] args ) { int n = 100 , A = 5 , B = 10 ; System . out . println ( " Sum ▁ = ▁ " + sumMultiples ( A , B , n ) ) ; } }
Check if a prime number can be expressed as sum of two Prime Numbers | Java program to check if a prime number can be expressed as sum of two Prime Numbers ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; if the number is prime , and number - 2 is also prime ; Driver code
public class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static boolean isPossible ( int N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 13 ; if ( isPossible ( n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count pairs from two arrays whose modulo operation yields K | Java implementation of above approach ; Function to return the total pairs of elements whose modulo yield K ; set is used to avoid duplicate pairs ; check which element is greater and proceed according to it ; check if modulo is equal to K ; return size of the set ; 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 int totalPairs ( int [ ] arr1 , int [ ] arr2 , int K , int n , int m ) { HashSet < pair > s = new HashSet < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr1 [ i ] > arr2 [ j ] ) { if ( arr1 [ i ] % arr2 [ j ] == K ) s . add ( new pair ( arr1 [ i ] , arr2 [ j ] ) ) ; } else { if ( arr2 [ j ] % arr1 [ i ] == K ) s . add ( new pair ( arr2 [ j ] , arr1 [ i ] ) ) ; } } } return s . size ( ) ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 8 , 3 , 7 , 50 } ; int [ ] arr2 = { 5 , 1 , 10 , 4 } ; int K = 3 ; int n = arr1 . length ; int m = arr2 . length ; System . out . println ( totalPairs ( arr1 , arr2 , K , n , m ) ) ; } }
Largest sub | Java program to find the length of the largest sub - array of an array every element of whose is a perfect square ; function to return the length of the largest sub - array of an array every element of whose is a perfect square ; if both a and b are equal then arr [ i ] is a perfect square ; Driver code
import java . io . * ; class GFG { static int contiguousPerfectSquare ( int [ ] arr , int n ) { int a ; float b ; int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { b = ( float ) Math . sqrt ( arr [ i ] ) ; a = ( int ) b ; if ( a == b ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 75 , 4 , 64 , 121 , 25 } ; int n = arr . length ; System . out . print ( contiguousPerfectSquare ( arr , n ) ) ; } }
Count pairs of numbers from 1 to N with Product divisible by their Sum | Java program to count pairs of numbers from 1 to N with Product divisible by their Sum ; Function to count pairs ; variable to store count ; Generate all possible pairs such that 1 <= x < y < n ; Driver code
import java . io . * ; class GFG { static int countPairs ( int n ) { int count = 0 ; for ( int x = 1 ; x < n ; x ++ ) { for ( int y = x + 1 ; y <= n ; y ++ ) { if ( ( y * x ) % ( y + x ) == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( countPairs ( n ) ) ; } }
Find the index of the left pointer after possible moves in the array | Java program to find the index of the left pointer ; Function that returns the index of the left pointer ; there 's only one element in the array ; initially both are at end ; Driver code
import java . io . * ; class GFG { static int getIndex ( int a [ ] , int n ) { if ( n == 1 ) return 0 ; int ptrL = 0 , ptrR = n - 1 , sumL = a [ 0 ] , sumR = a [ n - 1 ] ; while ( ptrR - ptrL > 1 ) { if ( sumL < sumR ) { ptrL ++ ; sumL += a [ ptrL ] ; } else if ( sumL > sumR ) { ptrR -- ; sumR += a [ ptrR ] ; } else { break ; } } return ptrL ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 7 , 9 , 8 , 7 } ; int n = a . length ; System . out . println ( getIndex ( a , n ) ) ; } }
Find the position of the last removed element from the array | Java program to find the position of the last removed element from the array ; Function to find the original position of the element which will be removed last ; take ceil of every number ; Since position is index + 1 ; Driver code
import java . util . * ; class solution { static int getPosition ( int a [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] / m + ( a [ i ] % m ) ) ; } int ans = - 1 , max = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( max < a [ i ] ) { max = a [ i ] ; ans = i ; } } return ans + 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 5 , 4 } ; int n = a . length ; int m = 2 ; System . out . println ( getPosition ( a , n , m ) ) ; } }
Find the value of f ( n ) / f ( r ) * f ( n | Java program to find the value of f ( n ) / f ( r ) * f ( n - r ) ; Function to find value of given F ( n ) ; iterate over n ; calculate result ; return the result ; Driver code
class GFG { static int calcFunction ( int n , int r ) { int finalDenominator = 1 ; int mx = Math . max ( r , n - r ) ; for ( int i = mx + 1 ; i <= n ; i ++ ) { int denominator = ( int ) Math . pow ( i , i ) ; int numerator = ( int ) Math . pow ( i - mx , i - mx ) ; finalDenominator = ( finalDenominator * denominator ) / numerator ; } return finalDenominator ; } public static void main ( String [ ] args ) { int n = 6 , r = 2 ; System . out . println ( "1 / " + calcFunction ( n , r ) ) ; } }
Check if the array has an element which is equal to sum of all the remaining elements | Java program to Check if the array has an element which is equal to sum of all the remaining elements ; Function to check if such element exists or not ; Storing frequency in map ; Stores the sum ; Traverse the array and count the array elements ; Only possible if sum is even ; If half sum is available ; Driver code
import java . util . * ; class Solution { static boolean isExists ( int a [ ] , int n ) { Map < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq . put ( a [ i ] , freq . get ( a [ i ] ) == null ? 0 : freq . get ( a [ i ] ) + 1 ) ; sum += a [ i ] ; } if ( sum % 2 == 0 ) { if ( freq . get ( sum / 2 ) != null ) return true ; } return false ; } public static void main ( String args [ ] ) { int a [ ] = { 5 , 1 , 2 , 2 } ; int n = a . length ; if ( isExists ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }