Java Hyd Team
746 subscribers
986 photos
39 videos
670 files
690 links
https://teamhydteam.my.canva.site/

Can visit us on our website 😊
Still working on it 😊
Download Telegram
Java Hyd Team
Photo
/*
You are given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
*/

/**
* Approach: Add Two Integer LinkedList in Reverse Order
* Just like how you would sum two numbers on a piece of paper,
* we begin by summing the least-significant digits, which is the head of l1 and l2.
* Since each digit is in the range of 0…9, summing two digits may "overflow".
* For example 5 + 7 = 12.
* In this case, we set the current digit to 2 and bring over the carry = 1 to the next iteration.
* carry must be either 0 or 1 because the largest possible sum of two digits (including the carry) is 9 + 1 = 19.
*
* The pseudocode is as following:
* 1. Initialize current node to dummy head of the returning list.
* 2. Initialize carry to 0.
* 3. Initialize p1 and p2 to head of l1 and l2 respectively.
* 4. Loop through lists l1 and l2 until you reach both ends.
* 5. Set x to node p1's value. If p1 has reached the end of l1, set to 0.
* 6. Set y to node p2's value. If p2 has reached the end of l2, set to 0.
* 7. Set sum = x + y + carry.
* 8. Update carry = sum / 10.
* 9. Create a new node with the digit value of (sum mod 10) and set it to current node's next, then advance current node to next.
* 10. Advance both p1 and p2.
* 11. Check if carry = 1, if so append a new node with digit 11 to the returning list.
* 12. Return dummy head's next node.
*
* Note that we use a dummy head to simplify the code.
* Without a dummy head, you would have to write extra conditional statements to initialize the head's value.
* Take extra caution of the following cases:
* Test case Explanation
* l1=[0,1]
* l2=[0,1,2] When one list is longer than the other.
* l1=[]
* l2=[0,1] When one list is null, which means an empty list.
* l1=[9,9]
* l2=[1] The sum could have an extra carry of one at the end, which is easy to forget.
*
* Complexity Analysis
* Time complexity : O(max(m,n)).
* Assume that m and n represents the length of l1 and l2 respectively,
* the algorithm above iterates at most max(m,n) times.
* Space complexity : O(1).
*
* Follow up
* What if the the digits in the linked list are stored in non-reversed order? For example:
* (3→4→2)+(4→6→5)=8→0→7
*
* Add Two Numbers II:
*
*
* Reference: */

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
int carr = 0;

while (l1 != null || l2 != null) {
int x = l1 == null ? 0 : l1.val;
int y = l2 == null ? 0 : l2.val;

int sum = x + y + carr;

carr = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;

if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
}
if (carr != 0) {
curr.next = new ListNode(carr);
}

return dummy.next;
}
}
1. Two Sum
Easy

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.



Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:

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


Constraints:

2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
1323. Maximum 69 Number
Easy
2K
176
Companies
You are given a positive integer num consisting only of digits 6 and 9.

Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).



Example 1:

Input: num = 9669
Output: 9969
Explanation:
Changing the first digit results in 6669.
Changing the second digit results in 9969.
Changing the third digit results in 9699.
Changing the fourth digit results in 9666.
The maximum number is 9969.
Example 2:

Input: num = 9996
Output: 9999
Explanation: Changing the last digit 6 to 9 results in the maximum number.
Example 3:

Input: num = 9999
Output: 9999
Explanation: It is better not to apply any change.


Constraints:

1 <= num <= 104
num consists of only 6 and 9 digits.
public static void main(String[] args) {
int[] a={9,5,7};
int n=14;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a.length;j++)
{
if((a[i]+a[j])==n)
{
System.out.println("["+i+","+j+"]");
}
}
}
}
public class PrimeNumberExample {
public static void main(String[] args) {
int i=0;
int num=0;
//Empty String
String primeNumbers = "";
for (i = 1; i <= 20000; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
}
if (counter ==2)
{
//Appended the Prime number to the String
primeNumbers = primeNumbers + i + " ";
}
}
System.out.println("Prime numbers from 1 to 200,00 are :");
System.out.println(primeNumbers);
}
}
package com.Aman;
public class Q27_1 {
static int maxConsecutiveOnes(int x)
{
int count = 0;
while (x!=0)
{
x = (x & (x << 1));
count++;
}
return count;
}
public static void main(String args[])
{
int x = 1000;
System.out.println(maxConsecutiveOnes(x));
}
}
public class Q29_1 {
static void printAllSubsets(int[] A, int K) {
int N = A.length;
for (int i = 0; i < (1 << N); i++) {
int sum = 0;
for (int j = 0; j < N; j++) {
if ((i & (1 << j)) > 0)
sum += A[j];
}
if (sum == K) {
for (int j = 0; j < N; j++) {
if ((i & (1 << j)) > 0)
System.out.print(A[j] + " ");
}
System.out.println();
}
}
}
public static void main(String[] args) {
int[] A = {0, 1, 2, 3, 5, 6, 7, 11, 12, 14, 20};
int K = 5;
printAllSubsets(A, K);
}
}
public class Q26_1 {
public static void main(String[] args) {
int[]arr = {5,6,8,9,2,4,6,1};
int largest = arr[0];
int secondLargest = arr[0];
System.out.println("The given array is:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest) {
secondLargest = arr[i];
}
}
System.out.print("Second largest number is:" + secondLargest);
}
}
Java Hyd Team
public class Q29_1 { static void printAllSubsets(int[] A, int K) { int N = A.length; for (int i = 0; i < (1 << N); i++) { int sum = 0; for (int j = 0; j < N; j++) { if ((i & (1 << j)) > 0) …
public class Q29_2 {
public static void main(String[] args) {
int[] a = {0, 1, 2, 3, 5, 6, 7, 11, 12, 14, 20};
int n = 5;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if ((a[i] + a[j]) == n) {
System.out.println("[" + i + "," + j + "]");
}
}
}
}
}
3Sum

Medium


Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.



Example 1:

Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
Example 2:

Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.
Example 3:

Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.


Constraints:

3 <= nums.length <= 3000
-105 <= nums[i] <= 105
public class Q25_1 {
public static void main(String[] args) {
int[] arr = {5, 6, 8, 9, 2, 4, 6, 1};
int left = 0, right = arr.length - 1;
while (left < right) {
while (arr[left] % 2 == 0 && left < right)
left++;
while (arr[right] % 2 == 1 && left < right)
right--;
if (left < right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
System.out.println("Even numbers on left side and Odd numbers on right side: ");
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
public class Q24_1 {
public static void main(String[] args) {
int[] arr = {5, 6, 8, 9, 2, 4, 6, 1};
int toFind = 59;
boolean found = search(arr, toFind);
if (found)
System.out.println(toFind + " is found");
else
System.out.println(toFind + " is not found");
}
/*
* A method to search toFind in arr
* arr: the array to search
* toFind: the element to search
*/
public static boolean search(int[] arr, int toFind) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == toFind) {
return true;
} else if (arr[mid] < toFind) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
}
Danaher Hiring for QA Engineer
Role: QA Enginee
Experience: Fresher
Passout year: 2022 and before
Location: Bangalore
Qualification: B.tech/BE/MCA/BCA
Salary: 4.9 - 6 LPA



Apply Now: https://jobs.danaher.com/global/en/job/DANAGLOBALR1191438EXTERNALENGLOBAL/QA-Engineer-I
public static void main(String[] args) {
int i;
int num;
StringBuilder primeNumbers = new StringBuilder();
for (i = 1; i <= 100; i++) {
int counter = 0;
for (num = i; num >= 1; num--) {
if (i % num == 0) {
counter = counter + 1;
}
}
if (counter == 2) {
primeNumbers.append(i).append(" ");
}
}
System.out.println("Prime numbers from 1 to 100 are :");
System.out.println(primeNumbers);