Java Loops II – Hacker Rank Solution Java

Java Loops II – Hacker Rank Solution Java

Hello Friends, How are you? Today I am going to solve the HackerRank Java Loops II Problem with a very easy explanation. This is the 7th problem of Java on HackerRank. In this article, you will get more than three approaches to solve this problem. So let's start-

Java Loops II – Hacker Rank Solution Java


Table of Content (toc)

HackerRank Java Loops II - Problem Statement

Problem Statement Link - Java Loops II HackerRank

Input Format

The first line contains an integer, q, denoting the number of queries.
Each line i of the q subsequent lines contains three space-separated integers describing the respective ai, bi, and ni values for that query.

Constraints

  • 0 <= q <= 500
  • 0 <= a,b <= 50
  • 1 <= n <= 15

Output Format

For each query, print the corresponding series on a new line. Each series must be printed in order as a single line of n space-separated integers.

Sample Input:

2 0 2 10 5 3 5 (code-box)

Sample Output:

2 6 14 30 62 126 254 510 1022 2046 8 14 26 50 98 (code-box)

Java Loops II – Hacker Rank Solution Java

Approach I:

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

class Solution
{
    public static void main(String []argh)
    {
        Scanner in = new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++)
        {
            int a = in.nextInt();
            int b = in.nextInt();
            int n = in.nextInt();
            // Java Loops II - Hacker Rank Solution Java START
            for (int j = 0; j < n; j++) 
            {
                a += b * (int) Math.pow(2, j);
                System.out.print(a + " ");
            }
            System.out.println();
            // Java Loops II - Hacker Rank Solution Java END
        }
        in.close();
    }
}


Approach II:

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

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int j = sc.nextInt();
        for (int i = 0; i < j; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int n = sc.nextInt();
            int x = 1;
            int o = a;
            for (int k = 0; k < n; k++) {
                o = o + x * b;
                System.out.print(o);
                x = x << 1;
                System.out.print(' ');
            }
            System.out.println();
        }
    }
}


Approach III:

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

public class Solution {

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


Approach IV:

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

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
		int testCases = sc.nextInt();
		for (int i = 0; i < testCases; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			int n = sc.nextInt();

			int sum = a;
			for (int j = 0; j < n; j++) {
				sum += b * Math.pow(2, j);
				System.out.print(sum + " ");
			}
			System.out.println();
		}
    }
}


Disclaimer: The above Problem ( Java Loops II ) is generated by Hackerrank but the Solution is Provided by Code Solution. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these four solutions will pass all the test cases. Now visit Java Loops II HackerRank Problem and try to solve it again.

All the Best!

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.