# Accenture off campus coding question

Here we will discuss Accenture off campus coding question. Accenture off campus coding question on arrays is solved with step by step explanation here.

Question:

Write a program to take input as an array of size n and print the second highest number in an array of size n.

NOTE: You cannot sort the array and don’t make use of any java collection API’s or built in methods

Example:

Input:

Array1={5,1,9,2,6,8};

Output:

8

Explanation:

From above array we can observe the first highest number is 9, after that the second highest number is 8 (Second highest Number).

Algorithm

• Take in input array from user
• declare two variables namely firstnumber indicating first highest number
• Later second variable namely secondnumber indicating second highest number.
• Use if-else loop to initialize the first and second numbers.
• Later use a for loop to iterate through elements of given array starting from index position 2 .
• Run loop untill you reach the end of given array.
• If the array element is greater than firstnumber then, reassign firstnumber with array element, then update secondnumber with firstnumber.
• in ELSE part check if array element is greater than secondnumber if so re-assign second number with array element
• later just print the second number which is required answer.

JAVA CODE

```package arrayPrograms;
class problem3 {
public static void main(String[] args) {

// coded by VRASHIKESH PATIL

// second highest number without sort method
// Accenture off campus coding question

int[] array1= {5,1,9,2,6,8,12};
int firstnumber;
int secondnumber;
if(array1[0]>array1[1]) {
firstnumber=array1[0];
secondnumber=array1[1];
}
else {
firstnumber=array1[1];
secondnumber=array1[0];
}
for(int i=2;i<array1.length;i++) {
if(array1[i]>firstnumber ) {
int temp=firstnumber;
secondnumber=temp;
firstnumber=array1[i];
}
else if(array1[i]>secondnumber) {
secondnumber=array1[i];
}
}
System.out.println(secondnumber );
}
}
```

Watch step by step explanation for above code

Similar Posts

Rotate Array Elements towards Right Side

What is Jagged Array In Java

Dynamic Programming HACKER EARTH questions

HACKER EARTH off Campus Coding Question