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

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

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

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