Replacing maximum value with minimum value in a array : <p>First input is n numbers then the numbers itself, then find the maximum value and minimum value and last, replace the maximum value with the minimum one. But this code doesn't work first test case ( 5 1 3 3 3 4 ), it returns 3 3 3 3 3. But it works on second test case ( 8 5 4 2 2 4 2 2 5 ). What is wrong with this code ? I have checked the loop which check for maximum and minimum value, nothing wrong.   </p>

<pre><code>import java.util.Scanner;

class Main {
public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
     int[] marks = new int[sc.nextInt()];
     int max = 0;
     int min = 100;
     for(int i=0;i&lt;marks.length;i++){
        marks[i] = sc.nextInt();
     }
     for(int i=0;i&lt;marks.length;i++){
        if(min&gt;marks[i]){
            min = marks[i];
        }
     }
     for(int i=0;i&lt;marks.length;i++){
        if(max&lt;marks[i]){
            max = marks[i];
        }
     }
     for(int i=0;i&lt;marks.length;i++){
        if(marks[i]==max){
            marks[i]=min;
        }
    }
     for(int i=0;i&lt;marks.length;i++){
        if(i!=marks.length-1){
            System.out.print(marks[i]+" ");
        }else{
            System.out.println(marks[i]);
            System.out.println();
        }
    }
}    
</code></pre>

<p>}</p>
