0% found this document useful (0 votes)
6 views7 pages

21bcs5376 Brijesh Java 13

sentimental analysis

Uploaded by

try.ankitk
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)
6 views7 pages

21bcs5376 Brijesh Java 13

sentimental analysis

Uploaded by

try.ankitk
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/ 7

Name: Brijesh UID: 21BCS5376

Sec/Group: 21BCS-CC-639/B Subject: Java

Assignment: 13
Q1: Valid Perfect Square
Ans: Code:
class Solution { public boolean
isPerfectSquare(int num) {
long l = 1, r = num;
while (l <= r) { long
mid = l + (r - l) / 2; if
(mid * mid == num)
return true; else
if (mid * mid > num)
r = mid - 1;
else
l = mid + 1;
}
return false;
}}

Output:
Q2: Median of Two Sorted Arrays
Ans: Code:
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] list = new int[nums1.length + nums2.length];
for(int i=0;i<nums1.length;i++)
{
list[i]=nums1[i];
}
int length = nums1.length;
for(int i=0;i<nums2.length;i++)
{
list[length]=nums2[i];
length++;
}
Arrays.sort(list);
if(list.length%2 == 0)
{ return (double) (list[list.length/2] +
list[list.length/2-1])/2;
}
else{
return (double) list[list.length/2];
}
}
}

Output:
Q3: Median of Two Sorted Arrays
Ans: Code:
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] list = new int[nums1.length + nums2.length];

for(int i=0;i<nums1.length;i++)
{
list[i]=nums1[i];
}
int length = nums1.length;
for(int i=0;i<nums2.length;i++)
{

list[length]=nums2[i];
length++;
}

Arrays.sort(list);
if(list.length%2 == 0)
{
return (double) (list[list.length/2] + list[list.length/2-1])/2;
}
else{
return (double) list[list.length/2];
}

}
}

Output:
Q6: Search in Rotated Sorted Array
Ans: Code:
class Solution {
public int search(int[] nums, int target) {
int low = 0, high = nums.length - 1;

while (low <= high) {


int mid = (low + high) / 2;

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

if (nums[low] <= nums[mid]) {


if (nums[low] <= target && target < nums[mid]) {
high = mid - 1;
} else { low =
mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[high]) {
low = mid + 1;
} else { high =
mid - 1;
}
}
}

return -1;
}}
Output:

Q7: Plus One Ans:


Code:
class Solution { public int[]
plusOne(int[] digits) { int i =
digits.length - 1; while
(digits[i] == 9){ if (i == 0){
int[] res = new int[digits.length + 1];
res[0] = 1;
return res; }
digits[i] = 0;
i -= 1; }
digits[i] += 1;
return digits;
}
}

Output:
Q8: Gray Code Ans:
Code:
class Solution { public List<Integer>
grayCode(int n) { // if(n==0)return
new ArrayList<>();
List<Integer> crnt=new ArrayList<>(Arrays.asList(0,1));
if(n==1)return crnt;
crnt=grayCode(n-1);
List<Integer> ret=new ArrayList<>();
int add=(int)Math.pow(2,n-1); for(int
i=crnt.size()-1;i>-1;i--){
crnt.add(crnt.get(i)+add);
}
return crnt;
}}

Output:

You might also like