0% found this document useful (0 votes)
3 views2 pages

Array Programs

The document provides Java code snippets for various array operations including input and printing, calculating the sum, finding the maximum element, reversing the array, and finding pairs that sum to a target value. Each section includes example input and output to demonstrate the functionality of the code. The examples use an array of integers and illustrate common tasks performed on arrays.

Uploaded by

Rajat Tripathi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Array Programs

The document provides Java code snippets for various array operations including input and printing, calculating the sum, finding the maximum element, reversing the array, and finding pairs that sum to a target value. Each section includes example input and output to demonstrate the functionality of the code. The examples use an array of integers and illustrate common tasks performed on arrays.

Uploaded by

Rajat Tripathi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Input and Print Array

Code:
int n=[Link]();
int[] arr=new int[n];
for(int i=0;i<n;i++) arr[i]=[Link]();
for(int i=0;i<n;i++) [Link](arr[i]+" ");

Example Input/Output:
Input:
5
1 2 3 4 5

Output:
1 2 3 4 5

2. Sum of Array

Code:
int sum=0;
for(int i=0;i<n;i++){
arr[i]=[Link]();
sum+=arr[i];
}
[Link](sum);

Example Input/Output:
Input:
5
1 2 3 4 5

Output:
15

3. Maximum Element

Code:
int max=arr[0];
for(int i=1;i<n;i++){
if(arr[i]>max) max=arr[i];
}
[Link](max);

Example Input/Output:
Input:
5
1 9 3 4 2

Output:
9
4. Reverse Array

Code:
for(int i=n-1;i>=0;i--){
[Link](arr[i]+" ");
}

Example Input/Output:
Input:
5
1 2 3 4 5

Output:
5 4 3 2 1

5. Pair Sum

Code:
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(arr[i]+arr[j]==target){
[Link](arr[i]+" "+arr[j]);
}
}
}

Example Input/Output:
Input:
5
1 2 3 4 5
Target: 5

Output:
1 4
2 3

You might also like