1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
puteri [66]
3 years ago
11

We say that a sequence of numbers is a palindrome if it is read the same backward or forward. For example, the sequence: 6, 15,

6, 3, 47, 3, 6, 15, 6 is a palindrome.
Implement the following function:
def construct_a_longest palindrome(numbers_bank)
When given numbers_bank, a non-empty list of integers, it will create and return a list containing a longest possible palindrome made only with numbers from numbers_bank.
Notes:
1. The longest palindrome might NOT contain all of the numbers in the sequence.
2. If no multi-number palindromes can be constructed, the function may return just one number (as a single number, alone, is a palindrome).
3. If there is more than one possible longest palindrome, your function can return any one of them.
For example, if numbers_bank = [3, 47, 6, 6, 5, 6, 15, 3, 22, 1, 6, 15), Then the call construct_a_longest_palindrome (numbers_bank) could return: [6, 15, 6, 3, 47, 3, 6, 15, 6] (Which is a palindrome of length 9, and there is no palindrome made only with numbers from numbers_bank that is longer than 9).
Requirements:
1. You may use one ArrayQueue, one ArrayStack, and one ChaniningHash TableMap.
2. Your function has to run in expected (average) linear time. That is, if numbers bank is a list with n numbers, your function should run in O(n) average case.
3. Besides the queue, stack, hash table, and the list that is created and returned, you may use only constant additional space. That is, besides the queue, stack, hash table, and the returned list, you may use variables to store an integer, a double, etc. However, you may not use an additional data structure (such as another list, stack, queue, etc.) to store non-constant number of elements.
Computers and Technology
1 answer:
SSSSS [86.1K]3 years ago
4 0

Answer:

The below code would be used to answer the above question

Explanation:

import java.io.*;

import java.util.*;

public class Main

{

 

public static List<Integer> construct_a_longest_pallindrome(int a[]){

Map <Integer,Integer> map = new HashMap<>();

List<Integer> ans1 = new ArrayList<>();

List<Integer> ans2 = new ArrayList<>();

for(int i=0;i<a.length;i++){

if(map.containsKey(a[i])){

map.put(a[i],map.get(a[i])+1);

}else

map.put(a[i],1);

}

int pos1 = 0;

int pos2 = 0;

for(Integer key : map.keySet()){

int val = map.get(key);

while(val % 2 == 0){

ans1.add(key);

ans2.add(key);

val = val-2;

}

if(val == 0)

map.remove(key);

else

map.put(key,1);

}

if(map.isEmpty() == false){

ans1.add(map.keySet().iterator().next());

}

Collections.reverse(ans2);

ans1.addAll(ans2);

return ans1;

}

  public static void main(String[] args) {

      Scanner sc = new Scanner(System.in);

      System.out.println("Enter the size of array");

      int n = sc.nextInt();

      int arr[] = new int[n];

      for(int i=0;i<n;i++){

      arr[i] = sc.nextInt();

      }

      System.out.println(construct_a_longest_pallindrome(arr));

     

  }

}

You might be interested in
Company-wide systems that connect one or more local area networks (LANs) or wide area networks (WANs) are called _____. a) legac
il63 [147K]

Answer:

The answer is "option c".

Explanation:

A Web-centered view ensures anything has been programmed for the Web app or program. example Webified. This is a definition, that is used for personal use only and all other duplication is strictly forbidden without the approval of the author. and other options are not correct that can be defined as:

  • In option a, the legacy system is a method that is obsoleted today.
  • In option b, The mission-critical systems are used in the navigational system.
  • In option d, A distributed system permits the sharing of information that includes software that is connected with a network.

6 0
4 years ago
write a java program using a do while loop to prompt the user to enter a password. The user should be prompted to enter the pass
Alex787 [66]
We can import Scanner from Java.utils.Scanner to pull in text entered in the console.

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String password = "";
do {
System.out.print("Enter your password: ");
password = sc.nextLine();
if(!password.equals("pals") {
System.out.println(\nIncorrect Password. Try again.");
}
} while(!password.equals("pals");
3 0
3 years ago
Seth would like to make sure as many interested customers as possible are seeing his business’s website displayed in their searc
GenaCL600 [577]

Answer:

Hi! Make sure that the website has a few things. Proper Keywords, the pages have the proper tags, a form on the website, contact information, CIty State, Etc., Then a phone number. Social media icons that link properly to the social media pages.

Explanation:

5 0
3 years ago
What are two type of physicals connection
netineya [11]
Two types of physical connection are:

A wired connection using a cable

A wireless connection using radio waves
7 0
4 years ago
You must lower your high beams when within how many feet of an approaching vehicle?
drek231 [11]

2000 feet of any vehicle

7 0
3 years ago
Other questions:
  • Suppose an instruction takes 4 cycles to execute in an unpipelined CPU: one cycle to fetch the instruction, one cycle to decode
    10·1 answer
  • Claim: Raising the minimum wage does not help most minimum wage employees Reason: Many small businesses cannot afford high minim
    14·2 answers
  • Expressing your needs to others is aggressive behavior.<br> True or False?
    5·2 answers
  • For homework, we have to figure out what's in the picture. It's " too close to tell " but I can't figure out what it is. Any ide
    11·1 answer
  • You select a database or change to a different database with the ____ function.
    10·1 answer
  • Which of the following loop conditions will read all the data in the file assuming that each line in the file contains two integ
    5·1 answer
  • What part of the boot-up process initializes hardware and finds an operating system to load?
    14·1 answer
  • What privacy risks do new technologies present,<br> and how do we decide if they're worth it?
    11·1 answer
  • You are reviewing the output of the show interfaces command for the Gi0/1 interface on a switch. You notice a significant number
    12·1 answer
  • What is the answer???​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!