Professional Documents
Culture Documents
Roll no 2100290100039
CSE(6)A
Weekly Coading day 2
Question
Example 1:
Output: 6
Example 2:
Output: 1
Example 3:
Output: 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 {
int n=nums.length;
sum += nums[i];
if (sum < 0)
sum = 0;
return max;
}
Question 2------------
Example 1:
Output: 4
Example 2:
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
Code in java
class Solution {
int s=0;
int e=nums.length-1;
while(s<=e){
else s=mid+1;
return -1;
Output
Leet code question no 34
Given an array of integers nums sorted in non-decreasing order, find the starting and ending
position of a given target value.
Example 1:
Example 2:
Example 3:
Input: nums = [], target = 0
Output: [-1,-1]
Constraints:
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);
}