Posts

Showing posts from May, 2022

Minimum Number in sorted rotated array Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/minimum-number-in-a-sorted-rotated-array-1587115620/1 (It is also the problem of the day for 17-05-2022.) Solution: class Solution {     //Function to find the minimum element in the sorted and rotated array.     static int minNumber(int arr[], int start, int high)     {         int mid=0;                int min=Integer.MAX_VALUE;         while(start<=high){             mid=start+(high-start)/2;             if(min>arr[mid]){                 min=arr[mid];             }             if(arr[0]<arr[mid] && arr[mid]>arr[arr.length-1]){                 start=mid+1;             }             else if(arr[0]>arr[mid] && arr[mid]<arr[arr.length-1]){                 high=mid-1;             }             else if(arr[0]<arr[mid] && arr[mid]<arr[arr.length-1]){                 high=mid-1;             }             else{                 start=mid+1;             }                     }         retu

Shortest Path between Cities Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/shortest-path-between-cities/1# (It is also the problem of the day for 28-05-2022) Solution: class Solution {      int shortestPath( int x, int y){         int height=0;        while(x!=y){           if(x>y){               x=x/2;               height++;           }           else{               y=y/2;               height++;           }        }        return height;     } } Time Complexity:O(LogN)[GFG Time:0.12/1.16] Space Complexity:O(LogN) Auxiliary Space:O(1) Approach Explained: Here we will apply the concept of gcd(x,y). It is so because 1. We will traverse the nodes from bottom to top.  2. While traversing we will continuously keep on dividing node value by 2 which means we are moving from lower level to upper level. 3. We will do this till the time both value becomes equal and simultaneously keep on incrementing the height variable by 1. Better consider an example:                  1 / \ /

Even And Odd Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/even-and-odd/1# (It is also the problem of the day for 26-05-2022) Solution: class Solution {     static void reArrange(int[] arr, int n) {        int  arr1[]=new int[arr.length/2];//odd element array        int arr2[]=new int[arr.length/2];//even element array        int index1=0;        int index2=0;        for(int i=0;i<n;i++){            if((arr[i]&1)==0){                arr2[index1++]=arr[i];            }            else{                arr1[index2++]=arr[i];            }        } Time Complexity: O(N)[GFG Time: 0.52/1.59] Space Complexity: O(N)[Array is used] Auxiliary Space: O(N/2)[Two small subarrays of half-sized of original array size is used] Total Test Cases:221 Approach Used: 1. Traverse the whole array and see whether the element is odd or even. 2. If the element is odd then add in an array named arr1 and if it is even then add it to arr2. 3. Later modify the original array by taking one element from arr1 an

A Special Keyboard Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/228d0aa9f26db93ee5b2cb3583dbd4b197447e16/1 (It is also the problem of the day for 23-05-2022) Solution: Approach 1: Using Map class Solution {     static int findTime(String s1 , String s2) {         LinkedHashMap<Character,Integer>map=new LinkedHashMap<Character,Integer>();                  for(int i=0;i<s1.length();i++){             map.put(s1.charAt(i),i);         }        // System.out.println(map);                  int sum=0;                  int point=0;                  for(int i=0;i<s2.length();i++){             char a=s2.charAt(i);             int val=map.get(a);             sum=sum+Math.abs(point-val);             point=val;         }         return sum;              } }; Time Complexity: O(S1)[GFG Time:0.49/6.28] Space Complexity: O(1)[Strings are given] Auxiliary Space: O(S1)[Map is used] Total Test Cases:10203 Approach Used: 1. Here we will store all the characters with their index on the map. 2. N

Merge Two Sorted Linked List Geeks For Geeks

 Problem Link: https://practice.geeksforgeeks.org/problems/merge-two-sorted-linked-lists/1# (It is also the problem of the day for 07-05-2022) Solution: class LinkedList {     Node sortedMerge(Node head1, Node head2) {         Node temp1=head1;         Node temp2=head2;         Node temp3=new Node(-1);         Node temp4=temp3;                  while(temp1!=null && temp2!=null){             if(temp1.data<=temp2.data){                 temp3.next=new Node(temp1.data);                 temp3=temp3.next;                 temp1=temp1.next;                              }             else{                 temp3.next=new Node(temp2.data);                 temp3=temp3.next;                 temp2=temp2.next;             }                                         }                  while(temp1!=null){             temp3.next=new Node(temp1.data);             temp1=temp1.next;              temp3=temp3.next;         }                   while(temp2!=null){             temp3.next=new Node(temp2

Super Primes Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/super-primes2443/1 (It is also the problem of the day for 02-05-2022) Solution: class Solution {     public boolean isPrime(int n){         if(n==2 || n==3){             return true;                      }         else if(n==1){             return false;         }         else if((n%2)==0 || (n%3)==0){             return false;         }         else{             for(int i=5;i<Math.sqrt(n)+1;i+=6){                 if((n%i)==0 || (n%(i+2))==0){                     return false;                 }                              }             return true;         }     }     int superPrimes(int n) {        HashSet<Integer>set=new HashSet<Integer>();        set.add(2);        set.add(3);        int count=0;        for(int i=5;i<n+1;i++){            if(isPrime(i)==true){                //System.out.println("true");                if(set.contains(i-2)==true){                    count++;                }