Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Find permutations in increasing order using octal numbers: Assume that you are d

ID: 3674210 • Letter: F

Question

Find permutations in increasing order using octal numbers: Assume that you are dealing with numbers written in octal notation. Assume further that you are given an octal number that is eight digits long. Finally, assume that this number uses eight DIFFERENT octal digits – that is, no digit appears more than once. Here is one such number: 70162534. The largest such number that exists is 76543210. The smallest such number that exists is 01234567. (NOTE: we allow a leading zero in our numbers.) Write an algorithm in java that allows the user to input an eight digit octal number and then returns the next highest number where no digit appears more than once. Example input: 74536201 Example output: 74536210

Explanation / Answer

import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
import java.io.*;


class nextperm {  
   public static boolean nextPermutation(int[] array) {
       // Find non-increasing suffix
       int i = array.length - 1;
       while (i > 0 && array[i - 1] >= array[i])
           i--;
       if (i <= 0)
           return false;
      
       // Find successor to pivot
       int j = array.length - 1;
       while (array[j] <= array[i - 1])
           j--;
       int temp = array[i - 1];
       array[i - 1] = array[j];
       array[j] = temp;
      
       // Reverse suffix
       j = array.length - 1;
       while (i < j) {
           temp = array[i];
           array[i] = array[j];
           array[j] = temp;
           i++;
           j--;
       }
       return true;
   }
   public static void main (String [] args) {
    Scanner sc = new Scanner ( System.in );
    String s = sc.nextLine();
   int arr[] = new int[8];
    for(int i = 0 ;i<8;i++)
       arr[i] = (int)(s.charAt(i) - '0');
   if(nextPermutation(arr)){
       for(int i = 0 ;i<8;i++)
           System.out.print(arr[i]);
   }
   else
       System.out.println("no next permutation possible");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote