codeparrot/xlcost-text-to-code huggingface dataset:
Count triplets in a sorted doubly linked list whose product is equal to a given value x | Java implementation to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; Structure of node of doubly linked list ; Function to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; Unordered_map ' um ' implemented as hash table ; Insert the < node data , node pointer > tuple in ' um ' ; Generate all possible pairs ; p_product = product of elements in the current pair ; If ' x / p _ product ' is present in ' um ' and either of the two nodes are not equal to the ' um [ x / p _ product ] ' node ; Increment count ; Required count of triplets division by 3 as each triplet is counted 3 times ; A utility function to insert a new node at the beginning of doubly linked list ; Allocate node ; Put in the data ; Driver code ; Start with an empty doubly linked list ; Insert values in sorted order
import java . io . * ; import java . util . * ; class Node { int data ; Node next , prev ; } class GFG { static int countTriplets ( Node head , int x ) { Node ptr , ptr1 , ptr2 ; int count = 0 ; Map < Integer , Node > um = new HashMap < Integer , Node > ( ) ; for ( ptr = head ; ptr != null ; ptr = ptr . next ) { um . put ( ptr . data , ptr ) ; } for ( ptr1 = head ; ptr1 != null ; ptr1 = ptr1 . next ) { for ( ptr2 = ptr1 . next ; ptr2 != null ; ptr2 = ptr2 . next ) { int p_product = ( ptr1 . data * ptr2 . data ) ; if ( um . containsKey ( x / p_product ) && um . get ( x / p_product ) != ptr1 && um . get ( x / p_product ) != ptr2 ) { count ++ ; } } } return ( count / 3 ) ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( head == null ) { head = temp ; } else { temp . next = head ; head . prev = temp ; head = temp ; } return head ; } public static void main ( String [ ] args ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 8 ; System . out . println ( " Count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }
Maximize the profit by selling at | Java implementation of above approach : ; Function to find profit ; Calculating profit for each gadget ; sort the profit array in descending order ; variable to calculate total profit ; check for best M profits ; Driver Code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int solve ( int N , int M , int cp [ ] , int sp [ ] ) { Integer [ ] profit = new Integer [ N ] ; for ( int i = 0 ; i < N ; i ++ ) profit [ i ] = sp [ i ] - cp [ i ] ; Arrays . sort ( profit , Collections . reverseOrder ( ) ) ; int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( profit [ i ] > 0 ) sum += profit [ i ] ; else break ; } return sum ; } public static void main ( String args [ ] ) { int N = 5 , M = 3 ; int CP [ ] = { 5 , 10 , 35 , 7 , 23 } ; int SP [ ] = { 11 , 10 , 0 , 9 , 19 } ; System . out . println ( solve ( N , M , CP , SP ) ) ; } }
Find the largest number that can be formed with the given digits | Java program to generate largest possible number with given digits ; Function to generate largest possible number with given digits ; sort the given array in ascending order and then traverse into descending ; generate the number ; Driver code
import java . * ; import java . util . Arrays ; class GFG { static int findMaxNum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int num = arr [ 0 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { num = num * 10 + arr [ i ] ; } return num ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 0 } ; int n = arr . length ; System . out . println ( findMaxNum ( arr , n ) ) ; } }
Minimum partitions of maximum size 2 and sum limited by given value | Java program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; if sum of ith smaller and jth larger element is less than key , then pack both numbers in a set otherwise pack the jth larger number alone in the set ; After ending of loop i will contain minimum number of sets ; Driver Code
import java . util . Arrays ; class GFG { static int minimumSets ( int arr [ ] , int n , int key ) { int i , j ; Arrays . sort ( arr ) ; for ( i = 0 , j = n - 1 ; i <= j ; ++ i ) if ( arr [ i ] + arr [ j ] <= key ) j -- ; return i ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 3 , 4 } ; int n = arr . length ; int key = 5 ; System . out . println ( minimumSets ( arr , n , key ) ) ; } }
Bubble Sort On Doubly Linked List | Java program to sort a doubly linked list using bubble sort ; structure of a node ; Function to insert a node at the beginning of a linked list ; Function to print nodes in a given linked list ; Bubble sort the given linked list ; Checking for empty list ; Driver code ; start with empty linked list ; Create linked list from the array arr [ ] . Created linked list will be 1 -> 11 -> 2 -> 56 -> 12 ; print list before sorting ; sort the linked list ; print list after sorting
class GFG { static class Node { int data ; Node prev ; Node next ; } ; static Node insertAtTheBegin ( Node start_ref , int data ) { Node ptr1 = new Node ( ) ; ptr1 . data = data ; ptr1 . next = start_ref ; if ( start_ref != null ) ( start_ref ) . prev = ptr1 ; start_ref = ptr1 ; return start_ref ; } static void printList ( Node start ) { Node temp = start ; System . out . println ( ) ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } static Node bubbleSort ( Node start ) { int swapped , i ; Node ptr1 ; Node lptr = null ; if ( start == null ) return null ; do { swapped = 0 ; ptr1 = start ; while ( ptr1 . next != lptr ) { if ( ptr1 . data > ptr1 . next . data ) { int t = ptr1 . data ; ptr1 . data = ptr1 . next . data ; ptr1 . next . data = t ; swapped = 1 ; } ptr1 = ptr1 . next ; } lptr = ptr1 ; } while ( swapped != 0 ) ; return start ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 56 , 2 , 11 , 1 , 90 } ; int list_size , i ; Node start = null ; for ( i = 0 ; i < 6 ; i ++ ) start = insertAtTheBegin ( start , arr [ i ] ) ; System . out . printf ( " Linked list before sorting "); printList ( start ) ; start = bubbleSort ( start ) ; System . out . printf ( " Linked list after sorting "); printList ( start ) ; } }
Substring Sort | Alternative code to sort substrings ; sort the input array ; repeated length should be the same string ; two different strings with the same length input array cannot be sorted ; validate that each string is a substring of the following one ; get first element ; The array is valid and sorted print the strings in order ; Test 1 ; Test 2
import java . util . Arrays ; public class Demo { public static void substringSort ( String [ ] arr , int n , int maxLen ) { int count [ ] = new int [ maxLen ] ; String [ ] sortedArr = new String [ maxLen ] ; Arrays . fill ( count , 0 ) ; Arrays . fill ( sortedArr , " " ) ; for ( int i = 0 ; i < n ; i ++ ) { String s = arr [ i ] ; int len = s . length ( ) ; if ( count [ len - 1 ] == 0 ) { sortedArr [ len - 1 ] = s ; count [ len - 1 ] = 1 ; } else if ( sortedArr [ len - 1 ] . equals ( s ) ) { count [ len - 1 ] ++ ; } else { System . out . println ( " Cannot ▁ be ▁ sorted " ) ; return ; } } int index = 0 ; while ( count [ index ] == 0 ) index ++ ; int prev = index ; String prevString = sortedArr [ prev ] ; index ++ ; for ( ; index < maxLen ; index ++ ) { if ( count [ index ] != 0 ) { String current = sortedArr [ index ] ; if ( current . contains ( prevString ) ) { prev = index ; prevString = current ; } else { System . out . println ( " Cannot ▁ be ▁ sorted " ) ; return ; } } } for ( int i = 0 ; i < maxLen ; i ++ ) { String s = sortedArr [ i ] ; for ( int j = 0 ; j < count [ i ] ; j ++ ) { System . out . println ( s ) ; } } } public static void main ( String [ ] args ) { int maxLen = 100 ; String [ ] arr1 = { " d " , " zddsaaz " , " ds " , " ddsaa " , " dds " , " dds " } ; substringSort ( arr1 , arr1 . length , maxLen ) ; String [ ] arr2 = { " for " , " rof " } ; substringSort ( arr2 , arr2 . length , maxLen ) ; } }
Print all the pairs that contains the positive and negative values of an element | Java program to find pairs of positive and negative values present in an array . ; Function ; Sort the array ; Traverse the array ; For every arr [ i ] < 0 element , do a binary search for arr [ i ] > 0. ; If found , print the pair . ; Driver code
import java . util . * ; class GFG { static void printPairs ( int arr [ ] , int n ) { boolean pair_exists = false ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( java . util . Arrays . binarySearch ( arr , - arr [ i ] ) != - 1 ) { System . out . println ( arr [ i ] + " , ▁ " + - arr [ i ] ) ; pair_exists = true ; } } else break ; } if ( pair_exists == false ) System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . length ; printPairs ( arr , n ) ; } }
Multiset Equivalence Problem | Java program to check if two given multisets are equivalent ; sort the elements of both multisets ; Return true if both multisets are same . ; Driver code
import java . util . * ; class GFG { static boolean areSame ( Vector < Integer > a , Vector < Integer > b ) { Collections . sort ( a ) ; Collections . sort ( b ) ; return ( a == b ) ; } public static void main ( String [ ] args ) { Vector < Integer > a = new Vector < Integer > ( Arrays . asList ( 7 , 7 , 5 ) ) ; Vector < Integer > b = new Vector < Integer > ( Arrays . asList ( 7 , 5 , 5 ) ) ; if ( areSame ( a , b ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
Minimum number of edges between two vertices of a Graph | Java program to find minimum edge between given two vertex of Graph ; Method for finding minimum no . of edge using BFS ; visited [ n ] for keeping track of visited node in BFS ; Initialize distances as 0 ; queue to do BFS . ; update distance for i ; method for addition of edge ; Driver method ; To store adjacency list of graph
import java . util . LinkedList ; import java . util . Queue ; import java . util . Vector ; class Test { static int minEdgeBFS ( Vector < Integer > edges [ ] , int u , int v , int n ) { Vector < Boolean > visited = new Vector < Boolean > ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { visited . addElement ( false ) ; } Vector < Integer > distance = new Vector < Integer > ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { distance . addElement ( 0 ) ; } Queue < Integer > Q = new LinkedList < > ( ) ; distance . setElementAt ( 0 , u ) ; Q . add ( u ) ; visited . setElementAt ( true , u ) ; while ( ! Q . isEmpty ( ) ) { int x = Q . peek ( ) ; Q . poll ( ) ; for ( int i = 0 ; i < edges [ x ] . size ( ) ; i ++ ) { if ( visited . elementAt ( edges [ x ] . get ( i ) ) ) continue ; distance . setElementAt ( distance . get ( x ) + 1 , edges [ x ] . get ( i ) ) ; Q . add ( edges [ x ] . get ( i ) ) ; visited . setElementAt ( true , edges [ x ] . get ( i ) ) ; } } return distance . get ( v ) ; } static void addEdge ( Vector < Integer > edges [ ] , int u , int v ) { edges [ u ] . add ( v ) ; edges [ v ] . add ( u ) ; } public static void main ( String args [ ] ) { int n = 9 ; Vector < Integer > edges [ ] = new Vector [ 9 ] ; for ( int i = 0 ; i < edges . length ; i ++ ) { edges [ i ] = new Vector < > ( ) ; } addEdge ( edges , 0 , 1 ) ; addEdge ( edges , 0 , 7 ) ; addEdge ( edges , 1 , 7 ) ; addEdge ( edges , 1 , 2 ) ; addEdge ( edges , 2 , 3 ) ; addEdge ( edges , 2 , 5 ) ; addEdge ( edges , 2 , 8 ) ; addEdge ( edges , 3 , 4 ) ; addEdge ( edges , 3 , 5 ) ; addEdge ( edges , 4 , 5 ) ; addEdge ( edges , 5 , 6 ) ; addEdge ( edges , 6 , 7 ) ; addEdge ( edges , 7 , 8 ) ; int u = 0 ; int v = 5 ; System . out . println ( minEdgeBFS ( edges , u , v , n ) ) ; } }
Minimum swaps so that binary search can be applied | Java program to find Minimum number of swaps to get the position of the element if we provide an unsorted array to binary search . ; Function to find minimum swaps . ; Here we are getting number of smaller and greater elements of k . ; Here we are calculating the actual position of k in the array . ; Implementing binary search as per the above - discussed cases . ; If we find the k . ; If we need minimum element swap . ; Else the element is at the right position . ; If we need maximum element swap . ; Else element is at the right position ; Calculating the required swaps . ; If it is impossible . ; Driver function
public class GFG { static int findMinimumSwaps ( int [ ] arr , int n , int k ) { int pos = 0 , num_min , num_max , need_minimum , need_maximum , swaps ; num_min = num_max = need_minimum = 0 ; need_maximum = swaps = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < k ) { num_min ++ ; } else if ( arr [ i ] > k ) { num_max ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == k ) { pos = i ; break ; } } int left , right , mid ; left = 0 ; right = n - 1 ; while ( left <= right ) { mid = ( left + right ) / 2 ; if ( arr [ mid ] == k ) { break ; } else if ( arr [ mid ] > k ) { if ( pos > mid ) { need_minimum ++ ; } else { num_min -- ; } left = mid + 1 ; } else { if ( pos < mid ) { need_maximum ++ ; } else { num_max -- ; } right = mid - 1 ; } } if ( need_minimum > need_maximum ) { swaps = swaps + need_maximum ; num_min = num_min - need_maximum ; need_minimum = need_minimum - need_maximum ; need_maximum = 0 ; } else { swaps = swaps + need_minimum ; num_max = num_max - need_minimum ; need_maximum = need_maximum - need_minimum ; need_minimum = 0 ; } if ( need_maximum > num_max need_minimum > num_min ) { return - 1 ; } else { return ( swaps + need_maximum + need_minimum ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 10 , 6 , 7 , 2 , 5 , 4 } , k = 4 ; int n = arr . length ; System . out . println ( findMinimumSwaps ( arr , n , k ) ) ; } }
Stable sort for descending order | Java program to demonstrate descending order stable sort using greater < > ( ) . ; Driver code
import java . util . * ; class GFG { static void reverse ( int a [ ] ) { int i , k , n = a . length ; int t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . length ; Arrays . sort ( arr ) ; reverse ( arr ) ; System . out . println ( "Array after sorting : NEW_LINE"); for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
Check if both halves of the string have at least one different character | Java implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Sorts the two halves separately Compares the two halves return true if any index has non - zero value ; Declaration and initialization of counter array ; Driver code
import java . io . * ; import java . util . * ; class GFG { static Boolean function ( char str [ ] ) { int l = str . length ; Arrays . sort ( str , 0 , ( l / 2 ) ) ; Arrays . sort ( str , ( l / 2 ) , l ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) if ( str [ i ] != str [ l / 2 + i ] ) return true ; return false ; } public static void main ( String [ ] args ) { char str [ ] = ( " abcasdsabcae " ) . toCharArray ( ) ; if ( function ( str ) ) System . out . println ( " Yes , ▁ both ▁ halves ▁ differ " + " ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else System . out . println ( " No , ▁ both ▁ halves ▁ do " + " ▁ not ▁ differ ▁ at ▁ all " ) ; } }
Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | Example input = [ 0 , 1 , 2 , 2 , 0 , 0 ] output = [ 0 , 0 , 0 , 1 , 2 , 2 ]
import java . util . ArrayList ; import java . util . List ; class GFG { static int [ ] inputArray = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; static List < Integer > outputArray = new ArrayList < > ( ) ; static int indexOfOne = 0 ; static void print ( ) { for ( int item : inputArray ) if ( item == 2 ) outputArray . add ( item ) ; else if ( item == 1 ) { outputArray . add ( indexOfOne , item ) ; indexOfOne += 1 ; } else if ( item == 0 ) { outputArray . add ( 0 , item ) ; indexOfOne += 1 ; } else { System . out . println ( " ▁ wrong ▁ value ▁ - ▁ Aborting ▁ " ) ; continue ; } } public static void main ( String [ ] args ) { print ( ) ; for ( int item : outputArray ) System . out . print ( item + " , ▁ " ) ; } }
Number of visible boxes after putting one inside another | Java program to count number of visible boxes . ; return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; Driver code
import java . util . LinkedList ; import java . util . Queue ; import java . util . Arrays ; public class GFG { static int minimumBox ( int [ ] arr , int n ) { Queue < Integer > q = new LinkedList < > ( ) ; Arrays . sort ( arr ) ; q . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int now = q . element ( ) ; if ( arr [ i ] >= 2 * now ) q . remove ( ) ; q . add ( arr [ i ] ) ; } return q . size ( ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 1 , 2 , 8 } ; int n = arr . length ; System . out . println ( minimumBox ( arr , n ) ) ; } }
Sort a binary array using one traversal | JAVA Code for Sort a binary array using one traversal ; if number is smaller than 1 then swap it with j - th number ; Driver program to test above function
import java . util . * ; class GFG { static void sortBinaryArray ( int a [ ] , int n ) { int j = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 ) { j ++ ; int temp = a [ j ] ; a [ j ] = a [ i ] ; a [ i ] = temp ; } } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 } ; int n = a . length ; sortBinaryArray ( a , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } }
Smallest element in an array that is repeated exactly ' k ' times . | Java program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] that is repeated k times ; Since arr [ ] has numbers in range from 1 to MAX ; set count to 1 as number is present once ; If frequency of number is equal to ' k ' ; Driver code
public class GFG { static final int MAX = 1000 ; static int findDuplicate ( int arr [ ] , int n , int k ) { int res = MAX + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { int count = 1 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) count += 1 ; if ( count == k ) res = Math . min ( res , arr [ i ] ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = arr . length ; System . out . println ( findDuplicate ( arr , n , k ) ) ; } }
Smallest element in an array that is repeated exactly ' k ' times . | Java program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] that is repeated k times ; Sort the array ; Find the first element with exactly k occurrences . ; Driver code
import java . util . Arrays ; public class GFG { static int findDuplicate ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < n ) { int j , count = 1 ; for ( j = i + 1 ; j < n && arr [ j ] == arr [ i ] ; j ++ ) count ++ ; if ( count == k ) return arr [ i ] ; i = j ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = arr . length ; System . out . println ( findDuplicate ( arr , n , k ) ) ; } }
Longest Common Prefix using Sorting | Java program to find longest common prefix of given array of words . ; if size is 0 , return empty string ; sort the array of strings ; find the minimum length from first and last string ; find the common prefix between the first and last string ; Driver Function to test other function
import java . util . * ; public class GFG { public String longestCommonPrefix ( String [ ] a ) { int size = a . length ; if ( size == 0 ) return " " ; if ( size == 1 ) return a [ 0 ] ; Arrays . sort ( a ) ; int end = Math . min ( a [ 0 ] . length ( ) , a [ size - 1 ] . length ( ) ) ; int i = 0 ; while ( i < end && a [ 0 ] . charAt ( i ) == a [ size - 1 ] . charAt ( i ) ) i ++ ; String pre = a [ 0 ] . substring ( 0 , i ) ; return pre ; } public static void main ( String [ ] args ) { GFG gfg = new GFG ( ) ; String [ ] input = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; System . out . println ( " The ▁ longest ▁ Common ▁ Prefix ▁ is ▁ : ▁ " + gfg . longestCommonPrefix ( input ) ) ; } }
Find the minimum and maximum amount to buy all N candies | Java implementation to find the minimum and maximum amount ; Function to find the maximum and the minimum cost required ; Sort the array ; Print the minimum cost ; Print the maximum cost ; Driver code ; Function call
import java . io . * ; import java . util . Arrays ; import java . lang . Math ; class GFG { static void find ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; int b = ( int ) Math . ceil ( n / k * 1.0 ) ; int min_sum = 0 , max_sum = 0 ; for ( int i = 0 ; i < b ; i ++ ) min_sum += arr [ i ] ; for ( int i = 2 ; i < arr . length ; i ++ ) max_sum += arr [ i ] ; System . out . println ( " minimum ▁ " + min_sum ) ; System . out . println ( " maximum ▁ " + max_sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 4 } ; int n = arr . length ; int k = 2 ; find ( arr , n , k ) ; } }
Check if it is possible to sort an array with conditional swapping of adjacent allowed | Returns true if it is possible to sort else false / ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver function
class Main { static boolean checkForSorting ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] - arr [ i + 1 ] == 1 ) { int temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } else return false ; } } return true ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 3 , 2 } ; int n = arr . length ; if ( checkForSorting ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Sort an array of strings according to string lengths | Java program to sort an Array of Strings according to their lengths ; Function to Sort the array of string according to lengths . This function implements Insertion Sort . ; Insert s [ j ] at its correct position ; Function to print the sorted array of string ; Driver function ; Function to perform sorting ; Calling the function to print result
import java . util . * ; class solution { static void sort ( String [ ] s , int n ) { for ( int i = 1 ; i < n ; i ++ ) { String temp = s [ i ] ; int j = i - 1 ; while ( j >= 0 && temp . length ( ) < s [ j ] . length ( ) ) { s [ j + 1 ] = s [ j ] ; j -- ; } s [ j + 1 ] = temp ; } } static void printArraystring ( String str [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( str [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { String [ ] arr = { " GeeksforGeeks " , " I " , " from " , " am " } ; int n = arr . length ; sort ( arr , n ) ; printArraystring ( arr , n ) ; } }