In java write a code that returns true or false whenever every number from 1 to
ID: 3632362 • Letter: I
Question
In java write a code that returns true or false whenever every number from 1 to 10 appears in the array, but only occurs at most as many times as its value. For example the number 4 must appear in the array at least once but at a maximum of 4 times.Here is how the code should start. ( Note I'm only asking for what i need to fill in under the boolean method oneThroughTen, not under main.)
public class Numbers{
public static void main(String[] args){
oneThroughTen({1,2,3,4,5,6,7,8,9,10});
oneThroughTen({1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10});
oneThroughTen({1,2,2,3,3,3});
}
public boolean oneThroughTen(int[] nums){
// This is where I'm stuck
}
}
The output of this code should look something like this based on the test cases above.
True
False
False
Any help would greatly be appreciated.
Explanation / Answer
in the boolean function create an array say xyz with size 10 then as a number comes add 1 to the corresponding index ex: if number is 3 xyx[3] = xyz[3] +1 at the see if there is any zero's in the array xyz and if the value in particular index is greater than index if so return false else true.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.