SPOJ PALIN NZEC Error - java

I have used the following code for the question but it keeps giving me NZEC error when I submit it on SPOJ. My code runs perfectly on Eclipse or through cmd.
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
int line,num,numb,rev = 0,r,i,flag;
ArrayList <Integer> nums=new ArrayList <Integer>();
Scanner b=new Scanner(System.in);
Scanner a=new Scanner(System.in);
line=b.nextInt();
for(i=0;i<line;i++)
{flag=0;
num=a.nextInt();
num=num+1;
numb=num;
do {
while(numb>0)
{r=numb%10;
rev=(rev*10)+r;
numb=numb/10;
}
if (rev==num)
{nums.add(num);
rev=0;
flag=1;
break;
}
else
{num=num+1;
numb=num;
rev=0;}
}while(flag==0);
}
for (int newnum : nums)
{System.out.println(newnum);}
}
}

the mistake that you are doing is that you are storing num in an integer datatype..the constraint says that
K of not more than 1000000 digits(it is digits and not up to that number)
It is impossible for an integer to store such a huge number..so you have to use string to solve this particular problem..try this input in your eclipse
454646546546546546546546464646464646
you will realize where you are wrong

Related

ArrayList Scanner input help Java

I am trying to write a code where you sort a given list of numbers, and I'm trying to do so using ArrayList. I am using a while loop to allow for repeated inputs. This is my code:
import java.util.Scanner;
import java.util.ArrayList;
public class sortinggg {
public static Scanner keyboard = new Scanner(System.in);
public static ArrayList<Integer> number = new ArrayList<Integer>();
public static void main (String [] args) {
int count= 0;
System.out.println("Enter your numbers.");
while (keyboard.hasNextInt()); {
number.add(keyboard.nextInt());
}
The integer count is irrelevant right now, as I only use it when I am sorting the list.
The problem is that after I input my numbers, even if I type in a string (for example), the program doesn't move on to the next line of code. Am i missing anything here?
P.S. I tired looking up questions that have been asked previously on this topic, but none of the solutions suggested worked for me.Thank you for your help in advance!
Try this:
import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
ArrayList<Integer> number = new ArrayList<Integer>();
System.out.println("Enter your numbers:");
while (keyboard.hasNextInt()) {
number.add(keyboard.nextInt());
}
}
}
Modifications:
In while (keyboard.hasNextInt()); remove semicolon(;) at the end.
Here while loop will keep adding values to the arraylist until you provide int values.
In the first place the ; just after the while is wrong. It does not let you to execute the body. Then how are you going to skip the loop. You may ask the end user to enter some special value and use it to break the loop. The corrected version is given below.
public static void main(String[] args) {
int count = 0;
System.out.println("Enter your numbers or -1 to skip.");
while (keyboard.hasNextInt()) {
int num = keyboard.nextInt();
if (num == -1) {
break;
}
number.add(num);
}
System.out.println(number);
}

How do I loop nextInt to define variables in an array?

the question I've asked isn't too clear but essentially I have been tasked to:
Write a program to read in 5 integer numbers from the user. You
should store the numbers in an array.
Use loops for efficiency.
I would know how to do this if it weren't for loops, however I am unsure how to use loops for this certain task.
What I have is this (I know this is completely wrong I was just trying to base it on the example from the book).
import java.util.Arrays;
import java.util.Scanner;
public class fivenumbersloops {
public static void main(String[] args)throws Exception {
Scanner aids = new Scanner(System.in);
int[] numbers = new int[5];
do
{
System.out.println("Enter a whole number: ");
numbers[0] = aids.nextInt();
numbers[0]++;
}while (numbers[0]==numbers[5]);
}
}
I know it's the bottom bit which is completely wrong, but if someone could help point me in the right direction I would appreciate it.
What you want is a for loop. This for loop initializes the variable i to 0, continues while i is less than 5 and increments i for each iteration. The variable i is used to specify the position to place the input number into the array.
public static void main(String[] args) throws Exception {
Scanner aids = new Scanner(System.in);
int[] numbers = new int[5];
for (int i = 0; i < 5; i++) {
System.out.println("Enter a whole number: ");
numbers[i] = aids.nextInt();
}
}

How to solve this terminated due to timeout in Java

Can I get this problem of Terminated due to time out solved?
I mean how to reduce the complexity or unwanted code in order to solve the issue?
Here is my code:
public class Solution {
public static void main(String[] args) {
int i,n,hit,count=0,p=0,t,tmp,j;
int h[]=new int[100000];
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
hit=sc.nextInt();
t=sc.nextInt();
for(i=0;i<n;i++){
h[i]=sc.nextInt();
}
for(i=0;i<n;i++){
for(j=i;j<n;j++){
if(h[i]>h[j]){
tmp=h[i];
h[i]=h[j];
h[j]=tmp;
}
}
}
for(i=1;i<=t;i++){
h[p]-=hit;
if(h[p]<=0){
count++;
p++;
}
}
System.out.println(count);
}
}
Since I don't know the problem statement, the only thing which I can suggest is to always avoid Bubble Sort. It's complexity is O(n^2), and probably that's what hindering you're time requirement.
Use Arrays.sort like Arrays.sort(h)
The only problem I can see is you need to import the scanner before you can use it. When I ran the code you provided it gave me a timeout because it couldn't locate the scanner, but when I imported the scanner it ran just fine, so I'm assuming that's the same timeout error you're getting. Put this statement at the beginning of your code:
import java.util.Scanner;
The finished code should look like this:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
int i,n,hit,count=0,p=0,t,tmp,j;
int h[]=new int[100000];
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
hit=sc.nextInt();
t=sc.nextInt();
for(i=0;i<n;i++){
h[i]=sc.nextInt();
}
for(i=0;i<n;i++){
for(j=i;j<n;j++){
if(h[i]>h[j]){
tmp=h[i];
h[i]=h[j];
h[j]=tmp;
}
}
}
for(i=1;i<=t;i++){
h[p]-=hit;
if(h[p]<=0){
count++;
p++;
}
}
System.out.println(count);
}
}

"not a statement" error

im actually pretty new into java , my homework was to create a array .
After a while of searching for a good video i found this .
--
If i compile this blue j shows me that "int 1=0;" is not a statement .
Can someone tell me where the mistake is ?
Thanks for helping
import java.util.*;
class binarysearch
{
public static void main ()
{
Scanner sc= new Scanner (System.in);
int num[]= new int [] {23,34,45,56,67,78,89,90,12};
System.out.println("Enter the Number to search");
int n=sc.nextInt();
int u=num.length-1,mid=0;
int 1=0;
int c=0;
while(1<=u)
{
mid=(1+u)/2;
if(n<num[mid])
u=mid-1;
if(n>num[mid])
1=mid+1;
if(n==num[mid])
{
c=1;
break;
}
}
if(c==1)
System.out.println("search successful"+"\n"+"this number " +n+ "position" +(mid+1));
else
System.out.println("Number not found");
}
}
1 is an invalid variable name. With that declaration, you are basically telling Java to re-create math. Thus, int 1 = 0; is impossible to compile.
Consider restating the constructor:
int i = 0;

Substring during looping in java

I was trying a problem yesterday(well that's not the problem),but it repeatedly gave me error.
What I had to do was to take a substring,convert it itno int and just print it
but it gave error
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
Here is my implementation
import java.util.Scanner;
import java.io.*;
class gerald{
public static void main(String [] s)
{
long t2,t3,t,a,b,c;
Scanner sc=new Scanner(System.in);
t=sc.nextInt();
for(int i=1;i<=t;i++)
{
String time1=new String(sc.nextLine());
long t1=Integer.parseInt(time1.substring(0,2));
System.out.println(t1);
}
}
}
If I don't use loop,code works fine. Any thoughts?
You should check the length of time1 first before you execute time1.substring(0,2)
The best thing you can do is debug it. Another thing you can do is to handle anormal behaviour you can do it asking first with if-else like if(timer1.length() < 2) or using try-catch.
Let see with try-catch.
public static void main(String [] args){
int t,t1;
Scanner sc=new Scanner(System.in);
t=sc.nextInt();
String time1=null;
for(int i=1;i<=t;i++){
time1=sc.nextLine();
try{
t1=Integer.parseInt(time1.substring(0,2));
System.out.println(t1);
}catch(NumberFormatException e){
System.err.println("input string is not a number ---> "+time1);
}catch(StringIndexOutOfBoundsException e){
System.err.println("input string is shorter than required ---> "+time1);
}
}
}

Categories