Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.9 kB
0
Indexable
Never
2.	arr.java
package com.wiprotraining;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class arr {
	static void reverse(int a[], int n) 
    { 
        int[] b = new int[n]; 
        int j = n; 
        for (int i = 0; i < n; i++) { 
            b[j - 1] = a[i]; 
            j = j - 1; 
        } 
  
        // printing the reversed array 
        System.out.println("Reversed array is: \n"); 
        for (int k = 0; k < n; k++) { 
            System.out.print( b[k] ); 
        } 
    } 

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n;
		System.out.println("Enter n: ");
		Scanner s=new Scanner(System.in);
		n=s.nextInt();
		int[] ar = new int[n];
		
		System.out.print("Enter array elemens:");
		for(int i=0;i<n;i++)
		{
			ar[i]=s.nextInt();
			System.out.println(" "+ar[i]+" ");
		}
		int pos,x;
		System.out.println("enter pos:");
		pos = s.nextInt();
		System.out.print("enter element");
		x=s.nextInt();
		int[] arr= new int[n+1];
		for(int i=0;i<n+1;i++)
		{
			if(i<pos-1)
			{
				arr[i]=ar[i];
			}
			else if(i==pos-1)
			{
				arr[i]=x;
			}
			else 
			{
				arr[i]=ar[i-1];
			}
			System.out.print(" "+arr[i]+" ");
		}
		System.out.print("Array deletion");
		int ele;
		ele = s.nextInt();
		for(int i = 0; i < arr.length; i++)
	    {
	      if(arr[i] == ele)   //If element found
	      {
	        // shifting elements
	        for(int j = i; j < arr.length - 1; j++)
	        {
	            arr[j] = arr[j+1];
	        }
	        break;
	      }
	}
		System.out.println("Elements after deletion " );
	       for(int i = 0; i < arr.length-1; i++)
	       {
	             System.out.print(arr[i]+ " ");
	             
	       } 
	       reverse(ar , ar.length);
	       
	        

}
}
Leave a Comment