Question
Is there any wrong with this code? And kindly show how this program works. I execute this program but it always give me 0 answer
Is there any wrong with this code? And kindly show how this program works. I execute this program but it always give me "0" answer which is wrong.
If you find something wrong I appreciate your help.
Here's the context of the code.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Tys {
public static int answer;
static int triplets(int[] a, int[] b, int[] c) {
int[] Adist = nodups(a);
int[] Bdist = nodups(b);
int[] Cdist = nodups(c);
int distinctTripletCount = 0;
Arrays.parallelSort(Adist);
Arrays.parallelSort(Bdist);
Arrays.parallelSort(Cdist);
for (int q : Bdist) {
long c1 = getValidIndex(Adist, q) + 1;
long c3 = getValidIndex(Cdist, q) + 1;
distinctTripletCount += c1 * c3;
}
return distinctTripletCount;
}
private static int [] nodups(int[] a) {
Set
for (int item : a) {
set.add(item);
}
int len = set.size();
int result[] = new int [len];
int i = 0;
for (int item : set) {
result[i++] = item;
}
return result;
}
static int getValidIndex(int[] distinctA, int key) {
int low = 0;
int high = distinctA.length - 1;
int count = -1;
while (low
int mid = low + (high -low) / 2;
if(distinctA[mid]
count = mid;
low = mid + 1;
}else
high = mid -1;
}
return count;
}
public static void set(JTextField t) {
t.setText(""+answer);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame cp = new JFrame();
cp.setLayout(new FlowLayout());
cp.setSize(400,400);
cp.add(new JLabel("lena: "));
JTextField lena = new JTextField(10);
cp.add(lena);
cp.add(new JLabel("lenb: "));
JTextField lenb = new JTextField(10);
cp.add(lenb);
cp.add(new JLabel("lenc: "));
JTextField lenc = new JTextField(10);
cp.add(lenc);
cp.add(new JLabel("A:"));
JTextField arr_a = new JTextField(10);
cp.add(arr_a);
cp.add(new JLabel("B:"));
JTextField arr_b = new JTextField(10);
cp.add(arr_b);
cp.add(new JLabel("C:"));
JTextField arr_c = new JTextField(10);
cp.add(arr_c);
JLabel output = new JLabel();
cp.add(output);
JTextField tfOutput = new JTextField(10);
tfOutput.setEditable(false);
JButton button = new JButton("TRIPLET");
cp.add(button);
cp.add(tfOutput);
try {
int len_a = Integer.parseInt(lena.getText());
int len_b = Integer.parseInt(lenb.getText());
int len_c = Integer.parseInt(lenc.getText());
int[] arra = new int[len_a];
int[] arrb = new int[len_b];
int[] arrc = new int[len_c];
Scanner texta = new Scanner((Readable)arr_a);
Scanner textb = new Scanner((Readable)arr_b);
Scanner textc = new Scanner((Readable)arr_c);
for (int i=0;i
arra[i] = texta.nextInt();
}
for (int i=0;i
arrb[i] = textb.nextInt();
}
for (int i=0;i
arrc[i] = textc.nextInt();
}
answer = triplets(arra, arrb, arrc);
System.out.println(""+answer);
} catch (Exception e) {
// TODO: handle exception
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
set(tfOutput);
JOptionPane.showMessageDialog(new JFrame(),answer);
}
});
cp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cp.setTitle("TRIPLE SUM");
cp.setSize(550, 620);
cp.setVisible(true);
}
}
}
r. For example, given a = [3, 5, 7], b = [3,6], and [4, 6, 9), we find four distinct triplets: (3, 6, 4), (3, 6, 6), (5, 6, 4), (5,6,6). C= Function Description Complete the triplets function in the editor below. It must return the number of distinct triplets that can be formed from the given arrays. triplets has the following parameter(s): a, b, c: three arrays of integers. Input Format The first line contains 3 integers lena, lenb, and lenc, the sizes of the three arrays. The next 3 lines contain space-separated integers numbering lena, lenb, and lenc Constraints 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started