Find the odd index and even index in array and print the subtracted output

For this challenge we need to take size of array in one line and on other line elements separated by space. Find the odd index and add them ,find even index and add them , then subtract the smaller one from larger one.

 import java.io.*;  
 import java.util.*;  
 public class CandidateCode {  
   public static void main(String args[] ) throws Exception {  
      Scanner sc=new Scanner(System.in);  
      String arraySize=sc.nextLine();  
      int size=Integer.parseInt(arraySize);  
      String elements=sc.nextLine();  
      String [] array=new String[size];  
      int j=0,x=0,positive=0,negative=0;  
      for(int i=0;inegative){  
        System.out.print(positive-negative);  
      }  
      else{  
        System.out.print(negative-positive);  
      }  
      sc.close();  
   }  
 }  

Output:
6
11 22 33 44 55 66
33

Comments

Popular posts from this blog

Reasoning-Number Series

Reasoning-Letter Series

Multiply Negative numbers in java