How can I match the original array and the shuffled array? - java

This is the Prompt:
This is what I wrote:
import java.util.Scanner;
public class ContagionControl {
public static void main(String[] args) {
System.out.println("Please Enter the Number of Citizens: ");
Scanner input= new Scanner(System.in);
int a = input.nextInt();
int[] A = new int [a];
System.out.printf(" Id");
for (int i=0; i< A.length; i++) {
System.out.printf("%4d", i);
}
System.out.println();
System.out.printf("Cantactee");
for (int i=0; i< A.length; i++) {
int b= (int) (Math.random() * A.length);
System.out.printf ("%4d", b);
}
System.out.println();
System.out.println("Please Enter the Number of Citizens");
int c = input.nextInt();
for (int j=0; )
}
}
How can I match the infected id with his/her contactee ?

you need to create another ArrayList to hold quarantine_citizens;
step 1 Then get the contactee from to the citizen array base (index of citizen, map index to contactee array)
create variable to hold contactee and put into quarantine_citizens ArrayList
and it needs to map to the citizen array again ( do step 1 again )

#Jay explained the required algorithm in his answer. Below is the code that implements the algorithm.
The following appears in the assignment instructions that you posted in your question:
use the shuffling algorithm
And also
Be aware that all the elements are distinct
Your shuffling algorithm does not ensure that all elements are distinct. In other words, the algorithm that appears in the code in your question may produce the same number more than once.
The below code uses code taken from this Web page:
Shuffle Array in Java
The below code also checks for valid user input. More explanation appears after the code.
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class CC {
private static int[] getCitizens(int numberOfCitizens) {
int[] citizens = new int[numberOfCitizens];
for (int i = 0; i < numberOfCitizens; i++) {
citizens[i] = i;
}
return citizens;
}
private static int[] getContactees(int[] array, int a) {
int[] contactees = new int[array.length];
System.arraycopy(array, 0, contactees, 0, array.length);
Random rd = new Random();
// Starting from the last element and swapping one by one.
for (int i = a - 1; i > 0; i--) {
// Pick a random index from 0 to i
int j = rd.nextInt(i + 1);
// Swap array[i] with the element at random index
int temp = contactees[i];
contactees[i] = contactees[j];
contactees[j] = temp;
}
return contactees;
}
private static void printArray(int[] array, String format, String prompt) {
System.out.print(prompt);
for (int i : array) {
System.out.printf(format, i);
}
System.out.println();
}
private static void printResult(List<Integer> list) {
System.out.println("These citizens are to be self-isolated in the following 14 days:");
for (Integer id : list) {
System.out.print(id + " ");
}
System.out.println();
}
public static void main(String[] args) {
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in); // Don't close 'Scanner' that wraps stdin.
// (Even though IDE warns that you should.)
int numberOfCitizens = 0;
do {
System.out.print("Number of citizens: ");
try {
numberOfCitizens = input.nextInt();
if (numberOfCitizens <= 0) {
System.out.println("Must be positive integer. Try again.");
}
}
catch (InputMismatchException xInputMismatch) {
System.out.println("Not an integer. Try again.");
input.nextLine();
}
} while (numberOfCitizens <= 0);
int[] citizens = getCitizens(numberOfCitizens);
String format = "%" + Integer.toString(numberOfCitizens).length() + "d ";
printArray(citizens, format, " ID ");
int[] contactees = getContactees(citizens, numberOfCitizens);
printArray(contactees, format, "Contactee ");
int infectedCitizen = -1;
int limit = numberOfCitizens - 1;
do {
System.out.printf("ID of infected citizen [0-%d]: ", limit);
try {
infectedCitizen = input.nextInt();
if (infectedCitizen < 0 || infectedCitizen > limit) {
System.out.println("Not within range. Try again.");
}
}
catch (InputMismatchException xInputMismatch) {
System.out.println("Not an integer. Try again.");
input.nextLine();
}
} while (infectedCitizen < 0 || infectedCitizen > limit);
List<Integer> infected = new ArrayList<>(numberOfCitizens);
while (true) {
if (!infected.contains(infectedCitizen)) {
infected.add(infectedCitizen);
infectedCitizen = contactees[infectedCitizen];
}
else {
break;
}
}
Collections.sort(infected);
printResult(infected);
}
}
The last while loop, in method main is the actual algorithm. All preceding code is just preparation. I refer to the following code:
List<Integer> infected = new ArrayList<>(numberOfCitizens);
while (true) {
if (!infected.contains(infectedCitizen)) {
infected.add(infectedCitizen);
infectedCitizen = contactees[infectedCitizen];
}
else {
break;
}
}
I now use the sample data from your question to explain the above algorithm. Here is the sample data:
ID 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Contactee 2 0 7 11 15 4 14 1 8 13 3 5 12 9 6 10
We create an empty List.
The user-entered ID is 0 (zero).
We add 0 to the List.
We get the element in Contactee array at index 0 – which is 2
We add 2 to the List.
We get the element in Contactee array at index 2 – which is 7
We add 7 to the List.
We get the element in Contactee array at index 7 – which is 1
We add 1 to the List.
The element in Contactee at index 1 is 0. Since 0 already appears in the List, we are done.
List now contains all the infected citizens, namely 0, 1, 2 and 7.
Note that sorting the list is only to display the list in ascending order. It is not required.
Here is output from a sample run of the above code:
Number of citizens: 16
ID 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Contactee 5 8 3 0 11 12 14 9 7 1 15 2 4 13 6 10
ID of infected citizen [0-15]: 0
These citizens are to be self-isolated in the following 14 days:
0 2 3 4 5 11 12

Related

How to fix a solution similar to Counting Sort?

I wanted to check if frequency of minimum element is odd: I would print lucky, otherwise print "Unlucky".
I used a technique related to counting sort arr[arr1[i]]++ counting duplicated as value and reference to the actual value to it's INDEX. I tried with this but it didn't bring the correct answer [appreciate if you correct my code rather than a new different solution]
input: 6 6 6 4 4 4 4 3 3 3 7 7
output: Unlucky
since the minimum frequency is 7, 7%2!=0 so Unlucky
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t=in.nextInt();
while(t-->0) { //Test cases
int n=in.nextInt();//array's length
int arr[]=new int[n];// array
for(int i=0;i<arr.length;i++) {
arr[i]=in.nextInt();
}
int counter[]=new int[256];//counting duplicates
for(int i=0;i<arr.length;i++) {
counter[arr[i]]++;
}
int min=1;
int pos=0;
for(int i=0;i<counter.length;i++) {
if(counter[i]<min && counter[i]!=0) { //search for minimal frequency (counter[i]!=0 since our counter array could easily have lot zeroes
min=counter[i];
pos=i;
}
}
System.out.println((pos%2==0)?"Unlucky":"Lucky");
}
}
There is no need to the while loop and other loop for counter, you assign the array using n variable,
then you enter values to the arr and check frequency in the same loop.
And select the min frequency to check if it's lucky or not
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();// array's length
int arr[] = new int[n];// array
int counter[] = new int[n];
int min = 1;
int pos = 0;
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
counter[arr[i]]++;
if (counter[i] < min && counter[i] != 0) {
min = counter[i];
pos = i;
}
}
System.out.println((pos % 2 == 0) ? "Unlucky" : "Lucky");
in.close();
}
, input
12 6 6 6 4 4 4 4 3 3 3 7 7
, output
Unlucky
Consider the following points:
Your program is less useful/efficient for three reasons: (A) It will work for integers from 0 to 255 only (B) You are using a fixed size array of size 256 which means that even if the user enters just a few numbers, it will consume space for 256 integers (C) You are iterating the loop 256 times to calculate the frequency even if the user enters just a few numbers.
There are so many ways to count the frequency of numbers. Commonly people use a Map to do so. I've used the same thing in the function, minFrequencyNumber shown below:
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of array: ");
int n = in.nextInt();
int arr[] = new int[n];
System.out.print("Enter " + n + " integers: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = in.nextInt();
}
System.out.println("The number with minimum frequency is: " + minFrequencyNumber(arr));
System.out.println((minFrequencyNumber(arr) % 2 == 0) ? "Unlucky" : "Lucky");
}
public static int minFrequencyNumber(int[] arr) {
Map<Integer, Integer> m = new HashMap<>();
// Iterate the array
for (int i : arr) {
// If the number does not exist in the Map, put the number as the key and 1 as
// the value (frequency); otherwise, put the number as the key in the map by
// increasing its value (frequency) by 1
m.put(i, m.getOrDefault(i, 0) + 1);
}
int num = 0, minFrequency = Integer.MAX_VALUE;
// Iterate the map
for (Entry<Integer, Integer> e : m.entrySet()) {
if (e.getValue() < minFrequency) {
minFrequency = e.getValue();
// Store the key (the number) whose frequency is minimum
num = e.getKey();
}
}
return num;
}
}
A sample run:
Enter the size of array: 12
Enter 12 integers: 6 6 6 4 4 4 4 3 3 3 7 7
The number with minimum frequency is: 7
Lucky

Storing data from the keyboard to an array while ignoring duplicates

So I have a program that I'm working on as an assignment, and part of the requirement is asking me to call a method that retrieve numbers from the user, and storing them into an array of size 20. I have to do this while not including any duplicate values in the array, and I will continue storing the users numbers until the array is full, or the user has entered a negative value. I'm running into a couple of issues.
The user has to be able to enter data through multiple lines of text (using the 'enter' key) without interruption, for example:
Enter the integers for the array:
1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 18 18 18 18 19
20
That should store: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 into the array without duplicate values. The program didn't accept any other values because 20 unique digits had been entered, which filled the array.
I've tried to use different options such as nextInt(), nextLine(), and even storing the numbers as a List and then converting but I'm having so many issues.
So my question is, how do I allow the user to enter values across multiple lines of input as well as checking if the current value in the keyboard buffer is duplicate/negative, so I can know to skip that value in the buffer or end the program?
I'll include my code, but honestly I've tried reworking this so many times that it's an absolute mess. Any help will be more than welcomed.
public static int getData(int [] set){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the integers for the array.");
System.out.println("The program will stop accepting values when you enter a negative number or reach the 20 unique limit.");
int usrInput=keyboard.nextInt(), arraySizeUsed = 0;
for(int position = 0; position < 20; ) {
if(!Arrays.asList(set).contains(usrInput)) {
if(usrInput > -1) {
set[position] = usrInput;
position++;
arraySizeUsed++;
}
else {
return arraySizeUsed;
}
}
else {
continue;
}
}
return arraySizeUsed;
}
I don't think I should use 'continue' in the loop as I just want to keep the position from incrementing in the case that the current number in the keyboard buffer is already in the array. What am I missing?
Set<Integer> values = new HashSet<>();
Scanner sc = new Scanner(System.in);
int count = 0;
do {
int in = sc.nextInt();
if(in>0 && count < 20) {
values.add(in);
}else {
break;
}
}while(sc.hasNext());
Here is another solution using a int array and a direct comparison search.
import java.util.Scanner;
public class NumberScanner {
private static final int MAX_SIZE = 20;
static int[] intArray = new int[MAX_SIZE];
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int value = 0;
int size = 0; // Not an array counter, starts at 1. 0 means empty.
System.out.println("Enter a series of positive integers on one or more lines. Enter a negative number to quit.");
// Loop until a negative number is found or the MAX_SIZE is reached
while (size < MAX_SIZE) {
value = keyboard.nextInt();
if (value < 0)
break;
boolean found = false;
if (value >= 0 && size > 0) {
// Add the value to the array
// First, check if its already there
for (int i = 0; i<size; i++) {
if (intArray[i] == value) {
found = true;
break; // Stop the loop
}
}
}
if (!found) {
intArray[size] = value;
size++;
}
}
for (int i=0; i<size; i++) {
System.out.printf(intArray[i] + " ");
}
System.out.printf("\n");
keyboard.close();
}
}
Try something like this;
int position = 0;
while (position < 20) {
int next = keyboard.nextInt();
if (next < 0) {
break; // Negative so exit loop and return
}
if (!ArrayUtils.contains(set, next)) {
set[position] = next;
position++;
}
return set;

Specific output pattern of array in java

I've figured out how to create an array in which the output look like this:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
if the user enters a 4 indicating a 4x4 array.
My question is how could I manipulate this code in a way that it would out put the array in this order
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
where every other row is "backwards"
`import java.util.Scanner;
import java.util.Arrays;
import java.util.Arrays;
public class Question2 {
public static void main(String[] args) {
//Title
System.out.println("[----------------------]");
System.out.println("[ Array Pattern ]");
System.out.println("[----------------------]");
System.out.println("");
//declare scanner
Scanner keyboard = new Scanner (System.in);
//Prompt user to enter a digit greater than or equal to 3
System.out.println("How many rows/columns do you want your array to have? (Must be at least 3):");
//read user input
int num = keyboard.nextInt();
//place constraints on int num so that if it is less than 3, the program does not execute
while(num<3 )
{
System.out.println("Lets's try this again....");
System.out.println("How many rows/colums do you want your array to have? (Must be at least 3):");
num = keyboard.nextInt();
}
//2D array with number of rows and columns entered by user
int[][] array = new int [num][num];
int inc=1;
for(int i=0;i<array.length;i++)
for(int j=0;j<array.length;j++)
{
array[i][j]=inc;
inc++;
}
//replace all square brackets in array display & format into order
//replace all commas in array display
String a = Arrays.toString(array);
a = Arrays.deepToString(array).replace("], [", "\n").replaceAll("[\\[\\],]", "");
System.out.println(a);`
This loop will do it:
int reverse = 0;
for(int i=0;i<array.length;i++)
for(int j=0;j<array.length;j++)
{
if(i%2 == 0){
if(j == 0){
inc = reverse;
if(i > 0 )inc = inc + num + 1;
}
array[i][j]=inc;
inc++;
}
else{
if(j == 0)reverse = inc + num - 1;
array[i][j]=reverse;
reverse--;
}
}
Every other row it place numbers in a descending order.
Also to print out the numbers with tabs between them use:
String a = Arrays.toString(array);
a = Arrays.deepToString(array).replace("], [", "\n").replace(", ", "\t").replaceAll("[\\[\\],]", "");
System.out.println(a);
This will stop a table forming diagonally.
What you could do is reverse the order of the for loop on every other line, so that it decrements instead:
for(int i=0;i<array.length;i++)
{
if(i%2 == 0){
for(int j=0;j<array.length;j++)
{
array[i][j]=inc;
inc++;
}
}
else{
for(int j=num-1;j>=0;j--)
{
array[i][j]=inc;
inc++;
}
}
}
I don't really know how to format the columns the way you wrote the code (with Arrays.deepToString) but if you instead loop through it manually you could pad the string:
String [][]stringConvertedTable= new String[num][num];
for(int i=0; i<num; i++) {
for(int j=0; j<num; j++) {
stringConvertedTable[i][j]= Integer.toString(array[i][j]);
System.out.print(stringConvertedTable[i][j] + "\t");
}
System.out.println("");
}
It's not the most elegant way to do it though...

sorting(ascending order and descending order) [duplicate]

This question already has answers here:
Sort an array in Java
(19 answers)
Closed 7 years ago.
Can someone help me to continue this code using array?this is the sample output
Enter number of element(2-10): 5
Element[1]: -3
Element[2]: 0
Element[3]: 10
Element[4]: 2
Element[5]: 7
Max Value is: 10
Min Value is: -3
Ascending order: -3 0 2 7 10
Descending order: 10 7 2 0 -3
I only have this code.
import java.util.Scanner;
public class Array1 {
public static void main(String[] args) {
Scanner n = new Scanner(System. in );
int num[] = new int[];
int ne = 0;
int e = 0;
System.out.print("Enter Number of Elements(2-10): ");
ne = n.nextInt();
for (int x = 1; x <= ne; x++) {
System.out.print("Element[" + x + "]: ");
e = n.nextInt();
}
}
}
The output of this is:
Enter number of element(2-10): 5
Element[1]: -3
Element[2]: 0
Element[3]: 10
Element[4]: 2
Element[5]: 7
Then I don't know how to get the others.
I can share the logic but not the code.
The input numbers from user can be added to an array.
Arrays.sort() can be used to sort in ascending or descending order.
Reference: Arrays Sort and Reverse order
Change code to below
1.) Take user input first for creating array with size ne.
2.) Initialize your array with size ne.
3.) In loop, keep getting and putting elements in array.
public static void main(String[] args) {
Scanner n = new Scanner(System.in);
int ne = 0;
int e = 0;
System.out.print("Enter Number of Elements(2-10): ");
ne = n.nextInt();
int num[] = new int[ne];
for (int x = 0; x < ne; x++) {
System.out.print("Eneter element");
num[x] = n.nextInt();
}
System.out.println("original " + num);
}
For Sorting, please try youserf first and then post

Put a range on the input of an array in java

I am new to java programming. I would like to know if there is a way that I can fill the array with integers from the keyboard(range: 10 to 65). Here is my code:
public static void main(String[] args)
{
//Keyboard Initialization
Scanner kbin = new Scanner(System.in);
//a.Declare an array to hold 10 intgers values
int list[]=new int[10];
int i=0;
//b.Fill the array with intgers from the keyboard(range: 10 to 50).
System.out.print("\n\tInput numbers from 10 to 50: \n");
list[i]= kbin.nextInt();
if(10<=list[i] && list[i] <= 50)
{
for(i=1; i<=9;i++)
{
list [i] = kbin.nextInt();
}
}
}
please help.Thanks!
If I understand you intent properly...
You need to loop until you have 10 valid numbers. If a number entered by the user is out of range, then it needs to be discarded.
import java.util.Scanner;
public class TestStuff {
public static void main(String[] args) {
//Keyboard Initialization
Scanner kbin = new Scanner(System.in);
//a.Declare an array to hold 10 intgers values
int list[] = new int[10];
int i = 0;
System.out.print("\n\tInput numbers from 10 to 50: \n");
while (i < 10) {
//b.Fill the array with intgers from the keyboard(range: 10 to 50).
int value = kbin.nextInt();
if (value >= 10 && value <= 50) {
list[i] = value;
i++;
} else {
System.out.println("!! Bad number !!");
}
}
for (int value : list) {
System.out.println("..." + value);
}
}
}
Example output...
Input numbers from 10 to 50:
1
!! Bad number !!
2
!! Bad number !!
3
!! Bad number !!
4
!! Bad number !!
5
!! Bad number !!
6
!! Bad number !!
7
!! Bad number !!
8
!! Bad number !!
9
!! Bad number !!
10
11
12
13
14
15
16
17
18
19
...10
...11
...12
...13
...14
...15
...16
...17
...18
...19
This should fix it...
System.out.print("\n\tInput numbers from 10 to 50: \n");
for(int i=0; i<10;)
{
int k = kbin.nextInt();
if (k >= 10 && k <= 50)
{
list[i] = k;
++i;
}
}
I am not really sure what you are trying to do. But, I am guessing that you are trying to take the first 10 numbers the users enters?
One important thing to remember is that java (and other languages) uses 0 based indexing. So your for loop where i = 1, i <= 9; i++ I think that i should start at 0, but again I am not sure what you are going for here.
Though this question has been answered already but I noticed that no one provided or talked about the .hasNext() regex method so below I am providing an answer to this question using regex expression:
import java.util.Scanner;
public class Main3 {
public static void main (String [] args) {
int list [] = new int [10];
for (int i = 0; i < 10; i ++) {
Scanner sc = new Scanner (System.in);
if (sc.hasNext("([1-4][0-9])?(50)?")) {
list[i] = sc.nextInt();
} else {
System.err.println("Entered value is out of range 10 - 50. Please enter a valid number");
i --;
}
}
for (int i = 0; i < 9; i ++) {
System.out.print(list[i] + " ");
}
System.out.println(list[9]);
}
}

Categories

Resources