In: Computer Science
Complete the following methods:
secondlargest(): returns the second largest in the list without
sorting. Assumes list
has at least 2 elements.
palindrome(): checks if list elements are palindrome, i.e. list
elements from left to
right and right to left, are identical.
Code:
import java.util.*;
import java.io.*;
public class xxxxxL1 {
   // class Variables
    private int n, count, arr[];
   PrintStream prt = System.out;
   // xxxxxL1 Constructor
   xxxxxL1(int k){
      count = 0;
      n = k;
      //   Allocate Space for
array         
      arr = new int[n+1];// index 0 is not
used
      prt.printf("\tCreating array of size
%2d:",n);
   } // end constructor
  
   //prints list formatted 10 elements per line.
   private void printlist(){
      int i;
      prt.print("\n\tList
contents:\n\t");
      for (i = 1; i <= count;
i++){
       prt.printf("%2d, ", arr[i]);
       if (i % 10 == 0)
prt.printf("\n\t");
      }
    } // end printlist  
  
   // Return second largest in the list.
   // Assume list has 2 or more elements.
    private int secondlargest(){
       return 0;
    } // end secondlargest
      
    // deletes x from list, if exist.
    private int delete(int x){
      int i, j;
      if (count == 0)
           return 0; //
invalid position
   //shift array elements from position p to right
      for (i = 1 ; i <= count ; i++)
       if (arr[i] == x){
       for (j = i ; j < count ;
j++)
            
arr[j] = arr[j+1];
       count--;
       return 1;
       }
      return 1;
    } //end delete
   //checks if list elements are palindrome, i.e.
   //list elements from left to right and right to
   //left, are identical.
    private int palindrome(){
       return 0;
    } // end palindrome
   //insert x at position p, in list
   //for successful insertion: list should not be
   // full(count=n) and 1 <= p <= count+1
    private int insert(int x, int p){
      int i;
      if (count == n || p < 1 || p >
count+1)
           return 0; //
invalid position
   //shift array elements from position p to right
      for (i = count ; i >= p ; i--)
       arr[i+1] = arr[i];
      // end for
          
   
      arr[p] = x; // insert x at position
p
      count++; // increment no. of
elements.
      return 1; // successful insertion
   } // end insert
   public static void main(String args[]) throws
Exception{
      int j, m, k, p, s, x;
      try{
       // open input file
       Scanner inf = new
Scanner(System.in);
        // read list size  
   
       k = inf.nextInt();
       // Create a List of type Integer of
size k
       xxxxxL1 lst = new
xxxxxL1(k);          
       //read no. of elements to
insert
       m = inf.nextInt();
       //Insert m elements in the
list
        for(j = 1; j <= m;
j++){
           x =
inf.nextInt();   // read x
           p =
inf.nextInt(); // read position
          //insert x at position
p
          s = lst.insert(x,
p);
           //if (s ==
1)System.out.printf(" Successful insertion.");  
           //else
       
      System.out.printf(" Can not
insert.");
         } // end for
      
       //prints list formatted 10 elements
per line.
       lst.printlist();
       //print second largest
       //p = lst.secondlargest();
        
//System.out.printf("\n\t2nd largest = %d.", p);
        //read no. of elements to
delete from list
        k = inf.nextInt();
       //Delete k elements from the
lis
        for(j = 1; j <= k;
j++){
           x =
inf.nextInt();   // read next int
           p =
lst.delete(x);   //delete x
           //if (p ==1)
System.out.printf(": deleted.");
           //else
System.out.printf(": not found.");
        }// end for
       //prints list formatted 10
elements per line.
       lst.printlist();
      
       // close input file   
   
       inf.close();
       } catch (Exception e) {
        System.out.print("\nException
" + e + "\n");}
    } // end main
   } // end class xxxxxL1
I have made a program to find 2nd largest and check if
it is palindrome. It acts on a list. Please incorporate the same in
your code.
2nd Largest
 private int secondlargest() {
     int largest = 0, secondLargest = 0;
     for (int i = 0; i < arr.length; i++) {
         if (i == 0) {
             largest = arr[i]; // initially consider 1st element as largest
         } else {
             if (largest < arr[i]) {       // if 2nd element is larger than 1st, secondLargest becomes the previous largest and currect largest is the ith element
                 secondLargest = largest;
                 largest = arr[i];
             } else {                      
                 if (secondLargest < arr[i]) {      // if ith element is lesser than largest, we compare the secondLargest element with ith element and change secondLargest to ith element if ith element is greater
                     secondLargest = arr[i];
                 }
             }
         }
     }
     return secondLargest;   //last value of secondLargesr after we are done with traversing is 2nd largest value of list
 }
INPUT/OUTPUT
arr = {1 , 2 , 3 , 5}
secondLargest is 3
Palindrome -> As it checks, the return type I have
changed to boolean. You can use int also. 1 for palindrome and 0
for not palindrome.
  private boolean palindrome() {
      int length = arr.length;
      if (length % 2 == 0) {
          //if length is even
          for (int i = 0; i < (length) / 2; i++) { // if length is odd, we compare first and last. 2nd and 2nd last. 3rd and 3rd last and so on..... The middle element is NOT common while traversing from both sides. SO, comparing first half and 2nd half.
              if (arr[i] != arr[length - 1 - i]) {
                  return false;
              }
          }
      } else {
          //if length is odd
          for (int i = 0; i < (length - 1) / 2; i++) { // if length is odd, we compare first and last. 2nd and 2nd last. 3rd and 3rd last and so on..... The middle element is common while traversing from both sides
              if (arr[i] != arr[length - 1 - i]) {
                  return false;
              }
          }
      }
      return true;
  }
INPUT/OUTPUT
arr = {1 , 2 , 3 , 0 , 0 , 3 , 2 , 1} -> TRUE
arr = { 1 , 2 , 3 , 2 , 1} -> TRUE
arr={1 ,2 ,3 ,1 , 2} -> FALSE
Kindly upvote if
this helped