codeparrot/xlcost-text-to-code huggingface dataset:
Count of triplets of numbers 1 to N such that middle element is always largest | Java program to implement the above approach ; Function to find number of triplets for given number N such that middle element is always greater than left and right side element . ; Check if arrangement is possible or not ; Else return total ways ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int findArrangement ( int N ) { if ( N < 3 ) return 0 ; return ( ( N ) * ( N - 1 ) * ( N - 2 ) ) / 3 ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( findArrangement ( N ) ) ; } }
Sum of all perfect square divisors of numbers from 1 to N | Java program to find the sum of all perfect square divisors of numbers from 1 to N ; Function for finding inverse of a number iteratively Here we will find the inverse of 6 , since it appears as denominator in the formula of sum of squares from 1 to N ; Store the value of the inverse of 6 once as we don 't need to call the function again and again ; Formula for finding the sum of first n squares ; No perfect square exists which is less than 4 ; Starting from 2 , present value of start is denoted here as curStart ; Finding end of the segment for which the contribution will be same ; Using the above mentioned formula to find ans % MOD ; Now for mthe next iteration start will become end + 1 ; Finally returning the answer ; Driver Code ; Here we are adding x because we have not counted 1 as perfect squares so if u want to add it you can just add that number to the ans
import java . util . * ; class GFG { static final int MOD = 7 ; static int inv ( int a ) { int o = 1 ; for ( int p = MOD - 2 ; p > 0 ; p >>= 1 ) { if ( ( p & 1 ) == 1 ) o = ( o * a ) % MOD ; a = ( a * a ) % MOD ; } return o ; } static int inv6 = inv ( 6 ) ; static int sumOfSquares ( int n ) { n %= MOD ; return ( ( ( n * ( n + 1 ) ) % MOD * ( 2 * n + 1 ) ) % MOD * inv6 ) % MOD ; } static int sums ( int n ) { if ( n < 4 ) return 0 ; int curStart = 2 , ans = 0 ; int sqrtN = ( int ) Math . sqrt ( n ) ; while ( curStart <= n / curStart ) { int V = n / ( curStart * curStart ) ; int end = ( int ) Math . sqrt ( n / V ) ; ans += ( n / ( curStart * curStart ) % MOD * ( sumOfSquares ( end ) + MOD - sumOfSquares ( curStart - 1 ) ) ) % MOD ; if ( ans >= MOD ) ans -= MOD ; curStart = end + 1 ; } return ans ; } public static void main ( String [ ] args ) { int input [ ] = { 5 } ; for ( int x : input ) { System . out . print ( " sum ▁ of ▁ all ▁ perfect ▁ " + " square ▁ divisors ▁ from ▁ " + "1 ▁ to ▁ " + x + " ▁ is : ▁ " ) ; System . out . print ( x + sums ( x ) + "NEW_LINE"); } } }
Check whether the binary equivalent of a number ends with "001" or not | Java implementation of the above approach ; Function returns true if s1 is suffix of s2 ; Function to check if binary equivalent of a number ends in "001" or not ; To store the binary number ; Count used to store exponent value ; Driver code
class GFG { static boolean isSuffix ( String s1 , String s2 ) { int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 . charAt ( n1 - i - 1 ) != s2 . charAt ( n2 - i - 1 ) ) return false ; return true ; } static boolean CheckBinaryEquivalent ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; int c = ( int ) Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } String bin = Integer . toString ( B_Number ) ; return isSuffix ( "001" , bin ) ; } public static void main ( String [ ] args ) { int N = 9 ; if ( CheckBinaryEquivalent ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check whether the binary equivalent of a number ends with "001" or not | Java implementation of the above approach ; Function to check if binary equivalent of a number ends in "001" or not ; To check if binary equivalent of a number ends in "001" or not ; Driver code
class GFG { static boolean CheckBinaryEquivalent ( int N ) { return ( N - 1 ) % 8 == 0 ; } public static void main ( String [ ] args ) { int N = 9 ; if ( CheckBinaryEquivalent ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Panarithmic numbers within a given range | Java program to print practical Numbers in given range ; Function to compute divisors of a number ; Vector to store divisors ; 1 will always be a divisor ; Check if i is squareroot of A then only one time insert it in ans ; Function to check that a number can be represented as sum of distinct divisor or not ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in bottom up manner ; Return the possibility of given sum ; Function to check a number is Practical or not ; Vector to store divisors ; If all numbers can be represented as sum of unique divisors ; Function to print Practical Numbers in a range ; Driver Code
import java . util . * ; import java . math . * ; class GFG { static ArrayList < Integer > get_divisors ( int A ) { ArrayList < Integer > ans = new ArrayList < > ( ) ; ans . add ( 1 ) ; for ( int i = 2 ; i <= Math . sqrt ( A ) ; i ++ ) { if ( A % i == 0 ) { ans . add ( i ) ; if ( ( i * i ) != A ) ans . add ( A / i ) ; } } return ans ; } static boolean Sum_Possible ( ArrayList < Integer > set , int sum ) { int n = set . size ( ) ; boolean subset [ ] [ ] = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < set . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set . get ( i - 1 ) ] ; } } return subset [ n ] [ sum ] ; } static boolean Is_Practical ( int A ) { ArrayList < Integer > divisors ; divisors = get_divisors ( A ) ; for ( int i = 2 ; i < A ; i ++ ) { if ( Sum_Possible ( divisors , i ) == false ) return false ; } return true ; } static void print_practica_No ( int A , int B ) { for ( int i = A ; i <= B ; i ++ ) { if ( Is_Practical ( i ) == true ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String args [ ] ) { int A = 1 , B = 100 ; print_practica_No ( A , B ) ; } }
Maximize the division result of Array using given operations | Java implementation to maximize the result of division of the given array elements ; Function to find the max result ; Sort the array in descending order ; Loop to divide in this order arr [ 0 ] / ( arr [ 1 ] / arr [ 2 ] / ... . arr [ n - 2 ] / arr [ n - 1 ] ) ; Return the final result ; Driver code
import java . util . * ; class GFG { static float maxDivision ( Integer arr [ ] , int n ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; float mxdiv = arr [ 1 ] ; for ( int i = 2 ; i < n ; ++ i ) mxdiv = mxdiv / arr [ i ] ; return arr [ 0 ] / mxdiv ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 100 , 1000 , 10 , 2 } ; int n = arr . length ; System . out . print ( ( int ) maxDivision ( arr , n ) ) ; } }
Print all distinct Coprime sets possible from 1 to N | Java implementation to print all distinct co - prime sets possible for numbers from 1 to N ; Function to print all co - prime sets ; Check if n is less than 4 then simply print all values till n ; For all the values of n > 3 ; Check if n is even then every set will contain 2 adjacent elements up - to n ; If n is odd then every set will contain 2 adjacent element except the last set which will have last three elements ; Last element for odd case ; Driver code
import java . util . * ; class GFG { static void coPrimeSet ( int n ) { int firstadj , secadj ; if ( n < 4 ) { System . out . print ( " ( ▁ " ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( i + " , ▁ " ) ; System . out . print ( ")NEW_LINE"); } else { if ( n % 2 == 0 ) { for ( int i = 0 ; i < n / 2 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; System . out . print ( " ( " + firstadj + " , ▁ " + secadj + ")NEW_LINE"); } } else { for ( int i = 0 ; i < n / 2 - 1 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; System . out . print ( " ( " + firstadj + " , ▁ " + secadj + ")NEW_LINE"); } System . out . print ( " ( " + ( n - 2 ) + " , ▁ " + ( n - 1 ) + " , ▁ " + n + ")NEW_LINE"); } } } public static void main ( String [ ] args ) { int n = 5 ; coPrimeSet ( n ) ; } }
Find the sequence number of a triangular number | Java code to print sequence number of a triangular number ; If N is not tringular number
import java . util . * ; class GFG { public static void main ( String args [ ] ) { int N = 21 ; int A = ( int ) ( Math . sqrt ( 2 * N + 0.25 ) - 0.5 ) ; int B = A ; if ( B != A ) System . out . print ( " - 1" ) ; else System . out . print ( B ) ; } }
Count subarrays with sum equal to its XOR value | Java program to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Function to count the number of subarrays such that Xor of all the elements of that subarray is equal to sum of the elements ; Maintain two pointers left and right ; Iterating through the array ; Calculate the window where the above condition is satisfied ; Count will be ( right - left ) ; Remove the previous element as it is already included ; Driver code
import java . io . * ; class GFG { static long operation ( int arr [ ] , int N ) { int right = 0 ; int num = 0 ; long ans = 0 ; for ( int left = 0 ; left < N ; left ++ ) { while ( right < N && num + arr [ right ] == ( num ^ arr [ right ] ) ) { num += arr [ right ] ; right ++ ; } ans += right - left ; if ( left == right ) right ++ ; else num -= arr [ left ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; System . out . println ( operation ( arr , N ) ) ; } }
Count of pairs in an Array whose sum is Prime | Java code to find number of pairs of elements in an array whose sum is prime ; Function for Sieve Of Eratosthenes ; Function to count total number of pairs of elements whose sum is prime ; Driver code
import java . io . * ; import java . util . * ; class GFG { public static boolean [ ] sieveOfEratosthenes ( int N ) { boolean [ ] isPrime = new boolean [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( isPrime [ i ] == true ) { int j = 2 ; while ( i * j <= N ) { isPrime [ i * j ] = false ; j ++ ; } } } return isPrime ; } public static int numPairsWithPrimeSum ( int [ ] arr , int n ) { int N = 2 * 1000000 ; boolean [ ] isPrime = sieveOfEratosthenes ( N ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( isPrime [ sum ] ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( numPairsWithPrimeSum ( arr , n ) ) ; } }
Count of numbers in Array ending with digits of number N | Java program to find the count of numbers in Array ending with digits of number N ; Array to keep the track of digits occurred Initially all are 0 ( false ) ; Function to initialize 1 ( true ) if the digit is present ; Variable to store the last digit ; Loop to iterate through every digit of the number N ; Updating the array according to the presence of the digit in n at the array index ; Function to check if the numbers in the array end with the digits of the number N ; Variable to store the count ; Variable to store the last digit ; Checking the presence of the last digit in N ; Function to find the required count ; Driver code ; Preprocessing
class GFG { public static int [ ] digit = new int [ 10 ] ; public static void digitsPresent ( int n ) { int lastDigit ; while ( n != 0 ) { lastDigit = n % 10 ; digit [ lastDigit ] = 1 ; n /= 10 ; } } public static int checkLastDigit ( int num ) { int count = 0 ; int lastDigit ; lastDigit = num % 10 ; if ( digit [ lastDigit ] == 1 ) count ++ ; return count ; } public static void findCount ( int N , int K , int arr [ ] ) { int count = 0 ; for ( int i = 0 ; i < K ; i ++ ) { count = checkLastDigit ( arr [ i ] ) == 1 ? count + 1 : count ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int N = 1731 ; digitsPresent ( N ) ; int K = 5 ; int arr [ ] = { 57 , 6786 , 1111 , 3 , 9812 } ; findCount ( N , K , arr ) ; } }
Check if count of even divisors of N is equal to count of odd divisors | Java code for the above program ; Function to check if count of even and odd divisors are equal ; If ( n - 2 ) % 4 is an integer , then return true else return false ; Driver code ; Given number ; Function call
import java . util . * ; class GFG { static boolean divisorsSame ( int n ) { return ( n - 2 ) % 4 == 0 ; } public static void main ( String [ ] args ) { int N = 6 ; if ( divisorsSame ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit | Java program for the above approach . ; Function to find integer ; Because 1 st integer is K itself ; Initialize min_d and max_d ; Updating min_d and max_d ; Break if min digit is 0 ; Driver code
import java . util . * ; class GFG { static int find ( int K , int N ) { N -- ; while ( N -- != 0 ) { int curr_term = K ; int min_d = 9 ; int max_d = 0 ; while ( curr_term > 0 ) { int r = curr_term % 10 ; min_d = Math . min ( min_d , r ) ; max_d = Math . max ( max_d , r ) ; curr_term = curr_term / 10 ; } if ( min_d == 0 ) { break ; } K = K + min_d * max_d ; } return K ; } public static void main ( String [ ] args ) { int K = 487 ; int N = 2 ; System . out . print ( find ( K , N ) + "NEW_LINE"); } }
Count of subarrays whose sum is a perfect square | Java code for the above approach . ; Function to find count of subarrays whose sum is a perfect square . ; To search for index with ( current prefix sum - j * j ) ; Storing the prefix sum ; Used to track the minimum value in prefixSum ; Calculating the prefixSum and tracking the prefixMin ; Below statement is used if array contains negative numbers ; Counts the no of subarrays with perfect square sum ; As 0 is a perfect square , so we initialize 0 th index - key with value 1 ; Here we count the perfect square subarray sum by searching if there is a prefix with sum = ( current prefixSum - ( sq * sq ) ) ; Increasing our subarray count ; Increasing the current prefixSum index value in map by 1 to count the other perfect squares while traversing further ; Driver code ; Printing the result
import java . util . * ; class GFG { static long countSubarrays ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int [ ] prefixSum = new int [ n ] ; int prefixMin = 0 ; prefixSum [ 0 ] = arr [ 0 ] ; prefixMin = Math . min ( prefixMin , prefixSum [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; prefixMin = Math . min ( prefixMin , prefixSum [ i ] ) ; } long countSubs = 0 ; mp . put ( 0 , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; prefixSum [ i ] - j * j >= prefixMin ; j ++ ) { if ( mp . containsKey ( prefixSum [ i ] - j * j ) ) countSubs += mp . get ( prefixSum [ i ] - j * j ) ; } if ( mp . containsKey ( prefixSum [ i ] ) ) { mp . put ( prefixSum [ i ] , mp . get ( prefixSum [ i ] ) + 1 ) ; } else { mp . put ( prefixSum [ i ] , 1 ) ; } } return countSubs ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , - 5 , 6 , - 7 , 4 } ; int n = arr . length ; long ans = countSubarrays ( arr , n ) ; System . out . print ( ans ) ; } }
Find if two given Quadratic equations have common roots or not | Java Program to Find if two given quadratic equations have common roots or not ; Function to check if 2 quadratic equations have common roots or not . ; Driver code
class GFG { static boolean checkSolution ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { return ( ( a1 / a2 ) == ( b1 / b2 ) && ( b1 / b2 ) == ( c1 / c2 ) ) ; } public static void main ( String [ ] args ) { float a1 = 1 , b1 = - 5 , c1 = 6 ; float a2 = 2 , b2 = - 10 , c2 = 12 ; if ( checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not | Java program to check if roots of a Quadratic Equation are numerically equal but opposite in sign or not ; Function to find the required answer ; Driver code
import java . util . * ; class GFG { static void checkSolution ( int a , int b , int c ) { if ( b == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String args [ ] ) { int a = 2 , b = 0 , c = 2 ; checkSolution ( a , b , c ) ; } }
Count of index pairs in array whose range product is a positive integer | Java program to find the count of index pairs in the array positive range product ; Condition if number of negative elements is even then increase even_count ; Otherwise increase odd_count ; Condition if current element is negative ; Condition if number of negative elements is even then add even_count in answer ; Otherwise add odd_count in answer ; Driver Code
class GFG { public static void positiveProduct ( int arr [ ] , int n ) { int even_count = 0 ; int odd_count = 0 ; int total_count = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( total_count % 2 == 0 ) { even_count ++ ; } else { odd_count ++ ; } if ( arr [ i ] < 0 ) { total_count ++ ; } if ( total_count % 2 == 0 ) ans += even_count ; else ans += odd_count ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , - 3 , 3 , - 1 , 1 } ; int size = A . length ; positiveProduct ( A , size ) ; } }
Pair of integers having difference of their fifth power as X | Java implementation to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Function to find a pair of integers A & B such that difference of fifth power is equal to the given number X ; Loop to choose every possible pair with in the range ; Check if equation holds ; Driver Code ; Function Call
class GFG { static void findPair ( int x ) { int lim = 120 ; for ( int i = - lim ; i <= lim ; i ++ ) { for ( int j = - lim ; j <= lim ; j ++ ) { if ( Math . pow ( i , 5 ) - Math . pow ( j , 5 ) == x ) { System . out . print ( i + " ▁ " + j + "NEW_LINE"); return ; } } } System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int X = 33 ; findPair ( X ) ; } }
Sum of GCD of all numbers upto N with N itself | Java program to find the Sum of GCD of all integers up to N with N itself ; Function to Find Sum of GCD of each numbers ; Consider all prime factors of no . and subtract their multiples from result ; Check if p is a prime factor ; If yes , then update no and result ; If no has a prime factor greater than Math . sqrt ( n ) then at - most one such prime factor exists ; Return the result ; Finding GCD of pairs ; Calculate the divisors ; Return count of numbers from 1 to N with GCD d with N ; Check if d1 and d2 are equal then skip this ; Driver code
class GFG { static int getCount ( int d , int n ) { int no = n / d ; int result = no ; for ( int p = 2 ; p * p <= no ; ++ p ) { if ( no % p == 0 ) { while ( no % p == 0 ) no /= p ; result -= result / p ; } } if ( no > 1 ) result -= result / no ; return result ; } static int sumOfGCDofPairs ( int n ) { int res = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int d1 = i ; int d2 = n / i ; res += d1 * getCount ( d1 , n ) ; if ( d1 != d2 ) res += d2 * getCount ( d2 , n ) ; } } return res ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . print ( sumOfGCDofPairs ( n ) ) ; } }
Check whether Array represents a Fibonacci Series or not | Java program to check if the elements of a given array can form a Fibonacci Series ; Returns true if a permutation of arr [ 0. . n - 1 ] can form a Fibonacci Series ; Sort array ; After sorting , check if every element is equal to the sum of previous 2 elements ; Driver code
import java . util . Arrays ; class GFG { public static boolean checkIsFibonacci ( int arr [ ] , int n ) { if ( n == 1 n == 2 ) return true ; Arrays . sort ( arr ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( ( arr [ i - 1 ] + arr [ i - 2 ] ) != arr [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 3 , 5 , 13 } ; int n = arr . length ; if ( checkIsFibonacci ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Last digit in a power of 2 | Java program to find last digit in a power of 2. ; Corner case ; Find the shift in current cycle and return value accordingly ; return 6 ; When n % 4 == 0 ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int lastDigit2PowerN ( int n ) { if ( n == 0 ) return 1 ; else if ( n % 4 == 1 ) return 2 ; else if ( n % 4 == 2 ) return 4 ; else if ( n % 4 == 3 ) return 8 ; else } public static void main ( String [ ] args ) { for ( int n = 0 ; n < 20 ; n ++ ) System . out . print ( lastDigit2PowerN ( n ) + " ▁ " ) ; } }
Sum of all subsequences of length K | Java implementation to find sum of all subsequences of length K ; Function to find nCr ; Function that returns factorial of n ; Function for finding sum of all K length subsequences ; Calculate the sum of array ; Calculate nCk ; Return the final result ; Driver code
class GFG { static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int sumSubsequences ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int kLengthSubSequence ; kLengthSubSequence = nCr ( n , k ) ; int ans = sum * ( ( k * kLengthSubSequence ) / n ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 8 , 9 , 2 } ; int K = 2 ; int n = arr . length ; System . out . print ( sumSubsequences ( arr , n , K ) ) ; } }
Sum of first K numbers which are not divisible by N | Java program to calculate the sum of first K numbers not divisible by N ; Function to find the sum ; Find the last multiple of N ; Find the K - th non - multiple of N ; Calculate the sum of all elements from 1 to val ; Calculate the sum of all multiples of N between 1 to val ; Driver code
import java . util . * ; class GFG { static int findSum ( int n , int k ) { int val = ( k / ( n - 1 ) ) * n ; int rem = k % ( n - 1 ) ; if ( rem == 0 ) { val = val - 1 ; } else { val = val + rem ; } int sum = ( val * ( val + 1 ) ) / 2 ; int x = k / ( n - 1 ) ; int sum_of_multiples = ( x * ( x + 1 ) * n ) / 2 ; sum -= sum_of_multiples ; return sum ; } public static void main ( String [ ] args ) { int n = 7 , k = 13 ; System . out . println ( findSum ( n , k ) ) ; } }