Objective In this challenge, we're going to use loops to help us do some simple math. Task Given an integer, , print its first multiples. Each multiple (where ) should be printed on a new line in the form: N x i = result . Input Format A single integer, . Constraints Output Format Print lines of output; each line (where ) contains the of in the form: N x i = result . Sample Input 2 Sample Output 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 Explanation: Here, we just need to use for loops to achieve the result Solution : import java.io.* ; import java.math.* ; import java.security.* ; import java.text.* ; import java.util.* ; import java.util.concurrent.* ; import java.util.regex.* ; public class Solution { public static void main ( String [] args ) throws IOException { BufferedReader bufferedReader = new BufferedReader ( new InputStreamReader ( Syste
Even Sum
A number is even if it is divisible by 2, but in this case a number is even if the active bits (1s in its binary representation) of a given number are 2. So your task is to find the sum of first N even numbers.
Input Format : The first line of the input contains an integer T denoting the number of test cases Each of the next T lines contains a single integer N.
Input Constraints : 0 < N <=10^5
Output Format : For each test case, print a single integer denoting sum of first N even numbers mod 1000000007.
Sample Input :
2 15 20
Sample Output :
315
666
Explanation of Question:They are asking for sum of even numbers but here the even numbers are those having even number of active bits
Active bits are nothing but one.
Logic:
- Active even bits come in the format like
3,5,6,9,10,12,17,18,20,24...
- There are two patterns we find out
1.2**i+1 [2+1,4+1,8+1,...]
2.The number + 2**j [5+2**0=6][9+2**0=10,10+2**1=12]..
Here j is respect to i-1.
Code logic:
- For loop for test case
- Input n
- sum=0,i=1,mod=1000000007
- while n!=0
- Inside while n--
- then apply two patterns orderly x=2**i+1
- n--
- Inside while give another pattern x=x+2**j inside for loop
- Also check base condition x%mod
- Inside for loop check n!=0
- n--
- End of the while loop i+=1
- Finally print the result
Program:
Comments
Post a Comment