Python every other element – Java Program to Subtract an Element to Every Other Elements of the Array

Python every other element: In the previous article, we have seen Java Program to Add an Element to Every Other Elements of the Array

In this article we are going to see how we can subtract an element to every other elements of the array except itself by using Java Language.

Java Program to Subtract an Element to Every Other Elements of the Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to subtract an element to every other elements of the array except itself.

Method-1: Java Program to Subtract an Element to Every Element of the Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Enter the index of array element to find that specific element.
  • This array element  will be subtracted with other array elements.
  • Iterate each element of the array and subtract that specific array element with others elements except self.

Program:

public class Main
{

    public static void main(String[] args) 
    {
        //array initialized
        int arr[] = {10,20,30,40,50,60};
        // creating new array with size of actual array 
        int result[]= new int[arr.length];
        
        System.out.println("Array length is : "+arr.length);
        
        //declaring arrauy index of the specified number 
        //which will be subtracted with other array elements
        int num  = 2;
        
        //if the entered index(means specified number) exists in the array 
        //then only the specified array element can be subtracted with other array elements 
        if(num<arr.length)
        {
            //iterating the array
           for(int i=0;i<arr.length;i++)
           {
               // checking condition 
               // if array element is not the specified number 
               // then it will enter into the if block 
               // and the number will be subtracted with other elements except itself 
               if(arr[i]!=arr[num])
               {
                    // adding the specified array element with other array elements
                    result[i] = arr[i]-arr[num];
               }
           }
        }
        
        //assigning the specified number to the same index of result array
        //as the specified number will be same 
        result[num]=arr[num];
        
        //printing the result array 
        System.out.println("New array after subtraction of array with a specific array element : ");
        for(int i=0;i<result.length;i++)
        {
            System.out.print(result[i]+" ");
        }    
   }
}
Output:
Array length is : 6
New array after subtraction of array with a specific array element : 
-20 -10 30 10 20 30

Method-2: Java Program to Subtract an Element to Every Element of the Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size as user input.
  • Then take array elements as user input.
  • Enter the index of array element to find that specific element.
  • This array element  will be subtracted with other array elements.
  • Iterate each element of the array and subtract that specific array element with others elements except self.

Program:

import java.util.Scanner;

public class Main
{

    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        
        // initialize array with size n
        int arr[] = new int[n];
        // creating new array with size of actual array 
        int result[]= new int[arr.length];
        
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        
        System.out.println("Array length is : "+arr.length);
        
        //taking input of array index
        System.out.print("Enter index of the element to be subtracted : ");
        int num  = sc.nextInt();
        
        //if the entered index(means specified number) exists in the array 
        //then only the specified array element can be added with other array elements 
        if(num<arr.length)
        {
            //iterating the array
           for(int i=0;i<arr.length;i++)
           {
               // checking condition 
               // if array element is not the specified number 
               // then it will enter into the if block 
               // and the number will be subtracted with other elements except itself
               if(arr[i]!=arr[num])
               {
                    // subtracting the specified array element with other array elements
                    result[i] = arr[i]-arr[num];
               }
           }
        }
        
        //assigning the specified number to the same index of result array
        //as the specified number will be same 
        result[num]=arr[num];
        
        //printing the result array 
        System.out.println("New array after subtraction of array with a specific array element : ");
        for(int i=0;i<result.length;i++)
        {
            System.out.print(result[i]+" ");
        }    
   }
}
Output:

Enter the size of array: 5
Enter array elements: 10 20 30 40 50
Array length is : 5
Enter index of the element to be subtracted : 3
New array after subtraction of array with a specific array element : 
-30 -20 -10 40 10

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: