0% found this document useful (0 votes)
30 views12 pages

Assignment - 1

This document contains solutions to 8 questions on arrays and algorithms. The questions cover topics like finding the second largest element, removing duplicates from a sorted array, moving all zeros to the end of an array, finding the missing number, sorting colors, finding the maximum subarray sum, calculating maximum profit from stock prices, and identifying leader elements in an array. The solutions provided use techniques like sorting, two pointers, prefix sums, and iterating in reverse order.

Uploaded by

drishtianand732
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)
30 views12 pages

Assignment - 1

This document contains solutions to 8 questions on arrays and algorithms. The questions cover topics like finding the second largest element, removing duplicates from a sorted array, moving all zeros to the end of an array, finding the missing number, sorting colors, finding the maximum subarray sum, calculating maximum profit from stock prices, and identifying leader elements in an array. The solutions provided use techniques like sorting, two pointers, prefix sums, and iterating in reverse order.

Uploaded by

drishtianand732
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
You are on page 1/ 12

ASSIGNMENT - 1

DRISHTI ANAND(CSE-1)
BATCH(2021-2025)

Question 1:
Given an array Arr of size N, print second largest element from an array.
Solution

class Solution {
int print2largest(int arr[], int n) {
Arrays.sort(arr);

int second_largest = 0;
for (int i = n - 2; i >= 0; i--){

if (arr[i] != arr[n - 1])


{
second_largest = arr[i];
break;
}
}

return second_largest;
}
}
Question 2:
Remove Duplicates from Sorted Array
Solution
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length==0)
return 0;
int j=0;
for (int i=0; i<nums.length; i++)
if (nums[i]!=nums[j])
nums[++j]=nums[i];
return ++j;

}
}
Question 3:
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the
non-zero elements.
Solution
class Solution {
public void moveZeroes(int[] nums) {
int j = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] != 0) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
j++;
}
}

}
}
Question 4:
Given an array nums containing n distinct numbers in the range [0, n], return the only
number in the range that is missing from the array.
Solution

class Solution {
public int missingNumber(int[] nums) {
int sum = 0;
for(int num: nums)
sum += num;

return (nums.length * (nums.length + 1) )/ 2 - sum;

}
}
Question 5:
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of
the same color are adjacent, with the colors in the order red, white, and blue.
Solution

class Solution {
public void sortColors(int[] nums) {
int p1 = 0, p2 = nums.length - 1, index = 0;
while (index <= p2) {
if (nums[index] == 0) {
nums[index] = nums[p1];
nums[p1] = 0;
p1++;
}
if (nums[index] == 2) {
nums[index] = nums[p2];
nums[p2] = 2;
p2--;
index--;
}
index++;
}
}
}
Question 6:
Given an integer array nums, find the subarray which has the largest sum and return its
sum.
Solution

class Solution {
public int maxSubArray(int[] nums) {
int max = Integer.MIN_VALUE, sum = 0;
for (int i = 0; i < nums.length; i++) {
if (sum < 0)
sum = nums[i];
else
sum += nums[i];
if (sum > max)
max = sum;
}
return max;

}
}
Question 7:
You are given an array prices where prices[i] is the price of a given stock on the ith day.
Solution

class Solution {
public int maxProfit(int[] prices) {
int lsf = Integer.MAX_VALUE;
int op = 0;
int pist = 0;

for(int i = 0; i < prices.length; i++){


if(prices[i] < lsf){
lsf = prices[i];
}
pist = prices[i] - lsf;
if(op < pist){
op = pist;
}
}
return op;

}
}
Question 8:
Given an array A of positive integers. Your task is to find the leaders in the array. An element of
array is leader if it is greater than or equal to all the elements to its right side. The rightmost
element is always a leader.
Solution
import java.util.*;
import java.io.*;
class Solution{
static ArrayList<Integer> leaders(int arr[], int n){
ArrayList<Integer>ans = new ArrayList<>();
int rmax = Integer.MIN_VALUE;

for (int i = n - 1; i >= 0; i--) {


if (arr[i] >= rmax) {
ans.add(arr[i]);
rmax = arr[i];
}
}
Collections.reverse(ans);
return ans;
}
}

You might also like