Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Is there anything wrong in this code? My input is 4 distinct triplets but in this program it only have 3 jtextfield. I want to

Is there anything wrong in this code? My input is 4 distinct triplets but in this program it only have 3 jtextfield. I want to execute those input below.

If you find something wrong I appreciate your help.

Here is the input1: 3 2 3 1 3 5 2 3 1 2 3 The output should be: 8

Here is the input2: 3 3 3 1 4 5 2 3 3 1 2 3 The output should be: 5

Here is the input3: 4 3 4 1 3 5 7 5 7 9 7 9 11 13 The output should be: 12

the context of the code

image text in transcribed

image text in transcribed

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;

import java.io.ByteArrayInputStream;

import java.nio.charset.StandardCharsets;

public class Eguls {

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 set = new HashSet();

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);

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

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 (new ByteArrayInputStream(arr_a.getText().getBytes(StandardCharsets.UTF_8)));

Scanner textb = new Scanner (new ByteArrayInputStream(arr_b.getText().getBytes(StandardCharsets.UTF_8)));

Scanner textc = new Scanner (new ByteArrayInputStream(arr_c.getText().getBytes(StandardCharsets.UTF_8)));

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 exception ) {

// TODO: handle exception

exception.printStackTrace();

}

set(tfOutput);

JOptionPane.showMessageDialog(new JFrame(), answer);

}

});

cp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

cp.setTitle("TRIPLE SUM");

cp.setSize(500, 620);

cp.setVisible(true);

}

}

Given 3 arrays a, b, c of different sizes, find the number of distinct triplets (p, q, r) where pis an element of a, written as pea, q E b, and rec, satisfying the criteria: p 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

a. What are S, F, and P? Pg45

Answered: 1 week ago