codeparrot/xlcost-text-to-code huggingface dataset:
Range Queries for Longest Correct Bracket Subsequence Set | 2 | Java code to answer the query in constant time ; Function for precomputation ; Create a stack and push - 1 as initial index to it . ; Traverse all characters of given String ; If opening bracket , push index of it ; If closing bracket , i . e . , str [ i ] = ' ) ' ; If closing bracket , i . e . , str [ i ] = ' ) ' && stack is not empty then mark both " open ▁ & ▁ close " bracket indexs as 1 . Pop the previous opening bracket 's index ; If stack is empty . ; Function return output of each query in O ( 1 ) ; Driver code
import java . util . * ; class GFG { static void constructBlanceArray ( int BOP [ ] , int BCP [ ] , String str , int n ) { Stack < Integer > stk = new Stack < > ( ) ; ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) stk . add ( i ) ; else { if ( ! stk . isEmpty ( ) ) { BCP [ i ] = 1 ; BOP [ stk . peek ( ) ] = 1 ; stk . pop ( ) ; } else BCP [ i ] = 0 ; } } for ( int i = 1 ; i < n ; i ++ ) { BCP [ i ] += BCP [ i - 1 ] ; BOP [ i ] += BOP [ i - 1 ] ; } } static int query ( int BOP [ ] , int BCP [ ] , int s , int e ) { if ( BOP [ s - 1 ] == BOP [ s ] ) { return ( BCP [ e ] - BOP [ s ] ) * 2 ; } else { return ( BCP [ e ] - BOP [ s ] + 1 ) * 2 ; } } public static void main ( String [ ] args ) { String str = " ( ) ) ( ( ) ) ( ( ) ) ( " ; int n = str . length ( ) ; int BCP [ ] = new int [ n + 1 ] ; int BOP [ ] = new int [ n + 1 ] ; constructBlanceArray ( BOP , BCP , str , n ) ; int startIndex = 5 , endIndex = 11 ; System . out . print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + "NEW_LINE"); startIndex = 4 ; endIndex = 5 ; System . out . print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + "NEW_LINE"); startIndex = 1 ; endIndex = 5 ; System . out . print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + "NEW_LINE"); } }
HeapSort | Java program for implementation of Heap Sort ; To heapify a subtree rooted with node i which is an index in arr [ ] . n is size of heap ; Initialize largest as root ; left = 2 * i + 1 ; right = 2 * i + 2 ; If left child is larger than root ; If right child is larger than largest so far ; If largest is not root ; Recursively heapify the affected sub - tree ; The main function to sort an array of given size ; Build heap ( rearrange array ) ; One by one extract an element from heap ; Move current root to end ; call max heapify on the reduced heap ; A utility function to print array of size n ; Driver code
public class HeapSort { void heapify ( int arr [ ] , int n , int i ) { int largest = i ; int l = 2 * i + 1 ; int r = 2 * i + 2 ; if ( l < n && arr [ l ] > arr [ largest ] ) largest = l ; if ( r < n && arr [ r ] > arr [ largest ] ) largest = r ; if ( largest != i ) { int swap = arr [ i ] ; arr [ i ] = arr [ largest ] ; arr [ largest ] = swap ; heapify ( arr , n , largest ) ; } } public void sort ( int arr [ ] ) { int n = arr . length ; for ( int i = n / 2 - 1 ; i >= 0 ; i -- ) heapify ( arr , n , i ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { int temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapify ( arr , i , 0 ) ; } } static void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; int n = arr . length ; HeapSort ob = new HeapSort ( ) ; ob . sort ( arr ) ; System . out . println ( " Sorted ▁ array ▁ is " ) ; printArray ( arr ) ; } }
Iterative HeapSort | Java implementation of Iterative Heap Sort ; function build Max Heap where value of each child is always smaller than value of their parent ; if child is bigger than parent ; swap child and parent until parent is smaller ; swap value of first indexed with last indexed ; maintaining heap property after each swapping ; if left child is smaller than right child point index variable to right child ; if parent is smaller than child then swapping parent with child having higher value ; A utility function to print array of size n ; Driver program
public class HeapSort { static void buildMaxHeap ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ ( i - 1 ) / 2 ] ) { int j = i ; while ( arr [ j ] > arr [ ( j - 1 ) / 2 ] ) { swap ( arr , j , ( j - 1 ) / 2 ) ; j = ( j - 1 ) / 2 ; } } } } static void heapSort ( int arr [ ] , int n ) { buildMaxHeap ( arr , n ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { swap ( arr , 0 , i ) ; int j = 0 , index ; do { index = ( 2 * j + 1 ) ; if ( index < ( i - 1 ) && arr [ index ] < arr [ index + 1 ] ) index ++ ; if ( index < i && arr [ j ] < arr [ index ] ) swap ( arr , j , index ) ; j = index ; } while ( index < i ) ; } } public static void swap ( int [ ] a , int i , int j ) { int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } static void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 20 , 15 , 17 , 9 , 21 } ; int n = arr . length ; System . out . print ( " Given ▁ array : ▁ " ) ; printArray ( arr ) ; heapSort ( arr , n ) ; System . out . print ( " Sorted ▁ array : ▁ " ) ; printArray ( arr ) ; } }
Flip Binary Tree | Java program to flip a binary tree ; A binary tree node ; Utility function to create a new Binary Tree Node ; method to flip the binary tree ; Initialization of pointers ; Iterate through all left nodes ; Swapping nodes now , need temp to keep the previous right child Making prev ' s ▁ right ▁ ▁ as ▁ curr ' s left child ; Storing curr 's right child ; Making prev as curr 's  right child ; Iterative method to do level order traversal line by line ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; nodeCount ( queue size ) indicates number of nodes at current lelvel . ; Dequeue all nodes of current level and Enqueue all nodes of next level ; Driver code
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node flipBinaryTree ( Node root ) { Node curr = root ; Node next = null ; Node temp = null ; Node prev = null ; while ( curr != null ) { next = curr . left ; curr . left = temp ; temp = curr . right ; curr . right = prev ; prev = curr ; curr = next ; } return prev ; } static void printLevelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( true ) { int nodeCount = q . size ( ) ; if ( nodeCount == 0 ) break ; while ( nodeCount > 0 ) { Node node = q . peek ( ) ; System . out . print ( node . data + " ▁ " ) ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; nodeCount -- ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; System . out . print ( " Level ▁ order ▁ traversal ▁ " + "of given treeNEW_LINE"); printLevelOrder ( root ) ; root = flipBinaryTree ( root ) ; System . out . print ( " Level order traversal " ▁ + ▁ " of the flipped tree "); printLevelOrder ( root ) ; } }
Foldable Binary Trees | A binary tree node has data , pointer to left child and a pointer to right child ; Returns true if the given tree can be folded ; A utility function that checks if trees with roots as n1 and n2 are mirror of each other ; If both left and right subtrees are NULL , then return true ; If one of the trees is NULL and other is not , then return false ; Otherwise check if left and right subtrees are mirrors of their counterparts ; Driver program to test above functions ; The constructed binary tree is 1 / \ 2 3 \ / 4 5
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; boolean IsFoldable ( Node node ) { if ( node == null ) return true ; return IsFoldableUtil ( node . left , node . right ) ; } boolean IsFoldableUtil ( Node n1 , Node n2 ) { if ( n1 == null && n2 == null ) return true ; if ( n1 == null n2 == null ) return false ; return IsFoldableUtil ( n1 . left , n2 . right ) && IsFoldableUtil ( n1 . right , n2 . left ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . right . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; if ( tree . IsFoldable ( tree . root ) ) System . out . println ( " tree ▁ is ▁ foldable " ) ; else System . out . println ( " Tree ▁ is ▁ not ▁ foldable " ) ; } }
Check for Children Sum Property in a Binary Tree | A binary tree node has data , pointer to left child and a pointer to right child ; returns 1 if children sum property holds for the given node and both of its children ; left_data is left child data and right_data is for right child data ; If node is NULL or it 's a leaf node then  return true  ; If left child is not present then 0 is used as data of left child ; If right child is not present then 0 is used as data of right child ; if the node and both of its children satisfy the property return 1 else 0 ; driver program to test the above functions
class Node { int data ; Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { Node root ; int isSumProperty ( Node node ) { int left_data = 0 , right_data = 0 ; if ( node == null || ( node . left == null && node . right == null ) ) return 1 ; else { if ( node . left != null ) left_data = node . left . data ; if ( node . right != null ) right_data = node . right . data ; if ( ( node . data == left_data + right_data ) && ( isSumProperty ( node . left ) != 0 ) && isSumProperty ( node . right ) != 0 ) return 1 ; else return 0 ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 2 ) ; if ( tree . isSumProperty ( tree . root ) != 0 ) System . out . println ( " The ▁ given ▁ tree ▁ satisfies ▁ children " + " ▁ sum ▁ property " ) ; else System . out . println ( " The ▁ given ▁ tree ▁ does ▁ not ▁ satisfy ▁ children " + " ▁ sum ▁ property " ) ; } }
How to check if a given array represents a Binary Heap ? | Java program to check whether a given array represents a max - heap or not ; Returns true if arr [ i . . n - 1 ] represents a max - heap ; If a leaf node ; If an internal node and is greater than its children , and same is recursively true for the children ; Driver program
class GFG { static boolean isHeap ( int arr [ ] , int i , int n ) { if ( i >= ( n - 2 ) / 2 ) { return true ; } if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && isHeap ( arr , 2 * i + 1 , n ) && isHeap ( arr , 2 * i + 2 , n ) ) { return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 } ; int n = arr . length - 1 ; if ( isHeap ( arr , 0 , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
How to check if a given array represents a Binary Heap ? | Java program to check whether a given array represents a max - heap or not ; Returns true if arr [ i . . n - 1 ] represents a max - heap ; Start from root and go till the last internal node ; If left child is greater , return false ; If right child is greater , return false ; Driver program
class GFG { static boolean isHeap ( int arr [ ] , int n ) { for ( int i = 0 ; i <= ( n - 2 ) / 2 ; i ++ ) { if ( arr [ 2 * i + 1 ] > arr [ i ] ) { return false ; } if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 } ; int n = arr . length ; if ( isHeap ( arr , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Connect n ropes with minimum cost | A class for Min Heap ; Array of elements in heap ; Current number of elements in min heap ; maximum possible size of min heap ; Constructor : Builds a heap from a given array a [ ] of given size ; A recursive method to heapify a subtree with the root at given index This method assumes that the subtrees are already heapified ; to get index of left child of node at index i ; to get index of right child of node at index i ; Method to remove minimum element ( or root ) from min heap ; Store the minimum value , and remove it from heap ; Inserts a new key ' k ' ; First insert the new key at the end ; Fix the min heap property if it is violated ; A utility function to check if size of heap is 1 or not ; A utility function to swap two elements ; The main function that returns the minimum cost to connect n ropes of lengths stored in len [ 0. . n - 1 ] ; Initialize result ; Create a min heap of capacity equal to n and put all ropes in it ; Iterate while size of heap doesn 't become 1 ; Extract two minimum length ropes from min heap ; Update total cost ; Insert a new rope in min heap with length equal to sum of two extracted minimum lengths ; Finally return total minimum cost for connecting all ropes ; Driver code
class MinHeap { int [ ] harr ; int heap_size ; int capacity ; public MinHeap ( int a [ ] , int size ) { heap_size = size ; capacity = size ; harr = a ; int i = ( heap_size - 1 ) / 2 ; while ( i >= 0 ) { MinHeapify ( i ) ; i -- ; } } void MinHeapify ( int i ) { int l = left ( i ) ; int r = right ( i ) ; int smallest = i ; if ( l < heap_size && harr [ l ] < harr [ i ] ) smallest = l ; if ( r < heap_size && harr [ r ] < harr [ smallest ] ) smallest = r ; if ( smallest != i ) { swap ( i , smallest ) ; MinHeapify ( smallest ) ; } } int parent ( int i ) { return ( i - 1 ) / 2 ; } int left ( int i ) { return ( 2 * i + 1 ) ; } int right ( int i ) { return ( 2 * i + 2 ) ; } int extractMin ( ) { if ( heap_size <= 0 ) return Integer . MAX_VALUE ; if ( heap_size == 1 ) { heap_size -- ; return harr [ 0 ] ; } int root = harr [ 0 ] ; harr [ 0 ] = harr [ heap_size - 1 ] ; heap_size -- ; MinHeapify ( 0 ) ; return root ; } void insertKey ( int k ) { if ( heap_size == capacity ) { System . out . println ( " Overflow : ▁ Could ▁ not ▁ insertKey " ) ; return ; } heap_size ++ ; int i = heap_size - 1 ; harr [ i ] = k ; while ( i != 0 && harr [ parent ( i ) ] > harr [ i ] ) { swap ( i , parent ( i ) ) ; i = parent ( i ) ; } } boolean isSizeOne ( ) { return ( heap_size == 1 ) ; } void swap ( int x , int y ) { int temp = harr [ x ] ; harr [ x ] = harr [ y ] ; harr [ y ] = temp ; } static int minCost ( int len [ ] , int n ) { int cost = 0 ; MinHeap minHeap = new MinHeap ( len , n ) ; while ( ! minHeap . isSizeOne ( ) ) { int min = minHeap . extractMin ( ) ; int sec_min = minHeap . extractMin ( ) ; cost += ( min + sec_min ) ; minHeap . insertKey ( min + sec_min ) ; } return cost ; } public static void main ( String args [ ] ) { int len [ ] = { 4 , 3 , 2 , 6 } ; int size = len . length ; System . out . println ( " Total ▁ cost ▁ for ▁ connecting ▁ ropes ▁ is ▁ " + minCost ( len , size ) ) ; } } ;
Connect n ropes with minimum cost | Java program to connect n ropes with minimum cost ; Create a priority queue ; Initialize result ; While size of priority queue is more than 1 ; Extract shortest two ropes from pq ; Connect the ropes : update result and insert the new rope to pq ; Driver program to test above function
import java . util . * ; class ConnectRopes { static int minCost ( int arr [ ] , int n ) { PriorityQueue < Integer > pq = new PriorityQueue < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . add ( arr [ i ] ) ; } int res = 0 ; while ( pq . size ( ) > 1 ) { int first = pq . poll ( ) ; int second = pq . poll ( ) ; res += first + second ; pq . add ( first + second ) ; } return res ; } public static void main ( String args [ ] ) { int len [ ] = { 4 , 3 , 2 , 6 } ; int size = len . length ; System . out . println ( " Total ▁ cost ▁ for ▁ connecting " + " ▁ ropes ▁ is ▁ " + minCost ( len , size ) ) ; } }
Merge k sorted arrays | Set 1 | Java program to merge k sorted arrays of size n each . ; This function takes an array of arrays as an argument and All arrays are assumed to be sorted . It merges them together and prints the final sorted output . ; traverse the matrix ; sort the array ; A utility function to print array elements ; Driver program to test above functions
import java . io . * ; import java . util . * ; class GFG { public static void mergeKArrays ( int [ ] [ ] arr , int a , int [ ] output ) { int c = 0 ; for ( int i = 0 ; i < a ; i ++ ) { for ( int j = 0 ; j < 4 ; j ++ ) output [ c ++ ] = arr [ i ] [ j ] ; } Arrays . sort ( output ) ; } public static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 2 , 6 , 12 , 34 } , { 1 , 9 , 20 , 1000 } , { 23 , 34 , 90 , 2000 } } ; int k = 4 ; int n = 3 ; int [ ] output = new int [ n * k ] ; mergeKArrays ( arr , n , output ) ; System . out . println ( " Merged ▁ array ▁ is ▁ " ) ; printArray ( output , n * k ) ; } }
Merge k sorted arrays | Set 1 | Java program to merge k sorted arrays of size n each . ; Merge arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] into arr3 [ 0. . n1 + n2 - 1 ] ; Traverse both array ; Check if current element of first array is smaller than current element of second array . If yes , store first array element and increment first array index . Otherwise do same with second array ; Store remaining elements of first array ; Store remaining elements of second array ; A utility function to print array elements ; This function takes an array of arrays as an argument and All arrays are assumed to be sorted . It merges them together and prints the final sorted output . ; if one array is in range ; if only two arrays are left them merge them ; output arrays ; divide the array into halves ; merge the output array ; Driver program to test above functions ; Change n at the top to change number of elements in an array
import java . util . * ; class GFG { static final int n = 4 ; static void mergeArrays ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 , int arr3 [ ] ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) arr3 [ k ++ ] = arr1 [ i ++ ] ; else arr3 [ k ++ ] = arr2 [ j ++ ] ; } while ( i < n1 ) arr3 [ k ++ ] = arr1 [ i ++ ] ; while ( j < n2 ) arr3 [ k ++ ] = arr2 [ j ++ ] ; } static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void mergeKArrays ( int arr [ ] [ ] , int i , int j , int output [ ] ) { if ( i == j ) { for ( int p = 0 ; p < n ; p ++ ) output [ p ] = arr [ i ] [ p ] ; return ; } if ( j - i == 1 ) { mergeArrays ( arr [ i ] , arr [ j ] , n , n , output ) ; return ; } int [ ] out1 = new int [ n * ( ( ( i + j ) / 2 ) - i + 1 ) ] ; int [ ] out2 = new int [ n * ( j - ( ( i + j ) / 2 ) ) ] ; mergeKArrays ( arr , i , ( i + j ) / 2 , out1 ) ; mergeKArrays ( arr , ( i + j ) / 2 + 1 , j , out2 ) ; mergeArrays ( out1 , out2 , n * ( ( ( i + j ) / 2 ) - i + 1 ) , n * ( j - ( ( i + j ) / 2 ) ) , output ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 2 , 6 , 12 , 34 } , { 1 , 9 , 20 , 1000 } , { 23 , 34 , 90 , 2000 } } ; int k = arr . length ; int [ ] output = new int [ n * k ] ; mergeKArrays ( arr , 0 , 2 , output ) ; System . out . print ( " Merged ▁ array ▁ is ▁ " + "NEW_LINE"); printArray ( output , n * k ) ; } }
Smallest Derangement of Sequence | Java program to generate smallest derangement using priority queue . ; Generate Sequence and insert into a priority queue . ; Generate Least Derangement ; Print Derangement ; Driver code
import java . util . * ; class GFG { static void generate_derangement ( int N ) { int [ ] S = new int [ N + 1 ] ; PriorityQueue < Integer > PQ = new PriorityQueue < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { S [ i ] = i ; PQ . add ( S [ i ] ) ; } int [ ] D = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { int d = PQ . peek ( ) ; PQ . remove ( ) ; if ( d != S [ i ] i == N ) { D [ i ] = d ; } else { D [ i ] = PQ . peek ( ) ; PQ . remove ( ) ; PQ . add ( d ) ; } } if ( D [ N ] == S [ N ] ) { int t = D [ N - 1 ] ; D [ N - 1 ] = D [ N ] ; D [ N ] = t ; } for ( int i = 1 ; i <= N ; i ++ ) System . out . printf ( " % d ▁ " , D [ i ] ) ; System . out . printf ( "NEW_LINE"); } public static void main ( String [ ] args ) { generate_derangement ( 10 ) ; } }
Smallest Derangement of Sequence | Efficient Java program to find smallest derangement . ; Generate Sequence S ; Generate Derangement ; Only if i is odd Swap S [ N - 1 ] and S [ N ] ; Print Derangement ; Driver Program
class GFG { static void generate_derangement ( int N ) { int S [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) S [ i ] = i ; int D [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 2 ) { if ( i == N ) { D [ N ] = S [ N - 1 ] ; D [ N - 1 ] = S [ N ] ; } else { D [ i ] = i + 1 ; D [ i + 1 ] = i ; } } for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( D [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { generate_derangement ( 10 ) ; } }
Largest Derangement of a Sequence | Java program to find the largest derangement ; Stores result ; Insert all elements into a priority queue ; Fill Up res [ ] from left to right ; New Element poped equals the element in original sequence . Get the next largest element ; If given sequence is in descending order then we need to swap last two elements again ; Driver code
import java . io . * ; import java . util . Collections ; import java . util . PriorityQueue ; class GFG { public static void printLargest ( int a [ ] , int n ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( Collections . reverseOrder ( ) ) ; int res [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { pq . add ( a [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { int p = pq . peek ( ) ; pq . remove ( ) ; if ( p != a [ i ] i == n - 1 ) { res [ i ] = p ; } else { res [ i ] = pq . peek ( ) ; pq . remove ( ) ; pq . add ( p ) ; } } if ( res [ n - 1 ] == a [ n - 1 ] ) { res [ n - 1 ] = res [ n - 2 ] ; res [ n - 2 ] = a [ n - 1 ] ; } System . out . println ( " Largest ▁ Derangement " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( res [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 7 ; int seq [ ] = { 92 , 3 , 52 , 13 , 2 , 31 , 1 } ; printLargest ( seq , n ) ; } }
Program to calculate Profit Or Loss | Java code to demonstrate Profit and Loss ; Function to calculate Profit . ; Function to calculate Loss . ; Driver Code .
class GFG { static int Profit ( int costPrice , int sellingPrice ) { int profit = ( sellingPrice - costPrice ) ; return profit ; } static int Loss ( int costPrice , int sellingPrice ) { int Loss = ( costPrice - sellingPrice ) ; return Loss ; } public static void main ( String [ ] args ) { int costPrice = 1500 , sellingPrice = 2000 ; if ( sellingPrice == costPrice ) System . out . println ( " No ▁ profit ▁ nor ▁ Loss " ) ; else if ( sellingPrice > costPrice ) System . out . println ( Profit ( costPrice , sellingPrice ) + " ▁ Profit ▁ " ) ; else System . out . println ( Loss ( costPrice , sellingPrice ) + " ▁ Loss ▁ " ) ; } }