codeparrot/xlcost-text-to-code huggingface dataset:
Split an array into minimum number of non | Java program for the above approach ; Function to split the array into minimum count of subarrays such that each subarray is either non - increasing or non - decreasing ; Initialize variable to keep track of current sequence ; Stores the required result ; Traverse the array , arr [ ] ; If current sequence is neither non - increasing nor non - decreasing ; If previous element is greater ; Update current ; If previous element is equal to the current element ; Update current ; Otherwise ; Update current ; If current sequence is in non - decreasing ; If previous element is less than or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; If current sequence is Non - Increasing ; If previous element is greater or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; Print the answer ; Driver Code ; Given array ; Given size of array
import java . io . * ; import java . util . * ; class GFG { static void minimumSubarrays ( int [ ] arr , int n ) { char current = ' N ' ; int answer = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( current == ' N ' ) { if ( arr [ i ] < arr [ i - 1 ] ) { current = ' D ' ; } else if ( arr [ i ] == arr [ i - 1 ] ) { current = ' N ' ; } else { current = ' I ' ; } } else if ( current == ' I ' ) { if ( arr [ i ] >= arr [ i - 1 ] ) { current = ' I ' ; } else { current = ' N ' ; answer += 1 ; } } else { if ( arr [ i ] <= arr [ i - 1 ] ) { current = ' D ' ; } else { current = ' N ' ; answer += 1 ; } } } System . out . print ( answer ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 9 , 5 , 4 , 6 , 8 } ; int n = arr . length ; minimumSubarrays ( arr , n ) ; } }
Convert a Matrix into another Matrix of given dimensions | Java program to implement the above approach ; Function to cona matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , mat [ ] [ ] ; Update idx ; Print the resultant matrix ; Driver Code
import java . util . * ; class GFG { static void ConstMatrix ( int [ ] [ ] mat , int N , int M , int A , int B ) { if ( N * M != A * B ) return ; int idx = 0 ; int [ ] [ ] res = new int [ A ] [ B ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { res [ idx / B ] [ idx % B ] = mat [ i ] [ j ] ; idx ++ ; } } for ( int i = 0 ; i < A ; i ++ ) { for ( int j = 0 ; j < B ; j ++ ) System . out . print ( res [ i ] [ j ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 , 6 } } ; int A = 2 ; int B = 3 ; int N = mat . length ; int M = mat [ 0 ] . length ; ConstMatrix ( mat , N , M , A , B ) ; } }
Modify given array by reducing each element by its next smaller element | Java program for the above approach ; Function to print the final array after reducing each array element by its next smaller element ; Initialize stack ; Array size ; To store the corresponding element ; If stack is not empty ; If top element is smaller than the current element ; Keep popping until stack is empty or top element is greater than the current element ; If stack is not empty ; Push current element ; Print the final array ; Driver Code ; Given array ; Function call
import java . util . * ; class GFG { static void printFinalPrices ( int [ ] arr ) { Stack < Integer > minStk = new Stack < > ( ) ; int n = arr . length ; int [ ] reduce = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ! minStk . isEmpty ( ) ) { if ( minStk . peek ( ) <= arr [ i ] ) { reduce [ i ] = minStk . peek ( ) ; } else { while ( ! minStk . isEmpty ( ) && ( minStk . peek ( ) > arr [ i ] ) ) { minStk . pop ( ) ; } if ( ! minStk . isEmpty ( ) ) { reduce [ i ] = minStk . peek ( ) ; } } } minStk . add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] - reduce [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 8 , 4 , 6 , 2 , 3 } ; printFinalPrices ( arr ) ; } }
Generate array having differences between count of occurrences of every array element on its left and right | Java program of the above approach ; Function to construct array of differences of counts on the left and right of the given array ; Initialize left and right frequency arrays ; Construct left cumulative frequency table ; Construct right cumulative frequency table ; Print the result ; Driver Code ; Function Call
import java . io . * ; class GFG { static void constructArray ( int A [ ] , int N ) { int [ ] left = new int [ N + 1 ] ; int [ ] right = new int [ N + 1 ] ; int [ ] X = new int [ N + 1 ] ; int [ ] Y = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { X [ i ] = left [ A [ i ] ] ; left [ A [ i ] ] ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { Y [ i ] = right [ A [ i ] ] ; right [ A [ i ] ] ++ ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( Y [ i ] - X [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 3 , 2 , 1 , 2 , 3 } ; int N = A . length ; constructArray ( A , N ) ; } }
Mean of distinct odd fibonacci nodes in a Linked List | Java program to implement the above approach ; Structure of a singly Linked List ; Stores data value of a Node ; Stores pointer to next Node ; Function to insert a node at the beginning of the singly Linked List ; Create a new Node ; Insert the data into the Node ; Insert pointer to the next Node ; Update head_ref ; Function to find the largest element from the linked list ; Stores the largest element in the linked list ; Iterate over the linked list ; If max is less than head . data ; Update max ; Update head ; Function to store all Fibonacci numbers up to the largest element of the list ; Store all Fibonacci numbers up to Max ; Stores first element of Fibonacci number ; Stores second element of Fibonacci number ; Insert prev into hashmap ; Insert curr into hashmap ; Insert all elements of Fibonacci numbers up to Max ; Stores current fibonacci number ; Insert temp into hashmap ; Update prev ; Update curr ; Function to find the mean of odd Fibonacci nodes ; Stores the largest element in the linked list ; Stores all fibonacci numbers up to Max ; Stores current node of linked list ; Stores count of odd Fibonacci nodes ; Stores sum of all odd fibonacci nodes ; Traverse the linked list ; if the data value of current node is an odd number ; if data value of the node is present in hashmap ; Update cnt ; Update sum ; Remove current fibonacci number from hashmap so that duplicate elements can 't be  counted ; Update curr ; Return the required mean ; Driver Code ; Stores head node of the linked list ; Insert all data values in the linked list
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node head ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int largestElement ( Node head_ref ) { int Max = Integer . MIN_VALUE ; Node head = head_ref ; while ( head != null ) { if ( Max < head . data ) { Max = head . data ; } head = head . next ; } return Max ; } static HashSet < Integer > createHashMap ( int Max ) { HashSet < Integer > hashmap = new HashSet < > ( ) ; int prev = 0 ; int curr = 1 ; hashmap . add ( prev ) ; hashmap . add ( curr ) ; while ( curr <= Max ) { int temp = curr + prev ; hashmap . add ( temp ) ; prev = curr ; curr = temp ; } return hashmap ; } static double meanofnodes ( ) { int Max = largestElement ( head ) ; HashSet < Integer > hashmap = createHashMap ( Max ) ; Node curr = head ; int cnt = 0 ; double sum = 0.0 ; while ( curr != null ) { if ( ( curr . data ) % 2 == 1 ) { if ( hashmap . contains ( curr . data ) ) { cnt ++ ; sum += curr . data ; hashmap . remove ( curr . data ) ; } } curr = curr . next ; } return ( sum / cnt ) ; } public static void main ( String [ ] args ) { head = null ; head = push ( head , 5 ) ; head = push ( head , 21 ) ; head = push ( head , 8 ) ; head = push ( head , 12 ) ; head = push ( head , 3 ) ; head = push ( head , 13 ) ; head = push ( head , 144 ) ; head = push ( head , 6 ) ; System . out . print ( meanofnodes ( ) ) ; } }
Generate an alternate odd | Java program to implement the above approach ; Function to print the required sequence ; Print ith odd number ; Driver code ; Function call
import java . util . * ; class GFG { static void findNumbers ( int n ) { int i = 0 ; while ( i <= n ) { System . out . print ( 2 * i * i + 4 * i + 1 + i % 2 + " ▁ " ) ; i ++ ; } } public static void main ( String [ ] args ) { int n = 6 ; findNumbers ( n ) ; } }
Program to calculate gross salary of a person | Java program to implement the above approach ; Function to calculate the salary of the person ; Condition to compute the allowance for the person ; Calculate gross salary ; Driver Code ; Function call
import java . util . * ; import java . lang . * ; class GFG { static int computeSalary ( int basic , char grade ) { double allowance ; double hra , da , pf ; hra = 0.2 * basic ; da = 0.5 * basic ; pf = 0.11 * basic ; if ( grade == ' A ' ) { allowance = 1700.0 ; } else if ( grade == ' B ' ) { allowance = 1500.0 ; } else { allowance = 1300.0 ; } double gross ; gross = Math . round ( basic + hra + da + allowance - pf ) ; return ( int ) gross ; } public static void main ( String [ ] args ) { int basic = 10000 ; char grade = ' A ' ; System . out . println ( computeSalary ( basic , grade ) ) ; } }
Rearrange and update array elements as specified by the given queries | Java program to implement the above approach ; Function to perform the given operations ; Dequeue to store the array elements ; Insert all element of the array into the dequeue ; Stores the size of the queue ; Traverse each query ; Query for left shift . ; Extract the element at the front of the queue ; Pop the element at the front of the queue ; Push the element at the back of the queue ; Query for right shift ; Extract the element at the back of the queue ; Pop the element at the back of the queue ; Push the element at the front of the queue ; Query for update ; Query to get the value ; Driver Code ; All possible Queries
import java . util . * ; class GFG { static void Queries ( int arr [ ] , int N , int [ ] [ ] Q ) { Vector < Integer > dq = new Vector < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { dq . add ( arr [ i ] ) ; } int sz = Q . length ; for ( int i = 0 ; i < sz ; i ++ ) { if ( Q [ i ] [ 0 ] == 0 ) { int front = dq . get ( 0 ) ; dq . remove ( 0 ) ; dq . add ( front ) ; } else if ( Q [ i ] [ 0 ] == 1 ) { int back = dq . elementAt ( dq . size ( ) - 1 ) ; dq . remove ( dq . size ( ) - 1 ) ; dq . add ( 0 , back ) ; } else if ( Q [ i ] [ 0 ] == 2 ) { dq . set ( Q [ i ] [ 1 ] , Q [ i ] [ 2 ] ) ; } else { System . out . print ( dq . get ( Q [ i ] [ 1 ] ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; int [ ] [ ] Q = { { 0 } , { 1 } , { 3 , 1 } , { 2 , 2 , 54 } , { 3 , 2 } } ; Queries ( arr , N , Q ) ; } }
Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal | Java program to implement the above approach ; Function ot find the length of the smallest subarray ; Stores ( prefix Sum , index ) as ( key , value ) mappings ; Iterate till N ; Update the prefixSum ; Update the length ; Put the latest index to find the minimum length ; Update the length ; Return the answer ; Function to find the length of the largest subarray ; Stores the sum of all array elements after modification ; Change greater than k to 1 ; Change smaller than k to - 1 ; Change equal to k to 0 ; Update total_sum ; No deletion required , return 0 ; Delete smallest subarray that has sum = total_sum ; Driver Code ; Function call
import java . util . * ; class GFG { static int smallSubarray ( int arr [ ] , int n , int total_sum ) { Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; int length = Integer . MAX_VALUE ; int prefixSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { prefixSum += arr [ i ] ; if ( prefixSum == total_sum ) { length = Math . min ( length , i + 1 ) ; } m . put ( prefixSum , i ) ; if ( m . containsKey ( prefixSum - total_sum ) ) { length = Math . min ( length , i - m . get ( prefixSum - total_sum ) ) ; } } return length ; } static int smallestSubarrayremoved ( int arr [ ] , int n , int k ) { int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > k ) { arr [ i ] = 1 ; } else if ( arr [ i ] < k ) { arr [ i ] = - 1 ; } else { arr [ i ] = 0 ; } total_sum += arr [ i ] ; } if ( total_sum == 0 ) { return 0 ; } else { return smallSubarray ( arr , n , total_sum ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 16 , 12 , 13 , 10 } ; int K = 13 ; int n = arr . length ; System . out . println ( smallestSubarrayremoved ( arr , n , K ) ) ; } }
Balanced Ternary Number System | Java program to convert positive decimals into balanced ternary system ; Driver code
class GFG { static String balancedTernary ( int n ) { String output = " " ; while ( n > 0 ) { int rem = n % 3 ; n = n / 3 ; if ( rem == 2 ) { rem = - 1 ; n ++ ; } output = ( rem == 0 ? '0' : ( rem == 1 ) ? '1' : ' Z ' ) + output ; } return output ; } public static void main ( String [ ] args ) { int n = 238 ; System . out . print ( " Equivalent ▁ Balanced ▁ Ternary ▁ of ▁ " + n + " ▁ is : ▁ " + balancedTernary ( n ) ) ; } }
Spt function or Smallest Parts Function of a given number | Java implementation to find the Spt Function to given number ; Variable to store spt function of a number ; Find the value of frequency of minimum element ; Function to add value of frequency of minimum element among all representations of n ; Find the value of frequency of minimum element ; Calculate spt ; Recursive function to find different ways in which n can be written as a sum of at one or more positive integers ; If sum becomes n , consider this representation ; Start from previous element in the representation till n ; Include current element from representation ; Call function again with reduced sum ; backtrack - remove current element from representation ; Function to find the spt function ; Using recurrence find different ways in which n can be written as a sum of at 1 or more positive integers ; Driver Code
import java . util . * ; class GFG { static int spt = 0 ; static int count ( Vector < Integer > arr , int min_i ) { int count = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) if ( min_i == arr . get ( i ) ) count ++ ; return count ; } static void printVector ( Vector < Integer > arr ) { int min_i = Integer . MAX_VALUE ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) min_i = Math . min ( min_i , arr . elementAt ( i ) ) ; int freq = count ( arr , min_i ) ; spt += freq ; } static void findWays ( Vector < Integer > arr , int i , int n ) { if ( n == 0 ) printVector ( arr ) ; for ( int j = i ; j <= n ; j ++ ) { arr . add ( j ) ; findWays ( arr , j , n - j ) ; arr . remove ( arr . size ( ) - 1 ) ; } } static void spt_function ( int n ) { Vector < Integer > arr = new Vector < > ( ) ; findWays ( arr , 1 , n ) ; System . out . print ( spt ) ; } public static void main ( String [ ] args ) { int N = 4 ; spt_function ( N ) ; } }
Find initial integral solution of Linear Diophantine equation if finite solution exists | Java program for the above approach ; Function to implement the extended euclid algorithm ; Base Case ; Recursively find the gcd ; Function to print the solutions of the given equations ax + by = c ; Condition for infinite solutions ; Condition for no solutions exist ; Condition for no solutions exist ; Print the solution ; Driver Code ; Given coefficients ; Function Call
class GFG { static int x , y ; static int gcd_extend ( int a , int b ) { if ( b == 0 ) { x = 1 ; y = 0 ; return a ; } else { int g = gcd_extend ( b , a % b ) ; int x1 = x , y1 = y ; x = y1 ; y = x1 - ( a / b ) * y1 ; return g ; } } static void print_solution ( int a , int b , int c ) { if ( a == 0 && b == 0 ) { if ( c == 0 ) { System . out . print ( " Infinite ▁ Solutions ▁ " + " Exist " + "NEW_LINE"); } else { System . out . print ( " No ▁ Solution ▁ exists " + "NEW_LINE"); } } int gcd = gcd_extend ( a , b ) ; if ( c % gcd != 0 ) { System . out . print ( " No ▁ Solution ▁ exists " + "NEW_LINE"); } else { System . out . print ( " x ▁ = ▁ " + x * ( c / gcd ) + " , ▁ y ▁ = ▁ " + y * ( c / gcd ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { int a , b , c ; a = 4 ; b = 18 ; c = 10 ; print_solution ( a , b , c ) ; } }
Count of pairs whose bitwise AND is a power of 2 | Java program for the above approach ; Method to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to return the number of valid pairs ; Iterate for all possible pairs ; Bitwise and value of the pair is passed ; Return the final count ; Driver Code ; Given array arr [ ] ; Function call
class GFG { static boolean check ( int x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } static int count ( int arr [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( check ( arr [ i ] & arr [ j ] ) ) cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 6 , 4 , 2 , 3 } ; int n = arr . length ; System . out . print ( count ( arr , n ) ) ; } }
Check if the Matrix follows the given constraints or not | Java implementation of the above approach ; Function checks if n is prime or not ; Corner case ; Check from 2 to Math . sqrt ( n ) ; Function returns sum of all elements of matrix ; Stores the sum of the matrix ; Function to check if all a [ i ] [ j ] with prime ( i + j ) are prime ; If index is prime ; If element not prime ; Driver code ; Check for both conditions
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 int takeSum ( int a [ ] [ ] ) { int sum = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) sum += a [ i ] [ j ] ; return sum ; } static boolean checkIndex ( int n , int m , int a [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( isPrime ( i + j ) ) { if ( ! isPrime ( a [ i ] [ j ] ) ) return false ; } } } return true ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 ; int a [ ] [ ] = { { 1 , 2 , 3 , 2 , 2 } , { 2 , 2 , 7 , 7 , 7 } , { 7 , 7 , 21 , 7 , 10 } , { 2 , 2 , 3 , 6 , 7 } } ; int sum = takeSum ( a ) ; if ( isPrime ( sum ) && checkIndex ( n , m , a ) ) { System . out . print ( " YES " + "NEW_LINE"); } else { System . out . print ( " NO " + "NEW_LINE"); } } }
Count of all possible pairs of array elements with same parity | Java program for above approach ; Function to return the answer ; Generate all possible pairs ; Increment the count if both even or both odd ; Driver Code
import java . util . * ; class GFG { static int countPairs ( int [ ] A , int n ) { int count = 0 , i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( ( A [ i ] % 2 == 0 && A [ j ] % 2 == 0 ) || ( A [ i ] % 2 != 0 && A [ j ] % 2 != 0 ) ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 1 , 3 } ; int n = A . length ; System . out . println ( countPairs ( A , n ) ) ; } }
Smallest number whose sum of digits is square of N | Java implementation of the above approach ; Function to return smallest number whose sum of digits is n ^ 2 ; Driver Code
import java . util . * ; class GFG { static int smallestNum ( int n ) { return ( int ) ( ( n * n % 9 + 1 ) * Math . pow ( 10 , n * n / 9 ) - 1 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( smallestNum ( n ) ) ; } }
Second heptagonal numbers | Java implementation to find N - th term in the series ; Function to find N - th term in the series ; Driver code ; Function call
class GFG { static void findNthTerm ( int n ) { System . out . println ( n * ( 5 * n + 3 ) / 2 ) ; } public static void main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } } findNthTerm ( N ) ; } }
Weakly Prime Numbers | Java program to check if n is Weakly Prime Number ; Function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if n is a Weakly Prime Number ; Number should be prime ; Converting N to String ; Loop to change digit at every character one by one . ; Loop to store every digit one by one at index j ; Driver code
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isWeaklyPrimeNum ( int N ) { if ( ! isPrime ( N ) ) return false ; String s = String . valueOf ( N ) ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { char [ ] str = s . toCharArray ( ) ; for ( int i = 0 ; i <= 9 ; i ++ ) { char c = ( char ) ( '0' + i ) ; str [ j ] = c ; int Num = Integer . valueOf ( String . valueOf ( str ) ) ; if ( str [ j ] != s . charAt ( j ) && isPrime ( Num ) ) { return false ; } } } return true ; } public static void main ( String [ ] args ) { int n = 294001 ; if ( isWeaklyPrimeNum ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Unprimeable Numbers | Java program to check if n is unprimeable Number ; Function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to check if n is a unprimeable Number ; Number should be composite ; Converting N to String ; Loop to change digit at every character one by one . ; Loop to store every digit one by one at index j ; Driver code ; Function Call
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isUnPrimeableNum ( int N ) { if ( isPrime ( N ) ) return false ; String s = String . valueOf ( N ) ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { char [ ] str = s . toCharArray ( ) ; for ( int i = 0 ; i <= 9 ; i ++ ) { char c = ( char ) ( '0' + i ) ; str [ j ] = c ; int Num = Integer . valueOf ( new String ( str ) ) ; if ( str [ j ] != s . charAt ( j ) && isPrime ( Num ) ) { return false ; } } } return true ; } public static void main ( String [ ] args ) { int n = 200 ; if ( isUnPrimeableNum ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Find the Nth row in Pascal 's Triangle | Java Program to find the Nth index row in Pascal 's triangle ; Function to find the elements of rowIndex in Pascal 's Triangle ; 1 st element of every row is 1 ; Check if the row that has to be returned is the first row ; Generate the previous row ; Generate the elements of the current row by the help of the previous row ; Return the row ; Driver Program
import java . util . ArrayList ; public class geeks { public static ArrayList < Integer > getRow ( int rowIndex ) { ArrayList < Integer > currow = new ArrayList < Integer > ( ) ; currow . add ( 1 ) ; if ( rowIndex == 0 ) { return currow ; } ArrayList < Integer > prev = getRow ( rowIndex - 1 ) ; for ( int i = 1 ; i < prev . size ( ) ; i ++ ) { int curr = prev . get ( i - 1 ) + prev . get ( i ) ; currow . add ( curr ) ; } currow . add ( 1 ) ; return currow ; } public static void main ( String [ ] args ) { int n = 3 ; ArrayList < Integer > arr = getRow ( n ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( i == arr . size ( ) - 1 ) System . out . print ( arr . get ( i ) ) ; else System . out . print ( arr . get ( i ) + " , ▁ " ) ; } } }