Count Binary recursion? - java

Write a method countBinary that accepts an integer n as a parameter and that prints all binary numbers that have n digits in ascending order, printing each value on a separate line. All n digits should be shown for all numbers, including leading zeros if necessary. You may assume that n is non-negative. If n is 0, a blank line of output should be produced. Do not use a loop in your solution; implement it recursively.
The issue Im having with this is I don't know how I would print the zeros and ones, since n is the only parameter I can have. I also cannot use a for loop and put my recursive call in the loop, so Im sorta stuck. this is what I have so far:
public static void countBinary(int n){
if (n < 0) {
throw new IllegalArgumentException();
}if(n == 0){
System.out.print("");
}else{
countBinary(n - 1);
System.out.println(n ); // I tried doing n + "0" and + "1" did not work
countBinary(n - 1 );
System.out.print(n );
// store += n;
}
}
And here is my output for countBinary(2):
1
12
1
12
When it should be this:
00
01
10
11
I am getting the right amount of "levels" for every other line which is strange, but Im really stuck
Note: this is NOT homework, simply practice. THanks!

You're on the right track with recursing twice. The concept is to print a "0" followed by an (n-1)-digit number, then a "1" followed by an (n-1)-digit number. The trick is to figure out how to handle the fact that for n > 1, there are many (n-1)-digit numbers and they each need to have that "0" or "1" in front of them. The way to handle that is to not actually print the "0" or "1" but to pass it down the recursion until you are ready to print each entire line. For that, you'll need an auxiliary method to do the actual recursion and you can use a char[], a StringBuilder, or even a String for the pending output so far. (I'd use the first and the second is fine too. I'd avoid using a String because it will generate a new String each time you need to add a "0" or "1"—a lot of garbage.)
Here's my solution:
public static void countBinary(int n){
if (n < 0) {
throw new IllegalArgumentException();
}
countBinary(new char[n], n);
}
private static void countBinary(char[] prefix, int n) {
if (n == 0) {
// base case -- no more recursion
System.out.println(prefix);
} else {
// position next digit counting from the right so output is in increasing order
final int i = prefix.length - n;
// prefix a '0' and recurse
prefix[i] = '0';
countBinary(prefix, n-1);
// prefix a '1' and recurse
prefix[i] = '1';
countBinary(prefix, n-1);
}
}

I don't believe the "NOT homework" thing, but anyways.
You want to print binary numbers by recursion. We can not do this by only having n as a single parameter. Because if we want to print a number of n digits haven the first x digits and then letting recursion add the oder y we need to know what the first x digits where.
That means, if someone tells me: "Print all binary numbers with n digits", I say: fine, this can be done by recursively printing all numbers with n digits and adding "" before them.
Now when someone asks me to print all numbers with n digits and adding "s" before it, I ask tell somebody else "Please print all binary numbers with n-1 digits and a "s" and 0 in front of it" because my number could start with 0.
After that I tell someone else "Please print all binary numbers with n-1 digits and a "s" and 1 in front of it" as my nubmer could also start with a 1.
This translates to code as follows:
// print all numbers with n digits
public static void printBinaryNumber(int n){
// print all numbers with n digits recursively adding ""
printBinaryNumbersRec("", n);
}
public static void printBinaryNumbersRec(String s, int n){
if(n < 0) throw new IllegalArgumentException();
if(n == 0) {
// if I should print all numbers with 0 digits and add s before them
// I just print s and am done
System.out.println(s);
return;
}
// add an additional 0 to s and print all numbers with n-1 digits
printBinaryNumbersRec(s + "0", n-1);
// add an additional 1 to s and print all numbers with n-1 digits
printBinaryNumbersRec(s + "1", n-1);
}

Related

Multiply large string numbers without using Big-Integer library

I coded a simple solution which multiplied 2 integers given as strings and returned the product as a string. But it doesn't work for big values and I'm not allowed to use the Big-Integer library.
This is my current solution.
class Solution {
public String multiply(String num1, String num2) {
int numOne = 0;
int numTwo = 0;
for(char c : num1.toCharArray()){
if((int)(c-'0') >= 0 && (int)(c-'0') <= 9){
numOne = numOne*10 + (int)(c-'0');
}
}
for(char c : num2.toCharArray()){
if((int)(c-'0') >= 0 && (int)(c-'0') <= 9){
numTwo = numTwo*10 + (int)(c-'0');
}
}
return numOne*numTwo + "";
}
}
I got an error in the following test case:
Input:
"123456789"
"987654321"
Output:
"-67153019"
Expected:
"121932631112635269"
What changes should I make so as to be able to multiply large numbers also?
For this, you can refer to the Karatsuba Multiplication algorithm.
You can do this too.
If any of the numbers is negative pick up from substring 1 to n.
Start from the last digit of second number.
Multiply it with the first number. store the result.
Do it with all the numbers of the second numbers and keep adding the result of the previous step to current step with ith shift in the position.
There are samples of this solution too on the internet. You will find it.

I'm just having hard time understanding the class material from my java class

Can someone explain this code line by line. I just started to learn how to code and I'm learning java right now,but I'm having hard time how this function executes. What does new int[10]; really do? and I don't really get the difference or how repeatedDigits[index] part is used differently from the repeatedDigits[remainder] part.
Write a function that counts the repeated digits in the integer x.
For * example: if x is 999, then your function must print back that 9 occurs 3 * times. Another example, if the integer x is 992121, then your function * must print back that 9 occurs 2 times, 2 occurs 2 times and 1 occurs 2 * times. This was a guideline for the function.
public static void countRepeatedDigitsInANumber(int x) {
int[] repeatedDigits = new int[10];
while (x > 0) {
int remainder = x % 10;
x = x / 10;
repeatedDigits[remainder] = repeatedDigits[remainder] + 1;
}
int index = 0;
while (index < 10) {
if (repeatedDigits[index] != 0)
System.out.println("The digit " + index + " occurs " +
repeatedDigits[index]);
index++;
}
}
This is a code that compiles. I'm just posting this because I'm having hard time to understand. I would realy appreciate if someone could give any suggestion to learn java quickly as well!
What does new int[10];
It creates a new array of size 10 that can hold int values. All of the values in the array will be initialized to 0 (this is just how Java handles int primitives).
In this case, it looks like this array is being used to store the number of times that the digit corresponding to its index has been seen.
Hmm, that was confusing to type, and probably even more confusing to read, so let me use an example. For the input 992121, you'd expect the repeatedDigits array to look like this when the program is done:
[0, 2, 1, 0, 0, 0, 0, 0, 0, 2]
i.e. Two 1s, one 2, and two 9s
while (x > 0) {
This starts a loop that will keep going until x is equal to (or smaller than, but that shouldn't happen) zero.
int remainder = x % 10;
This essentially "captures" the right-most digit of the number using modular arithmetic. It divides the number by 10, and any remainder is going to be what's in the "ones place" (i.e. the right-most digit).
x = x / 10;
This shifts digits to the right by one, and throws away the right-most digit (since you've already captured it).
repeatedDigits[remainder] = repeatedDigits[remainder] + 1
This just increments the value in the array at the appropriate location. For example, if you just pushed a 9 off the right end of the number, this will increment the value in repeatedDigits[9] so you know that you just saw an additional 9.
int index = 0;
while (index < 10) {
We're about to do some looping! There are (in my opinion) better ways to do this, but this works just as well. Basically, you want index to start at 0, and loop all the way up to 9, but stop when you get to 10.
if (repeatedDigits[index] != 0)
If the value at a given location in the repeatedDigits array is not zero, then...
System.out.println("The digit " + index + " occurs " + repeatedDigits[index]);
... print it out!
index++;
And finally, increment index so we can check the next number. This is exactly the same as index = index + 1.
Welcome to SO and the beautiful world of Java! I've attached hyperlinks all throughout my answer that could possibly help you out.
int[] repeatedDigits = new int[10];
The above line of code is creating an array with 10 elements, from index: 0 to index: 9.
int remainder = x % 10;
The above line of code is calculating the remainder once 'x' has been divided by 10. For example, if 'x = 9192' then it would be dividing '9192 / 10 = 919.2' And taking the remainder. Which in this case is the '2'. Notice how it's actually looking at the number in reverse? So a number like '9192' is actually being processed as '2919'.
x = x / 10;
The above line of code is dividing the number down by 10- since it's integer division it drops all decimal values. Since we have already calculated the last number. So our '9192' is now just '919'.
repeatedDigits[remainder] = repeatedDigits[remainder] + 1;
The above line of code is going to the index of the array of the remainder value that we stored earlier. Remember how our remainder was '2'? It's not going to index: 2 in our array and adding 1 to it's value- which was originally 0. So now our array at index: 2 has the value: 1. Meaning the number 2 was repeated 1 time.
What I've laid out above is just 1 iteration of the while loop that the top half of your code is in. So get a piece of paper and redo everything I did above with the remaining number '919'.
Once you're finished you should notice that your array looks something like this:
index 0: 0
index 1: 1
index 2: 1
index 3: 0
index 4 to 8: 0
index 9: 2
This means that '1' came once. '2' came once. and '9' came twice.
As for advice on learning Java? Read a lot. Google every little question you have, and if you don't understand how something is working get a piece of paper and write it out.
int[] repeatedDigits = new int[10];
//create an array of integers of size 10
while (x > 0) {
//while the integer provided is greater than 0
int remainder = x % 10;
//gets the smallest value digit in a base ten system. 101%10 =1
x = x / 10;
//in base ten this reduces the size of the number by a tens place. 101/10 =10 as 10 can be fully divided into 101 10 times. You've dropped the last digit of the number in this way. This is what will allow you to exit the while loop, as eventually x will be equal to 0.
repeatedDigits[remainder] = repeatedDigits[remainder] + 1;
remainder here can be between 0-9. This is because remainder is the remainder of dividing the provided integer by 10. You can't divide a number greater than 0 by ten and have a value less than zero or greater than 9.
The array you created has indexes 0-9, as it is ten spaces long (0,1,2,3,4,5,6,7,8,9). You are assigning the value of the index of repeated Digits that corresponds to your remainder to whatever it was previously plus 1.
What this means is you have found that the last digit in the number is equal to some value X. X must be between 0-9. As you have an array with indexes between 0-9 you get the value associated with repeatedDigits[X], add one to it as you have found another value of X, and set repeatedDigits[X]'s value equal to what is was before you found this new X plus one.
}
int index = 0;
You start at the first value of an array (0)
while (index < 10) {
Go through the values of the array from 0 to 9
if (repeatedDigits[index] != 0)
if the value of the array repeatedDigits at index is not 0. This is the same as saying if there was a value of index found in the input number.
System.out.println("The digit " + index + " occurs " +
repeatedDigits[index]);
index is the number we looked at. The count of how many times it occurred is stored at repeatedDigits[index]. We are printing the value and how many times it occurred.
index++;
Increase the index, examining the next index value.
}
}
Let me see if I can clear a few things up for you. My first suggestion is to format your code so that the lines within loops are indented:
public static void countRepeatedDigitsInANumber(int x) {
int[] repeatedDigits = new int[10];
while (x > 0) {
int remainder = x % 10;
x = x / 10;
repeatedDigits[remainder] = repeatedDigits[remainder] + 1;
}
int index = 0;
while (index < 10) {
if (repeatedDigits[index] != 0)
System.out.println("The digit " + index + " occurs " + repeatedDigits[index]);
index++;
}
}
}
This helps us to see the structure of the program. We can now see that the program runs as follows:
Define a new int array: int[] repeatedDigits = new int[10];
Run a while loop: while (x > 0) { ... }
Define an int variable: int index = 0;
Run a second while containing an if statement: while (index < 10) { if ( ... ) { ... } }
I'll try to explain each of the four pieces one at a time.
First we define our int array. new int[10] defines an array with ten spots in it, each of which can hold an integer. You can think of this like ten boxes next to each other, each labeled with an index. This array will be used to keep track of how many times we've seen each integer. repeatedDigits[0] will hold the number of times we've seen a 0 digit in our number; repeatedDigits[6] will hold the number of times we've seen a 6 digit, and so on.
Here's the heart of the program. We're going to break down the number, one digit at a time, and for each digit, we add one to the spot in the array used to keep track of how many times we've seen that digit. This line gets us the rightmost digit in our number: int remainder = x % 10;. % is called the modulo operator, and you can search for that to learn more about it. With the input 992121, this will assign remainder to 1.
The next line uses integer division to remove that last digit from the number, so that the next time through the loop we get the next digit. if x = 992121 then after x = x / 10, x will equal 99212.
Finally, we add one to the spot in the repeatedDigits array corresponding to the digit we saw. So in our example, remainder = 1, so we add 1 to the repeatedDigits[1] spot. This line takes advantage of the fact that an uninitialized int defaults to 0.
We've reached the end of the while loop, so we check the condition, and find that x > 0, so we run the loop again. We repeat this for every digit in the input number`.
The hard work is now done; the rest of the program is there to print out the results.
We assign index to 0. The next while loop will increment this variable from 0 to 9, printing the value of the repeatedDigits array for each digit.
We run the loop from index = 0 to index = 9. For each value, we check to see if we saw that digit in our input number (that's the if statement). We check the value in repeatedDigits[index] to see if it doesn't equal 0. If so, then we must have incremented it in the previous loop, so we know that digit appeared at least once in the original input. In that case, we print a line to the console using System.out.println. Then we increment index and start the loop again.
Please let me know if you have any questions, and good luck!

sum of first and last digit in the integer

The method written in the below code needs to take integer and result sum of 1st digit and last digit in the integer.
NOTE: The reason i am asking this question though i got to know the correct solution is i need to understand why my code is not working also as that makes me a better programmer please help.
public class FirstLastDigitSum {
public static int sumFirstAndLastDigit(int number)//to add first and last
//digits of a given interger
{
int firstdigit=0;
int lastdigit=0;
if(number>=0 && number<=9)
{
return number+number;
}
else if(number>9)
{
lastdigit=number%10;
while(number>0)
{
number/=10;
if(number<=9 & number>=0){
firstdigit=number;
}
}
return firstdigit+lastdigit;
}
else
return -1;
}
public static void main(String[] args)
{
System.out.println(sumFirstAndLastDigit(121));
}
}
In the above code if i keep number/=10 after if block like below
if(number<=9 & number>=0){
firstdigit=number;
}
number/=10;
then my code is giving proper results. like if i input 121 to the method as first digit is 1 and second digit is 1 it is summing both and giving me result 2. which is absolutely correct
But if keep number/=10 above the if block like below
number/=10;
if(number<=9 & number>=0){
firstdigit=number;
}
Then my code is not giving proper result it is giving only last number which is 1.
I am not at all understanding why this is happening can any one explain please.
Lets break this up into two parts. Get the last digit of the number, and getting the first digit of the number.
The first part of the problem is easy. Its exactly what you already have; just take modulo 10 of the number.
int lastdigit = number % 10;
The second part is a little trickier, but not too much. While the number is greater than 10 divide it by 10 (using integers you will have truncation for the remainder). One problem I see in your solution is that you keep checking the value even after a digit is discovered. That means if the value was 1234, you correctly find 1, but then overwrite it to 0 with an extra loop iteration
int firstdigit = number;
while (firstdigit >= 10) {
firstdigit /= 10;
}
And that's it, you are done. Just return the value.
return firstdigit + lastdigit;
If the number is less than 0 then return -1. Otherwise the last number can be found by modulus 10, and the first number found by dividing by 10 until that number is less than 10.
public static int sumFirstAndLastDigit(int number) {
if (number < 0) {
return -1;
}
int last = number % 10;
int first = number;
while (first >= 10) {
first = first / 10;
}
return first + last;
}
In this case:
while(number>0)
{
number/=10;
if(number<=9 & number>=0){
firstdigit=number;
}
}
Note that you change number after you check number > 0. That means that number can be 0 when reaches the if statement and if it is 0 it will be accepted as first digit because it satisfies the condition number<=9 & number>=0. As an example, when you test it with 121 you have this values for number 121, 12, 1 and 0. The last one satisfies the if statement and set first digit to 0.
In this case:
while(number>0)
{
if(number<=9 & number>=0){
firstdigit=number;
}
number/=10;
}
You change number before you check number > 0. That means that the if statement will never have a number = 0 and firstdigit will never be 0. As an example, when you test it with 121 you have this values for number 121, 12, and 1. The last one satisfies the if statement and set first digit to 1. After that you divide number by zero and it becomes 0 and it stop the while loop.
This is not a direct solution to your problem, but we can fairly easily handle this using regex and the base methods:
int number = 12345678;
String val = String.valueOf(number);
val = val.replaceAll("(?<=\\d)\\d+(?=\\d)", "");
number = Integer.parseInt(val);
int sum = number % 10 + number / 10;
System.out.println(sum);

I would like some help making a table that prints 1-100

So here are my assignment requirements:
Print out all the numbers between 1 and 100 inclusive, 10 numbers per line evenly spaced out using tabs.
Use a max of 2 loops and 1 if statement.
I have a pretty good understanding of how this needs to be done but I'm just having trouble figuring out a way to start a new line after every 10 numbers.
This is what I have so far:
import java.util.Scanner;
public class Table {
public static void main (String[] args) {
int counter, value;
counter = 1;
value = 0;
while (value < 100) {
value += counter;
System.out.print(value + "\t");
}
}
}
To start a new line, print the '\n' character.
if ((value % 10) == 0) {
System.out.print("\n"); // Or really, just System.out.println();, since that makes a new line.
}
There are two ways to approach this:
Work through each row in your table, and for each of these print each of the numbers x1, x2, x3, x4, ..., x + 10 followed by a newline character.
Work through all of the numbers individually, and after each one check to see if it's a round number of tens (think modulo arithmetic), and if is, follow it with a new line character.
Since you already know the span of numbers you need to iterate over, I'd use a for loop, starting at 0 and ending at < 100.
You're missing the increment, counter++ within your loop.
Use an if statement within the for loop, if (i % 10 = 0) {System.out.println(value + "/n";}, else System.out.println(value + " ");
Best of luck on your assignment.

How to know the lowest value that is not in a group of numbers

I have a group of numbers 0 to 9 => {0 .. 9}, but another group that represent each amount of {0 .. 9} we have, for example:
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 6
6 => 7
7 => 8
8 => 9
9 => 1
The lowest value that we cannot represent with those groups is 99. They are digits to represent a number, for example if we only have:
2 => 2
7 => 1
The lowest value should be 0 or 1, and the possibilities are, 2, 7, 22, 27, 72, 227, 272, 722.
I am doing an algorithym in java to calculate the lowest value that we cannot represent, but I even do not very well how to do it, and I need a little or big help.
Thanks.
You should decompose your problem.
Global algorithm
IMO, you should start at n = 0, and go up. For each value of n, check if the sequence of digits needed to represent it is allowed. As soon as you can't, n is your answer.
Smaller part
The new problem is to verify if the sequence of digits for a given number is allowed. That does not seem very difficult. You seem to deal with base 10 numbers only I guess:
decompose your number in base 10 digits
count the number of occurrences of each digit in your number
compare with the quantity available of that digit
if there are not enough, you're done, the number can't be represented
if there are enough, keep going with the other digits
Example implementation of the main algo. The argument nbAllowed would be a 10-sized array containing the quantity available of each digit. Ex: nbAllowed[2] is the number of times that the digit '2' is allowed to be used.
public static int smallestNonRepresentable(int[] nbAllowed) {
int n=0;
while (isRepresentable(n, nbAllowed)) {
n++;
}
return n;
}
Example implementation of the subroutine. This methods returns whether n is representable depending on the quantity available for each digit.
private static boolean isRepresentable(int n, int[] nbAllowed) {
int[] digitCount = getDigitCount(n);
// check if each digit is available enough times
for (int d = 0; d < nbAllowed.length; d++) {
if (nbAllowed[d] < digitCount[d]) {
return false; // not enough d digits to represent n
}
}
return true; // enough of all digits
}
Example implementation of how to count the number of occurrences of each digit in a number n. This method returns a 10-sized array containing the number of occurrences of each digit in n. Ex: digitCount[2] is the number of times that the digit '2' appears in the decimal representation of n.
private static int[] getDigitCount(int n) {
int[] digitCount = new int[10]; // 10 distinct digits
Arrays.fill(digitCount, 0); // start at 0 occurrence for each digit
// special case 0
if (n == 0) {
digitCount[0] = 1; // 1 occurrence for digit '0'
return digitCount;
}
// fill digitCount with the number of occurrences of each digit in n
int digit;
while(n > 0) {
digit = n % 10; // current last digit of n
digitCount[digit]++; // increments the number of occurrences for that digit
n /= 10; // shifts the decimal number n to the right (last digit disappears)
}
return digitCount;
}
The problem can be solved by not applying brute-force. There are four cases to distinguish in the given order, which means if we evaluate case 3, case 1 and 2 did not apply:
There is at least one digit with an amount of zero: then the smallest of those digits is not representable.
The set of digits with the smallest available amount does not contain the digit zero: now we know the amount is greater than zero and hence repeating the smallest of those digits one more time than available digits is the smallest non representable number.
The set of digits with the smallest available amount has a size of one: now we know it has to be the digit zero having an amount greater than zero. And we know the next greater digit, the one is available, due to passed through case 1. Hence the number composed of a leading 1 followed by amount many zeros is the smallest non representable number.
We know the set of digits with the smallest available amount contains the digit zero and at least one more digit: hence repeating the second smallest digit of this set one more time than available digits is the smallest non representable number.
Expressed in terms of a Java program the main algorithm looks like this:
private static final List<Integer> DIGIT_POOL =
Arrays.asList(2, 1, 3, 4, 5, 6, 7, 8, 9, 1);
public static void main(String[] args) {
Map<Integer, List<Integer>> amountToDigits = mapToDigits(DIGIT_POOL);
String number;
int amount = amountToDigits.keySet().iterator().next();
List<Integer> digits = amountToDigits.get(amount);
if (amount == 0) {
number = number(digits.get(0), 1);
} else if (!digits.contains(0)) {
number = number(digits.get(0), amount + 1);
} else if (digits.size() == 1) {
number = paddedWithZero(1, amount + 2);
} else {
number = number(digits.get(1), amount + 1);
}
System.out.println(number);
}
private static SortedMap<Integer, List<Integer>> mapToDigits(List<Integer> digitAmounts) {
SortedMap<Integer, List<Integer>> amountToDigits = new TreeMap<>();
for (int digit = 0; digit <= 9; digit++) {
int amount = digitAmounts.get(digit);
List<Integer> digits = amountToDigits.get(amount);
if (digits == null) {
digits = new ArrayList<>();
amountToDigits.put(amount, digits);
}
digits.add(digit);
}
return amountToDigits;
}
Constructing the resulting non representable number requires the following helper methods:
private static String paddedWithZero(Integer digit, int length) {
char[] letters = letters(0, length);
letters[0] = digit.toString().charAt(0);
return new String(letters);
}
private static String number(Integer digit, int length) {
return new String(letters(digit, length));
}
private static char[] letters(Integer digit, int length) {
char[] letters = new char[length];
Arrays.fill(letters, digit.toString().charAt(0));
return letters;
}
The total complexity of the algorithms is the sum of:
mapping n digits to their amounts: O(n)
distinguishing between four cases: O(1)
constructing a number of lenght m + 1, where m is the smallest amount of available digits: O(m)
Hence the total complexity is O(max(n, m)) which is linear. This solution works pretty fast for very large amounts (1k and greater) of given digits.
When you're going from the lowest point, it's usually prudent to start at 0. So we'll need some sort of loop, and because we don't know how many times we'll look, we'll implement a while loop.
boolean numberFound = false;
while(!numberFound) {
// Loops while a number has not been found.
}
We also need to keep track of our current number, so we'll declare a pointer p.
int p = 0;
boolean numberFound = false;
while(!numberFound) {
}
Now, let's assume you've got all these values in an array. So 1 is in position 0, 2 is in position 1 etc. You need to get the first value of p. Now, for any value of p between 9 and 0 inclusively, we can just grab the number of occurrences, so we do something like:
if(p <= 9 && p >= 0) {
// Get the number of occurrences.
int numberOfDigits = digits[p] // Digits is your array as described above.
if(numberOfDigits == 0) {
// You only need one digit to represent this number.
// If you don't have one digit, you've just found a number that works.
numberFound = true;
}
}
Next, you'll need to deal with numbers larger than 9. Because this has more than one digit, you'll need to do this in stages:
First, convert the Integer into a String.
Next, cycle through each character in that String.
Parse that character back to an integer, and grab the value from the array.
Get the number of times that character occurs in a string.
If the value in the array is less than the number of times that value occurs..
You've found a number.
Else, keep looping.
Good luck with the last part of the solution. As a general rule, when it looks like someone hasn't made much of an effort with a problem, I don't provide the entire solution.
My understanding is the same as Joffrey's algo.
Write a method like:
public boolean checkIfInGroup(int value) {
...
}
then you can create a loop in your outer method:
int n=0;
while (checkIfInGroup(n)) {
n++;
}
return n;
The challenge would be how to optimize the checkIfInGroup() method.
If I understood you correctly, you have one Array with numbers from 0 - 9 and another one with the same numbers in a different order. You want to know, which number is the one below the smallest displayable. So, if I understood you correctly, it should always be -1, because, if you have numbers from 0 - 9, 0 is the lowest displayable number, so -1 is the one beneath that. Could I help?

Categories

Resources