Posts

Showing posts from September, 2022

Geek and Number String Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/904237fa926d79126d42c437802b04287ea9d1c8/1 (It is also the problem of the day for 23-09-2022) Solution: class Solution {      public int minLength(String s1, int n) {      Stack<Character>stack=new Stack<Character>();         for(int i=0;i<s1.length();i++){         if(stack.isEmpty()){             stack.push((s1.charAt(i)));         }         else{             String s="";             s+=(stack.peek());             s+=(s1.charAt(i));             if(s.equals("12") || s.equals("21") || s.equals("34") || s.equals("43")|| s.equals("56") || s.equals("65")|| s.equals("78") || s.equals("87") || s.equals("90") || s.equals("09")){                                  stack.pop();             }             else{                 stack.push((s1.charAt(i)));             }         }    

Licence Key Formatting Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/license-key-formatting/1 (It is also the problem of the day for 21-09-2022) Solution: class Solution{     static String ReFormatString(String s, int k){         StringBuilder sb=new StringBuilder(s);         StringBuilder sb2=new StringBuilder();         for(int i=0;i<sb.length();i++){             if(sb.charAt(i)!='-'){                 sb.setCharAt(i,Character.toUpperCase(sb.charAt(i)));                 sb2.append(Character.toUpperCase(s.charAt(i)));             }                          }         if(sb.length()<k){                          return sb2.toString();         }         StringBuilder sb1=new StringBuilder();         int count=0;                  for(int i=s.length()-1;i>=0;i--){            if(s.charAt(i)!='-'){                if(count==k){                    count=01;                    sb1.append("-");                    sb1.append(sb.charAt(i));                }                els

Sum of K smallest Elements in BST Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/sum-of-k-smallest-elements-in-bst3029/1 Solution: class Tree {     public static void fun(Node root,ArrayList<Integer>aa){         if(root==null){             return ;         }         else{             fun(root.left,aa);             aa.add(root.data);             fun(root.right,aa);         }     }     int sum(Node root, int k) {          ArrayList<Integer>aa=new ArrayList<Integer>();         fun(root,aa);         int sum=0;         for(int i=0;i<k;i++){             sum=sum+aa.get(i);         }         return sum;              }  } Time Complexity: O(N)[Whole tree traversal][GFG Time:0.13] Space Complexity: O[N][A tree of N nodes is given] Auxiliary Space: O[N][An ArrayList storing N nodes data] Total Test Cases:112 Approach Used: Here we need to find out the sum of k smallest elements, so for that, if we get the list sorted in ascending order of all the elements then easily we can find out the sum of all

Smallest Subset With Greater Sum Geeks For Geeks

Problem Link: https://practice.geeksforgeeks.org/problems/smallest-subset-with-greater-sum/1 (It is also the problem of the day for 15-September-2022) Solution: class Solution {      int minSubset(int[] arr,int n) {          Arrays.sort(arr);        long sum=0;        for(int i=0;i<n;i++){            sum=sum+arr[i];        }        long sum2=0;        int index=n-1;        int count=0;        while(sum2<=sum){            sum2=sum2+arr[index];            sum=sum-arr[index];            index--;            count++;                    }        return count;              } } Approach : We want to find out the minimum number of elements such that their sum should be more than the sum of all remaining elements of the array. So for that, we need to have sum of those elements which are having maximum value, as a result, sum of them will be high and will be less in number as compared to having small value elements and large in number. So for that, we will apply the following approach: 1. S