codeparrot/xlcost-text-to-code huggingface dataset:
Multiply a number with 10 without using multiplication operator | Java Code to Multiply a number with 10 without using multiplication operator ; Function to find multiplication of n with 10 without using multiplication operator ; Driver program to test above function
import java . util . * ; class GFG { public static int multiplyTen ( int n ) { return ( n << 1 ) + ( n << 3 ) ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( multiplyTen ( n ) ) ; } }
Equal Sum and XOR | Java program to print count of values such that n + i = n ^ i ; function to count number of values less than equal to n that satisfy the given condition ; Traverse all numbers from 0 to n and increment result only when given condition is satisfied . ; Driver program to test above function
import java . util . * ; class GFG { public static int countValues ( int n ) { int countV = 0 ; for ( int i = 0 ; i <= n ; i ++ ) if ( ( n + i ) == ( n ^ i ) ) countV ++ ; return countV ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( countValues ( n ) ) ; } }
Equal Sum and XOR | Java program to print count of values such that n + i = n ^ i ; function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; Return 2 ^ unset_bits ; Driver program to test above function
import java . util . * ; class GFG { public static int countValues ( int n ) { int unset_bits = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) unset_bits ++ ; n = n >> 1 ; } return 1 << unset_bits ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( countValues ( n ) ) ; } }
Find profession in a special family | Java program to find profession of a person at given level and position ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Base case ; Recursively find parent 's  profession. If parent  is a Doctor, this node  will be a Doctor if it  is at odd position and an  engineer if at even position ; If parent is an engineer , then current node will be an engineer if at add position and doctor if even position . ; Driver code
import java . io . * ; class GFG { static char findProffesion ( int level , int pos ) { if ( level == 1 ) return ' e ' ; if ( findProffesion ( level - 1 , ( pos + 1 ) / 2 ) == ' d ' ) return ( pos % 2 > 0 ) ? ' d ' : ' e ' ; return ( pos % 2 > 0 ) ? ' e ' : ' d ' ; } public static void main ( String [ ] args ) { int level = 4 , pos = 2 ; if ( findProffesion ( level , pos ) == ' e ' ) System . out . println ( " Engineer " ) ; else System . out . println ( " Doctor " ) ; } }
Print first n numbers with exactly two set bits | Java program to print first n numbers with exactly two set bits ; Function to print first n numbers with two set bits ; Initialize higher of two sets bits ; Keep reducing n for every number with two set bits ; Consider all lower set bits for current higher set bit ; Print current number ; If we have found n numbers ; Consider next lower bit for current higher bit . ; Increment higher set bit ; Driver program
import java . io . * ; class GFG { static void printTwoSetBitNums ( int n ) { int x = 1 ; while ( n > 0 ) { int y = 0 ; while ( y < x ) { System . out . print ( ( ( 1 << x ) + ( 1 << y ) ) + " ▁ " ) ; n -- ; if ( n == 0 ) return ; y ++ ; } x ++ ; } } public static void main ( String [ ] args ) { int n = 4 ; printTwoSetBitNums ( n ) ; } }
Generate 0 and 1 with 25 % and 75 % probability | Java program to print 1 with 75 % probability and 0 with 25 % probability ; Random Function to that returns 0 or 1 with equal probability ; rand ( ) function will generate odd or even number with equal probability . If rand ( ) generates odd number , the function will return 1 else it will return 0. ; Random Function to that returns 1 with 75 % probability and 0 with 25 % probability using Bitwise OR ; Driver code ; Initialize random number generator srand ( time ( null ) ) ;
class GFG { static int rand50 ( ) { return ( int ) ( 10 * Math . random ( ) ) & 1 ; } static int rand75 ( ) { return rand50 ( ) | rand50 ( ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 50 ; i ++ ) { System . out . print ( rand75 ( ) ) ; } } }
Find even occurring elements in an array of limited range | Java Program to find the even occurring elements in given array ; Function to find the even occurring elements in given array ; do for each element of array ; left - shift 1 by value of current element ; Toggle the bit everytime element gets repeated ; Traverse array again and use _xor to find even occurring elements ; left - shift 1 by value of current element ; Each 0 in _xor represents an even occurrence ; print the even occurring numbers ; set bit as 1 to avoid printing duplicates ; Driver code
class GFG { static void printRepeatingEven ( int arr [ ] , int n ) { long _xor = 0L ; long pos ; for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; _xor ^= pos ; } for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; if ( ! ( ( pos & _xor ) != 0 ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; _xor ^= pos ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 } ; int n = arr . length ; printRepeatingEven ( arr , n ) ; } }
Check if a number is Bleak | A simple Java program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
import java . io . * ; class GFG { static int countSetBits ( int x ) { int count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } static boolean isBleak ( int n ) { for ( int x = 1 ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } public static void main ( String args [ ] ) { if ( isBleak ( 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isBleak ( 4 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if a number is Bleak | Java program to demonstrate Integer . bitCount ( )
import java . util . * ; class GFG { public static void main ( String [ ] args ) { System . out . print ( Integer . bitCount ( 4 ) + "NEW_LINE"); System . out . print ( Integer . bitCount ( 15 ) ) ; } }
Find the maximum subset XOR of a given set | Java program to find maximum XOR subset ; Number of bits to represent int ; Function to return maximum XOR subset in set [ ] ; Initialize index of chosen elements ; Traverse through all bits of integer starting from the most significant bit ( MSB ) ; Initialize index of maximum element and the maximum element ; If i 'th bit of set[j]  is set and set[j] is  greater than max so far. ; If there was no element with i 'th  bit set, move to  smaller i ; Put maximum element with i ' th ▁ bit ▁ set ▁ ▁ at ▁ index ▁ ' index ' ; Update maxInd and increment index ; Do XOR of set [ maxIndex ] with all numbers having i 'th bit as set. ; XOR set [ maxInd ] those numbers which have the i 'th bit set ; Increment index of chosen elements ; Final result is XOR of all elements ; Driver code
import java . util . * ; class GFG { static final int INT_BITS = 32 ; static int maxSubarrayXOR ( int set [ ] , int n ) { int index = 0 ; for ( int i = INT_BITS - 1 ; i >= 0 ; i -- ) { int maxInd = index ; int maxEle = Integer . MIN_VALUE ; for ( int j = index ; j < n ; j ++ ) { if ( ( set [ j ] & ( 1 << i ) ) != 0 && set [ j ] > maxEle ) { maxEle = set [ j ] ; maxInd = j ; } } if ( maxEle == - 2147483648 ) continue ; int temp = set [ index ] ; set [ index ] = set [ maxInd ] ; set [ maxInd ] = temp ; maxInd = index ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != maxInd && ( set [ j ] & ( 1 << i ) ) != 0 ) set [ j ] = set [ j ] ^ set [ maxInd ] ; } index ++ ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res ^= set [ i ] ; return res ; } public static void main ( String arg [ ] ) { int set [ ] = { 9 , 8 , 5 } ; int n = set . length ; System . out . print ( " Max ▁ subset ▁ XOR ▁ is ▁ " ) ; System . out . print ( maxSubarrayXOR ( set , n ) ) ; } }
Given a set , find XOR of the XOR 's of all subsets. | Java program to find XOR of XOR 's of all subsets ; Returns XOR of all XOR 's of given subset ; XOR is 1 only when n is 1 , else 0 ; Driver code
import java . util . * ; class GFG { static int findXOR ( int Set [ ] , int n ) { if ( n == 1 ) return Set [ 0 ] ; else return 0 ; } public static void main ( String arg [ ] ) { int Set [ ] = { 1 , 2 , 3 } ; int n = Set . length ; System . out . print ( " XOR ▁ of ▁ XOR ' s ▁ of ▁ all ▁ subsets ▁ is ▁ " + findXOR ( Set , n ) ) ; } }
Find XOR of two number without using XOR operator | Java program to find XOR without using ^ ; Returns XOR of x and y ; Initialize result ; Assuming 32 - bit Integer ; Find current bits in x and y ; If both are 1 then 0 else xor is same as OR ; Update result ; Driver Code
import java . io . * ; class GFG { static int myXOR ( int x , int y ) { int res = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { int b1 = ( ( x & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int b2 = ( ( y & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int xoredBit = ( ( b1 & b2 ) != 0 ) ? 0 : ( b1 b2 ) ; res <<= 1 ; res |= xoredBit ; } return res ; } public static void main ( String [ ] args ) { int x = 3 , y = 5 ; System . out . println ( " XOR ▁ is ▁ " + myXOR ( x , y ) ) ; } }
Find XOR of two number without using XOR operator | Java program to find XOR without using ^ ; Returns XOR of x and y ; Driver Code
import java . io . * ; class GFG { static int myXOR ( int x , int y ) { return ( x y ) & ( ~ x ~ y ) ; } public static void main ( String [ ] args ) { int x = 3 , y = 5 ; System . out . println ( " XOR ▁ is ▁ " + ( myXOR ( x , y ) ) ) ; } }
How to swap two bits in a given integer ? | Java program to swap bits in an integer ; This function swaps bit at positions p1 and p2 in an integer n ; Move p1 'th to rightmost side  ; Move p2 'th to rightmost side  ; XOR the two bits ; Put the xor bit back to their original positions ; XOR ' x ' with the original number so that the two sets are swapped ; Driver code
import java . io . * ; class GFG { static int swapBits ( int n , int p1 , int p2 ) { int bit1 = ( n >> p1 ) & 1 ; int bit2 = ( n >> p2 ) & 1 ; int x = ( bit1 ^ bit2 ) ; x = ( x << p1 ) | ( x << p2 ) ; int result = n ^ x ; return result ; } public static void main ( String [ ] args ) { int res = swapBits ( 28 , 0 , 3 ) ; System . out . println ( " Result ▁ = ▁ " + res ) ; } }
Write a function that returns 2 for input 1 and returns 1 for 2 |
static int invertSub ( int x ) { return ( 3 - x ) ; }
Maximum length sub | Java implementation of the approach ; Function to return the maximum length of the required sub - array ; For the first consecutive pair of elements ; While a consecutive pair can be selected ; If current pair forms a valid sub - array ; 2 is the length of the current sub - array ; To extend the sub - array both ways ; While elements at indices l and r are part of a valid sub - array ; Update the maximum length so far ; Select the next consecutive pair ; Return the maximum length ; Driver code
class GFG { static int maxLength ( int arr [ ] , int n ) { int maxLen = 0 ; int i = 0 ; int j = i + 1 ; while ( j < n ) { if ( arr [ i ] != arr [ j ] ) { maxLen = Math . max ( maxLen , 2 ) ; int l = i - 1 ; int r = j + 1 ; while ( l >= 0 && r < n && arr [ l ] == arr [ i ] && arr [ r ] == arr [ j ] ) { l -- ; r ++ ; } maxLen = Math . max ( maxLen , 2 * ( r - j ) ) ; } i ++ ; j = i + 1 ; } return maxLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( maxLength ( arr , n ) ) ; } }
Freivaldâ €™ s Algorithm to check if a matrix is product of two | Java code to implement Freivald 's Algorithm ; Function to check if ABx = Cx ; Generate a random vector ; Now comput B * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput C * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput A * ( B * r ) for evaluating expression A * ( B * r ) - ( C * r ) ; Finally check if value of expression A * ( B * r ) - ( C * r ) is 0 or not ; Runs k iterations Freivald . The value of k determines accuracy . Higher value means higher accuracy . ; Driver code
import java . io . * ; import java . util . * ; import java . math . * ; class GFG { static int N = 2 ; static boolean freivald ( int a [ ] [ ] , int b [ ] [ ] , int c [ ] [ ] ) { int r [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) r [ i ] = ( int ) ( Math . random ( ) ) % 2 ; int br [ ] = new int [ N ] ; Arrays . fill ( br , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) br [ i ] = br [ i ] + b [ i ] [ j ] * r [ j ] ; int cr [ ] = new int [ N ] ; Arrays . fill ( cr , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) cr [ i ] = cr [ i ] + c [ i ] [ j ] * r [ j ] ; int axbr [ ] = new int [ N ] ; Arrays . fill ( axbr , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) axbr [ i ] = axbr [ i ] + a [ i ] [ j ] * br [ j ] ; for ( int i = 0 ; i < N ; i ++ ) if ( axbr [ i ] - cr [ i ] != 0 ) return false ; return true ; } static boolean isProduct ( int a [ ] [ ] , int b [ ] [ ] , int c [ ] [ ] , int k ) { for ( int i = 0 ; i < k ; i ++ ) if ( freivald ( a , b , c ) == false ) return false ; return true ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 1 , 1 } , { 1 , 1 } } ; int b [ ] [ ] = { { 1 , 1 } , { 1 , 1 } } ; int c [ ] [ ] = { { 2 , 2 } , { 2 , 2 } } ; int k = 2 ; if ( isProduct ( a , b , c , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Expectation or expected value of an array | Java code to calculate expected value of an array ; Function to calculate expectation ; Variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; Driver program ; Function for calculating expectation ; Display expectation of given array
import java . io . * ; class GFG { static float calc_Expectation ( float a [ ] , float n ) { float prb = ( 1 / n ) ; float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] * prb ; return sum ; } public static void main ( String args [ ] ) { float expect , n = 6f ; float a [ ] = { 1f , 2f , 3f , 4f , 5f , 6f } ; expect = calc_Expectation ( a , n ) ; System . out . println ( " Expectation ▁ of ▁ array ▁ E ( X ) ▁ is ▁ : ▁ " + expect ) ; } }
Sum of N | Java implementation to illustrate the program ; Function to calculate the sum recursively ; Base cases ; If n is odd ; If n is even ; Function to prlong the value of Sum ; Driver Code ; First element ; Common difference ; Number of elements ; Mod value
import java . io . * ; class GFG { static long SumGPUtil ( long r , long n , long m ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return ( 1 + r ) % m ; long ans ; if ( n % 2 == 1 ) { ans = ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n - 1 ) / 2 , m ) ; } else { ans = 1 + ( r * ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n / 2 ) - 1 , m ) ) ; } return ( ans % m ) ; } static void SumGP ( long a , long r , long N , long M ) { long answer ; answer = a * SumGPUtil ( r , N , M ) ; answer = answer % M ; System . out . println ( answer ) ; } public static void main ( String [ ] args ) { long a = 1 ; long r = 4 ; long N = 10000 ; long M = 100000 ; SumGP ( a , r , N , M ) ; } }
Length of longest subarray in which elements greater than K are more than elements not greater than K | Java implementation of above approach ; Function to find the length of a longest subarray in which elements greater than K are more than elements not greater than K ; Create a new array in which we store 1 if a [ i ] > k otherwise we store - 1. ; Taking prefix sum over it ; len will store maximum length of subarray ; This indicate there is at least one subarray of length mid that has sum > 0 ; Check every subarray of length mid if it has sum > 0 or not if sum > 0 then it will satisfy our required condition ; x will store the sum of subarray of length mid ; Satisfy our given condition ; Check for higher length as we get length mid ; Check for lower length as we did not get length mid ; Driver code
class GFG { static int LongestSubarray ( int a [ ] , int n , int k ) { int [ ] pre = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > k ) pre [ i ] = 1 ; else pre [ i ] = - 1 ; } for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = pre [ i - 1 ] + pre [ i ] ; int len = 0 ; int lo = 1 , hi = n ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; boolean ok = false ; for ( int i = mid - 1 ; i < n ; i ++ ) { int x = pre [ i ] ; if ( i - mid >= 0 ) x -= pre [ i - mid ] ; if ( x > 0 ) { ok = true ; break ; } } if ( ok == true ) { len = mid ; lo = mid + 1 ; } else hi = mid - 1 ; } return len ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 4 , 5 , 3 , 7 } ; int k = 3 ; int n = a . length ; System . out . println ( LongestSubarray ( a , n , k ) ) ; } }
Choose points from two ranges such that no point lies in both the ranges | Java implementation of the approach ; Function to find the required points ; Driver code
class GFG { static void findPoints ( int l1 , int r1 , int l2 , int r2 ) { int x = ( l1 != l2 ) ? Math . min ( l1 , l2 ) : - 1 ; int y = ( r1 != r2 ) ? Math . max ( r1 , r2 ) : - 1 ; System . out . println ( x + " ▁ " + y ) ; } public static void main ( String [ ] args ) { int l1 = 5 , r1 = 10 , l2 = 1 , r2 = 7 ; findPoints ( l1 , r1 , l2 , r2 ) ; } }
Tail Recursion | A NON - tail - recursive function . The function is not tail recursive because the value returned by fact ( n - 1 ) is used in fact ( n ) and call to fact ( n - 1 ) is not the last thing done by fact ( n ) ; Driver program
class GFG { static int fact ( int n ) { if ( n == 0 ) return 1 ; return n * fact ( n - 1 ) ; } public static void main ( String [ ] args ) { System . out . println ( fact ( 5 ) ) ; } }
Minimum number of working days required to achieve each of the given scores | Java program for the above approach ; Function to find the minimum number of days required to work to at least arr [ i ] points for every array element ; Traverse the array P [ ] ; Find the prefix sum ; Traverse the array arr [ ] ; Find the minimum index of the array having value at least arr [ i ] ; If the index is not - 1 ; Otherwise ; Function to find the lower bound of N using binary search ; Stores the lower bound ; Stores the upper bound ; Stores the minimum index having value is at least N ; Iterater while i <= j ; Stores the mid index of the range [ i , j ] ; If P [ mid ] is at least N ; Update the value of mid to index ; Update the value of j ; Update the value of i ; Return the resultant index ; Driver Code
public class GFG { static void minDays ( int [ ] P , int [ ] arr ) { for ( int i = 1 ; i < P . length ; i ++ ) { P [ i ] += P [ i ] + P [ i - 1 ] ; } for ( int i = 0 ; i < arr . length ; i ++ ) { int index = binarySeach ( P , arr [ i ] ) ; if ( index != - 1 ) { System . out . print ( index + 1 + " ▁ " ) ; } else { System . out . print ( - 1 + " ▁ " ) ; } } } static int binarySeach ( int [ ] P , int N ) { int i = 0 ; int j = P . length - 1 ; int index = - 1 ; while ( i <= j ) { int mid = i + ( j - i ) / 2 ; if ( P [ mid ] >= N ) { index = mid ; j = mid - 1 ; } else { i = mid + 1 ; } } return index ; } public static void main ( String [ ] args ) { int [ ] arr = { 400 , 200 , 700 , 900 , 1400 } ; int [ ] P = { 100 , 300 , 400 , 500 , 600 } ; minDays ( P , arr ) ; } }
Maximize profit that can be earned by selling an item among N buyers | Java program for the above approach ; Function to find the maximum profit earned by selling an item among N buyers ; Stores the maximum profit ; Stores the price of the item ; Traverse the array ; Count of buyers with budget >= arr [ i ] ; Increment count ; Update the maximum profit ; Return the maximum possible price ; Driver Code
import java . util . * ; class GFG { public static int maximumProfit ( int arr [ ] ) { int ans = Integer . MIN_VALUE ; int price = 0 ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) { count ++ ; } } if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } public static void main ( String [ ] args ) { int arr [ ] = { 22 , 87 , 9 , 50 , 56 , 43 } ; System . out . print ( maximumProfit ( arr ) ) ; } }
Maximum length palindromic substring for every index such that it starts and ends at that index | Java program for the above approach ; Function to find for every index , longest palindromic substrings starting or ending at that index ; Stores the maximum palindromic substring length for each index ; Traverse the string ; Stores the maximum length of palindromic substring ; Consider that palindromic substring ends at index i ; If current character is a valid starting index ; If S [ i , j ] is palindrome , ; Update the length of the longest palindrome ; Consider that palindromic substring starts at index i ; If current character is a valid ending index ; If str [ i , j ] is palindrome ; Update the length of the longest palindrome ; Update length of the longest palindromic substring for index i ; Print the length of the longest palindromic substring ; Function to return true if S [ i ... j ] is a palindrome ; Iterate until i < j ; If unequal character encountered ; Otherwise ; Driver Code
class GFG { public static void printLongestPalindrome ( String S , int N ) { int palLength [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int maxlength = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( S . charAt ( j ) == S . charAt ( i ) ) { if ( isPalindrome ( S , j , i ) ) { maxlength = i - j + 1 ; break ; } } } for ( int j = N - 1 ; j > i ; j -- ) { if ( S . charAt ( j ) == S . charAt ( i ) ) { if ( isPalindrome ( S , i , j ) ) { maxlength = Math . max ( j - i + 1 , maxlength ) ; break ; } } } palLength [ i ] = maxlength ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( palLength [ i ] + " ▁ " ) ; } } public static boolean isPalindrome ( String S , int i , int j ) { while ( i < j ) { if ( S . charAt ( i ) != S . charAt ( j ) ) return false ; i ++ ; j -- ; } return true ; } public static void main ( String [ ] args ) { String S = " bababa " ; int N = S . length ( ) ; printLongestPalindrome ( S , N ) ; } }
Sum of array elements which are multiples of a given number | Java program for the above approach ; Function to find the sum of array elements which are multiples of N ; Stores the sum ; Traverse the given array ; If current element is a multiple of N ; Print total sum ; Driver Code ; Given arr [ ]
import java . io . * ; import java . util . * ; class GFG { static void mulsum ( int arr [ ] , int n , int N ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % N == 0 ) { sum = sum + arr [ i ] ; } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 , 6 } ; int n = arr . length ; int N = 3 ; mulsum ( arr , n , N ) ; } }