Write code in java. A company manufactures different types of software products. They…
- User
Write code in java.
A company manufactures different types of software products. They deliver their products to their N clients. Whenever the company fulfils the complete order of a client the orderID generated is the sum of the number of products delivered for every committed product type. The head of the sales team wants to find the client-specific data for the total number of products of any type delivered to each client.
Write an algorithm for the head of the sales team to calculate the total number of products of any type delivered to the respective clients.
Input The first line of the input consists of an integer - numOfClients, representing the number of clients (N). The second line consists of N space-separated integers - orderID0, orderID1, ..... , orderIDN-1, representing the orderIDs of the orders delivered to the clients.
Output Print N space-separated integers representing the client-specific data for the total number of products of any type delivered to each of the respective clients.
Constraints 0 ≤ numOfClients ≤ 106 0 ≤ orderIDi ≤ 106 0 ≤ i < numOfClients
Example Input: 4 43 345 20 987
Output: 7 12 2 24
Explanation: Client 0 ordered 2 types of products; thus, the total products delivered to it is 7. Client 1 ordered 3 types of products; thus, the total products delivered to it is 12. Client 2 ordered 2 types of products; thus, the total products delivered to it is 2. Client 3 ordered 3 types of products; thus, the total products delivered to it is 24.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; class Solution { public static void main(String args[] ) { // Write your code here } }
use above code block to write the code
- Assistant
- User
There are N people living in a state. In this state, people add their town name before their first name. Write an algorithm to find the name of the town of the given N people where the name of the town is the common substring and has the maximum length.
Input The first line of the input consists of an integer - size, representing the given number of people (N). The second line consists of N space-separated strings representing the first names of the people.
Output Print a string in lowercase letters representing the name of the town of the given N people where the name of the town is the common substring and has the maximum length. If no such common prefix is found, then do not print anything.
Constraints 0 ≤ size ≤ 105 0 < len < 106; where len is the length of the string.
Note The names of the people are spelled using English letters and are case insensitive.
Example Input: 5 Rosewood rose rosy rosemarry roshh
Output: ros
Explanation: "Ros" is the common starting substring with the maximum length in the peoples’ names.
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Scanner; class Solution { public static void main(String args[] ) { // Write your code here } }
use above code block to write the code
- Assistant
- User
remove comment from above code
- Assistant
Sure! Here's the Java code without any comments:
import java.io.BufferedReader; import java.io.InputStreamReader; class Solution { public static void main(String args[] ) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int size = Integer.parseInt(reader.readLine().trim()); if (size == 0) { return; } String[] names = reader.readLine().trim().split(" "); String commonPrefix = findLongestCommonPrefix(names); if (!commonPrefix.isEmpty()) { System.out.println(commonPrefix.toLowerCase()); } } catch (Exception e) { e.printStackTrace(); } } private static String findLongestCommonPrefix(String[] names) { if (names.length == 0) return ""; String prefix = names[0].toLowerCase(); for (int i = 1; i < names.length; i++) { while (names[i].toLowerCase().indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) return ""; } } return prefix; } }