Page 317 - Computer science 868 Class 12
P. 317

10              int a[] = new int[n];
                   11              System.out.println("Enter all the elements:");

                   12              for(i = 0; i < n; i++)
                   13              {
                   14                  a[i] = s.nextInt();

                   15              }

                   16              for(i = 0; i < n; i++)
                   17              {
                   18                  for(int j = i + 1; j < n; j++)

                   19                  {
                   20                      diff = Math.abs(a[i] - a[j]);

                   21                      if(diff > greatest_diff)
                   22                      {

                   23                          greatest_diff = diff;
                   24                          a1 = i;

                   25                          a2 = j;
                   26                      }

                   27                  }
                   28              }

                   29              System.out.println("Greatest Difference:"+greatest_diff);

                   30               System.out.println("Two elements with largest difference:"+a[a1]+" and
                                     "+a[a2]);
                   31          }
                   32      }

                 The output of the preceding program is as follows:

                 Enter the size of the array 5
                 Enter all the elements:
                 1 2 0 -5 -1
                 Greatest Difference:7
                 Two elements with the largest difference:2 and -5

















                                                                                                                       315
                                                                                                              Arrays   315
   312   313   314   315   316   317   318   319   320   321   322