codeparrot/xlcost-text-to-code huggingface dataset:
Minimum flips to make all 1 s in right and 0 s in left | Java program to implement the above approach ; Function to find the minimum count of flips required to make all 1 s on the right and all 0 s on the left of the given string ; Stores length of str ; Store count of 0 s in the string ; Traverse the string ; If current character is 0 ; Update zeros ; If count of 0 s in the string is 0 or n ; Store minimum count of flips required to make all 0 s on the left and 1 s on the right ; Stores count of 1 s on the left side of each index ; Stores count of flips required to make all 0 s on the left and 1 s on the right ; Traverse the string ; If current character is 1 ; Update currOnes ; Update flips ; Update the minimum count of flips ; Driver code
import java . io . * ; class GFG { public static int minimumCntOfFlipsRequired ( String str ) { int n = str . length ( ) ; int zeros = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) { zeros ++ ; } } if ( zeros == 0 zeros == n ) { return 0 ; } int minFlips = Integer . MAX_VALUE ; int currOnes = 0 ; int flips ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '1' ) { currOnes ++ ; } flips = currOnes + ( zeros - ( i + 1 - currOnes ) ) ; minFlips = Math . min ( minFlips , flips ) ; } return minFlips ; } public static void main ( String [ ] args ) { String s1 = "100101" ; System . out . println ( minimumCntOfFlipsRequired ( s1 ) ) ; } }
Maximize length of longest non | Java program to implement the above approach ; Function to find the length of the longest non - decreasing array that can be generated ; Stores the length of the longest non - decreasing array that can be generated from the array ; Stores index of start pointer ; Stores index of end pointer ; Stores previously inserted element into the new array ; Traverse the array ; If A [ start ] is less than or equal to A [ end ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update start ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ end ] is greater than A [ start ] ; If no element inserted into the newly generated array ; Update prev ; Update res ; Update end ; If A [ end ] is greater than or equal to prev ; Update res ; Update prev ; Update end ; If A [ start ] is greater than or equal to prev ; Update res ; Update prev ; Update start ; Driver Code ; Function call
import java . util . * ; class GFG { static int findLongestNonDecreasing ( int A [ ] , int N ) { int res = 0 ; int start = 0 ; int end = N - 1 ; int prev = - 1 ; while ( start <= end ) { if ( A [ start ] <= A [ end ] ) { if ( prev == - 1 ) { prev = A [ start ] ; res ++ ; start ++ ; } else { if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else { break ; } } } else { if ( prev == - 1 ) { prev = A [ end ] ; res ++ ; end -- ; } else { if ( A [ end ] >= prev ) { res ++ ; prev = A [ end ] ; end -- ; } else if ( A [ start ] >= prev ) { res ++ ; prev = A [ start ] ; start ++ ; } else { break ; } } } } return res ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 1 , 3 , 5 , 4 , 3 , 6 , 2 , 1 } ; int N = A . length ; System . out . print ( findLongestNonDecreasing ( A , N ) ) ; } }
Non | Java program for the above approach ; Function to print all pairs whose sum of Bitwise OR and AND is N ; Iterate from i = 0 to N ; Print pairs ( i , N - i ) ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static void findPairs ( int N ) { for ( int i = 0 ; i <= N ; i ++ ) { System . out . print ( " ( " + i + " , ▁ " + ( N - i ) + " ) , ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 ; findPairs ( N ) ; } }
Minimum index to split array into subarrays with co | Java program for the above approach ; Function to find the GCD of 2 numbers ; Base Case ; Find the GCD recursively ; Function to find the minimum partition index K s . t . product of both subarrays around that partition are co - prime ; Stores the prefix and suffix array product ; Update the prefix array ; Update the suffix array ; Iterate the given array ; Check if prefix [ k ] and suffix [ k + 1 ] are co - prime ; If no index for partition exists , then return - 1 ; Driver Code ; Function call
import java . util . * ; class solution { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int findPartition ( int nums [ ] , int N ) { int [ ] prefix = new int [ N ] ; int [ ] suffix = new int [ N ] ; int i , k ; prefix [ 0 ] = nums [ 0 ] ; for ( i = 1 ; i < N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] * nums [ i ] ; } suffix [ N - 1 ] = nums [ N - 1 ] ; for ( i = N - 2 ; i >= 0 ; i -- ) { suffix [ i ] = suffix [ i + 1 ] * nums [ i ] ; } for ( k = 0 ; k < N - 1 ; k ++ ) { if ( GCD ( prefix [ k ] , suffix [ k + 1 ] ) == 1 ) { return k ; } } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 5 } ; int N = arr . length ; System . out . println ( findPartition ( arr , N ) ) ; } }
Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd | Java program for the above approach ; Function to count reversals to separate elements with same parity ; Traverse the given array ; Count size of subarray having integers with same parity only ; Otherwise ; Reversals required is equal to one less than subarray size ; Return the total reversals ; Function to print the array elements ; Function to count the minimum reversals required to make make sum of all adjacent elements odd ; Stores operations required for separating adjacent odd elements ; Stores operations required for separating adjacent even elements ; Maximum of two is the return ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
import java . util . * ; class GFG { static int separate ( int arr [ ] , int n , int parity ) { int count = 1 , res = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( ( arr [ i ] + parity ) & 1 ) != 0 && ( ( arr [ i - 1 ] + parity ) & 1 ) != 0 ) count ++ ; else { if ( count > 1 ) res += count - 1 ; count = 1 ; } } return res ; } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . println ( arr [ i ] + " ▁ " ) ; } static void requiredOps ( int arr [ ] , int N ) { int res1 = separate ( arr , N , 0 ) ; int res2 = separate ( arr , N , 1 ) ; System . out . print ( Math . max ( res1 , res2 ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 13 , 2 , 6 , 8 , 3 , 5 , 7 , 10 , 14 , 15 } ; int N = arr . length ; requiredOps ( arr , N ) ; } }
Count prime pairs whose difference is also a Prime Number | Java program to implement the above approach ; Function to find all prime numbers in the range [ 1 , N ] ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Update isPrime ; Function to count pairs of prime numbers in the range [ 1 , N ] whose difference is prime ; Function to count pairs of prime numbers whose difference is also a prime number ; isPrime [ i ] : Stores if i is a prime number or not ; Iterate over the range [ 2 , N ] ; If i and i - 2 is a prime number ; Update cntPairs ; Driver Code
import java . util . * ; class GFG { public static boolean [ ] SieveOfEratosthenes ( int N ) { boolean [ ] isPrime = new boolean [ N + 1 ] ; Arrays . fill ( isPrime , true ) ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( isPrime [ p ] ) { for ( int i = p * p ; i <= N ; i += p ) { isPrime [ i ] = false ; } } } return isPrime ; } public static int cntPairsdiffOfPrimeisPrime ( int N ) { int cntPairs = 0 ; boolean [ ] isPrime = SieveOfEratosthenes ( N ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime [ i ] && isPrime [ i - 2 ] ) { cntPairs += 2 ; } } return cntPairs ; } public static void main ( String args [ ] ) { int N = 5 ; System . out . println ( cntPairsdiffOfPrimeisPrime ( N ) ) ; } }
Length of longest subsequence consisting of distinct adjacent elements | Java program for the above approach ; Function that finds the length of longest subsequence having different adjacent elements ; Stores the length of the longest subsequence ; Traverse the array ; If previous and current element are not same ; Increment the count ; Print the maximum length ; Driver Code ; Size of Array ; Function Call
import java . util . * ; class GFG { static void longestSubsequence ( int arr [ ] , int N ) { int count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 8 , 1 , 2 , 2 , 5 , 5 , 1 } ; int N = arr . length ; longestSubsequence ( arr , N ) ; } }
Count of substrings having the most frequent character in the string as first character | Java program for the above approach ; Function to find all substrings whose first character occurs maximum number of times ; Stores frequency of characters ; Stores character that appears maximum number of times ; Stores max frequency of character ; Updates frequency of characters ; Update maxfreq ; Character that occures maximum number of times ; Update the maximum frequency character ; Stores all count of substrings ; Traverse over string ; Get the current character ; Update count of substrings ; Return the count of all valid substrings ; Driver Code ; Function Call
import java . util . * ; class GFG { static int substringCount ( String s ) { int [ ] freq = new int [ 26 ] ; char max_char = ' # ' ; int maxfreq = Integer . MIN_VALUE ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; if ( maxfreq < freq [ s . charAt ( i ) - ' a ' ] ) maxfreq = freq [ s . charAt ( i ) - ' a ' ] ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( maxfreq == freq [ i ] ) { max_char = ( char ) ( i + ' a ' ) ; break ; } } int ans = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char ch = s . charAt ( i ) ; if ( max_char == ch ) { ans += ( s . length ( ) - i ) ; } } return ans ; } public static void main ( String [ ] args ) { String S = " abcab " ; System . out . println ( substringCount ( S ) ) ; } }
Minimum sum possible by assigning every increasing / decreasing consecutive pair with values in that order | Java program for the above approach ; Function to print the minimum sum of values assigned to each element of the array as per given conditions ; Initialize vectors with value 1 ; Traverse from left to right ; Update if ans [ i ] > ans [ i - 1 ] ; Traverse from right to left ; Update as ans [ i ] > ans [ i + 1 ] if arr [ i ] > arr [ i + 1 ] ; Find the minimum sum ; Print the sum ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static void minSum ( int [ ] arr , int n ) { int [ ] ans = new int [ n ] ; Arrays . fill ( ans , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { ans [ i ] = Math . max ( ans [ i ] , ans [ i - 1 ] + 1 ) ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { ans [ i ] = Math . max ( ans [ i ] , ans [ i + 1 ] + 1 ) ; } } int s = 0 ; for ( int x : ans ) { s = s + x ; } System . out . print ( s + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 } ; int N = arr . length ; minSum ( arr , N ) ; } }
Minimum removal of consecutive similar characters required to empty a Binary String | Java program for the above approach ; Function to find minimum steps to make the String empty ; Stores the modified String ; Size of String ; Removing subString of same character from modified String ; Print the minimum steps required ; Driver Code ; Given String S ; Function Call
import java . util . * ; class GFG { static void minSteps ( String S ) { String new_str = " " ; int N = S . length ( ) ; int i = 0 ; while ( i < N ) { new_str += S . charAt ( i ) ; int j = i ; while ( i < N && S . charAt ( i ) == S . charAt ( j ) ) ++ i ; } System . out . print ( ( int ) Math . ceil ( ( new_str . length ( ) + 1 ) / 2.0 ) ) ; } public static void main ( String [ ] args ) { String S = "0010100" ; minSteps ( S ) ; } }
Count pairs with equal Bitwise AND and Bitwise OR value | Java program to implement the above approach ; Function to count pairs in an array whose bitwise AND equal to bitwise OR ; Store count of pairs whose bitwise AND equal to bitwise OR ; Stores frequency of distinct elements of array ; Traverse the array ; Increment the frequency of arr [ i ] ; Traverse map ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int countPairs ( int [ ] arr , int N ) { int cntPairs = 0 ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { mp . put ( arr [ i ] , mp . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } for ( Map . Entry < Integer , Integer > freq : mp . entrySet ( ) ) { cntPairs += ( freq . getValue ( ) * ( freq . getValue ( ) - 1 ) ) / 2 ; } return cntPairs ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 1 , 2 , 2 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }
Minimum increments required to make given matrix palindromic | Java program for the above approach ; Function to evaluate minimum number of operation required to convert the matrix to a palindrome matrix ; Variable to store number of operations required ; Iterate over the first quadrant of the matrix ; Store positions of all four values from four quadrants ; Store the values having unique indexes ; Largest value in the values vector ; Evaluate minimum increments required to make all vector elements equal ; Print the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void palindromeMatrix ( int N , int M , int [ ] [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < ( N + 1 ) / 2 ; i ++ ) { for ( int j = 0 ; j < ( M + 1 ) / 2 ; j ++ ) { HashSet < pair > s = new HashSet < > ( ) ; s . add ( new pair ( i , j ) ) ; s . add ( new pair ( i , M - j - 1 ) ) ; s . add ( new pair ( N - i - 1 , j ) ) ; s . add ( new pair ( N - i - 1 , M - j - 1 ) ) ; Vector < Integer > values = new Vector < > ( ) ; for ( pair p : s ) { values . add ( arr [ p . first ] [ p . second ] ) ; } int max = Collections . max ( values ) ; for ( int k = 1 ; k < values . size ( ) ; k ++ ) { ans += max - values . get ( k ) ; } } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; int [ ] [ ] arr = { { 1 , 2 , 1 } , { 3 , 4 , 1 } , { 1 , 2 , 1 } } ; palindromeMatrix ( N , M , arr ) ; } }
Minimum division by 10 and multiplication by 2 required to reduce given number to 1 | Java program for the above approach ; Function to find the minimum number operations required to reduce N to 1 ; Stores count of powers of 2 and 5 ; Calculating the primefactors 2 ; Calculating the primefactors 5 ; If n is 1 and cnt2 <= cnt5 ; Return the minimum operations ; Otherwise , n can 't be reduced ; Driver Code ; Given Number N ; Function Call
import java . util . * ; class GFG { static int minimumMoves ( int n ) { int cnt2 = 0 , cnt5 = 0 ; while ( n % 2 == 0 ) { n /= 2 ; cnt2 ++ ; } while ( n % 5 == 0 ) { n /= 5 ; cnt5 ++ ; } if ( n == 1 && cnt2 <= cnt5 ) { return 2 * cnt5 - cnt2 ; } else return - 1 ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . print ( minimumMoves ( N ) ) ; } }
Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non | Java Program to implement the above approach ; Function to check if a permutation of first N natural numbers exist with Bitwise AND of adjacent elements not equal to 0 ; If n is a power of 2 ; Driver Code
import java . util . * ; class solution { static void check ( int n ) { if ( ( n & n - 1 ) != 0 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String args [ ] ) { int n = 5 ; check ( n ) ; } }
Minimize adding odd and subtracting even numbers to make all array elements equal to K | Java program to implement the above approach ; Function to find the minimum operations required to make array elements equal to K ; Stores minimum count of operations ; Traverse the given array ; If K is greater than arr [ i ] ; If ( K - arr [ i ] ) is even ; Update cntOpe ; Update cntOpe ; If K is less than arr [ i ] ; If ( arr [ i ] - K ) is even ; Update cntOpe ; Update cntOpe ; Driver code
class GFG { public static int MinOperation ( int arr [ ] , int N , int K ) { int cntOpe = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( K > arr [ i ] ) { if ( ( K - arr [ i ] ) % 2 == 0 ) { cntOpe += 2 ; } else { cntOpe += 1 ; } } else if ( K < arr [ i ] ) { if ( ( K - arr [ i ] ) % 2 == 0 ) { cntOpe += 1 ; } else { cntOpe += 2 ; } } } return cntOpe ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 7 , 2 , 1 , 3 } ; int K = 5 ; int N = arr . length ; System . out . println ( MinOperation ( arr , N , K ) ) ; } }
Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same | Java program for the above approach ; Function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store frequency of elements ; Stores xor value ; Taking xor of all the values of both arrays ; Store frequency of B [ ] ; Find the array B [ ] after rearrangement ; If the updated value is present then decrement its frequency ; Otherwise return empty vector ; Utility function to rearrange the array B [ ] such that A [ i ] ^ B [ i ] is same for each element ; Store rearranged array B ; If rearrangement possible ; Otherwise return - 1 ; Driver Code ; Given vector A ; Given vector B ; Size of the vector ; Function Call
import java . io . * ; import java . util . * ; class GFG { static ArrayList < Integer > rearrangeArray ( ArrayList < Integer > A , ArrayList < Integer > B , int N ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; int xor_value = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xor_value ^= A . get ( i ) ; xor_value ^= B . get ( i ) ; m . put ( B . get ( i ) , m . getOrDefault ( B . get ( i ) + 1 , 0 ) ) ; } for ( int i = 0 ; i < N ; i ++ ) { B . set ( i , A . get ( i ) ^ xor_value ) ; if ( m . getOrDefault ( B . get ( i ) , - 1 ) != - 1 ) { m . put ( B . get ( i ) , m . getOrDefault ( B . get ( i ) , 0 ) - 1 ) ; } else return ( new ArrayList < Integer > ( ) ) ; } return B ; } static void rearrangeArrayUtil ( ArrayList < Integer > A , ArrayList < Integer > B , int N ) { ArrayList < Integer > ans = rearrangeArray ( A , B , N ) ; if ( ans . size ( ) != 0 ) { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } else { System . out . println ( " - 1" ) ; } } public static void main ( String [ ] args ) { ArrayList < Integer > A = new ArrayList < Integer > ( Arrays . asList ( 13 , 21 , 33 , 49 , 53 ) ) ; ArrayList < Integer > B = new ArrayList < Integer > ( Arrays . asList ( 54 , 50 , 34 , 22 , 14 ) ) ; int N = ( int ) A . size ( ) ; rearrangeArrayUtil ( A , B , N ) ; } }
Count sequences of given length having non | Java program for the above approach ; Function to find the Binomial Coefficient C ( n , r ) ; Stores the value C ( n , r ) ; Update C ( n , r ) = C ( n , n - r ) ; Find C ( n , r ) iteratively ; Return the final value ; Function to find number of sequence whose prefix sum at each index is always non - negative ; Find n ; Value of C ( 2 n , n ) ; Catalan number ; Print the answer ; Driver Code ; Given M and X ; Function Call
import java . io . * ; class GFG { static long binCoff ( long n , long r ) { long val = 1 ; int i ; if ( r > ( n - r ) ) r = ( n - r ) ; for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } static void findWays ( int M ) { int n = M / 2 ; long a , b , ans ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; System . out . print ( b ) ; } public static void main ( String [ ] args ) { int M = 4 , X = 5 ; findWays ( M ) ; } }
XOR of array elements whose modular inverse with a given number exists | Java program for the above approach ; Function to return the gcd of a & b ; Base Case ; Recursively calculate GCD ; Function to print the Bitwise XOR of elements of arr [ ] if gcd ( arr [ i ] , M ) is 1 ; Initialize xor ; Traversing the array ; GCD of M and arr [ i ] ; If GCD is 1 , update xor ; Print xor ; Drive Code ; Given array arr [ ] ; Given number M ; Size of the array ; Function Call
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void countInverse ( int [ ] arr , int N , int M ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int gcdOfMandelement = gcd ( M , arr [ i ] ) ; if ( gcdOfMandelement == 1 ) { XOR ^= arr [ i ] ; } } System . out . println ( XOR ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int M = 4 ; int N = arr . length ; countInverse ( arr , N , M ) ; } }
Count positions in a chessboard that can be visited by the Queen which are not visited by the King | Java program for the above approach ; Function to print the number of cells only visited by the queen ; Find all the moves ; Find all moves for x + 1 , y + 1 ; Find all moves for x - 1 , y - 1 ; Find all moves for x - 1 , y + 1 ; Find all moves for x + 1 , y - 1 ; Find all squares visited by King x + 1 , in same row ; x - 1 , in same row ; y + 1 , in same column ; y - 1 , in same column ; Return answer ; Driver Code ; Dimension of Board ; Position of Cell ; Function Call
import java . io . * ; class GFG { static int Moves_Calculator ( int x , int y , int row , int col ) { int total_moves = 0 ; if ( ( row - x ) > 0 && ( col - y ) > 0 ) total_moves += Math . min ( ( row - x ) , ( col - y ) ) ; if ( ( y - 1 ) > 0 && ( x - 1 ) > 0 ) total_moves += Math . min ( ( y - 1 ) , ( x - 1 ) ) ; if ( ( x - 1 ) > 0 && ( col - y ) > 0 ) total_moves += Math . min ( ( x - 1 ) , ( col - y ) ) ; if ( ( row - x ) > 0 && ( y - 1 ) > 0 ) total_moves += Math . min ( ( row - x ) , ( y - 1 ) ) ; total_moves += ( row - 1 ) + ( col - 1 ) ; int king_moves = 0 ; if ( x + 1 <= row ) king_moves += 1 ; if ( x - 1 > 0 ) king_moves += 1 ; if ( y + 1 <= col ) king_moves += 1 ; if ( y - 1 > 0 ) king_moves += 1 ; if ( x + 1 <= row && y + 1 <= col ) king_moves += 1 ; if ( x + 1 <= row && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y + 1 <= col ) king_moves += 1 ; return total_moves - king_moves ; } public static void main ( String [ ] args ) { int n = 8 , m = 8 ; int x = 1 , y = 1 ; System . out . println ( Moves_Calculator ( x , y , m , n ) ) ; } }
Nearest smaller number to N having multiplicative inverse under modulo N equal to that number | Java program to implement the above approach ; Function to find the nearest smaller number satisfying the condition ; Driver Code
import java . io . * ; class GFG { static int clstNum ( int N ) { return ( N - 1 ) ; } public static void main ( String [ ] args ) { int N = 11 ; System . out . println ( clstNum ( N ) ) ; } }
Count array elements having modular inverse under given prime number P equal to itself | Java program to implement the above approach ; Function to get the count of elements that satisfy the given condition . ; Stores count of elements that satisfy the condition ; Traverse the given array . ; If square of current element is equal to 1 ; Driver Code
import java . io . * ; class GFG { static int equvInverse ( int [ ] arr , int N , int P ) { int cntElem = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] * arr [ i ] ) % P == 1 ) { cntElem ++ ; } } return cntElem ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 6 , 4 , 5 } ; int N = arr . length ; int P = 7 ; System . out . println ( equvInverse ( arr , N , P ) ) ; } }