# Field Manual for Hackers

## Java | Find the largest and smallest elements in array without sorting .Also find its position in array.

1. 3.
import java.util.Scanner;
public class JavaApplication8 {

public static void main(String[] args) {

Scanner obj=new Scanner(System.in);
int n,i;
System.out.println("Enter the number of elements");
n=obj.nextInt();
int arr[]=new int[n];
System.out.println("Enter the array");
for(i=0;i<n;i++)
{
arr[i]=obj.nextInt();
}
for(i=1;i<n;i++)
{
if(arr[0]<arr[i])
{
arr[0]=arr[i];
}

}
System.out.println("The largest element is="+arr[0]);
}

}

2. This comment has been removed by the author.

3. import java.util.Scanner;
class Array
{
public static void main(String args[])
{
int max=0,min,indexLar=0,indexSmall=0;
Scanner scan=new Scanner(System.in);
System.out.println("How many number you want to add");
int n=scan.nextInt();
int ar[]=new int[n];
System.out.println("Enter Numbers");
for (int i=0;i<n;i++)
{
ar[i]=scan.nextInt();
}
min=ar[0];
for(int i=0;i<n;i++)
{
if(max<ar[i])
{
max=ar[i];
indexLar=i;
}
if(ar[i]<min)
{
min=ar[i];
indexSmall=i;
}
}
System.out.println("Largest Value "+max+" at postion "+(indexLar+1));
System.out.println("Smallest Value "+min+" at postion "+(indexSmall+1));
}
}

4. import java.util.Scanner;

public class arraycomp
{
public static void main(String ar[])
{
Scanner scan = new Scanner(System.in);
int n; // size of array
int []a = new int[50];
System.out.println(" Enter the size of array (max size is 50)");
n = scan.nextInt();
System.out.println(" Enter the elements of array");
for( int i = 0;i < n ; i++)
{
a[i] = scan.nextInt();
}
int small = a[0];
int large = a[0];
int pos = 0;
int pos1 = 0 ;
for( int i = 0;i < n ; i++)
{
if(a[i]>large) // for largest element
{
large = a[i];
pos = i;
}
if(a[i]<small) // for smallest element
{
small = a[i];
pos1 = i;
}
}
System.out.println("the smallest element is : " +small);
System.out.println("the position of smallest element is at position :" +(pos1+1));
System.out.println("the largest element is : " +large);
System.out.print("the position of largest element is at position :" +(pos+1));

}
}

OUTPUT:

Enter the size of array (max size is 50)
4
Enter the elements of array
89 56 92 24
the smallest element is : 24
the position of smallest element is at position :4
the largest element is : 92
the position of largest element is at position :3

5. This comment has been removed by the author.

6. import java.util.Scanner;
class Position
{
public static void main(String Args[])
{
System.out.println("How many numbers you want to enter?");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int Ar[]=new int[n];
int pos1=0,pos2=0,i;
System.out.println("enter the values");
for(i=0;iAr[i])
{
Ar[0]=Ar[i];
pos2=i;
}
}
System.out.println("smallest value is"+Ar[0]);
System.out.println("smallest value is at"+pos2);
}
}

7. import java.util.Scanner;

class Largest_smallest{
public static void main(String a[]){
int ar[]=new int[100];
int number,i,largest_number,largest_index,smallest_number,smallest_index;
System.out.print("Enter the number of elements: ");
Scanner o=new Scanner(System.in);
number=o.nextInt();
System.out.println("Now enter the values of array: ");
for(i=0;ilargest_number){
ar[i]+=largest_number;
largest_number=ar[i]-largest_number;
ar[i]-=largest_number;
largest_index=i;
}
if(ar[i]<smallest_number){
ar[i]+=smallest_number;
smallest_number=ar[i]-smallest_number;
ar[i]-=smallest_number;
smallest_index=i;
}
}

System.out.println("The smallest element is: "+smallest_number+" at index "+smallest_index);
System.out.println("The largest element is: "+largest_number+" at index "+largest_index);
}
}

8. import java.util.Scanner;
class Small{
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
int n,i,temp,flag,small=0,large=0;
System.out.println("enter the value of n");
n=obj.nextInt();
int arr[]=new int[n];
for(i=0;itemp){
temp=arr[i];
large=i;}

}System.out.println("the largest no. is "+temp+"and position is "+large);
System.out.println("the smallest no. is "+flag+"and position is"+small);}

}

9. import java.util.Scanner;
class LSarray{
public static void main(String args[]){
int n,i=0,temp=0,j=0,locate=0;
Scanner scan=new Scanner(System.in);
System.out.println("enter the size of array");
n=scan.nextInt();
int ar[]=new int[n];
for(i=0;i<n;i++)
{
System.out.println("Enter the element"+(i+1));
ar[i]=scan.nextInt();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(ar[i]<ar[j])
{
temp=ar[j];
locate=j+1;
}
}
}
System.out.printf("The Greatest Number= "+temp);
System.out.printf(" Location= "+locate);
}
}

10. import java.util.Scanner;
class Largest{
public static void main(String ar[]){
int num,pos1=0,pos2=0;
//int arr[]=new int[10];
Scanner scan=new Scanner(System.in);
System.out.println("Enter the size:");
num=scan.nextInt();
int arr[]=new int[num];
System.out.println("Enter the values:");
for(int i=0;iarr[i]){
small=arr[i];
pos2=i;
}
}
System.out.println("The smallest value is:"+small);
System.out.println("At position:"+(pos2+1));
}
}