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;             } ...

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...

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 s...

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);  ...

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{   ...

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;                 } ...