codeparrot/xlcost-text-to-code huggingface dataset:
Minimum increment / decrement operations required on Array to satisfy given conditions | Java program for the above approach ; Function to find minimum number of operations to get desired array ; For odd ' i ' , sum of elements till ' i ' is positive ; If i is even and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; If i is odd and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; For odd ' i ' , sum of elements till ' i ' is negative ; Check if ' i ' is odd and sum is positive , make it negative by subtracting 1 + | s | from a [ i ] ; Check if ' i ' is even and sum is negative , make it positive by adding 1 + | s | into a [ i ] ; Return the minimum of the two ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static int minOperations ( int a [ ] , int N ) { int num_of_ops1 , num_of_ops2 , sum ; num_of_ops1 = num_of_ops2 = sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 0 && sum >= 0 ) { num_of_ops1 += ( 1 + Math . abs ( sum ) ) ; sum = - 1 ; } else if ( i % 2 == 1 && sum <= 0 ) { num_of_ops1 += ( 1 + Math . abs ( sum ) ) ; sum = 1 ; } } sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += a [ i ] ; if ( i % 2 == 1 && sum >= 0 ) { num_of_ops2 += ( 1 + Math . abs ( sum ) ) ; sum = - 1 ; } else if ( i % 2 == 0 && sum <= 0 ) { num_of_ops2 += ( 1 + Math . abs ( sum ) ) ; sum = 1 ; } } return Math . min ( num_of_ops1 , num_of_ops2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , - 4 , 5 , 0 , 1 } ; int N = arr . length ; System . out . print ( minOperations ( arr , N ) ) ; } }
Check if all elements of a Circular Array can be made equal by increments of adjacent pairs | Java program to implement the above approach ; Function to check if all array elements can be made equal ; Stores the sum of even and odd array elements ; If index is odd ; Driver Code
import java . util . * ; class GFG { static boolean checkEquall ( int arr [ ] , int N ) { int sumEven = 0 , sumOdd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) sumOdd += arr [ i ] ; else sumEven += arr [ i ] ; } if ( sumEven == sumOdd ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 3 , 5 , 7 } ; int N = arr . length ; if ( checkEquall ( arr , N ) ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " NO " + "NEW_LINE"); } }
Check if a number can be expressed as sum of two Perfect powers | Java program for the above approach ; Function that returns true if n can be written as a ^ m + b ^ n ; Taking isSum boolean array for check the sum exist or not ; To store perfect squares ; Initially all sums as false ; If sum exist then push that sum into perfect square vector ; Mark all perfect powers as false ; Traverse each perfectPowers ; Calculating Sum with perfect powers array ; Driver Code ; Given number n ; Function call
import java . util . * ; class GFG { static boolean isSumOfPower ( int n ) { boolean [ ] isSum = new boolean [ n + 1 ] ; Vector < Integer > perfectPowers = new Vector < Integer > ( ) ; perfectPowers . add ( 1 ) ; for ( int i = 0 ; i < ( n + 1 ) ; i ++ ) { isSum [ i ] = false ; } for ( int i = 2 ; i < ( n + 1 ) ; i ++ ) { if ( isSum [ i ] == true ) { perfectPowers . add ( i ) ; continue ; } for ( int j = i * i ; j > 0 && j < ( n + 1 ) ; j *= i ) { isSum [ j ] = true ; } } for ( int i = 0 ; i < perfectPowers . size ( ) ; i ++ ) { isSum [ perfectPowers . get ( i ) ] = false ; } for ( int i = 0 ; i < perfectPowers . size ( ) ; i ++ ) { for ( int j = i ; j < perfectPowers . size ( ) ; j ++ ) { int sum = perfectPowers . get ( i ) + perfectPowers . get ( j ) ; if ( sum < ( n + 1 ) ) isSum [ sum ] = true ; } } return isSum [ n ] ; } public static void main ( String [ ] args ) { int n = 9 ; if ( isSumOfPower ( n ) ) { System . out . print ( "trueNEW_LINE"); } else { System . out . print ( "falseNEW_LINE"); } } }
Minimum increments by index value required to obtain at least two equal Array elements | Java program to implement the above approach ; Function to update every element adding to it its index value ; Function to check if at least two elements are equal or not ; Count the frequency of arr [ i ] ; Function to calculate the number of increment operations required ; Stores the minimum number of steps ; Driver code
import java . util . * ; class GFG { static void update ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] += ( i + 1 ) ; } } static boolean check ( int arr [ ] , int N ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { count ++ ; } } if ( count >= 2 ) { f = 1 ; break ; } } if ( f == 1 ) return true ; else return false ; } static void incrementCount ( int arr [ ] , int N ) { int min = 0 ; while ( check ( arr , N ) != true ) { update ( arr , N ) ; min ++ ; } System . out . println ( min ) ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; } }
Rearrange array such that all even | Java Program to implement the above approach ; Function to check if it the array can be rearranged such such that every even indices contains an even element ; Stores the count of even elements ; Traverse array to count even numbers ; If even_no_count exceeds count of even indices ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void checkPossible ( int a [ ] ) { int even_no_count = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] % 2 == 0 ) even_no_count ++ ; } if ( a . length / 2 > even_no_count ) { System . out . println ( " No " ) ; return ; } System . out . println ( " Yes " ) ; int j = 0 ; for ( int i = 1 ; i < a . length ; i += 2 ) { if ( a [ i ] % 2 == 0 ) continue ; else { while ( j < a . length && a [ j ] % 2 != 0 ) j += 2 ; a [ i ] += a [ j ] ; a [ j ] = a [ i ] - a [ j ] ; a [ i ] -= a [ j ] ; } } for ( int i = 0 ; i < a . length ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; checkPossible ( arr ) ; } }
Largest number M having bit count of N such that difference between their OR and XOR value is maximized | Java program for the above approach ; Function to find the largest number M having the same length in binary form as N such that the difference between N | M and N ^ M is maximum ; Find the most significant bit of N ; Initialize M ; Set all the bits of M ; Return the answer ; Driver Code ; Given number N ; Function call
import java . util . * ; class GFG { static int maxORminusXOR ( int N ) { int MSB = ( int ) Math . ceil ( Math . log ( N ) ) ; int M = 0 ; for ( int i = 0 ; i <= MSB ; i ++ ) M += ( 1 << i ) ; return M ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( maxORminusXOR ( N ) ) ; } }
Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle | Java program for the above approach ; Function to find the number of equilateral triangle formed within another triangle ; Check for the valid condition ; Number of triangles having upward peak ; Number of inverted triangles ; Total no . of K sized triangle ; Driver Code ; Given N and K ; Function Call
import java . util . * ; class GFG { static int No_of_Triangle ( int N , int K ) { if ( N < K ) return - 1 ; else { int Tri_up = 0 ; Tri_up = ( ( N - K + 1 ) * ( N - K + 2 ) ) / 2 ; int Tri_down = 0 ; Tri_down = ( ( N - 2 * K + 1 ) * ( N - 2 * K + 2 ) ) / 2 ; return Tri_up + Tri_down ; } } public static void main ( String [ ] args ) { int N = 4 , K = 2 ; System . out . print ( No_of_Triangle ( N , K ) ) ; } }
Check if Array forms an increasing | Java program for the above approach ; Function to check if the given array forms an increasing decreasing sequence or vice versa ; Base Case ; First subarray is stricly increasing ; Check for strictly increasing condition & find the break point ; Check for strictly decreasing condition & find the break point ; If i is equal to length of array ; First subarray is strictly Decreasing ; Check for strictly increasing condition & find the break point ; Check for strictly increasing condition & find the break point ; If i is equal to length of array - 1 ; Condition if ar [ 0 ] == ar [ 1 ] ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static boolean canMake ( int n , int ar [ ] ) { if ( n == 1 ) return true ; else { if ( ar [ 0 ] < ar [ 1 ] ) { int i = 1 ; while ( i < n && ar [ i - 1 ] < ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] > ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else if ( ar [ 0 ] > ar [ 1 ] ) { int i = 1 ; while ( i < n && ar [ i - 1 ] > ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] < ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else { for ( int i = 2 ; i < n ; i ++ ) { if ( ar [ i - 1 ] <= ar [ i ] ) return false ; } return true ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; if ( ! canMake ( n , arr ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Minimum Count of Bit flips required to make a Binary String Palindromic | Java program to implement the above approach ; Function to calculate the length of the binary string ; Length ; Right shift of n ; Increment the length ; Return the length ; Function to check if the bit present at i - th position is a set bit or not ; Returns true if the bit is set ; Function to count the minimum number of bit flips required ; Length of the binary form ; Number of flips ; Pointer to the LSB ; Pointer to the MSB ; Check if the bits are equal ; Decrementing the left pointer ; Incrementing the right pointer ; Returns the number of bits to flip . ; Driver Code
class GFG { static int check_length ( int n ) { int ans = 0 ; while ( n != 0 ) { n = n >> 1 ; ans ++ ; } return ans ; } static boolean check_ith_bit ( int n , int i ) { return ( n & ( 1 << ( i - 1 ) ) ) != 0 ? true : false ; } static int no_of_flips ( int n ) { int len = check_length ( n ) ; int ans = 0 ; int right = 1 ; int left = len ; while ( right < left ) { if ( check_ith_bit ( n , right ) != check_ith_bit ( n , left ) ) ans ++ ; left -- ; right ++ ; } return ans ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( no_of_flips ( n ) ) ; } }
Split N into two integers whose addition to A and B makes them equal | Java program to implement the above approach ; Function to calculate the splitted numbers ; Calculate X ; If X is odd ; No pair is possible ; Otherwise ; Calculate X ; Calculate Y ; Driver function
import java . util . * ; class GFG { static void findPair ( int A , int B , int N ) { int X , Y ; X = N - B + A ; if ( X % 2 != 0 ) { System . out . print ( " - 1" ) ; } else { X = X / 2 ; Y = N - X ; System . out . print ( X + " ▁ " + Y ) ; } } public static void main ( String [ ] args ) { int A = 1 ; int B = 3 ; int N = 4 ; findPair ( A , B , N ) ; } }
Find the amplitude and number of waves for the given array | Java program for the above approach ; Function to find the amplitude and number of waves for the given array ; Check for both sides adjacent elements that both must be less or both must be greater than current element ; Update amplitude with max value ; Print the Amplitude ; Driver Code ; Given array a [ ] ; Calculate number of waves ; Function Call
import java . util . * ; class GFG { static boolean check ( int a [ ] , int n ) { int ma = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] > a [ i - 1 ] && a [ i + 1 ] < a [ i ] ) || ( a [ i ] < a [ i - 1 ] && a [ i + 1 ] > a [ i ] ) ) ma = Math . max ( ma , Math . abs ( a [ i ] - a [ i + 1 ] ) ) ; else return false ; } System . out . print ( " Amplitude ▁ = ▁ " + ma ) ; System . out . println ( ) ; return true ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 5 , 0 , 7 , - 6 } ; int n = a . length ; int wave = ( n - 1 ) / 2 ; if ( check ( a , n ) ) System . out . print ( " Waves ▁ = ▁ " + wave ) ; else System . out . print ( " - 1" ) ; } }
Find relative rank of each element in array | Java program to implement the above approach ; Function to find relative rank for each element in the array A [ ] ; Create Rank Array ; Stack to store numbers in non - decreasing order from right ; Push last element in stack ; Iterate from second last element to first element ; If current element is less than the top of stack and push A [ i ] in stack ; Rank is stack size - 1 for current element ; Pop elements from stack till current element is greater than the top ; Push current element in Stack ; Rank is stack size - 1 ; Print rank of all elements ; Driver Code ; Given array A [ ] ; Function call
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void findRank ( int [ ] A , int N ) { int [ ] rank = new int [ N ] ; Stack < Integer > s = new Stack < Integer > ( ) ; s . add ( A [ N - 1 ] ) ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( A [ i ] < s . peek ( ) ) { s . add ( A [ i ] ) ; rank [ i ] = s . size ( ) - 1 ; } else { while ( ! s . isEmpty ( ) && A [ i ] >= s . peek ( ) ) { s . pop ( ) ; } s . add ( A [ i ] ) ; rank [ i ] = s . size ( ) - 1 ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( rank [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 5 , 4 } ; int N = A . length ; findRank ( A , N ) ; } }
Largest string obtained in Dictionary order after deleting K characters | Java program for the above approach ; Function to find the largest string after deleting k characters ; Deque dq used to find the largest string in dictionary after deleting k characters ; Iterate till the length of the string ; Condition for popping characters from deque ; To store the resultant string ; To form resultant string ; Return the resultant string ; Driver Code ; Given String ; Function call ; Print the answer
import java . util . ArrayDeque ; import java . util . Deque ; import java . util . Scanner ; import java . io . IOException ; public class GFG { public static String largestString ( int n , int k , String sc ) { char [ ] s = sc . toCharArray ( ) ; Deque < Character > deq = new ArrayDeque < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { while ( deq . size ( ) > 0 && deq . getLast ( ) < s [ i ] && k > 0 ) { deq . pollLast ( ) ; k -- ; } deq . add ( s [ i ] ) ; } String st = " " ; for ( char c : deq ) st = st + Character . toString ( c ) ; return st ; } public static void main ( String [ ] args ) throws IOException { int n = 4 ; int k = 2 ; String sc = " ritz " ; String result = largestString ( n , k , sc ) ; System . out . println ( result ) ; } }
Count total set bits in all numbers from range L to R | Java program for the above approach ; Function that counts the set bits from 0 to N ; To store sum of set bits from 0 - N ; Until n >= to 2 ^ i ; This k will get flipped after 2 ^ i iterations ; Change is iterator from 2 ^ i to 1 ; This will loop from 0 to n for every bit position ; When change = 1 flip the bit ; Again set change to 2 ^ i ; Increment the position ; Function that counts the set bit in the range ( L , R ) ; Return the count ; Driver Code ; Given L and R ; Function Call
import java . util . * ; class GFG { static int countSetBit ( int n ) { int i = 0 ; int ans = 0 ; while ( ( 1 << i ) <= n ) { boolean k = true ; int change = 1 << i ; for ( int j = 0 ; j <= n ; j ++ ) { ans += k == true ? 0 : 1 ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; } } i ++ ; } return ans ; } static int countSetBits ( int L , int R ) { return Math . abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; } public static void main ( String [ ] args ) { int L = 3 , R = 5 ; System . out . print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) + "NEW_LINE"); } }
Count total set bits in all numbers from range L to R | Java program for the above approach ; Function to count set bit in range ; Count variable ; Find the set bit in Nth number ; If last bit is set ; Left sift by one bit ; Return count ; Driver Code ; Given Range L and R ; Function Call
class GFG { static int countSetBits ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { int n = i ; while ( n > 0 ) { count += ( n & 1 ) ; n = n >> 1 ; } } return count ; } public static void main ( String [ ] args ) { int L = 3 , R = 5 ; System . out . print ( " Total ▁ set ▁ Bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) ) ; } }
Count total set bits in all numbers from range L to R | Java program for the above approach ; Function to count set bit in [ L , R ] ; Variable for count set bit in range ; Count set bit for all number in range ; Use inbuilt function ; Driver Code ; Given range L and R ; Function Call
class GFG { static int countSetBits ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { count += Integer . bitCount ( i ) ; } return count ; } public static void main ( String [ ] args ) { int L = 3 , R = 5 ; System . out . print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) ) ; } }
Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | Java program for the above approach ; Function that count the number of integers from array B [ ] such that subtracting element in the range [ 0 , K ] given any element in A [ ] ; To store the count of element ; Traverse the array B [ ] ; Traverse the array A [ ] ; Find the difference ; If difference is atmost K then increment the cnt ; Print the count ; Driver Code ; Given array A [ ] and B [ ] ; Given K ; Function call
class GFG { static void countElement ( int A [ ] , int N , int B [ ] , int M , int K ) { int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int currentElement = B [ i ] ; for ( int j = 0 ; j < N ; j ++ ) { int diff = Math . abs ( currentElement - A [ j ] ) ; if ( diff <= K ) { cnt ++ ; break ; } } } System . out . print ( cnt ) ; } public static void main ( String [ ] args ) { int A [ ] = { 100 , 65 , 35 , 85 , 55 } ; int B [ ] = { 30 , 60 , 75 , 95 } ; int K = 5 ; int N = A . length ; int M = B . length ; countElement ( A , N , B , M , K ) ; } }
Generate a String of having N * N distinct non | Java Program to implement the above approach ; Function to construct a string having N * N non - palindromic substrings ; Driver Code
class GFG { static void createString ( int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ' a ' ) ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ' b ' ) ; } } public static void main ( String [ ] args ) { int N = 4 ; createString ( N ) ; } }
Maximum Subset Sum possible by negating the entire sum after selecting the first Array element | Java Program to implement the above approach ; Function returns maximum subset sum from the given array = ; Case 2 : Negate values from A [ 1 ] to A [ N - 1 ] ; Include only positives for max subset sum ; Return max sum obtained ; Function to return maximum of the maximum subset sum calculated for the two cases ; Case 1 ; Case 2 ; Modifying the sum ; Including first element ; Negating again ; Return the required answer ; Driver Code
import java . util . * ; class GFG { static int maxSubset ( int [ ] A , boolean flag ) { int n = A . length ; int sum = 0 ; if ( flag ) { for ( int i = 1 ; i < n ; i ++ ) A [ i ] = - A [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > 0 ) { sum += A [ i ] ; } } return sum ; } static int findBest ( int [ ] A ) { int x = maxSubset ( A , false ) ; int y = maxSubset ( A , true ) ; y = - y ; y += A [ 0 ] ; y = - y ; return Math . max ( x , y ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 10 , 4 , - 6 , 3 } ; System . out . print ( findBest ( A ) + "NEW_LINE"); } }
Construct an Array of Strings having Longest Common Prefix specified by the given Array | Java Program to implement the above approach ; Function to find the array of strings ; Marks the ( N + 1 ) th string ; To generate remaining N strings ; Check if current character is b ; Find i - th string using ( i + 1 ) - th string ; Otherwise ; Insert the string ; Return the answer ; Driver Code ; Print the strings
import java . util . * ; class GFG { static Vector < String > solve ( int n , int arr [ ] ) { String s = " aaa " ; Vector < String > ans = new Vector < String > ( ) ; ans . add ( s ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . length ( ) - 1 >= arr [ i ] ) { char ch = s . charAt ( arr [ i ] ) ; if ( ch == ' b ' ) ch = ' a ' ; else ch = ' b ' ; char [ ] myNameChars = s . toCharArray ( ) ; myNameChars [ arr [ i ] ] = ch ; s = String . valueOf ( myNameChars ) ; } ans . add ( s ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 0 , 3 } ; int n = arr . length ; Vector < String > ans = solve ( n , arr ) ; for ( int i = ans . size ( ) - 1 ; i >= 0 ; i -- ) { System . out . println ( ans . get ( i ) ) ; } } }
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | Java Program to implement the above approach ; Function to find the minimum number of swaps required ; Sort the array in ascending order ; Iterate until a greater element is found ; Keep incrementing ind ; If a greater element is found ; Increase count of swap ; Increment ind ; If end of array is reached ; Return the answer ; Driver Code
import java . util . * ; class GFG { static int countSwaps ( int A [ ] , int n ) { Arrays . sort ( A ) ; int ind = 1 , res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ind < n && A [ ind ] == A [ i ] ) ind ++ ; if ( ind < n && A [ ind ] > A [ i ] ) { res ++ ; ind ++ ; } if ( ind >= n ) break ; } return res ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 3 , 3 , 2 , 5 } ; System . out . print ( countSwaps ( A , 5 ) ) ; } }
Minimize swaps required to maximize the count of elements replacing a greater element in an Array | Java Program to implement the above approach ; Function to find the minimum number of swaps required ; Stores the frequency of the array elements ; Stores maximum frequency ; Find the max frequency ; Update frequency ; Update maximum frequency ; Driver Code ; function call
import java . util . * ; class GFG { static int countSwaps ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int max_frequency = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } max_frequency = Math . max ( max_frequency , mp . get ( arr [ i ] ) ) ; } return n - max_frequency ; } public static void main ( String [ ] args ) { int A [ ] = { 6 , 5 , 4 , 3 , 2 , 1 } ; System . out . print ( countSwaps ( A , 6 ) ) ; } }
Minimum number of steps required to obtain the given Array by the given operations | Java Program to implement the above approach ; Function to calculate the minimum steps to obtain the desired array ; Initialize variable ; Iterate over the array arr [ ] ; Check if i > 0 ; Update the answer ; Return the result ; Driver Code
import java . util . * ; class GFG { static int min_operation ( int a [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) ans += Math . abs ( a [ i ] - a [ i - 1 ] ) ; else ans += Math . abs ( a [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . print ( min_operation ( arr , n ) ) ; } }