[go: up one dir, main page]

0% found this document useful (0 votes)
20 views5 pages

LAB Q1: Java Util Scanner

lab work

Uploaded by

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

LAB Q1: Java Util Scanner

lab work

Uploaded by

utpalikaacharya5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB

Q1

//sum of n numbers//
import java.util.Scanner;
public class one {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number you:");
int n =sc.nextInt();
int i,sum = 0;
//m=0;
for(i=0;i<=n;i++) {
sum=sum+i;
//System.out.println(i);
}
System.out.print("The sum of n natural numbers is:"+sum);

}
}
Q2

//Finding maximum and minimum in an array//


import java.util.Scanner;
public class two {
public static void main(String args[]) {
int count, max, min, i;
int inputArray[] = new int[500];
Scanner in = new Scanner(System.in);
System.out.print("Enter number of elements:");
count = in.nextInt();
System.out.println("Enter " + count +" "+ "elements:");
for(i = 0; i < count; i++) {
inputArray[i] = in.nextInt();
}
max = min = inputArray[0];
for(i = 1; i < count; i++) {
if(inputArray[i] > max)
max = inputArray[i];
else if (inputArray[i] < min)
min = inputArray[i];
}
System.out.println("Largest Number : " + max);
System.out.println("Smallest Number : " + min);
}
}
Q3//Rotating array by k positions(Left)//
public class three {
// Function to rotate array
static void Rotate(int arr[], int d, int n)
{
// Storing rotated version of array
int temp[] = new int[n];
// Keeping track of the current index
// of temp[]
int k = 0;
// Storing the n - d elements of
// array arr[] to the front of temp[]
for (int i = d; i < n; i++) {
temp[k] = arr[i];
k++;
}
// Storing the first d elements of array arr[]
// into temp
for (int i = 0; i < d; i++) {
temp[k] = arr[i];
k++;
}
// Copying the elements of temp[] in arr[]
// to get the final rotated array
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
// Function to print elements of array
static void PrintTheArray(int arr[], int n)
{
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
}
public static void main (String[] args) {
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int N = arr.length;
int d = 2;
// Function calling
Rotate(arr, d, N);
PrintTheArray(arr, N);
}
}
Q4
public class four {
public static void main(String[] args)
{
int[] a = { -2, 1, -3, 4, -1, 2, 1, -5 ,4 };
System.out.println("Maximum contiguous sum is "
+ maxSubArraySum(a));
}

// Function Call
static int maxSubArraySum(int a[])
{
int size = a.length;
int best_sum = Integer.MIN_VALUE, current_sum = 0;
for (int i = 0; i < size; i++) {
current_sum = Math.max(a[i], current_sum+a[i]);
best_sum = Math.max(current_sum, best_sum);

}
return best_sum;
}
}
Q5
public class five {
static int solution(int[] A)
{
int n = A.length;
// Let this 1e6 be the maximum element provided in
// the array;
int N = 1000010;
// To mark the occurrence of elements
boolean[] present = new boolean[N];
int maxele = Integer.MIN_VALUE;
// Mark the occurrences
for (int i = 0; i < n; i++) {
// Only mark the required elements
// All non-positive elements and the elements
// greater n + 1 will never be the answer
// For example, the array will be {1, 2, 3} in
// the worst case and the result will be 4 which
// is n + 1
if (A[i] > 0 && A[i] <= n)
present[A[i]] = true;
// find the maximum element so that if all the
// elements are in order can directly return the
// next number
maxele = Math.max(maxele, A[i]);
}
// Find the first element which didn't
// appear in the original array
for (int i = 1; i < N; i++)
if (!present[i])
return i;
// If the original array was of the
// type {1, 2, 3} in its sorted form
return maxele + 1;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 0, 10, 2, -10, -20 };
System.out.println(solution(arr));
}
}

You might also like