Friday, March 5, 2021

Basic Programs asked in Interview

 Q. Write a program to find Second largest number in the array?

Ans: Method:-

public static void secondLargestNumber(int arr){

int first = Integer.MIN_VALUE;

int second = Integer.MIN_VALUE;

for(int i=0;i<arr.length;i++){

if(arr[i] > first){

        second = first;

        first = arr[i];

      }else if(arr[i] > second){

        second = arr[i];

      }

}

System.out.println(" Second Largest Number = " + second);

}

No comments:

Post a Comment

String coding questioon

  How do you reverse a given string in place? ( solution ) How do you print duplicate characters from a string? ( solution ) How do you chec...