codeparrot/xlcost-text-to-code huggingface dataset:
Farthest cell from a given cell in a Matrix | Java program for the above approach ; Function to find the farthest cell distance from the given cell ; From cell ( N , M ) ; From Cell ( 1 , 1 ) ; From cell ( N , 1 ) ; From cell ( 1 , M ) ; Finding out maximum ; Print the answer ; Driver Code
import java . io . * ; class GFG { static void farthestCellDistance ( int N , int M , int R , int C ) { int d1 = N + M - R - C ; int d2 = R + C - 2 ; int d3 = N - R + C - 1 ; int d4 = M - C + R - 1 ; int maxDistance = Math . max ( d1 , Math . max ( d2 , Math . max ( d3 , d4 ) ) ) ; System . out . println ( maxDistance ) ; } public static void main ( String [ ] args ) { int N = 15 , M = 12 , R = 1 , C = 6 ; farthestCellDistance ( N , M , R , C ) ; } }
Maximize every array element by repeatedly adding all valid i + a [ i ] th array element | Java program for the above approach ; Function to maximize value at every array index by performing given operations ; Traverse the array in reverse ; If the current index is a valid index ; Print the array ; Driver Code ; Given array ; Size of the array
import java . io . * ; class GFG { static void maxSum ( int [ ] arr , int N ) { int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { int t = i ; if ( t + arr [ i ] < N ) { arr [ i ] += arr [ t + arr [ i ] ] ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 7 , 1 , 8 } ; int N = arr . length ; maxSum ( arr , N ) ; } }
Generate an N | Java program for the above approach ; Function to conan array with given conditions ; Traverse the array arr [ ] ; Stores closest power of 2 less than or equal to arr [ i ] ; Stores R into brr [ i ] ; Print array elements ; Driver Code ; Given array ; Size of the array ; Function Call
import java . util . * ; class GFG { static void constructArray ( int arr [ ] , int N ) { int brr [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int K = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ; int R = ( int ) Math . pow ( 2 , K ) ; brr [ i ] = R ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( brr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 5 , 7 , 3 , 2 } ; int N = arr . length ; constructArray ( arr , N ) ; } }
Find the element at specified index in a Spiral Matrix | Java program for the above approach ; Function to the find element at ( i , j ) index ; Driver Code ; Function Call
import java . io . * ; class GFG { static int findInGrid ( int i , int j ) { if ( i == j ) return ( i * i - ( i - 1 ) ) ; else if ( i > j ) { if ( i % 2 == 0 ) return i * i - ( j - 1 ) ; else return ( i - 1 ) * ( i - 1 ) + 1 + ( j - 1 ) ; } else { if ( j % 2 == 0 ) return ( j - 1 ) * ( j - 1 ) + 1 + ( i - 1 ) ; else return j * j - ( i - 1 ) ; } } public static void main ( String [ ] args ) { int i = 3 , j = 4 ; System . out . println ( findInGrid ( i , j ) ) ; } }
Make all array elements equal to 0 by replacing minimum subsequences consisting of equal elements | Java program for the above approach ; Function to find minimum count of operations required to convert all array elements to zero br replacing subsequence of equal elements to 0 ; Store distinct elements present in the array ; Traverse the array ; If arr [ i ] is already present in the Set or arr [ i ] is equal to 0 ; Otherwise , increment ans by 1 and insert current element ; Driver Code ; Given array ; Size of the given array
import java . io . * ; import java . util . * ; class GFG { static void minOpsToTurnArrToZero ( int [ ] arr , int N ) { Set < Integer > st = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( st . contains ( arr [ i ] ) arr [ i ] == 0 ) { continue ; } else { st . add ( arr [ i ] ) ; } } System . out . println ( st . size ( ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 7 , 3 } ; int N = arr . length ; minOpsToTurnArrToZero ( arr , N ) ; } }
Split an array into equal length subsequences consisting of equal elements only | Java program for the above approach ; Function to find the GCD of two numbers a and b ; Function to check if it is possible to split the array into equal length subsequences such that all elements in the subsequence are equal ; Store frequencies of array elements ; Traverse the array ; Update frequency of arr [ i ] ; Store the GCD of frequencies of all array elements ; Traverse the map ; update gcd ; If the GCD is greater than 1 , print YES otherwise print NO ; Driver Code ; Given array ; Store the size of the array
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void splitArray ( int arr [ ] , int N ) { TreeMap < Integer , Integer > mp = new TreeMap < Integer , Integer > ( ) ; 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 ) ; } } int G = 0 ; for ( Map . Entry < Integer , Integer > m : mp . entrySet ( ) ) { Integer i = m . getValue ( ) ; G = gcd ( G , i . intValue ( ) ) ; } if ( G > 1 ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int n = arr . length ; new GFG ( ) . splitArray ( arr , n ) ; } }
Minimize count of given operations required to be performed to make all array elements equal to 1 | Java program to implement the above approach ; Function to check if all array elements are equal or not ; Traverse the array ; If all array elements are not equal ; Function to find minimum count of operation to make all the array elements equal to 1 ; Stores largest element of the array ; Check if a number is a power of 2 or not ; If Max is a power of 2 and all array elements are equal ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static boolean CheckAllEqual ( int arr [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ 0 ] != arr [ i ] ) { return false ; } } return true ; } static int minCntOperations ( int arr [ ] , int N ) { int Max = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; boolean isPower2 ; if ( ( int ) ( Math . ceil ( ( Math . log ( N ) / Math . log ( N ) ) ) ) == ( int ) ( Math . floor ( ( ( Math . log ( N ) / Math . log ( 2 ) ) ) ) ) ) { isPower2 = true ; } else { isPower2 = false ; } if ( isPower2 && CheckAllEqual ( arr , N ) ) { return ( int ) ( Math . log ( Max ) / Math . log ( 2 ) ) ; } else { return ( int ) Math . ceil ( Math . log ( Max ) / Math . log ( 2 ) ) + 1 ; } } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 2 , 4 } ; int N = arr . length ; System . out . println ( minCntOperations ( arr , N ) ) ; } }
Largest number not exceeding N that does not contain any of the digits of S | Java program to implement the above approach ; Function to obtain the largest number not exceeding num which does not contain any digits present in S ; Stores digits of S ; Traverse the string S ; Set occurrence as true ; Traverse the string n ; All digits of num are not present in string s ; Largest Digit not present in the string s ; Set all digits from positions in + 1 to n - 1 as LargestDig ; Counting leading zeroes ; Removing leading zeroes ; If the string becomes null ; Return the largest number ; Driver Code
import java . io . * ; import java . util . Arrays ; class GFG { static String greatestReducedNumber ( String num , String s ) { Boolean [ ] vis_s = new Boolean [ 10 ] ; Arrays . fill ( vis_s , Boolean . FALSE ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { vis_s [ ( int ) ( s . charAt ( i ) ) - 48 ] = true ; } int n = num . length ( ) ; int in = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis_s [ ( int ) num . charAt ( i ) - '0' ] ) { in = i ; break ; } } if ( in == - 1 ) { return num ; } for ( char dig = num . charAt ( in ) ; dig >= '0' ; dig -- ) { if ( vis_s [ ( int ) dig - '0' ] == false ) { num = num . substring ( 0 , in ) + dig + num . substring ( in + 1 , n ) ; break ; } } char LargestDig = '0' ; for ( char dig = '9' ; dig >= '0' ; dig -- ) { if ( vis_s [ dig - '0' ] == false ) { LargestDig = dig ; break ; } } for ( int i = in + 1 ; i < n ; i ++ ) { num = num . substring ( 0 , i ) + LargestDig ; } int Count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( num . charAt ( i ) == '0' ) Count ++ ; else break ; } num = num . substring ( Count , n ) ; if ( ( int ) num . length ( ) == 0 ) return "0" ; return num ; } public static void main ( String [ ] args ) { String N = "12345" ; String S = "23" ; System . out . print ( greatestReducedNumber ( N , S ) ) ; } }
Generate an array of minimum sum whose XOR of same | Java implementation of the above approach ; Function to generate an array whose XOR with same - indexed elements of the given array is always a prime ; Traverse the array ; If current array element is 2 ; Print its XOR with 3 ; Otherwise ; Print its XOR with 2 ; Driver code ; Given array ; Size of the array ; Prints the required array
class GFG { private static void minXOR ( int Arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( Arr [ i ] == 2 ) { System . out . print ( ( Arr [ i ] ^ 3 ) + " ▁ " ) ; } else { System . out . print ( ( Arr [ i ] ^ 2 ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int Arr [ ] = { 5 , 4 , 7 , 6 } ; int N = Arr . length ; minXOR ( Arr , N ) ; } }
Find the largest element in an array generated using the given conditions | Java program to implement the above approach ; Function to generate the required array and to find and return the maximum array element ; Stores the array ; Base case ; Iterate over the indices ; If current index is even ; Otherwise ; Function to find and return the maximum array element ; If n is 0 ; If n is 1 ; Generates the required array ; Return maximum element of Arr ; Driver Code
import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static int [ ] findArray ( int n ) { int Arr [ ] = new int [ n + 1 ] ; Arr [ 0 ] = 0 ; Arr [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { Arr [ i ] = Arr [ i / 2 ] ; } else { Arr [ i ] = Arr [ ( i - 1 ) / 2 ] + Arr [ ( i - 1 ) / 2 + 1 ] ; } } return Arr ; } static int maxElement ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; int [ ] Arr = findArray ( n ) ; int ans = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , Arr [ i ] ) ; } return ans ; } public static void main ( String args [ ] ) { int N = 7 ; System . out . println ( maxElement ( N ) ) ; } }
Construct a graph using N vertices whose shortest distance between K pair of vertices is 2 | Java program to implement the above approach ; Function to construct the simple and connected graph such that the distance between exactly K pairs of vertices is 2 ; Stores maximum possible count of edges in a graph ; Base Case ; Stores edges of a graph ; Connect all vertices of pairs ( i , j ) ; Print first ( ( N - 1 ) + Max - K ) elements of edges [ ] ; Driver Code
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 constGraphWithCon ( int N , int K ) { int Max = ( ( N - 1 ) * ( N - 2 ) ) / 2 ; if ( K > Max ) { System . out . print ( - 1 + "NEW_LINE"); return ; } Vector < pair > ans = new Vector < > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i + 1 ; j <= N ; j ++ ) { ans . add ( new pair ( i , j ) ) ; } } for ( int i = 0 ; i < ( N - 1 ) + Max - K ; i ++ ) { System . out . print ( ans . get ( i ) . first + " ▁ " + ans . get ( i ) . second + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 5 , K = 3 ; constGraphWithCon ( N , K ) ; } }
Minimum steps required to visit all corners of an N * M grid | Java Program to implement the above approach ; Function to find the minimum count of steps required to visit all the corners of the grid ; Stores corner of the grid ; Stores minimum count of steps required to visit the first corner of the grid ; Checking for leftmost upper corner ; Checking for leftmost down corner ; Checking for rightmost upper corner ; Checking for rightmost down corner ; Stores minimum count of steps required to visit remaining three corners of the grid ; Driver Code
import java . util . * ; class GFG { static int min_steps_required ( int n , int m , int r , int c ) { int i , j ; int corner_steps_req = Integer . MAX_VALUE ; i = 1 ; j = 1 ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = n ; j = 1 ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = 1 ; j = m ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = n ; j = m ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; int minimum_steps = Math . min ( 2 * ( n - 1 ) + m - 1 , 2 * ( m - 1 ) + n - 1 ) ; return minimum_steps + corner_steps_req ; } public static void main ( String [ ] args ) { int n = 3 ; int m = 2 ; int r = 1 ; int c = 1 ; System . out . print ( min_steps_required ( n , m , r , c ) ) ; } }
Minimum replacements required to have at most K distinct elements in the array | Java program to implement the above approach ; Function to find minimum count of array elements required to be replaced such that count of distinct elements is at most K ; Store the frequency of each distinct element of the array ; Traverse the array ; Update frequency of arr [ i ] ; Store frequency of each distinct element of the array ; Traverse the map ; Stores key of the map ; Insert mp [ i ] into Freq [ ] ; Sort Freq [ ] in descending order ; Stores size of Freq [ ] ; If len is less than or equal to K ; Stores minimum count of array elements required to be replaced such that count of distinct elements is at most K ; Iterate over the range [ K , len ] ; Update cntMin ; Driver Code
import java . util . * ; class GFG { static int min_elements ( int arr [ ] , int N , int K ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; 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 ) ; } } Vector < Integer > Freq = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { int i = it . getKey ( ) ; Freq . add ( mp . get ( i ) ) ; } Collections . sort ( Freq , Collections . reverseOrder ( ) ) ; int len = Freq . size ( ) ; if ( len <= K ) { return 0 ; } int cntMin = 0 ; for ( int i = K ; i < len ; i ++ ) { cntMin += Freq . get ( i ) ; } return cntMin ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 3 , 2 , 4 , 1 , 1 , 2 , 3 , 4 } ; int N = arr . length ; int K = 3 ; System . out . print ( min_elements ( arr , N , K ) ) ; } }
Check if sum of the given array can be reduced to 0 by reducing array elements by K | Java program for the above approach ; Function to check if the sum can be made 0 or not ; Stores sum of array elements ; Traverse the array ; Driver Code ; Given array arr [ ]
import java . util . * ; class GFG { static int sumzero ( int arr [ ] , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum == 0 ) System . out . print ( " Yes " ) ; else if ( sum > 0 ) { if ( sum % K == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } else System . out . print ( " No " ) ; return 0 ; } public static void main ( String [ ] args ) { int K , N ; int arr1 [ ] = { 1 , - 6 , 2 , 2 } ; K = 1 ; N = arr1 . length ; sumzero ( arr1 , N , K ) ; } }
Counts 1 s that can be obtained in an Array by performing given operations | Java program to implement the above approach ; Function to count total number of 1 s in array by performing given operations ; Stores count of 1 s in the array by performing the operations ; Iterate over the range [ 1 , N ] ; Flip all array elements whose index is multiple of i ; Update arr [ i ] ; Traverse the array ; If current element is 1 ; Update cntOnes ; Driver Code
class GFG { static int cntOnesArrWithGivenOp ( int arr [ ] , int N ) { int cntOnes = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i - 1 ; j < N ; j += i ) { arr [ j ] = arr [ j ] == 0 ? 1 : 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOnes += 1 ; } } return cntOnes ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . length ; System . out . print ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }
Counts 1 s that can be obtained in an Array by performing given operations | Java program to implement the above approach ; Function to count total number of 1 s in array by performing the given operations ; Stores count of 1 s in the array by performing the operations ; Update cntOnes ; Driver code
import java . util . * ; class GFG { static int cntOnesArrWithGivenOp ( int arr [ ] , int N ) { int cntOnes = 0 ; cntOnes = ( int ) Math . sqrt ( N ) ; return cntOnes ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . length ; System . out . println ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }
Count Knights that can attack a given pawn in an N * N board | Java program to implement the above approach ; Function to count the knights that are attacking the pawn in an M * M board ; Stores count of knights that are attacking the pawn ; Traverse the knights [ ] [ ] array ; Stores absolute difference of X co - ordinate of i - th knight and pawn ; Stores absolute difference of Y co - ordinate of i - th knight and pawn ; If X is 1 and Y is 2 or X is 2 and Y is 1 ; Update cntKnights ; Driver code ; Stores total count of knights
import java . io . * ; import java . lang . Math ; class GFG { static int cntKnightsAttackPawn ( int knights [ ] [ ] , int pawn [ ] , int M ) { int cntKnights = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int X = Math . abs ( knights [ i ] [ 0 ] - pawn [ 0 ] ) ; int Y = Math . abs ( knights [ i ] [ 1 ] - pawn [ 1 ] ) ; if ( ( X == 1 && Y == 2 ) || ( X == 2 && Y == 1 ) ) { cntKnights ++ ; } } return cntKnights ; } public static void main ( String [ ] args ) { int [ ] [ ] knights = { { 0 , 4 } , { 4 , 5 } , { 1 , 4 } , { 3 , 1 } } ; int [ ] pawn = new int [ ] { 2 , 3 } ; int M = knights . length ; System . out . println ( cntKnightsAttackPawn ( knights , pawn , M ) ) ; } }
Find the winner of game of removing array elements having GCD equal to 1 | Java program to implement the above approach ; Function to find the winner of the game by removing array elements whose GCD is 1 ; mp [ i ] : Stores count of array elements whose prime factor is i ; Traverse the array ; Calculate the prime factors of arr [ i ] ; If arr [ i ] is divisible by j ; Update mp [ j ] ; Update arr [ i ] ; If arr [ i ] exceeds 1 ; Stores maximum value present in the Map ; Traverse the map ; Update maxCnt ; If n is an even number ; If maxCnt is greater than or equal to n - 1 ; Player 1 wins ; Player 2 wins ; If maxCnt equal to n ; Player 1 wins ; Player 2 wins ; Driver code
import java . util . * ; class GFG { static void findWinnerGameRemoveGCD ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( mp . containsKey ( j ) ) { mp . put ( j , mp . get ( j ) + 1 ) ; } else { mp . put ( j , 1 ) ; } while ( arr [ i ] % j == 0 ) { arr [ i ] = arr [ i ] / j ; } } } if ( arr [ i ] > 1 ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } } int maxCnt = 0 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { maxCnt = Math . max ( maxCnt , i . getValue ( ) ) ; } if ( n % 2 == 0 ) { if ( maxCnt >= n - 1 ) { System . out . print ( " Player ▁ 1" ) ; } else { System . out . print ( " Player ▁ 2" ) ; } } else { if ( maxCnt == n ) { System . out . print ( " Player ▁ 1" ) ; } else { System . out . print ( " Player ▁ 2" ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 8 } ; int N = arr . length ; findWinnerGameRemoveGCD ( arr , N ) ; } }
Maximum possible sum of K even multiples of 5 in a given range | Java program to implement the above approach ; Function to find the maximum sum of K even multiples of 5 in the range [ L , R ] ; Store the total number of even multiples of 5 in the range [ L , R ] ; Check if K > N ; If true , print - 1 and return ; Otherwise , divide R by 10 ; Store the sum using the formula ; Print the sum ; Driver Code ; Given L , R and K ; Function Call
import java . util . * ; class GFG { static void maxksum ( int L , int R , int K ) { int N = ( R / 10 - L / 10 ) + 1 ; if ( K > N ) { System . out . print ( " - 1" ) ; return ; } R = R / 10 ; int X = R - K ; int sum = 10 * ( ( R * ( R + 1 ) ) / 2 - ( X * ( X + 1 ) ) / 2 ) ; System . out . print ( sum ) ; } public static void main ( String [ ] args ) { int L = 16 , R = 60 , K = 4 ; maxksum ( L , R , K ) ; } }
Minimum swaps of same | Java program for the above approach ; Function to count the minimum swaps of same - indexed elements from arrays arr1 [ ] and arr2 [ ] required to make the sum of both the arrays even ; Store the sum of elements of the array arr1 and arr2 respectively ; Store the array sum of both the arrays ; If both sumArr1 and sumArr2 are even , print 0 and return ; If both sumArr1 and sumArr2 are odd and check for a pair with sum odd sum ; Stores if a pair with odd sum exists or not ; Traverse the array ; If a pair exists with odd sum , set flag = 1 ; Print the answer and return ; For all other cases , print - 1 ; Driver code ; Function Call
import java . io . * ; class GFG { static void minimumSwaps ( int arr1 [ ] , int arr2 [ ] , int n ) { int sumArr1 = 0 , sumArr2 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { sumArr1 += arr1 [ i ] ; sumArr2 += arr2 [ i ] ; } if ( sumArr1 % 2 == 0 && sumArr2 % 2 == 0 ) { System . out . print ( 0 ) ; return ; } if ( sumArr1 % 2 != 0 && sumArr2 % 2 != 0 ) { int flag = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) { flag = 1 ; break ; } } System . out . print ( flag ) ; return ; } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 11 , 14 , 20 , 2 } ; int arr2 [ ] = { 5 , 9 , 6 , 3 } ; int N = arr1 . length ; minimumSwaps ( arr1 , arr2 , N ) ; } }
Count of seats booked on each of the given N flights | Java program for the above approach ; Function to find the total of the seats booked in each of the flights ; Stores the resultant sequence ; Traverse the array ; Store the first booked flight ; Store the last booked flight ; Store the total number of seats booked in flights [ l , r ] ; Add K to the flight L ; Subtract K from flight number R + 1 ; Find the prefix sum of the array ; Print the total number of seats booked in each flight ; Driver Code ; Given list of bookings ; Function Call
import java . util . * ; class GFG { static void corpFlightBookings ( int [ ] [ ] Bookings , int N ) { int res [ ] = new int [ N ] ; for ( int i = 0 ; i < Bookings . length ; i ++ ) { int l = Bookings [ i ] [ 0 ] ; int r = Bookings [ i ] [ 1 ] ; int K = Bookings [ i ] [ 2 ] ; res [ l - 1 ] = res [ l - 1 ] + K ; if ( r <= res . length - 1 ) res [ r ] = ( - K ) + res [ r ] ; } for ( int i = 1 ; i < res . length ; i ++ ) res [ i ] = res [ i ] + res [ i - 1 ] ; for ( int i = 0 ; i < res . length ; i ++ ) { System . out . print ( res [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int bookings [ ] [ ] = { { 1 , 3 , 100 } , { 2 , 6 , 100 } , { 3 , 4 , 100 } } ; int N = 6 ; corpFlightBookings ( bookings , N ) ; } }