codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs from an array with even product of count of distinct prime factors | Java program for the above approach ; Function to calculate count of distinct prime factors of a number ; Sieve of Eratosthenes ; Function to count pairs with even product of distinct prime factors ; Stores count of distinct prime factors ; Stores the count of numbers with even prime factors in B [ ] ; Stores the count of numbers with odd prime factors in B [ ] ; Even Product Pairs ; Traverse the array B [ ] ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; Increment oddCount by 1 ; Increment evenCount by 1 ; Since , product has to be positive i . e > 0 ; If count of prime factors is odd ; odd * even = even ; If count of prime factors is even ; even * odd = even even * even = even ; Driver Code
import java . util . * ; class GFG { static int MAX = 1000000 ; static void countOfPrimefactors ( int [ ] CountDistinct ) { boolean [ ] prime = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } } static int CountEvenPair ( int A [ ] , int B [ ] , int N , int M ) { int [ ] countDistinct = new int [ ( MAX + 1 ) ] ; countOfPrimefactors ( countDistinct ) ; int evenCount = 0 ; int oddCount = 0 ; int evenPairs = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( countDistinct [ B [ i ] ] == 0 ) continue ; if ( ( countDistinct [ B [ i ] ] & 1 ) != 0 ) { oddCount ++ ; } else { evenCount ++ ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( countDistinct [ A [ i ] ] == 0 ) continue ; if ( ( countDistinct [ A [ i ] ] & 1 ) != 0 ) { evenPairs += ( evenCount ) ; } else { evenPairs += evenCount + oddCount ; } } return evenPairs ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 4 , 5 , 6 } ; int N = A . length ; int M = B . length ; System . out . println ( CountEvenPair ( A , B , N , M ) ) ; } }
Minimum array elements required to be subtracted from either end to reduce K to 0 | Java program for the above approach ; Function to find the length of longest subarray having sum K ; Stores the index of the prefix sum ; Traverse the given array ; Update sum ; If the subarray starts from index 0 ; Add the current prefix sum with index if it is not present in the map um ; Check if sum - K is present in Map um or not ; Update the maxLength ; Return the required maximum length ; Function to find the minimum removal of array elements required to reduce K to 0 ; Stores the sum of the array ; Traverse the array arr [ ] ; Update sum of the array ; Find maxLen ; If the subarray with sum doesn 't exist ; Otherwise , print the required maximum length ; Driver Code
import java . util . * ; class GFG { static int longestSubarray ( int [ ] arr , int N , int K ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; if ( sum == K ) maxLen = i + 1 ; if ( ! um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - K ) ) { if ( maxLen < ( i - um . get ( sum - K ) ) ) maxLen = i - um . get ( sum - K ) ; } } return maxLen ; } static void minRequiredOperation ( int [ ] arr , int N , int K ) { int TotalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) TotalSum += arr [ i ] ; int maxLen = longestSubarray ( arr , N , TotalSum - K ) ; if ( maxLen == - 1 ) { System . out . println ( - 1 ) ; } else System . out . println ( N - maxLen ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 1 , 2 } ; int K = 4 ; int N = arr . length ; minRequiredOperation ( arr , N , K ) ; } }
Minimum number of digits required to be removed to make a number divisible by 4 | Java program for the above approach ; Function to count the minimum number of digits required to be removed to make a given number divisible by 4 ; Store the size of the string ; Stores the required result ; Check for every pair of digits if the number formed by them is divisible by 4 or not ; Store s [ i ] in a variable ; If it is divisible by 2 ; Store the number formed by s [ j ] and s [ i ] ; Check if it is divisible by 4 ; Store the number of digits required to be deleted ; Update ans ; If value of ans is unchanged , then check if any s [ i ] is divisible by 4 ; If true , update ans to n - 1 ; Print the result ; Driver Code
import java . util . * ; class GFG { static void minimumDeletions ( String s ) { int n = s . length ( ) ; int ans = n ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int t = s . charAt ( i ) - '0' ; if ( t % 2 == 0 ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { int num = ( s . charAt ( j ) - '0' ) * 10 + t ; if ( num % 4 == 0 ) { int k1 = i - j - 1 ; int k2 = n - i - 1 ; ans = Math . min ( ans , k1 + k2 ) ; } } } } if ( ans == n ) { for ( int i = 0 ; i < n ; i ++ ) { int num = s . charAt ( i ) - '0' ; if ( num % 4 == 0 ) { ans = n - 1 ; } } } System . out . println ( ans ) ; } static public void main ( String [ ] args ) { String str = "12367" ; minimumDeletions ( str ) ; } }
Generate a sequence such that float division of array elements is maximized | Java program for the above approach ; Function to place the parenthesis such that the result is maximized ; Store the required string ; Add the first integer to string ; If the size of array is 1 ; If the size of array is 2 , print the 1 st integer followed by / operator followed by the next integer ; If size of array is exceeds two , print 1 st integer concatenated with operators ' / ' , ' ( ' and next integers with the operator ' / ' ; Add parenthesis at the end ; Print the final expression ; Driver Code
import java . io . * ; class GFG { static void generateSequence ( int arr [ ] , int n ) { String ans ; ans = Integer . toString ( arr [ 0 ] ) ; if ( n == 1 ) System . out . println ( ans ) ; else if ( n == 2 ) { System . out . println ( ans + " / " + Integer . toString ( arr [ 1 ] ) ) ; } else { ans += " / ( " + Integer . toString ( arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { ans += " / " + Integer . toString ( arr [ i ] ) ; } ans += " ) " ; System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1000 , 100 , 10 , 2 } ; int N = arr . length ; generateSequence ( arr , N ) ; } }
Count of pairs having even and odd LCM from an array | Java program to implement the above approach ; Function to find count of distinct pairs having even LCM and odd LCM ; Store the total number of pairs ; Stores the count of odd numbers in the array ; Traverse the array arr [ ] ; Update the count of pairs with odd LCM ; Print the count of required pairs ; Driver Code
import java . util . * ; class GFG { static void LCMPairs ( int arr [ ] , int N ) { int total_pairs = ( N * ( N - 1 ) ) / 2 ; int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; } odd = ( odd * ( odd - 1 ) ) / 2 ; System . out . println ( " Even ▁ = ▁ " + ( total_pairs - odd ) + " , ▁ Odd ▁ = ▁ " + odd ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 5 , 4 } ; int N = arr . length ; LCMPairs ( arr , N ) ; } }
Value required to be added to N to obtain the sum of first M multiples of K | Java code of above approach ; Function to print the value to be added to N to obtain sum of first M multiples of K ; Store the sum of the first M multiples of K ; Store the value to be added to obtain N ; Driver code ; Input
import java . util . * ; class GFG { static int printNumber ( int N , int K , int M ) { int sum = K * ( M * ( M + 1 ) / 2 ) ; return sum - N ; } public static void main ( String [ ] args ) { int N = 17 ; int K = 3 ; int M = 4 ; System . out . print ( printNumber ( N , K , M ) ) ; } }
Count even and odd Bitwise XORs of consecutive numbers in a range [ L , R ] starting from L | Java program for the above approach ; Print count of even and odd numbers of XOR value from L to R ; Store the number of elements between L and R ; Count of even XOR values ; If L is odd and range % 4 = 3 ; Increment even by 1 ; If L is even and range % 4 != 0 ; Increment even by 1 ; Print the answer ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void countEvenOdd ( int L , int R ) { int range = R - L + 1 ; int even = ( range / 4 ) * 2 ; if ( ( L & 1 ) != 0 && ( range % 4 == 3 ) ) { even ++ ; } else if ( ( L & 1 ) == 0 && ( range % 4 != 0 ) ) { even ++ ; } System . out . print ( " Even ▁ = ▁ " + even + " , ▁ Odd ▁ = ▁ " + ( range - even ) ) ; } public static void main ( String [ ] args ) { int L = 2 , R = 7 ; countEvenOdd ( L , R ) ; } }
Minimize swaps of same | Java program for the above approach ; Function to count the number of swaps required to make the sum of ASCII values of the characters of both strings odd ; Initialize alphabets with value ; Initialize values for each alphabet ; Size of the string ; Sum of string S ; Sum of string T ; Stores whether there is any index i such that S [ i ] and T [ i ] have different parities ; Traverse the strings ; Update sum1 and sum2 ; If S [ i ] and T [ i ] have different parities ; If sum1 and sum2 are both odd ; If sum1 and sum2 are both even ; If exists print 1 ; Otherwise ; If sum1 and sum2 are of different parities ; Driver Code ; Function Call
import java . io . * ; class GFG { static void countSwaps ( String S , String T ) { int [ ] value = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) value [ i ] = i + 1 ; int N = S . length ( ) ; int sum1 = 0 ; int sum2 = 0 ; boolean flag = false ; for ( int i = 0 ; i < N ; i ++ ) { sum1 += value [ S . charAt ( i ) - ' a ' ] ; sum2 += value [ T . charAt ( i ) - ' a ' ] ; if ( ( value [ S . charAt ( i ) - ' a ' ] % 2 == 0 && value [ T . charAt ( i ) - ' a ' ] % 2 == 1 ) || ( value [ S . charAt ( i ) - ' a ' ] % 2 == 1 && value [ T . charAt ( i ) - ' a ' ] % 2 == 0 ) ) flag = false ; } if ( sum1 % 2 == 1 && sum2 % 2 == 1 ) System . out . println ( "0NEW_LINE"); else if ( sum1 % 2 == 0 && sum2 % 2 == 0 ) { if ( flag ) System . out . println ( "1" ) ; else System . out . println ( " - 1" ) ; } else { System . out . println ( " - 1" ) ; } } public static void main ( String [ ] args ) { String S = " acd " ; String T = " dbf " ; countSwaps ( S , T ) ; } }
Count distinct prime triplets up to N such that sum of two primes is equal to the third prime | Java program for the above approach ; Function to check if a number is a prime or not ; Function to count the number of valid prime triplets ; Stores the count of prime triplets ; Iterate from 2 to N and check for each p , whether p & ( p - 2 ) are prime or not ; Print the count obtained ; Driver Code
import java . io . * ; 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 void countPrimeTuples ( int N ) { int count = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime ( i ) && isPrime ( i - 2 ) ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int N = 6 ; countPrimeTuples ( N ) ; } }
Count pairs from an array whose Bitwise OR is greater than Bitwise AND | Java program to implement the above approach ; Function to count the number of pairs ( i , j ) their Bitwise OR is greater than Bitwise AND ; Store the required answer ; Check for all possible pairs ; If the condition satisfy then increment count by 1 ; Print the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static void countPairs ( int A [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( A [ i ] A [ j ] ) > ( A [ i ] & A [ j ] ) ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 4 , 7 } ; int N = A . length ; countPairs ( A , N ) ; } }
Count maximum number of consumable candies | Java implememtation of the above approach ; Function to find the count of maximum consumable candies ; Store the count of total candies ; Stores the count of maximum consumable candies ; Checks if it is safe to counsume all candies ; Traverse the array arr [ ] ; If A [ i ] + M is greater than B [ i ] ; Mark all_safe as false ; Update ans ; Update ans ; Increment total by A [ i ] ; If all_safe is true ; Otherwise , ; Driver Code ; Function call to find maximum consumable candies
public class GFG { static int maximumCandy ( int [ ] candies , int [ ] safety , int N , int M ) { int total = 0 ; int ans = Integer . MAX_VALUE ; boolean all_safe = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( candies [ i ] + M > safety [ i ] ) { all_safe = false ; ans = Math . min ( ans , safety [ i ] ) ; } else { ans = Math . min ( ans , candies [ i ] + M ) ; } total += candies [ i ] ; } if ( all_safe ) return total ; else return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 5 , 2 , 3 } ; int B [ ] = { 8 , 13 , 6 , 4 } ; int M = 5 ; int N = A . length ; System . out . println ( maximumCandy ( A , B , N , M ) ) ; } }
Check if two arrays can be made equal by swapping pairs of one of the arrays | Java program for above approach ; Function to check if two arrays can be made equal or not by swapping pairs of only one of the arrays ; Stores elements required to be replaced ; To check if the arrays can be made equal or not ; Traverse the array ; If array elements are not equal ; Increment count by 1 ; Decrement count by 1 ; If flag is true and count is 0 , print " Yes " . Otherwise " No " ; Driver Code ; Given arrays ; Size of the array
public class GFG { static void checkArrays ( int arr1 [ ] , int arr2 [ ] , int N ) { int count = 0 ; boolean flag = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { if ( arr1 [ i ] == 0 ) count ++ ; else { count -- ; if ( count < 0 ) { flag = false ; break ; } } } } if ( ( flag && ( count == 0 ) ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 0 , 0 , 1 , 1 } ; int arr2 [ ] = { 1 , 1 , 0 , 0 } ; int N = arr1 . length ; checkArrays ( arr1 , arr2 , N ) ; } }
Generate an N | Java program to implement the above approach ; Function to construct an array with sum of each subarray divisible by K ; Traverse a loop from 1 to N ; Print i - th multiple of K ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void construct_Array ( int N , int K ) { for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( K * i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 3 , K = 3 ; construct_Array ( N , K ) ; } }
Generate an N | Java Program to implement the above approach ; Fun dtion to print the required sequence ; Stores count of even and odd elements ; Stores sum of even and odd elements ; Print N / 2 even elements ; Print N / 2 - 1 odd elements ; Print final odd element ; Driver Code
import java . io . * ; class GFG { static void Print ( int N ) { if ( ( N / 2 ) % 2 == 1 || ( N % 2 == 1 ) ) { System . out . print ( - 1 ) ; return ; } int CurEven = 2 , CurOdd = 1 ; int SumOdd = 0 , SumEven = 0 ; for ( int i = 0 ; i < ( N / 2 ) ; i ++ ) { System . out . print ( CurEven + " ▁ " ) ; SumEven += CurEven ; CurEven += 2 ; } for ( int i = 0 ; i < N / 2 - 1 ; i ++ ) { System . out . print ( CurOdd + " ▁ " ) ; SumOdd += CurOdd ; CurOdd += 2 ; } CurOdd = SumEven - SumOdd ; System . out . println ( CurOdd ) ; } public static void main ( String [ ] args ) { int N = 12 ; Print ( N ) ; } }
Find the last remaining element after repeated removal of an element from pairs of increasing adjacent array elements | Java program for the above approach ; Function to print the last remaining array element after after performing given operations ; If size of the array is 1 ; Check for the condition ; If condition is not satisfied ; Driver Code ; Function Call
import java . io . * ; class GFG { static void canArrayBeReduced ( int [ ] arr , int N ) { if ( N == 1 ) { System . out . print ( arr [ 0 ] ) ; return ; } if ( arr [ 0 ] < arr [ N - 1 ] ) { System . out . print ( arr [ N - 1 ] ) ; } else System . out . print ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 6 , 5 , 2 , 4 , 1 , 3 , 7 } ; int N = arr . length ; canArrayBeReduced ( arr , N ) ; } }
Minimum prefix increments required to make all elements of an array multiples of another array | Java program for the above approach ; Function to find minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores minimum count of operations required to make A [ i ] multiple of B [ i ] by incrementing prefix subarray ; Stores the carry ; Stores minimum difference of correspoinding element in prefix subarray ; Traverse the array ; Stores the closest greater or equal number to A [ i ] which is a multiple of B [ i ] ; Stores minimum difference ; Update totalOperations ; Update carry ; Driver Code ; Input arrays A [ ] and B [ ] ; Length of arrays
import java . util . * ; class GFG { static int MinimumMoves ( int A [ ] , int B [ ] , int N ) { int totalOperations = 0 ; int carry = 0 ; int K = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { int nearestMultiple = ( int ) ( Math . ceil ( ( double ) ( A [ i ] + carry ) / ( double ) ( B [ i ] ) ) * B [ i ] ) ; K = nearestMultiple - ( A [ i ] + carry ) ; totalOperations += K ; carry += K ; } return totalOperations ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 4 , 5 , 2 , 5 , 5 , 9 } ; int B [ ] = { 1 , 1 , 9 , 6 , 3 , 8 , 7 } ; int N = A . length ; System . out . print ( MinimumMoves ( A , B , N ) + "NEW_LINE"); } }
Minimize deviation of an array by given operations | Java program for the above approach ; Function to find the minimum deviation of the array A [ ] ; Store all array elements in sorted order ; Odd number are transformed using 2 nd operation ; ( Maximum - Minimum ) ; Check if the size of set is > 0 and the maximum element is divisible by 2 ; Maximum element of the set ; Erase the maximum element ; Using operation 1 ; ( Maximum - Minimum ) ; Print the Minimum Deviation Obtained ; Driver code ; Function Call to find Minimum Deviation of A [ ]
import java . io . * ; import java . util . * ; class GFG { static void minimumDeviation ( int A [ ] , int N ) { TreeSet < Integer > s = new TreeSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) s . add ( A [ i ] ) ; else s . add ( 2 * A [ i ] ) ; } int diff = s . last ( ) - s . first ( ) ; while ( ( s . last ( ) % 2 == 0 ) ) { int maxEl = s . last ( ) ; s . remove ( maxEl ) ; s . add ( maxEl / 2 ) ; diff = Math . min ( diff , s . last ( ) - s . first ( ) ) ; } System . out . print ( diff ) ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 1 , 5 , 20 , 3 } ; int N = A . length ; minimumDeviation ( A , N ) ; } }
Maximize given integer by swapping pairs of unequal bits | Java implementation of the above approach ; Function to return the maximum possible value that can be obtained from the given integer by performing given operations ; Convert to equivalent binary representation ; Stores binary representation of the maximized value ; Store the count of 0 ' s ▁ and ▁ 1' s ; Stores the total Number of Bits ; If current bit is set ; Increment Count1 ; Increment Count0 ; Shift all set bits to left to maximize the value ; Shift all unset bits to right to maximize the value ; Return the maximized value ; Driver Code ; Given "Input ; Function call to find the Maximum Possible Number
import java . io . * ; class GFG { public static int findMaxNum ( int num ) { String binaryNumber = Integer . toBinaryString ( num ) ; String maxBinaryNumber = " " ; int count0 = 0 , count1 = 0 ; int N = binaryNumber . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( binaryNumber . charAt ( i ) == '1' ) { count1 ++ ; } else { count0 ++ ; } } for ( int i = 0 ; i < count1 ; i ++ ) { maxBinaryNumber += '1' ; } for ( int i = 0 ; i < count0 ; i ++ ) { maxBinaryNumber += '0' ; } return Integer . parseInt ( maxBinaryNumber , 2 ) ; } public static void main ( String [ ] args ) { int N = 11 ; System . out . println ( findMaxNum ( N ) ) ; } }
Minimum deletions to convert given integer to an odd number whose sum of digits is even | Set 2 | Java implementation of the above approach ; Function to find minimum count of digits required to be remove to make N odd and the sum of digits of N even ; Stores count of even digits ; Stores count of odd digits ; Iterate over the digits of N ; If current digit is even ; Update even ; Otherwise ; Update odd ; Base conditions ; Stores count of digits required to be removed to make N odd and the sum of digits of N even ; Iterate over the digits of N ; If current digit is even ; Update ans ; Otherwise , ; Update ans ; If count of odd digits is odd ; Update ans ; Finally print the ans ; Driver code ; Input String ; Function call
import java . util . * ; class GFG { static void minOperations ( String N ) { int even = 0 ; int odd = 0 ; for ( int it : N . toCharArray ( ) ) { if ( ( it - '0' ) % 2 == 0 ) { even ++ ; } else { odd ++ ; } } if ( odd == 0 odd == 1 ) { System . out . print ( " Not ▁ Possible " + "NEW_LINE"); } else { int ans = 0 ; for ( int it : N . toCharArray ( ) ) { if ( ( it - '0' ) % 2 == 0 ) { ans ++ ; } else { ans = 0 ; } } if ( odd % 2 != 0 ) { ans ++ ; } System . out . print ( ans + "NEW_LINE"); } } public static void main ( String [ ] args ) { String N = "12345" ; minOperations ( N ) ; } }
Find the player with least 0 s after emptying a Binary String by removing non | Function to find the player who wins the game ; Stores total count of 0 s in the string ; Stores count of consecutive 1 s ; Stores Nim - Sum on count of consecutive 1 s ; Stores length of the string ; Traverse the string ; If the current character is 1 ; Update cntConOne ; Update nimSum ; Update cntConOne ; Update cntZero ; Update nimSum ; If countZero is an even number ; nimSum is not 0 ; If nimSum is zero ; Driver Code
class GFG { public static void FindwinnerOfGame ( String S ) { int cntZero = 0 ; int cntConOne = 0 ; int nimSum = 0 ; int N = S . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) { cntConOne += 1 ; } else { nimSum ^= cntConOne ; cntConOne = 0 ; cntZero ++ ; } } nimSum ^= cntConOne ; if ( cntZero % 2 == 0 ) { System . out . print ( " Tie " ) ; } else if ( nimSum != 0 ) { System . out . print ( " player ▁ 1" ) ; } else { System . out . print ( " player ▁ 2" ) ; } } public static void main ( String [ ] args ) { String S = "0110011" ; FindwinnerOfGame ( S ) ; } }
Place first N natural numbers at indices not equal to their values in an array | Java program for the above approach ; Function to place first N natural numbers in an array such that none of the values are equal to its indices ; Stores the required array ; Place N at the first position ; Iterate the range [ 1 , N ) ; Append elements to the sequence ; Print the sequence ; Driver Code
import java . util . * ; class GFG { static void generatepermutation ( int N ) { Vector < Integer > answer = new Vector < Integer > ( ) ; answer . add ( N ) ; for ( int i = 1 ; i < N ; i ++ ) { answer . add ( i ) ; } for ( int i : answer ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 4 ; generatepermutation ( N ) ; } }
Maximize element at index K in an array with at most sum M and difference between adjacent elements at most 1 | Java program for the above approach ; Function to calculate the maximum possible value at index K ; Stores the sum of elements in the left and right of index K ; Stores the maximum possible value at index K ; Print the answer ; Driver Code ; Given N , K & M
import java . io . * ; class GFG { static void maxValueAtIndexK ( int N , int K , int M ) { int S1 = 0 , S2 = 0 ; S1 = K * ( K + 1 ) / 2 ; S2 = ( N - K - 1 ) * ( N - K ) / 2 ; int X = ( M + S1 + S2 ) / N ; System . out . println ( X ) ; } public static void main ( String [ ] args ) { int N = 3 , K = 1 , M = 7 ; maxValueAtIndexK ( N , K , M ) ; } }