Hey guys I am trying to sort a linked list alphabetically by multiplying the first 3 letters together. The way it works is that the first letter would take 26^2, the second letter would be 26^1 and the third would be 26^0. When i run the program it is giving me the same sum for say the name "lala" and "francis". If anybody could help me see what is wrong with the code, it will be greatly appreciated!
LinkedListNode class: (contains the getSum method)
public class LinkedListNode
{
public String data;
public LinkedListNode next;
public long sum;
public LinkedListNode(String data)
{
this.data = data;
this.next = null;
this.sum = getSum(data);
}//end node
public long getSum(String line)
{
int i;
long sum = 0;
String s = null;
char a;
for(i=0; i < 3; i++)
{
int j = 2;
a = line.charAt(i);
sum += Character.getNumericValue(a) * Math.pow(26, j);
//Return the value of the number 4 to be the power of 3 (4*4*4): Math.pow(4,3);
j--;
}//end for
return sum;
}//end getSum
public long getSum()
{
return sum;
}//end getSum
public String getData()
{
return data;
}//end getData
public void setData(String data)
{
this.data = data;
}//end setData
public LinkedListNode getNext()
{
return next;
}//end node
public void setNext(LinkedListNode next)
{
this.next = next;
}//end setNext
}//end class node
LinkedList class: (has other methods for the list)
public class LinkedList {
public LinkedListNode front;
public LinkedList() {
this.front = null;
}
public void insertBack(String data)
{
if(front == null){
front = new LinkedListNode(data);
}else{
LinkedListNode newNode = new LinkedListNode(data);
LinkedListNode current = front;
while(current.getNext() != null){
current = current.getNext();
}
current.setNext(newNode);
}
}//end insertBack
public void addAfter(LinkedListNode spot, String data)
{
LinkedListNode newNode;
newNode = new LinkedListNode(data);
newNode.next = spot.next;
spot.next = newNode;
}//end addAfter
public void addBefore(LinkedListNode spot, String data)
{
}//end addBefore
public void deleteAfter(LinkedListNode spot)
{
LinkedListNode nextNode;
nextNode = spot.next;
spot.next = nextNode.next;
}//end deleteAfter
public String showList()
{
int i = 0;
String retStr = "The nodes in the list are:\n";
LinkedListNode current = front;
while(current != null){
i++;
retStr += "Node " + i + " is: " + current.getData() + " and the sum is: " + current.getSum() + "\n";
current = current.getNext();
}
return retStr;
}
public LinkedListNode findTail()
{
LinkedListNode current = front;
while(current.getNext() != null)
{
current = current.getNext();
}
return current;
}//end findTail
}
fileIn class:
import java.util.Scanner;
import java.io.*;
public class fileIn
{
LinkedListNode front;
LinkedList myList = new LinkedList();
String fname;
public static void main(String[] args)
{
fileIn f = new fileIn();
}//end main
public fileIn()
{
getFileName();
readFileContents();
System.out.print(myList.showList());
}//end namesLinkedList
public void readFileContents()
{
boolean looping;
DataInputStream in;
String line;
int j, len;
char ch;
/* Read input from file and process. */
try
{
in = new DataInputStream(new FileInputStream(fname));
looping = true;
while(looping)
{
/* Get a line of input from the file. */
if (null == (line = in.readLine()))
{
looping = false;
/* Close and free up system resource. */
in.close();
}//end if
else
{
myList.insertBack(line);
j = 0;
len = line.length();
}//end else
} /* End while. */
} /* End try. */
catch(IOException e)
{
System.out.println("Error " + e);
} /* End catch. */
}//end readFileContents
public void getFileName()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter file name please.");
fname = in.nextLine();
}//end getFileName
}//end class namesLinkedList
for (i = 0; i < 3; i++) {
int j = 2;
a = line.charAt(i);
sum += Character.getNumericValue(a) * Math.pow(26, j);
j--;
}
You're getting the same result because the exponent is always 2. This leads to the same value for fra (15×262 + 27×262 + 10×262 = 35,152) and lal (21×262 + 10×262 + 21×262 = 35,152). Why is this?
The variable j is declared inside the loop instead of outside. The decrement at the end has no effect since it starts over at 2 at the beginning of each iteration.
You should move the declaration out of the loop:
int j = 2;
for (i = 0; i < 3; i++) {
a = line.charAt(i);
sum += Character.getNumericValue(a) * Math.pow(26, j);
j--;
}
Or you could replace j with 2 - i and get rid of the extra variable entirely.
for (i = 0; i < 3; i++) {
a = line.charAt(i);
sum += Character.getNumericValue(a) * Math.pow(26, 2 - i);
}
Looks like your math is wrong. Character.getNumericValue(a) will not return you a value between 0 and 25 like you seem to think.
Just make a custom Comparator class if you want to sort based on fist 3 letters and use that.
Edit: I was wrong about how getNumericValue works, but the math is still wrong (see comment below).
Related
I'm trying to write a program that will go forwards in a circular doubly linked list a certain amount of times and backwards in the same list a certain amount of times. If both methods end up at the same number then the number is 'worthy' and is removed from the list. If the methods don't end up at the same element on the list then they are unworthy and are removed from the list as well. I've written the method for going forwards and backwards a certain amount of times but I'm having difficulty with removing the elements once they have been deemed worthy or unworthy. This is what I have so far. Any help would be greatly appreciated.
import java.util.Arrays;
public class LinkedList {
private Node head;
private Node end;
LinkedList(){
head = end = null;
}
public void addAtStart(int x){
if (head == null) {
Node new_node = new Node(x);
new_node.data = x;
new_node.next = new_node.prev = new_node;
head = new_node;
} else if (head != null) {
Node last = (head).prev;
Node new_node = new Node(x);
new_node.data = x;
new_node.next = head;
(head).prev = new_node;
new_node.prev = last;
last.next = new_node;
}
}
public void printOutput(int N, int k, int m){
System.out.println("Output" + "\n" + "------" + "\n");
printCandidates(N,k,m);
}
public void printCandidates(int N, int k, int m){
int unworthy[] = new int[N];
int worthy[] = new int[N];
int count = 0;
int run = 0;
Node temp = head;
do {
if (forwards(k) == backwards(m)){ // puts in worthy list and deletes from linked list
worthy[count] = forwards(k);
count += 1;
System.out.println("hello");
deleteElement(forwards(k));
} else if (forwards(k) != backwards(m)){ //put in unworthy list and delete from linked list
unworthy[run] = forwards(k);
unworthy[run+1] = backwards(m);
run += 2;
System.out.println("goodbye");
deleteElement(forwards(k));
deleteElement(backwards(m));
}
} while (temp != null);
System.out.println("Removed candidates from being elected");
System.out.println(Arrays.toString(unworthy));
System.out.println("Worthy candidates");
System.out.println(Arrays.toString(worthy));
}
int forwards(int k){
int run = 0;
int x = 0;
Node temp = head;
while (temp.next != head){
if(run == (k)){
x = temp.data;
}
temp = temp.next;
run += 1;
}
return x;
}
int backwards(int m){
int run = 0;
int x = 0;
Node temp = head;
Node last = head.prev;
temp = last;
while (temp.next != head){
if(run == (m)){
x = temp.data;
}
temp = temp.next;
run += 1;
}
return x;
}
public void deleteElement(int elementToBeDeleted){
Node temp = head;
while (temp.next != head){
if(temp.data == elementToBeDeleted){
temp.setNext(temp.next);
}
temp = temp.next;
}
}
This is my driver:
public class Program2 {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
for (int i = 1; i < 11; i++){
ll.addAtStart(i);
}
int N = 10;
int k = 4;
int m = 3;
System.out.println("N = " + N + ", " + "k = " + k + ", " + "m = " + m + "\n");
ll.printOutput(N,k,m);
}
}
This is my node class:
public class Node {
public int data;
public Node next;
public Node prev;
// Constructor to intialize/fill data
public Node(int data){
this.data = data;
}
// set the address of next node
public void setNext(Node temp) {
this.next = temp;
}
// get the address of next node
public Node getNext(){
return this.next;
}
public Node getPrev(){
return this.prev;
}
public void setPrev(Node temp) {
this.prev = temp;
}
// to get data of current node
public int getData(){
return this.data;
}
}
EDIT: As part of this exercise I need to write the class myself, therefore why I am implementing my own LinkedList.
I insert some elements in Node in java,displaying element also working fine.But when i search any element occurrence using recursive its always return value zero .Sorry wrong for my english.I am new in data structure implementation in java . Thanks
public class pal{
private static Node head;
private static class Node {
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
public static void addToTheLast(Node node) {
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
public static void printList() {
Node temp = head;
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args) {
int no ;
Node head = null ;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number of Element ");
no = sc.nextInt();
for(int i = 0 ;i< no; i ++){
System.out.print("Enter Element ");
int a = sc.nextInt();
if(head == null){
head = new Node(a);
addToTheLast(head);
}else{
addToTheLast(new Node(a));
}
}
printList();
System.out.print("Enter search key");
int key = sc.nextInt();
int yo = count(head,key);
System.out.print(String.valueOf(yo));
}
public int count (Node head,int key){
int cnt = 0;
if(head == null){
return cnt;
}else{
if(temp.value == key)
cnt++;
count(temp.next,key)
}
return cnt;
}
}
Always retun value 0 if element is present in linklist
Your code does not compile as you are referring to a variable temp within count method, but it does not exist.
You have to replace temp with head.
Also, you are losing the cnt value in your recursive call. You are incrementing cnt which is a local variable.
I have modified the code to pass along the value of count as a parameter.
public static int count(Node head, int key, int count){
if(head == null){
return count;
} else {
if(head.value == key) {
return count(head.next, key, count + 1);
} else {
return count(head.next, key, count);
}
}
}
If you don't want the callers of count to pass 0 to the last parameter (as count(head, key, 0)), make it a private method and make the public search method call this (I prefer this method as it is cleaner)
public static int count(Node head, int key) { //main method calls this
return call(head, key, 0); //calls the above method
}
I am trying to add two non negative numbers, the digits of which are stored in reverse order in two separate linked lists. The answer should also be a linked list with digits reversed and no trailing zeros.
I understand that there is a way to solve this question by adding digits and maintaining a carry each time, but I am trying to solve it by using addition operation on numbers.
Here's my code:
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode a, ListNode b) {
if(a==null || b==null){
return null;
}
String num1 = "";
String num2 = "";
ListNode temp1 = a;
ListNode temp2 = b;
while(temp1!=null){
num1 = num1+Integer.toString(temp1.val);
temp1 = temp1.next;
}
new StringBuilder(num1).reverse().toString();
double value1 = Double.parseDouble(num1);
while(temp2!=null){
num2 = num2+Integer.toString(temp2.val);
temp2 = temp2.next;
}
new StringBuilder(num2).reverse().toString();
double value2 = Double.parseDouble(num2);
double result = value1+value2;
String res = String.format("%.0f",result);
ListNode first_node = new ListNode(Character.getNumericValue(res.charAt(0)));
ListNode ans = first_node;
for(int j=1;j<res.length();j++){
ListNode node = new ListNode(Character.getNumericValue(res.charAt(j)));
add(node,ans);
}
return ans;
}
public void add(ListNode node, ListNode ans){
ListNode temp;
temp = ans;
ans = node;
ans.next = temp;
}
}
My code has been giving wrong answers. Can anyone point out the errors?
Your approach is not correct and indirect.
You are trying to do big numbers arithmetics using floating point operations.
As a result - computing errors.
We have:
List<Integer> firstNumber;
List<Integer> secondNumber;
Lets assume firstNumber > secondNumber.
Try this alogrithm:
List<Integer> result = new ArrayList<>();
int i = 0;
int appendix = 0;
for (; i < secondNumber.size(); i++) {
int sum = firstNumber.get(i) + secondNumber.get(i) + appendix;
result.append(sum % 10);
appendix = sum / 10;
}
for (; i < firstNumber.size(); i++) {
int sum = firstNumber.get(i) + appendix;
result.append(sum % 10);
appendix = sum / 10;
}
if (appendix != 0)
result.append(appendix);
return result;
Your add function looks incorrect. You will get you number in the reverse order than what is expected.
Also, your solution is missing the point of the question. Your approach will fail if you have a number with a lot of digits (even double has its limits ~ 2^1024 I think). A linked list representation allows for numbers even bigger.
The correct solution would just iterate through both the lists simultaneously with a carry digit while creating the solution list. If this is a question in an assignment or coding competition, your solution would be judged wrong.
Your add method is wrong, it doesn't build correctly the list. Here is how the final part of your method should look, without the add method:
for(int j=1;j<res.length();j++){
ans.next = new ListNode(Character.getNumericValue(res.charAt(j)));;
ans = ans.next;
}
return first_node;
In your approach, the variable ans is not updating. You can try this:
ans = add(node,ans);
and in your add method, change the method to return ListNode ans
Your approach is not straightforward and wouldn't give you expected results.
Here is a simple approach which wouldn't require much explanation as the addition is simple integer by integer.
Do note that i carry forward when the sum of two integers is greater than 9 else continue with the sum of next integers from both the list.
class Node {
private Object data;
private Node next;
public Object getData() { return data; }
public void setData(Object data) { this.data = data; }
public Node getNext() { return next; }
public void setNext(Node next) { this.next = next; }
public Node(final Object data, final Node next) {
this.data = data;
this.next = next;
}
#Override
public String toString() { return "Node:[Data=" + data + "]"; }
}
class SinglyLinkedList {
Node start;
public SinglyLinkedList() { start = null; }
public void addFront(final Object data) {
// create a reference to the start node with new data
Node node = new Node(data, start);
// assign our start to a new node
start = node;
}
public void addRear(final Object data) {
Node node = new Node(data, null);
Node current = start;
if (current != null) {
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(node);
} else {
addFront(data);
}
}
public void deleteNode(final Object data) {
Node previous = start;
if (previous == null) {
return;
}
Node current = previous.getNext();
if (previous != null && previous.getData().equals(data)) {
start = previous.getNext();
previous = current;
current = previous.getNext();
return;
}
while (current != null) {
if (current.getData().equals(data)) {
previous.setNext(current.getNext());
current = previous.getNext();
} else {
previous = previous.getNext();
current = previous.getNext();
}
}
}
public Object getFront() {
if (start != null) {
return start.getData();
} else {
return null;
}
}
public void print() {
Node current = start;
if (current == null) {
System.out.println("SingleLinkedList is Empty");
}
while (current != null) {
System.out.print(current);
current = current.getNext();
if (current != null) {
System.out.print(", ");
}
}
}
public int size() {
int size = 0;
Node current = start;
while (current != null) {
current = current.getNext();
size++;
}
return size;
}
public Node getStart() {
return this.start;
}
public Node getRear() {
Node current = start;
Node previous = current;
while (current != null) {
previous = current;
current = current.getNext();
}
return previous;
}
}
public class AddNumbersInSinglyLinkedList {
public static void main(String[] args) {
SinglyLinkedList listOne = new SinglyLinkedList();
SinglyLinkedList listTwo = new SinglyLinkedList();
listOne.addFront(5);
listOne.addFront(1);
listOne.addFront(3);
listOne.print();
System.out.println();
listTwo.addFront(2);
listTwo.addFront(9);
listTwo.addFront(5);
listTwo.print();
SinglyLinkedList listThree = add(listOne, listTwo);
System.out.println();
listThree.print();
}
private static SinglyLinkedList add(SinglyLinkedList listOne, SinglyLinkedList listTwo) {
SinglyLinkedList result = new SinglyLinkedList();
Node startOne = listOne.getStart();
Node startTwo = listTwo.getStart();
int carry = 0;
while (startOne != null || startTwo != null) {
int one = 0;
int two = 0;
if (startOne != null) {
one = (Integer) startOne.getData();
startOne = startOne.getNext();
}
if (startTwo != null) {
two = (Integer) startTwo.getData();
startTwo = startTwo.getNext();
}
int sum = carry + one + two;
carry = 0;
if (sum > 9) {
carry = sum / 10;
result.addRear(sum % 10);
} else {
result.addRear(sum);
}
}
return result;
}
}
Sample Run
Node:[Data=3], Node:[Data=1], Node:[Data=5]
Node:[Data=5], Node:[Data=9], Node:[Data=2]
Node:[Data=8], Node:[Data=0], Node:[Data=8]
I'm working on an assignment where I need to create a Linked List given a template. For each new node that is created, I need to print out the updated list. However, up until this point, I've been stumped on how to print out the linked list. Can anyone figure out what am I doing wrong? What I currently have just prints out the number that has been created, followed by blank space, instead of the entire list up to that point.
NumberList.java
import java.util.*;
public class NumberList {
private Node head;
public NumberList() {
}
public void insertAtHead(int x) {
Node newNode = new Node(x);
if (head == null)
head = newNode;
else {
newNode.setNext(head);
head = newNode;
}
}
public void insertAtTail(int x) {
}
public void insertInOrder(int x) {
}
public String toString() {
Node tmp = head;
String result = "";
while (tmp.getNext() != null) {
result += tmp.toString() + " ";
}
return result;
}
//---------------------
// test methods
//---------------------
public static void testInsertAtHead() {
Random r = new Random();
int n = 20;
int range = 1000;
NumberList list = new NumberList();
for (int i=1; i<=n; i++) {
int x = r.nextInt(range);
list.insertAtHead(x);
System.out.println("" + x + ": " + list);
}
}
public static void testInsertAtTail() {
Random r = new Random();
int n = 20;
int range = 1000;
NumberList list = new NumberList();
for (int i=1; i<=n; i++) {
int x = r.nextInt(range);
list.insertAtTail(x);
System.out.println("" + x + ": " + list);
}
}
public static void testInsertInOrder() {
Random r = new Random();
int n = 20;
int range = 1000;
NumberList list = new NumberList();
for (int i=1; i<=n; i++) {
int x = r.nextInt(range);
list.insertInOrder(x);
System.out.println("" + x + ": " + list);
}
}
public static void main(String[] args) {
//testInsertAtHead();
//testInsertAtTail();
testInsertInOrder();
}
}
Node.java
class Node {
private int number;
private Node next;
public Node(int n) {
this.number = n;
this.next = null;
}
public Node getNext() {
return next;
}
public int getNumber() {
return number;
}
public void setNext(Node n) {
if (n == null)
return;
n.setNext(next);
next = n;
}
public String toString() {
return number + "";
}
}
I think your toString() is looping endlessly once the second element is added. You need to move your pointer to the next node:
public String toString() {
Node tmp = head;
String result = "";
while (tmp != null) {
result += tmp.toString() + " ";
tmp = tmp.getNext();
}
return result;
}
I've also updated the condition to handle a null head.
I am trying this program but i am not able to achieve deletion. The execution is going into infinite loop. Also, i am not sure if i am forming linked list properly.
What am i missing in the following program:
public class SpecificNodeRemoval {
private static class Node {
String item;
Node next;
Node prev;
private Node(String item, Node next, Node prev) {
this.item = item;
this.next = next;
this.prev = prev;
}
}
public static void main(String[] args) {
int k = 3;
Node fourth = new Node("Fourth", null, null);
Node third = new Node("Third", fourth, null);
Node second = new Node("Second", third, null);
Node first = new Node("First", second, null);
second.prev = first;
third.prev = second;
fourth.prev = third;
Node list = first;
Node result = removalKthNode(list, k);
int j = 1;
while(result.next!=null){
System.out.println(j+": "+result.item);
}
}
private static Node removalKthNode(Node first, int k) {
Node temp = first;
for(int i=1; i < k; i++) {
temp = temp.next;
}
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
return temp;
}
}
THANKS A TON for answer and comments.. the working program is listed below:
public class SpecificNodeRemoval {
private static class Node {
String item;
Node next;
Node prev;
private Node(String item, Node next, Node prev) {
this.item = item;
this.next = next;
this.prev = prev;
}
}
public static void main(String[] args) {
int k = 3;
Node fourth = new Node("Fourth", null, null);
Node third = new Node("Third", fourth, null);
Node second = new Node("Second", third, null);
Node first = new Node("First", second, null);
second.prev = first;
third.prev = second;
fourth.prev = third;
Node list = first;
Node result = removalKthNode(list, k);
int j = 1;
while(result != null){
System.out.println(j+": "+result.item);
result = result.next;
j++;
}
}
private static Node removalKthNode(Node first, int k) {
Node temp = first;
for(int i=1; i < k; i++) {
temp = temp.next;
}
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
return first;
}
}
The output is:
1: First
2: Second
3: Fourth
This looks like the culprit.
while(result.next!=null){
System.out.println(j+": "+result.item);
}
you are not progressing forward in the linked list.
I'm not exactly sure what you intended, but you may want to write as follows to avoid infinite loop...
while(result !=null){
System.out.println(j+": "+result.item);
result = result.next;
j++;
}
But again if you want to print whole linked list, you should not initialise result with the value returned from removalKthNode function. You should start from first.
Hope this makes sense.
You have several issues in your code:
1) The removalKthNode method should return the 1st element in the list to make your code print meaningful results (or you'll have to navigate to the 1st element again to output the remaining list.
2) The while loop which prints your list is wrong in two places.
a) You do not increment j, so you always put the same position for the items.
b) You do not really iterate through that list, meaning you do not reassign your variable result.
Try something like this:
int j = 1;
while (result != null) {
System.out.println(j++ + ": " + result.item);
result = result.next;
}
The code
Node result = removalKthNode(list, k);
now result = Third
and you have while loop as while(result.next!=null) which is always be in the Third element so it's going for infinite loop. Change the result as below
while(result!=null){
System.out.println(j+": "+result.item);
result = result.next;
}
Try this : Might help you to accompalish the Task:
package com.amazon;
class Linkedlist{
Node head;
public Linkedlist() {
head = null;
}
public Node addNode(int data){
Node newNode = new Node(data);
if(head==null) head = newNode;
else{
Node current = head;
while(current.next!=null){
current = current.next;
}
current.next = newNode;
}
return newNode;
}
}
class Node
{
Node next;
int data;
Node(int d)
{
data = d;
next = null;
}
}
public class DeleteEveryKthNodes {
void modifyList(Node head,int k){
Node current = head;
Node previous = null;
Node newHead = null;
if(current==null)return;
int count;
while (current != null) {
for (count = 1; count < k && current != null; count++) {
previous = current;
current = current.next; // 1--2--3--4--5
}
if (current != null) {
Node temp = current;
previous.next = current.next;
// current = null;
temp = null;
current = current.next;
}
}
current = head;
while(current!=null){
System.out.print(" "+current.data);
current = current.next;
}
}
public static void main(String args[]) {
Linkedlist list = new Linkedlist();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.head.next.next.next = new Node(4);
list.head.next.next.next.next = new Node(5);
new DeleteEveryKthNodes().modifyList(list.head, 2);
//list.head.next.next.next.next = new Node(1);
}
}
Simple Java Script for circular - n=5 and k=3, it will delete every 3 element in circular list.
public class TEST {
public static int killed_position(int[] newarr,int n,int a,int p) {
int iteration=0;
while(true) {
if(newarr[p-1] != 0) {
iteration++;
if(iteration>a-1) {
break;
}
}
p++;
if(p>n) {
p=1;
}
}
return p;
}
public static int next_position(int[] newarr,int n,int a,int p) {
int iteration=0;
while(iteration<1) {
if(newarr[p-1] != 0) {
iteration++;
}
else {
p++;
if(p>n) {
p=1;
}
}
}
System.out.println("NEXT START ->" + p);
return p;
}
public static void main(String[] args) {
int n=5;
int k=3;
int newarr[] = new int[n];
int a=1;
for(int i=0;i<n;i++) {
newarr[i]=i+1;
}
for(int i=1;i<n;i++) {
System.out.println("START -> " + a);
a=killed_position(newarr, n,k,a);
newarr[a-1]=0;
System.out.println("KILLED -> " + a);
a=next_position(newarr, n,k,a);
System.out.println("---------------------");
}
System.out.println("POSITION FINAL MAN -> " + a);
System.out.println("POSITION FINAL MAN NAME -> " + a);
for(int i=0;i<n;i++) {
System.out.print(newarr[i]);
}
}
}