Please update this java code so 1. A scanner can be used to import the files. Then compute and test with a randomly generated array instead of a file. Compute all possible distinctive pair of values from an an array. [A,B,C, A]
A,B
A,C
B,C
- i.e. (A,B)==(B,A)
(A, A) is not valid
hint: sort the array first
Example: input [1, 2, 3, 2, 3, 4, 3] result 4 (with distinct
pairs) and 8 (with symmetric pairs
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class Exercise8 {
public static void main(String[] args) {
In in = new In(args[0]);
int[] values = in.readAllInts();
StdOut.println(countNumberOfPairs(values));
// Tests
int[] values1 = {1, 2, 4, 1, 2, 1, 2, 4, 5, 1, 2, 4, 5, 1, 2 ,5, 6,
7, 7, 8, 2, 1, 2, 4, 5};
StdOut.println("Equal pairs 1: " + countNumberOfPairs(values1) + "
Expected: 49");
int[] values2 = {1, 1, 1};
StdOut.println("Equal pairs 2: " + countNumberOfPairs(values2) + "
Expected: 3");
}
// O(n lg n) solution
private static int countNumberOfPairs(int[] values) {
Arrays.sort(values);
int count = 0;
int currentFrequency = 1;
for (int i = 1; i < values.length; i++) {
if (values[i] == values[i - 1]) {
currentFrequency++;
} else {
if (currentFrequency > 1) {
count += (currentFrequency - 1) * currentFrequency / 2;
currentFrequency = 1;
}
}
}
if (currentFrequency > 1) {
count += (currentFrequency - 1) * currentFrequency / 2;
}
return count;
}
// O(n) solution
private static int countNumberOfPairs2(int[] values) {
Map<Integer, Integer> valuesMap = new
HashMap<>();
int equalNumbersCount = 0;
for(int i = 0; i < values.length; i++) {
int count = 0;
if (valuesMap.containsKey(values[i])) {
count = valuesMap.get(values[i]);
}
count++;
valuesMap.put(values[i], count);
}
for(int numberKey : valuesMap.keySet()) {
if (valuesMap.get(numberKey) > 1) {
int n = valuesMap.get(numberKey);
equalNumbersCount += (n - 1) * n / 2;
}
}
return equalNumbersCount;
}
}
ANS.,)
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Exercise8 {
public static void main(String args[]){
Scanner sc = new
Scanner(System.in);
System.out.println("Enter file with
full path for reading data :");
String filePath=sc.next();
File file = new
File(filePath);
try{
Scanner scRead = new
Scanner(file);
// Read line by line
while (scRead.hasNextLine())
{
String
line=scRead.next();
//convert the
line into array
String
arrTemp[]=line.trim().split(",");
int values[]=new
int[arrTemp.length];
for(int
i=0;i<arrTemp.length;i++){
int num = Integer.parseInt(arrTemp[i]);
values[i]=num;
}
System.out.println("Equal distinct pairs
:"+countNumberOfPairs(values));
}
sc.close();
}catch(Exception e){
e.printStackTrace();
}
}
private static int countNumberOfPairs(int[] values)
{
int count = 0;
Arrays.sort(values);
int currentFrequency = 1;
for (int i = 1; i <
values.length; i++) {
if (values[i] == values[i - 1])
{
currentFrequency++;
} else {
if (currentFrequency > 1)
{
count += (currentFrequency - 1) *
currentFrequency / 2;
currentFrequency = 1;
}
}
}
if (currentFrequency > 1)
{
count += (currentFrequency - 1) *
currentFrequency / 2;
}
return count;
}
}
Input :
Enter file with full path for reading data :
D:\\tempfile.txt
in that file data is :
1,2,3,2,3,4,3
1,1,1
Output :
Equal distinct pairs :4
Equal distinct pairs :3
Explanation : The above code is reading file line by line and splitted by comma(,) and assign to array. And should be numbers only , otherwise you can get exceptions.
Please update this java code so 1. A scanner can be used to import the files....
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...
Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Template { public static void main(String args[]) { Scanner sc1 = new Scanner(System.in); //Standard input data String[] line1 = sc1.nextLine().split(","); //getting input and spliting on basis of "," String[] line2 = sc1.nextLine().split(" "); //getting input and spiliting on basis of " " Map<String, String> mapping = new HashMap<String, String>(); for(int i=0;i<line1.length;i++) { mapping.put(line1[i].split("=")[0], line1[i].split("=")[1]); //string in map where in [] as key and...
Reimpliment this bottom-up merge-sort queue code so it does not rely on the "import edu.princeton.cs.algs4.StdRandom" and "import edu.princeton.cs.algs4.StdOut"imports: /* import java.util.Queue; import java.util.LinkedList; import edu.princeton.cs.algs4.StdRandom; import edu.princeton.cs.algs4.StdOut; public class BottomUpMergeSort { public static void main(String[] args) { int N=Integer.parseInt(args[0]); Double [] a = new Double [N]; for(int i=0;i<N;i++) { a[i]=StdRandom.uniform(); } StdOut.println("Unsorted"); printArr(a); Queue que=sort(a); StdOut.println("\nSorted"); BottomUpMergeSort.printQue(que); } public static Queue sort(Double[] a) { int N=a.length; Queue->ques=new LinkedList>(); for(int i=0;i<N;i++) { ...
import java.util.*;
public class PairFinder {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read in the value of k
int k = Integer.parseInt(sc.nextLine());
// Read in the list of numbers
int[] numbers;
String input = sc.nextLine();
if (input.equals("")) {
numbers = new int[0];
} else {
String[] numberStrings = input.split(" ");
numbers = new int[numberStrings.length];
for (int i = 0; i < numberStrings.length; i++) {
numbers[i] = Integer.parseInt(numberStrings[i]);
}
}
System.out.println(findPairs(numbers, k));
}
//method that...
import java.util.Arrays; import stdlib.*; public class CSC300Homework4 { /** * As a model for Problem 1, here are two functions to find the minimum value of an array of ints * an iterative version and a recursive version * * precondition: list is not empty /** iterative version */ public static double minValueIterative (int[] list) { int result = list[0]; int i = 1; while (i <...
I am getting this Error can you please fix my Java
code.
import java.awt.Dialog;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fall_2017 {
public TextArea courseInput;
public Label textAreaLabel;
public JButton addData;
public Dialog confirmDialog;
HashMap<Integer, ArrayList<String>> students;
public Fall_2017(){
courseInput = new TextArea(20, 40);
textAreaLabel = new Label("Student's data:");
addData = new JButton("Add...
// can someone explain the code line by line shortly.thanks import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; // Java 8 example for RSA encryption/decryption. // Uses strong encryption with 2048 key size. public class GlobalMembers { public static void main(String[] args) throws Exception { String plainText = "Hello World!"; // Generate public and private keys using RSA Map<String, Object> keys = getRSAKeys(); PrivateKey privateKey = (PrivateKey) keys.get("private"); PublicKey publicKey = (PublicKey)...
package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * * * * Find the 3 Sections marked TODO: * * TODO #1: SumOddsRecursive * TODO #2: ReverseArray * TODO #3: MergeArrays * * You must not add static variables. You MAY add static functions. * * It is okay to add functions, such as * * <pre> * public static double sumOfOddsHelper (double[] list, int i) { * </pre> * * but it is NOT okay to...
Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() { super(); array = new Integer[0]; } Array(Array other) { super(); array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) { ...
Write a Java program to find the duplicate values of an array of integer values. import java.util.Arrays; public class ex7DuplicateValue { public static void main(String[] args) { int[] my_array = {1, 2, 3, 3, 4, 5, 6, 2}; } } //Duplicate Element : 2 //Duplicate Element : 3