codeparrot/xlcost-text-to-code huggingface dataset:
Product of absolute difference of every pair in given Array | Java program for the above approach ; Function to return the product of abs diff of all pairs ( x , y ) ; To store product ; Iterate all possible pairs ; Find the product ; Return product ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static int getProduct ( int a [ ] , int n ) { int p = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { p *= Math . abs ( a [ i ] - a [ j ] ) ; } } return p ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . println ( getProduct ( arr , N ) ) ; } }
Check whether a number can be represented as difference of two consecutive cubes | Java program for the above approach ; Function to print the two consecutive numbers whose difference is N ; Iterate in the range [ 0 , 10 ^ 5 ] ; Function to check if N is a perfect cube ; Find floating point value of square root of x . ; If square root is an integer ; Function to check whether a number can be represented as difference of two consecutive cubes ; Check if 12 * N - 3 is a perfect square or not ; Driver Code ; Given Number N
import java . util . * ; class GFG { static void print ( int N ) { for ( int i = 0 ; i < 100000 ; i ++ ) { if ( Math . pow ( i + 1 , 3 ) - Math . pow ( i , 3 ) == N ) { int j = i + 1 ; System . out . println ( i + " ▁ " + j ) ; return ; } } } static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static boolean diffCube ( int N ) { return isPerfectSquare ( 12 * N - 3 ) ; } public static void main ( String [ ] args ) { int N = 19 ; if ( diffCube ( N ) ) { System . out . println ( " Yes " ) ; print ( N ) ; } else { System . out . println ( " No " ) ; } } }
Sum of bit differences for numbers from 0 to N | Set 2 | Java program to find the sum of bit differences between consecutive numbers from 0 to N using recursion ; Recursive function to find sum of different bits between consecutive numbers from 0 to N ; Base case ; Calculate the Nth term ; Driver Code ; Given number ; Function call
class GFG { static int totalCountDifference ( int n ) { if ( n == 1 ) return 1 ; return n + totalCountDifference ( n / 2 ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( totalCountDifference ( N ) ) ; } }
Sum of elements of a Geometric Progression ( GP ) in a given range | Java program to find the sum of elements of an GP in the given range ; Function to find sum in the given range ; Find the value of k ; Find the common difference ; Find the sum ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int findSum ( int [ ] arr , int n , int left , int right ) { int k = right - left + 1 ; int d = arr [ 1 ] / arr [ 0 ] ; int ans = arr [ left - 1 ] ; if ( d == 1 ) ans = ans * d * k ; else ans = ans * ( ( int ) Math . pow ( d , k ) - 1 / ( d - 1 ) ) ; return ans ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 } ; int queries = 3 ; int [ ] [ ] q = { { 2 , 4 } , { 2 , 6 } , { 5 , 8 } } ; int n = arr . length ; for ( int i = 0 ; i < queries ; i ++ ) System . out . println ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) ; } }
Find the first and last M digits from K | Java program to implement the above approach ; Function to find a ^ b modulo M ; Function to find the first and last M digits from N ^ K ; Calculate Last M digits ; Calculate First M digits ; Extract the number after decimal ; Find 10 ^ y ; Move the Decimal Point M - 1 digits forward ; Print the result ; Driver Code
class GFG { static long modPower ( long a , long b , long M ) { long res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) res = res * a % M ; a = a * a % M ; b >>= 1 ; } return res ; } static void findFirstAndLastM ( long N , long K , long M ) { long lastM = modPower ( N , K , ( 1L ) * ( long ) Math . pow ( 10 , M ) ) ; long firstM ; double y = ( double ) K * Math . log10 ( N * 1.0 ) ; y = y - ( long ) y ; double temp = Math . pow ( 10.0 , y ) ; firstM = ( long ) ( temp * ( 1L ) * Math . pow ( 10 , ( M - 1 ) ) ) ; System . out . print ( firstM + " ▁ " + lastM + "NEW_LINE"); } public static void main ( String [ ] args ) { long N = 12 , K = 12 , M = 4 ; findFirstAndLastM ( N , K , M ) ; } }
Minimize increment / decrement of Array elements to make each modulo K equal | Java program for the above approach ; Function to find the minimum operations required to make the modulo of each element of the array equal to each other ; Variable to store minimum operation required ; To store operation required to make all modulo equal ; Iterating through all possible modulo value ; Iterating through all different modulo obtained so far ; Calculating oprn required to make all modulos equal to x ; Checking the operations that will cost less ; Check operation that will cost less ; Update the minimum number of operations ; Returning the answer ; Function to store different modulos ; Set to store all different modulo ; Map to store count of all different modulo obtained ; Storing all different modulo count ; Insert into the set ; Increment count ; Function call to return value of min oprn required ; Driver Code
import java . util . * ; class GFG { static int Find_min ( HashSet < Integer > diff_mod , HashMap < Integer , Integer > count_mod , int k ) { int min_oprn = Integer . MAX_VALUE ; int oprn = 0 ; for ( int x = 0 ; x < k ; x ++ ) { oprn = 0 ; for ( int w : diff_mod ) { if ( w != x ) { if ( w == 0 ) { oprn += Math . min ( x , k - x ) * count_mod . get ( w ) ; } else { oprn += Math . min ( Math . abs ( x - w ) , k + x - w ) * count_mod . get ( w ) ; } } } if ( oprn < min_oprn ) min_oprn = oprn ; } return min_oprn ; } static int Cal_min ( int arr [ ] , int n , int k ) { HashSet < Integer > diff_mod = new HashSet < > ( ) ; HashMap < Integer , Integer > count_mod = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { diff_mod . add ( arr [ i ] % k ) ; count_mod . put ( arr [ i ] % k , count_mod . getOrDefault ( arr [ i ] % k , 0 ) + 1 ) ; } return Find_min ( diff_mod , count_mod , k ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 35 , 48 , 23 , 52 } ; int n = arr . length ; int k = 3 ; System . out . print ( Cal_min ( arr , n , k ) ) ; } }
Minimize Steps required to obtain Sorted Order of an Array | Java program to implement the above approach ; Function to find GCD of two numbers ; Function to calculate the LCM of array elements ; Initialize result ; Function to find minimum steps required to obtain sorted sequence ; Inititalize dat [ ] array for Direct Address Table . ; Calculating steps required for each element to reach its sorted position ; Calculate LCM of the array ; Driver code
class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findlcm ( int arr [ ] , int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; return ans ; } static void minimumSteps ( int arr [ ] , int n ) { int i ; int dat [ ] = new int [ n + 1 ] ; for ( i = 1 ; i <= n ; i ++ ) dat [ arr [ i - 1 ] ] = i ; int b [ ] = new int [ n + 1 ] ; int j = 0 , c ; for ( i = 1 ; i <= n ; i ++ ) { c = 1 ; j = dat [ i ] ; while ( j != i ) { c ++ ; j = dat [ j ] ; } b [ i ] = c ; } System . out . println ( findlcm ( b , n ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 4 , 3 , 2 , 7 , 6 } ; int N = arr . length ; minimumSteps ( arr , N ) ; } }
Largest Ratio Contiguous subarray | Java program for the above approach ; Function to return maximum of two double values ; Check if a is greater than b then return a ; Function that returns the Ratio of max Ratio subarray ; Variable to store the maximum ratio ; Compute the product while traversing for subarrays ; Calculate the ratio ; Update max ratio ; Print the answer ; Driver code
class GFG { static double maximum ( double a , double b ) { if ( a > b ) return a ; return b ; } static double maxSubarrayRatio ( double arr [ ] , int n ) { double maxRatio = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { double ratio = arr [ i ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { ratio = ratio / arr [ k ] ; } maxRatio = maximum ( maxRatio , ratio ) ; } } return maxRatio ; } public static void main ( String [ ] args ) { double arr [ ] = { 2 , 2 , 4 , - 0.2 , - 1 } ; int n = arr . length ; System . out . println ( maxSubarrayRatio ( arr , n ) ) ; } }
Even Perfect Number | Java program for the above approach ; Function to check for perfect number ; Find a number close to 2 ^ q - 1 ; Calculate q - 1 ; Condition of perfect number ; Check whether q is prime or not ; Check whether 2 ^ q - 1 is a prime number or not ; Function to check for prime number ; Check whether it is equal to 2 or 3 ; Check if it can be divided by 2 and 3 then it is not prime number ; Check whether the given number be divide by other prime numbers ; Driver code
class GFG { private static void check ( long num ) { long root = ( long ) Math . sqrt ( num ) ; long pow = ( long ) ( Math . log ( root ) / Math . log ( 2 ) ) ; if ( num == ( long ) ( Math . pow ( 2 , pow ) * ( Math . pow ( 2 , pow + 1 ) - 1 ) ) ) { if ( isPrime ( pow + 1 ) ) { if ( isPrime ( ( long ) Math . pow ( 2 , pow + 1 ) - 1 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } else System . out . println ( " No " ) ; } else System . out . println ( " No " ) ; } public static boolean isPrime ( long n ) { if ( n <= 1 ) return false ; else if ( n == 2 n == 3 ) return true ; else { if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i <= Math . sqrt ( n ) ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; } return true ; } } public static void main ( String args [ ] ) { long num = 6 ; check ( num ) ; } }
Twin Pythagorean triplets in an array | Java program for the above approach ; Function to check if there exist a twin pythagorean triplet in the given array ; Loop to check if there is a Pythagorean triplet in the array ; Check if there is consecutive triple ; Calculate square of array elements ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static boolean isTriplet ( int ar [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( Math . abs ( ar [ i ] - ar [ j ] ) == 1 || Math . abs ( ar [ j ] - ar [ k ] ) == 1 || Math . abs ( ar [ i ] - ar [ k ] ) == 1 ) { int x = ar [ i ] * ar [ i ] , y = ar [ j ] * ar [ j ] , z = ar [ k ] * ar [ k ] ; if ( x == y + z y == x + z z == x + y ) return true ; } } } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 4 , 6 , 5 } ; int ar_size = arr . length ; if ( isTriplet ( arr , ar_size ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Pair with min absolute difference and whose product is N + 1 or N + 2 | Java program for the above approach ; Function to print pair ( a , b ) such that a * b = N + 1 or N + 2 ; Loop to iterate over the desired possible values ; Check for condition 1 ; Check for condition 2 ; Driver Code ; Given Number ; Function Call
import java . util . * ; class GFG { static void closestDivisors ( int n ) { for ( int i = ( int ) Math . sqrt ( n + 2 ) ; i > 0 ; i -- ) { if ( ( n + 1 ) % i == 0 ) { System . out . print ( i + " , ▁ " + ( n + 1 ) / i ) ; break ; } if ( ( n + 2 ) % i == 0 ) { System . out . print ( i + " , ▁ " + ( n + 2 ) / i ) ; break ; } } } public static void main ( String [ ] args ) { int N = 123 ; closestDivisors ( N ) ; } }
Is it possible to reach N and M from 1 and 0 respectively as per given condition | Java program for the above approach ; Function that find given x and y is possible or not ; Check if x is less than 2 and y is not equal to 0 ; Perform subtraction ; Check if y is divisible by 2 and greater than equal to 0 ; Driver Code ; Given X and Y ; Function Call
class GFG { static boolean is_possible ( int x , int y ) { if ( x < 2 && y != 0 ) return false ; y = y - x + 1 ; if ( y % 2 == 0 && y >= 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int x = 5 , y = 2 ; if ( is_possible ( x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count of integers up to N which are non divisors and non coprime with N | Java program to implement the above approach ; Function to return the count of integers less than N satisfying given conditions ; Stores Euler counts ; Store Divisor counts ; Based on Sieve of Eratosthenes ; Update phi values of all multiples of i ; Update count of divisors ; Return the final count ; Driver Code
import java . util . Arrays ; class GFG { public static int count ( int n ) { int [ ] phi = new int [ n + 1 ] ; Arrays . fill ( phi , 0 ) ; int [ ] divs = new int [ n + 1 ] ; Arrays . fill ( divs , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) { phi [ i ] += i ; for ( int j = i * 2 ; j <= n ; j += i ) phi [ j ] -= phi [ i ] ; for ( int j = i ; j <= n ; j += i ) divs [ j ] ++ ; } return ( n - phi [ n ] - divs [ n ] + 1 ) ; } public static void main ( String [ ] args ) { int N = 42 ; System . out . println ( count ( N ) ) ; } }
Sum of all differences between Maximum and Minimum of increasing Subarrays | Java program to find the sum of differences of maximum and minimum of strictly increasing subarrays ; Function to calculate and return the sum of differences of maximum and minimum of strictly increasing subarrays ; Stores the sum ; Traverse the array ; If last element of the increasing sub - array is found ; Update sum ; If the last element of the array is reached ; Update sum ; Return the sum ; Driver Code
class GFG { static int sum_of_differences ( int arr [ ] , int N ) { int sum = 0 ; int i , j , flag ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { flag = 0 ; for ( j = i + 1 ; j < N - 1 ; j ++ ) { if ( arr [ j ] >= arr [ j + 1 ] ) { sum += ( arr [ j ] - arr [ i ] ) ; i = j ; flag = 1 ; break ; } } if ( flag == 0 && arr [ i ] < arr [ N - 1 ] ) { sum += ( arr [ N - 1 ] - arr [ i ] ) ; break ; } } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 1 , 2 , 5 , 3 , 4 } ; int N = arr . length ; System . out . print ( sum_of_differences ( arr , N ) ) ; } }
Construct a Maximum Binary Tree from two given Binary Trees | A binary tree node has data , pointer to left child and a pointer to right child ; Helper method that allocates a new node with the given data and NULL left and right pointers . ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Method to find the maximum binary tree from two binary trees ; Driver Code ; First Binary Tree 3 / \ 2 6 / 20 ; Second Binary Tree 5 / \ 1 8 \ \ 2 8
class Node { int data ; Node left , right ; public Node ( int data , Node left , Node right ) { this . data = data ; this . left = left ; this . right = right ; } static Node newNode ( int data ) { return new Node ( data , null , null ) ; } static void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; System . out . printf ( " % d ▁ " , node . data ) ; inorder ( node . right ) ; } static Node MaximumBinaryTree ( Node t1 , Node t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; t1 . data = Math . max ( t1 . data , t2 . data ) ; t1 . left = MaximumBinaryTree ( t1 . left , t2 . left ) ; t1 . right = MaximumBinaryTree ( t1 . right , t2 . right ) ; return t1 ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 3 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 6 ) ; root1 . left . left = newNode ( 20 ) ; Node root2 = newNode ( 5 ) ; root2 . left = newNode ( 1 ) ; root2 . right = newNode ( 8 ) ; root2 . left . right = newNode ( 2 ) ; root2 . right . right = newNode ( 8 ) ; Node root3 = MaximumBinaryTree ( root1 , root2 ) ; inorder ( root3 ) ; } }
Minimum size binary string required such that probability of deleting two 1 's at random is 1/X | Java implementation of the above approach ; Function returns the minimum size of the String ; From formula ; Left limit of r ; Right limit of r ; Smallest integer in the valid range ; Driver Code
import java . util . * ; class GFG { static int MinimumString ( int x ) { int b = 1 ; double left_lim = Math . sqrt ( x ) + 1.0 ; double right_lim = Math . sqrt ( x ) + 2.0 ; int r = 0 ; for ( int i = ( int ) left_lim ; i <= right_lim ; i ++ ) { if ( i > left_lim && i < right_lim ) { r = i ; break ; } } return b + r ; } public static void main ( String [ ] args ) { int X = 2 ; System . out . print ( MinimumString ( X ) ) ; } }
Minimum number of squares whose sum equals to a given number N | Set | Java program for the above approach ; Function that returns true if N is a perfect square ; Function that returns true check if N is sum of three squares ; Factor out the powers of 4 ; N is NOT of the form 4 ^ a * ( 8 b + 7 ) ; Function that finds the minimum number of square whose sum is N ; If N is perfect square ; If N is sum of 2 perfect squares ; If N is sum of 3 perfect squares ; Otherwise , N is the sum of 4 perfect squares ; Driver code ; Given number ; Function call
import java . util . * ; class GFG { static boolean isPerfectSquare ( int N ) { int floorSqrt = ( int ) Math . sqrt ( N ) ; return ( N == floorSqrt * floorSqrt ) ; } static boolean legendreFunction ( int N ) { while ( N % 4 == 0 ) N /= 4 ; if ( N % 8 != 7 ) return true ; else return false ; } static int minSquares ( int N ) { if ( isPerfectSquare ( N ) ) return 1 ; for ( int i = 1 ; i * i < N ; i ++ ) { if ( isPerfectSquare ( N - i * i ) ) return 2 ; } if ( legendreFunction ( N ) ) return 3 ; return 4 ; } public static void main ( String [ ] args ) { int N = 123 ; System . out . print ( minSquares ( N ) ) ; } }
Check if N leaves only distinct remainders on division by all values up to K | Java program to check if all remainders are distinct or not ; Function to check and return if all remainders are distinct ; Stores the remainder ; Calculate the remainder ; If remainder already occurred ; Insert into the set ; Driver Code
import java . util . * ; class GFG { static boolean is_distinct ( long n , long k ) { HashSet < Long > s = new HashSet < Long > ( ) ; for ( int i = 1 ; i <= k ; i ++ ) { long tmp = n % i ; if ( s . contains ( tmp ) ) { return false ; } s . add ( tmp ) ; } return true ; } public static void main ( String [ ] args ) { long N = 5 , K = 3 ; if ( is_distinct ( N , K ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Count of multiplicative partitions of N | Java implementation to find the multiplicative partitions of the given number N ; Function to return number of ways of factoring N with all factors greater than 1 ; Variable to store number of ways of factoring n with all factors greater than 1 ; Driver Code ; 2 is the minimum factor of number other than 1. So calling recursive function to find number of ways of factoring N with all factors greater than 1
class MultiPart { static int getDivisors ( int min , int n ) { int total = 0 ; for ( int i = min ; i < n ; ++ i ) if ( n % i == 0 && n / i >= i ) { ++ total ; if ( n / i > i ) total += getDivisors ( i , n / i ) ; } return total ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( 1 + getDivisors ( 2 , n ) ) ; } }
Find last 2 survivors in N persons standing in a circle after killing next to immediate neighbour | Java implementation of the approach ; Node for a Linked List ; Function to find the last 2 survivors ; Total is the count of alive people ; Initiating the list of n people ; Total != 2 is terminating condition because at last only two - person will remain alive ; Del represent next person to be deleted or killed ; Last two person to survive ( in any order ) ; Driver code
class GFG { static class Node { int val ; Node next ; Node ( int _val ) { val = _val ; next = null ; } } ; static void getLastTwoPerson ( int n ) { int total = n ; Node head = new Node ( 1 ) ; Node temp = head ; for ( int i = 2 ; i <= n ; i ++ ) { temp . next = new Node ( i ) ; temp = temp . next ; } temp . next = head ; temp = head ; Node del ; while ( total != 2 ) { del = temp . next . next ; temp . next . next = temp . next . next . next ; temp = temp . next ; del = null ; System . gc ( ) ; total -= 1 ; } System . out . print ( temp . val + " ▁ " + temp . next . val ) ; } public static void main ( String [ ] args ) { int n = 2 ; getLastTwoPerson ( n ) ; } }
Philaland Coin | TCS Mockvita 2020 | Java implementation to find the minimum number of denominations required for any number ; Function to find the minimum number of denomminations required ; Driver Code ; Function Call
import java . io . * ; class GFG { static int findMinDenomin ( int n ) { return ( ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findMinDenomin ( n ) ) ; } }
Content of a Polynomial | Java implementation to find the content of the polynomial ; Function to find the content of the polynomial ; Loop to iterate over the elements of the array ; __gcd ( a , b ) is a inbuilt function for Greatest Common Divisor ; Driver Code ; Function call
class GFG { static int findContent ( int arr [ ] , int n ) { int content = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { content = __gcd ( content , arr [ i ] ) ; } return content ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 3 ; int arr [ ] = { 9 , 6 , 12 } ; System . out . print ( findContent ( arr , n ) ) ; } }
Check if the given array is same as its inverse permutation | Java program to implement the above approach ; Function to check if the inverse permutation of the given array is same as the original array ; Stores the inverse permutation ; Generate the inverse permutation ; Check if the inverse permutation is same as the given array ; Driver code
import java . util . * ; class GFG { static void inverseEqual ( int arr [ ] , int n ) { int [ ] brr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int present_index = arr [ i ] - 1 ; brr [ present_index ] = i + 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != brr [ i ] ) { System . out . println ( " No " ) ; return ; } } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 1 , 4 , 3 , 2 } ; inverseEqual ( arr , n ) ; } }
Linear Congruence method for generating Pseudo Random Numbers | Java implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the linear congruential method ; Driver code ; Seed value ; Modulus parameter ; Multiplier term ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
import java . util . * ; class GFG { static void linearCongruentialMethod ( int Xo , int m , int a , int c , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( ( randomNums [ i - 1 ] * a ) + c ) % m ; } } public static void main ( String [ ] args ) { int Xo = 5 ; int m = 7 ; int a = 3 ; int c = 3 ; int noOfRandomNums = 10 ; int [ ] randomNums = new int [ noOfRandomNums ] ; linearCongruentialMethod ( Xo , m , a , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { System . out . print ( randomNums [ i ] + " ▁ " ) ; } } }