-1

Problem statement is find sum of each adjacent numbers and finally print second highest sum and two adjacent numbers from this came.

Condition - Only one loop.

public class AdjencentSumOfWithSecondHighest {
        public static void main(String[] args) {
            int[] arr = {2, 5, 3, 8, 9, 1, 7};

            int[] newArr = new int[arr.length - 1];

            int highest = arr[0], previoushighest = highest;

            int index = 0, prevIndex = 0;

            for (int i = 0; i < arr.length - 1; i++) {
                newArr[i] = arr[i] + arr[i + 1];

                if(highest < newArr[i]){
                    previoushighest = highest;
                    highest = newArr[i];


                    prevIndex = index;

                    index = i;              
                }
            }

            System.out.println("Second Highest NO : "+previoushighest);

            System.out.println("no one is "+arr[prevIndex]+ " and no two is "+arr[index]);

        }
    }

Working very good. But if in array there is duplicate numbers then wrong result like {2, 5, 3, 3, 8, 9, 1, 7}

xenteros
  • 15,586
  • 12
  • 56
  • 91
Sagar Yadav
  • 137
  • 2
  • 11

2 Answers2

0

UPDATED: NEW

This may help you.

int[] arr =  { 2, 5, 3, 3, 3, 3, 1, 7 }   ;

    int[] newArr = new int[arr.length - 1];

    int highest = arr[0]+arr[1], previoushighest = Integer.MIN_VALUE;

    int index = 0, prevIndex = 0;

    for (int i = 0; i < arr.length - 1; i++) {
        newArr[i] = arr[i] + arr[i + 1];


        if (highest < newArr[i]) {
            int temp=highest;
            highest = newArr[i];

            previoushighest=temp;
            index = i;
        }

        else if( previoushighest < newArr[i] && highest>newArr[i]){
            previoushighest=newArr[i];
            prevIndex=i;
        }


    }

    System.out.println("Second Highest NO : " + previoushighest);
    System.out.println("Highest NO : " + highest);

    System.out.println("no one is " + arr[prevIndex] + " and no two is "
            + arr[index]);

I don't understand. why this line you are printing

System.out.println("no one is " + arr[prevIndex] + " and no two is "
                + arr[index]);
Md. Nasir Uddin Bhuiyan
  • 1,598
  • 1
  • 14
  • 24
0

This is working fine for me.. atleast for the cases discussed so far (Not sure yet for completeness).

    int[] arr = { 2, 4, 3, 3, 3, 3, 1, 7,8};

    int[] newArr = new int[arr.length - 1];

    int highest = arr[0], previoushighest = Integer.MIN_VALUE;

    int index = 0; // to capture position of second highest pair's second element
    int hindex = 0; // to capture position of highest pair's second element

    if(arr.length < 3) {
        System.out.println("No second highest exists");
        return;
    }

    for (int i = 0; i < arr.length - 1; i++) {
        newArr[i] = arr[i] + arr[i + 1];

        if(previoushighest < newArr[i]){
            if(highest < newArr[i]){
                previoushighest = highest;
                highest = newArr[i];
                index = hindex;
                hindex = i+1;
            } else if(highest != newArr[i]) {
                previoushighest = newArr[i];
                index = i+1;
            } else {
                continue;
            }

        }
    }

    System.out.println("Second Highest NO : "+previoushighest);

    System.out.println("no one is "+arr[index-1]+ " and no two is "+arr[index]);
Anupam Jain
  • 101
  • 10