Posts

Showing posts from October, 2022

Help Ishaan

Problem Link: https://practice.geeksforgeeks.org/problems/help-ishaan5837/1 (It is also the problem of the day for 09-10-2022) JAVA: ------------------------------------------------------------------------------------------------------------> Solution: class Solution { public static boolean isPrime ( int n ){ if ( n == 1 ){ return false ; } else if ( n == 2 || n == 3 ){ return true ; } else if ( n % 2 == 0 || n % 3 == 0 ){ return false ; } else { for ( int i = 5 ; i <= Math . sqrt ( n ); i += 6 ){ if ( n %( i )== 0 || n %( i + 2 )== 0 ){ return false ; } } return true ; } } public int NthTerm ( int n ) { if ( isPrime ( n )== true ){ return 0 ; } else { int diff =

Modified Numbers And Queries

Problem Link: https://practice.geeksforgeeks.org/problems/modified-numbers-and-queries0904/1 Solution: class Solution {          public boolean isPrime(int n){         if(n==1){             return false;         }         else if(n==2 || n==3){             return true;         }         else if(n%2==0 || n%3==0){             return false;         }         else{             for(int i=5;i<=Math.sqrt(n);i+=6){                 if((n%i)==0 || (n%(i+2))==0){                     return false;                 }             }             return true;         }     }         public int sumOfAll(int l, int r)     {         ArrayList<Integer>aa=new ArrayList<Integer>();         int sum=0;         for(int i=2;i<l;i++){             if(isPrime(i)){                 aa.add(i);             }         }                  for(int i=l;i<=r;i++){             if(i==1){                 sum=sum+1;             }             else{                 if(isPrime(i)){                     aa.add(i);

Path Sum

Problem Link: https://leetcode.com/problems/path-sum/ (It is also problem of day for 04-10-2022) Solutions: ========================================================================= Solution 1: class Solution {     public static void fun(TreeNode root, int sum , int val, ArrayList<Integer>aa){         if(root==null){             return ;         }         else if(root.left==null && root.right==null){             val=val+root.val;             aa.add(val);             return ;         }         else{             fun(root.left,sum,val+root.val,aa);             fun(root.right,sum,val+root.val,aa);         }     }     public boolean hasPathSum(TreeNode root, int sum) {         if(root==null){             return false;         }         else{             ArrayList<Integer>aa=new ArrayList<Integer>();             fun(root,sum,0,aa);            // System.out.println(aa);             if(aa.contains(sum)){                 return true;             }             return fa