LAB Q1: Java Util Scanner
LAB Q1: Java Util Scanner
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
// 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));
}
}