Java Loops II

Sort by

recency

|

3261 Discussions

|

  • + 0 comments

    import java.util.Scanner; import java.lang.Math; // This is no longer needed if we remove Math.pow

    class Test { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int q = scn.nextInt();

        // The outer 'if' check is not needed.
        // A 'for' loop with q=0 will just not run, which is correct.
        for(int i = 0; i < q; i++) { // Using i=0; i<q is the more common Java convention
            int a, b, n;
            a = scn.nextInt();
            b = scn.nextInt();
            n = scn.nextInt();
    
            // The inner 'if' check is also not needed for HackerRank
    
            int series = a;
    
            for(int j = 0; j < n; j++) {
                // Use (1 << j) instead of Math.pow(2, j)
                // It's faster and works directly with integers.
                series += (1 << j) * b; 
                System.out.print(series + " ");
            }
            System.out.println();
        }
    
        // Good practice to always close the scanner!
        scn.close(); 
    }
    

    }

  • + 0 comments

    Java Optimal Code (100% beats) #java #coding

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scn = new Scanner(System.in);
        int q = scn.nextInt(); // q represent number of series u want to print
    
        for(int i = 1; i <= q; i++) {
            int a, b, n;
    
            a = scn.nextInt(); //First element in the series
            b = scn.nextInt(); //Second element in the series
            n = scn.nextInt(); // number of elements in the series
    
            int series = a; // we initialize with a to avoid multiple addition
                            // of a in the main logic 
    
            // j represent the exponential of base case (b^e)
            for(int j = 0; j < n; j++){
                //main logic
                //(1<<j) is the fast way to get 2^j
                series += (1<<j) * b;
                System.out.print(series + " ");
            }
            System.out.println();
        }
    
    }
    

    }

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
    
        for (int i = 0; i < q; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int n = sc.nextInt();
        if (q>=0 && q<=500) {
            int series = a;
            for (int j = 0; j < n; j++) {
                series = series + (int)Math.pow(2,j) * b;
                System.out.print(series + " ");
            }
            System.out.println();
        }
        }
    }
    

    }

  • + 0 comments

    For Java15 Platform

    I wrote the code from scratch just to get more practice

    import java.util.Scanner;
    
    class Solution
    {
        public static void main(String args[])
        {
            Scanner sc = new Scanner(System.in);
            int q = sc.nextInt();
            
            for(int i=0; i<q; i++)
            {
                int a, b, n;
                a = sc.nextInt();
                b = sc.nextInt();
                n = sc.nextInt();
                
                int series = a;
                
                for(int j=0; j<n; j++)
                {
                    series += Math.pow(2, j) * b;
                    
                    System.out.print((int)series + " ");
                }
                
                System.out.print("\n");
            }
            
            sc.close();
        }
    }
    
  • + 0 comments

    Java

    Solution using Map for memoization for better performance

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            sc.nextLine();
            while(sc.hasNextLine()){
                String[] linea = sc.nextLine().split(" ");
                int a = Integer.parseInt(linea[0]); 
                int b = Integer.parseInt(linea[1]);
                int n = Integer.parseInt(linea[2]);
                
                // Create a map in order to save previously calculated values
                Map<Integer, Double> map = new HashMap<>();
                StringBuilder sb = new StringBuilder();
                
                map.put(0, Math.pow(2, 0)*b);
                for(int i = 1; i < n; i++){
                    map.put(i, map.get(i-1) + Math.pow(2, i)*b);
                }
                map.values().forEach(v -> sb.append( a + Math.round(v) +" "));
                System.out.println(sb.substring(0, sb.length()-1))    ;
                
            }
            sc.close();
        }