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
Related
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
public static void checkMultiple(int a[]){
//will use this count variable later
//int[] count = new int[10];
//first scan over the array
for (int i = 0; i < a.length; i++)
{
//seeded loop to check against 1st loop
for (int j = 0; j < i; j++)
{
if (a[i] == a[j])
{
System.out.print(a[i] + " ");
}
}
}
}
I'm having trouble counting repeated numbers in an integer array of 10 random numbers. I havent wrote the "count" function yet but the checkMultiple() will print out the numbers that are repeated. However, some of the time it prints correctly such as:
4 2 9 0 9 6 3 3 7 5
9 3
the first line being the whole array and the second the numbers that are repeated at least once in the array. But when there is more than two of a single integer, it counts every single one of that integer such as:
9 5 2 8 5 5 7 6 3 3
5 5 5 3
Any tips or advice would be much appreciated!
It looks like as you are looping through and then immediately outputting the results.
This prevents the program from comparing what's being currently parsed and what has already been been parsed and counted.
I would pass an empty array into the first FOR loop and instead of "System.out.print," store the number in the passed-in array. You can then compare the values that have already been parsed against the value currently being parsed to see if it has already been counted.
When the outside FOR loop exits, output the array that was originally passed in empty (and now has a record of every duplicate in the initial array)
You are counting the number of duplicate instances. Your second example has three pairs of duplicate 5's. That's why you see the 5 repeated three times. Only output unique duplicate results.
Just use a hash map, if the key does not exist add it to the map with the value 1, if it does increase the value, then print the hash map.
A single iteration of the array will solve the problem.
Map<Integer, Integer> counter = new HashMap<>();
for (int i = 0; i < a.length; i++) {
if (counter.containsKey(a[i])) {
counter.put(a[i], counter.get(a[i]) + 1);
} else {
counter.put(a[i], 1);
}
}
Iterator it = counter.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
for (int i = 0; i < pair.getValue(); ++i) {
System.out.print(pair.getKey() + " ");
}
// or you could just print the number and how many times it was found
System.out.println(pair.getKey() + " " + pair.getValue());
it.remove();
}
try this
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class BirthdayCake {
private static void printcake(int n,int[] arr){
//Map<Integer,Integer> ha = new HashMap<Integer,Integer>();
int temp = arr[0],max=0, count=0;
/*for(int i=1;i<n;i++){
max = arr[i];
if(max<=temp){
temp=max;
count++;
break;
}
else{
max = arr[i];
count++;
break;
}
}*/
Arrays.sort(arr);
for(int i:arr){
System.out.println(i);
}
System.out.println("max:" +max);
max = arr[arr.length-1];
for(int i=0;i<n;i++){
if(arr[i]==max){
count++;
}
}
System.out.println("count:" +count);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Entere thye array size:");
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
printcake(n,arr);
}
}
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
I'm supposed to create a mainmethod that calculates how many times a number have occured in a row. The user is supposed to input the numbers.
I guess I have to loop through an array though I'm not sure how..
Userinput to array, loop through and check if the number is the previous one.
Example:
1 3 4 5 6 6 6 9 - Output: The number 6 occured 3 times in a row
This is the code I have right now(sry missed to paste it):
public static void main(String[] args)
{
Scanner inp = new Scanner(System.in);
int counter = 0;
int[] x = new int[inp.nextInt()];
for (int i = 0; i < x.length; i++)
{
if(i == x[i])
{
counter++;
}
else
{
continue;
}
}
System.out.print(counter);
}
Any ideas?
Use a HashMap<Integer, Long> to track the number of occurences for each number:
Map<Integer, Long> numberOccurences = new HashMap<>();
for(int n : numbers) {
Long occurences = numberOccurences.get(n);
if(occurrences == null) { // first time the number n is seen
numberOccurences.put(n, 1L);
} else {
numberOccurences.put(n, occurences + 1);
}
}
I tried to sort an array accending and count the array elements
please help me find whats missing, have debugged many times. here is my code and the output i got. Thanks
package habeeb;
import java.util.*;
public class Habeeb {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] num = new int[30];
int i, count=0;
System.out.println("Enter the integers between 1 and 100" );
for( i=0; i<num.length; i++){
num[i]= input.nextInt();
if(num[i]==0)
break;
count++;
}
calling the function here
Sorting(num, i, count);
}
public static void Sorting(int[] sort, int a, int con){
if (a<0) return;
/*am sorting the array here*/
Arrays.sort(sort);
int j, count=0;
for(j=0; j<con; j++){
if(sort[a]==sort[j])
count++;
}
System.out.println(sort[a]+" occurs "+count+" times");
Sorting(sort, a-1, con);
}
}
Here is the output:
run:
Enter the integers between 1 and 100
2
5
4
8
1
6
0
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
0 occurs 6 times
Your problem is that the array is of size 30 and when you sort it you have all the values you did not assign to equal to 0 and thus they go in the front of the sorted array. Later on out of the first 6 numbers all are 0 so the output you have is correct.
To aviod the problem you face I suggest you use ArrayList instead of simple array so that you can add elements dynamically to it.
Try this
The counting method
public int count(int[] values, int value)
{
int count = 0;
for (int current : values)
{
if (current == value)
count++;
}
return count;
}
Then use
int[] sorted = Arrays.sort(num);
for (int value : sorted)
{
System.out.println("" + value + " occurs " + count(sorted, value) + " times");
}
This works for sure.
You are doing a bit more work than is necessary. I would solve this like so:
Map<Integer, Integer> countMap = new HashMap<Integer,Integer>();
for( i=0; i<num.length; i++){
int current = input.nextInt();
if(countMap.get(current) != null)
{
int incrementMe = countMap.get(current);
countMap.put(current,++incrementMe);
}
else
{
countMap.put(input.nextInt(),1);
}
}