Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Input:
The first line of input contains an integer T, denoting the number of testcases. The description of T testcases follows. The first line of each testcase contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.


Output:
For each testcase in a new line, print the maximum and minimum element in a single line with space in between.


import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
	public static void main (String[] args) {
		//code
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	
	int a[] = new int[n];
	
	
	
	for (int i = 0;  i < n ;  i++){
	    a[i] = sc.nextInt();
	    
	}
	int max = a[1];
	int min = a[1];
	
	for (int i = 0 ; i < n ; i++){
	    if (a[i]>max){
	        max = a[i];
	        
	    }
	    
	}
	
	for (int i = 0; i< n ; i++){
	    if (a[i]< min )
	    min = a[i];
	    
	}
    return max + " " + min;
    }
}


What I have tried:

can you please finf my mistake the error is
Compilation Error:
Compilation Error

prog.java:37: error: incompatible types: unexpected return value
    return max + " " + min;
                     ^
1 error
Posted
Updated 28-Feb-21 1:21am
Comments
Richard MacCutchan 28-Feb-21 7:22am    
Apart from the fact that the expression is not valid, you should not be trying to return it from your main method, as it has nowhere to be returned to.

Try like this (spot the differences):
import java.util.*;
import java.lang.*;
import java.io.*;

public class GFG {
	public static void main (String[] args) {
		//code
	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	
	int a[] = new int[n];

	for (int i = 0;  i < n ;  i++){
	    a[i] = sc.nextInt();
	    
	}
	int max = a[1];
	int min = a[1];
	
	for (int i = 0 ; i < n ; i++){
	    if (a[i]>max){
	        max = a[i];
	    }
	}
	
	for (int i = 0; i< n ; i++){
	    if (a[i]< min )
	    min = a[i];
	}
	
    System.out.println(max + " " + min);
    }
}
 
Share this answer
 
max is an integer.
min is an integer.
" " is a string.

You cannot add them together: it would either have to convert the string to a number - and it's doesn't think that's quiet what you meant to do - or convert the numbers to strings - and that sounds odd as well.

So it gives you an error which is basically saying "I don't; know what you want me to do here!"

And returning a string from your Main method isn't probably what you wanted to do either ... read the assignment again, and pay attention to this bit:
Quote:
Output:
For each testcase in a new line, print the maximum and minimum element in a single line with space in between.

Your code doesn't attempt to do that, either for each testcase as required, or even once!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900