Wednesday, 29 September 2021

Remove Duplicates from Sorted Array - LeetCode 26

 Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

Custom Judge:

The judge will test your solution with the following code:

int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length

int k = removeDuplicates(nums); // Calls your implementation

assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
    assert nums[i] == expectedNums[i];
}

If all assertions pass, then your solution will be accepted.

 

Example 1:

Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).

 

Solution(Only function):

class Solution {

    public int removeDuplicates(int[] a) {    

        int n=a.length;

        int i=0;

        int j=i+1;

        

        while(i<j && j<n)

        {

            if(a[i]==a[j])

            {

                ++j;

            }else{

                a[i+1]=a[j];

                ++i;

                ++j;

            }

        }

         return i+1;

    }

}

Sort Array By Parity II - LeetCode

 Given an array of integers nums, half of the integers in nums are odd, and the other half are even.

Sort the array so that whenever nums[i] is odd, i is odd, and whenever nums[i] is even, i is even.

Return any answer array that satisfies this condition.

 

Example 1:

Input: nums = [4,2,5,7]
Output: [4,5,2,7]
Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

Example 2:

Input: nums = [2,3]
Output: [2,3]

 

Constraints:

  • 2 <= nums.length <= 2 * 104
  • nums.length is even.
  • Half of the integers in nums are even.
  • 0 <= nums[i] <= 1000
Solution(Only function):

class Solution {
    public int[] sortArrayByParityII(int[] nums) {
        
        int i=0;
        int j=1;
    while(i<nums.length && j<nums.length){    
        while(i<nums.length)
        {
            if(nums[i]%2!=0)
                break;
            
            i+=2;
        }
        while(j<nums.length)
        {
            if(nums[j]%2==0)
                break;
            
            j+=2;
        }
       
      if(i<nums.length && j<nums.length){  
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
        }
    }    
    return nums;    
    }       
}

Finding middle element in a linked list - GeeksForGeeks

 Given a singly linked list of N nodes.

The task is to find the middle of the linked list. For example, if the linked list is
1-> 2->3->4->5, then the middle node of the list is 3.
If there are two middle nodes(in case, when N is even), print the second middle element.
For example, if the linked list given is 1->2->3->4->5->6, then the middle node of the list is 4.

Example 1:

Input:
LinkedList: 1->2->3->4->5
Output: 3 
Explanation: 
Middle of linked list is 3.

Example 2: 

Input:
LinkedList: 2->4->6->7->5->1
Output: 7 
Explanation: 
Middle of linked list is 7.

Your Task:
The task is to complete the function getMiddle() which takes a head reference as the only argument and should return the data at the middle node of the linked list.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Solution in C++:

class Solution{

    public:

    /* Should return data of middle node. If linked list is empty, then  -1*/

    int getMiddle(Node *head)

    {

        // Your code here

        

        if(head==nullptr)

            return -1;

            

        

        Node* slow=head;

        Node* fast=head;

        

        while(fast!=nullptr && fast->next!=nullptr){

            slow=slow->next;

            fast=fast->next->next;

        }

        return slow->data;

        

    }

};

Solution in Java:
class Solution
{
    int getMiddle(Node head)
    {
         // Your code here.
         
         Node fast=head;
         Node slow=head;
         
         while(fast!=null && fast.next!=null)
         {
             slow=slow.next;
             fast=fast.next.next;
         }
         return slow.data;
    }
}



Tuesday, 28 September 2021

Climbing Stairs - LeetCode 70

 You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

 

Example 1:

Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: n = 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

 

Solution(Only function):

class Solution {

    public int climbStairs(int n) {

        

         

        if(n==1)

            return 1;

        else if(n==2)

            return 2;

        else{

            int x=1,y=2;

            

            int index=3,sum=0;

            

            while(index<=n) {

                sum=x+y;

                x=y;

                y=sum;

                index++;

            }

            return sum;

        }   

    }

}

N-th Tribonacci Number - LeetCode 1137

 The Tribonacci sequence Tn is defined as follows: 

T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.

Given n, return the value of Tn.

 

Example 1:

Input: n = 4
Output: 4
Explanation:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4

Example 2:

Input: n = 25
Output: 1389537

 

Solution(Only function):

class Solution {

    public int tribonacci(int n) {

        

       if(n==0)

            return 0;

        else if(n<=2)

            return 1;

        else{

                int a=0,b=1,c=1;

                int sum=0;

                int index=3;

                

                while(index<=n)

                {

                    sum=a+b+c;

                    index++;

                    a=b;

                    b=c;

                    c=sum;

                    

                }

                return sum;

            }   

    }

}

Swapping Nodes in a Linked List - LeetCode 1721

 You are given the head of a linked list, and an integer k.

Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed).

 

Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [1,4,3,2,5]

Example 2:

Input: head = [7,9,6,6,7,8,3,0,9,5], k = 5
Output: [7,9,6,6,8,7,3,0,9,5]

Example 3:

Input: head = [1], k = 1
Output: [1]

Example 4:

Input: head = [1,2], k = 1
Output: [2,1]

Example 5:

Input: head = [1,2,3], k = 2
Output: [1,2,3]

 

Solutions(Only function):

class Solution {

    public ListNode swapNodes(ListNode head, int k) {

        

        ListNode first=head;

        

        k=k-1;

      

        while(k>0)

        {

            first=first.next;

            k--;

        }

        ListNode f=first;

        ListNode second=head;

      

        while(first.next!=null)

        {

            first=first.next;

            second=second.next;

        }

        int temp=f.val;

        f.val=second.val;

        second.val=temp;

        return head;

    }

}

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...