You are on page 1of 7

Awadhesh Kumar Maurya

Roll no 2100290100039
CSE(6)A
Weekly Coading day 2
Question

Leet code 53.

53. Maximum Subarray


Medium
332141394Add to ListShare
Given an integer array nums , find the subarray with the largest sum, and return its sum.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]

Output: 6

Explanation: The subarray [4,-1,2,1] has the largest sum 6.

Example 2:

Input: nums = [1]

Output: 1

Explanation: The subarray [1] has the largest sum 1.

Example 3:

Input: nums = [5,4,-1,7,8]

Output: 23

Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.

Constraints:
 1 <= nums.length <= 10 5
 -10 4 <= nums[i] <= 10 4

Follow up: If you have figured out the O(n) solution, try coding another solution using
the divide and conquer approach, which is more subtle.

Accepted
3,690,662
Submissions
7,294,419

class Solution {

public int maxSubArray(int[] nums) {

int n=nums.length;

int max =Integer.MIN_VALUE;

int sum =0;

for (int i=0;i< n;i++) {

sum += nums[i];

max = Math.max(max, sum);

if (sum < 0)

sum = 0;

return max;

}
Question 2------------

704. Binary Search


Easy
11438234Add to ListShare
Given an array of integers nums which is sorted in ascending order, and an integer target ,
write a function to search target in nums . If target exists, then return its index. Otherwise,
return -1 .

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9

Output: 4

Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2

Output: -1
Explanation: 2 does not exist in nums so return -1

Constraints:

 1 <= nums.length <= 10 4


 -10 4 < nums[i], target < 10 4
 All the integers in nums are unique.
 nums is sorted in ascending order.

Code in java

class Solution {

public int search(int[] nums, int target) {

int s=0;

int e=nums.length-1;

while(s<=e){

int mid= s+(e-s)/2;

if(nums[mid]==target) return mid;

else if(nums[mid]>target) e=mid-1;

else s=mid+1;

return -1;

Output
Leet code question no 34

34. Find First and Last Position of Element in Sorted


Array

Given an array of integers nums sorted in non-decreasing order, find the starting and ending
position of a given target value.

If target is not found in the array, return [-1, -1].

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8


Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6


Output: [-1,-1]

Example 3:
Input: nums = [], target = 0
Output: [-1,-1]

Constraints:

 0 <= nums.length <= 10 5


 -109 <= nums[i] <= 10 9
 nums is a non-decreasing array.
 -109 <= target <= 10 9

Code in java

class Solution {
public int[] searchRange(int[] arr, int target) {
int [] result=new int [2];
int n = arr.length;
int x=target;
int first = -1, last = -1;
for (int i = 0; i < n; i++) {
if (x != arr[i])
continue;
if (first == -1)
first = i;
last = i;
}
if (first != -1) {
result[0]=first;
result[1]=last;
return result;
}
return new int [] {-1,-1};
}
}
Question
Countion number of ones in binary sorted array

Code in java
public class NoOfOnesInBinarySortedArray {
public static void main(String[] args) {
int[] binaryArray = { 0, 0, 0, 1, 1, 1, 1 };
int countOnes = countOnes(binaryArray);
System.out.println("Number of ones in the binary array: " +
countOnes);
}

public static int countOnes(int[] nums) {


int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == 1) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return nums.length - left;
}
}

You might also like