Skip to content

Commit c035572

Browse files
Merge pull request #153 from sachin4429/master
Make it Divisible (Java)
2 parents 2f5f578 + dd8f64f commit c035572

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

CodeChef/MakeitDivisible.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Given an integer N, help Chef in finding an N-digit odd positive integer X such that X is divisible by 3 but not by 9.
3+
4+
Note: There should not be any leading zeroes in X. In other words, 003 is not a valid 3-digit odd positive integer.
5+
6+
Input Format
7+
The first line of input contains a single integer T, denoting the number of testcases. The description of the T testcases follows.
8+
The first and only line of each test case contains a single integer N, denoting the number of digits in X.
9+
Output Format
10+
For each testcase, output a single line containing an N-digit odd positive integer X in decimal number system, such that X is divisible by 3 but not by 9.
11+
12+
Constraints
13+
1?T?500
14+
1?N?104
15+
The sum of N over all test cases does not exceed 105
16+
Sample Input 1
17+
3
18+
1
19+
2
20+
3
21+
Sample Output 1
22+
3
23+
15
24+
123
25+
*/
26+
27+
/* package codechef; // don't place package name! */
28+
29+
import java.util.*;
30+
import java.lang.*;
31+
import java.io.*;
32+
33+
/* Name of the class has to be "Main" only if the class is public. */
34+
class MakeitDivisible
35+
{
36+
public static void main (String[] args) throws java.lang.Exception
37+
{
38+
// your code goes here
39+
Scanner in = new Scanner(System.in); //Scanner for taking input
40+
int t = in.nextInt(); //variable t for number of test case
41+
while(t>0) //Loop for each test case
42+
{
43+
int n = in.nextInt(); //integer N, denoting the number of digits in X.
44+
if(n==1) //checking if n is 1
45+
{
46+
System.out.println(3); // then printing the value of 1.
47+
}
48+
else //if not 1 (else part)
49+
{
50+
int arr[] = new int[n]; // Array arr of int type.
51+
arr[0] = 3; // Setting the value of 1st index of Array to 3.
52+
arr[n-1] = 3; //Setting the value of last index of Array to 3.
53+
System.out.println(Arrays.toString(arr).replaceAll("[\\[\\], ]", "")); //Converting array to sting and then replcing all special characters and printing it.
54+
}
55+
t--; // decrementing the value of t.
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)