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
A cell has an unlimited number of conditions for Conditional Formatting.<br> A.true<br> B.false
Mekhanik [1.2K]
I believe the answer is false.
I Hope this helps! :)
If you don't understand plz message me
If you do plz brainlest
Thank you!
7 0
3 years ago
What is (12.2a + 9.7b) – (18.1b – 0.2a) – (6.7a + 6.8b), simplified?
Inga [223]
The answer should be 5.7a - 15.2b
8 0
3 years ago
Explain what is the difference between a "Mode Switch" and a "Process Switch".
11111nata11111 [884]

Answer:

The main difference between mode switch and process switch is that mode switch changes the process privilege between modes like user mode and kernel mode while process switch changes the process state between different states. These processes then load into the main memory for the CPU to execute them.

Explanation:

8 0
3 years ago
What are the disadvantages of using pointers?
garik1379 [7]

Explanation:

The pointer is the variable that will store the address of the other variable of the same datatype.

Following are the disadvantages of using a pointer.

1. Pointer sometimes causes a segmentation fault in the program.

2. Sometimes pointer leads to a memory leak.

3. A pointer variable is slower than normal variable.

4. Sometimes the program is crash if we using pointer because sufficient memory is not allocated during runtime.

5. If we using pointer it is difficult to find the error in the program.

5 0
3 years ago
The default (preset slide layouts are set up in ____ orientation.
Rama09 [41]
The default is normal view orientation.
3 0
3 years ago
Other questions:
  • A server-side extension ________________. provides its services to the web server in a way that is totally transparent to the cl
    13·1 answer
  • In your own words, describe the advantages and disadvantages of the auto-negotiation protocol used in Ethernet communications.
    6·1 answer
  • A difference between crt monitors and flat-panel displays is that most flat-panel displays use digital signals to display images
    8·1 answer
  • Write a program that implements the FIFO and LRU page-replacement algorithms learned in class. First, generate a random page ref
    9·1 answer
  • Linux is a kind of software whose code is provided for use, modification, and redistribution. what kind of software is this?
    5·1 answer
  • How to do a linear equations
    8·1 answer
  • Let A and B be two stations attempting to transmit on an Ethernet. Each has a steady queue of frames ready to send; A’s frames w
    7·1 answer
  • A ----------------has the least flexibility and is best suited for mass production. It has an arrangement of machines that proce
    14·1 answer
  • B) Describe the computer processing that is required to maintain the correct growing<br>conditions.​
    13·1 answer
  • In java language I want the code
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!