Well in this problem, i am sharing, my solution runs in my compiler in 0.3 sec for the highest test case and it gave tle on cses compiler.
Well the same concept runs in cpp though, how can i optimize it?
Problem Link :- https://cses.fi/problemset/task/1635/
Code Link :- https://cses.fi/paste/e594003bb8e220534e1324/
Well, tell me pls, same problem on many questions regarding time.
#CSES TIME PROBLEM ON SAME WITH JAVA
1 messages ยท Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If your code is long, or you have multiple files to share, consider posting it on sites like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
I'm sorry this is not helpful at all but I just want to let you know that you have written the most C-like Java program I have ever seen. ๐

well yeah
I need to fix this for you. You've made my night. ๐
yayy, the same concept got ac in cpp, so well there should be a way to faster this code 
package com.example.application;
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
private static long MOD = 1000000007L;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = 1;
while (T-- > 0) {
int n = scanner.nextInt();
int k = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
long[] dp = new long[k + 1];
dp[0] = 1;
for(int i = 1; i <= k; i++) {
for(int j = 0; j < n; j++) {
if(i < arr[j]) {
continue;
}
dp[i] += dp[i - arr[j]];
dp[i] %= MOD;
}
}
System.out.println(dp[k]);
}
}
}
Detected code, here are some useful tools:
Formatted code
package com.example.application;
import java.util. * ;
import java.lang. * ;
import java.io. * ;
class Main {
private static long MOD = 1000000007L;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = 1;
while (T-- > 0) {
int n = scanner.nextInt();
int k = scanner.nextInt();
int [] arr = new int [n] ;
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
long [] dp = new long [k + 1] ;
dp[0] = 1;
for (int i = 1; i <= k; i++) {
for (int j = 0; j < n; j++) {
if (i < arr[j] ) {
continue ;
}
dp[i] += dp[i - arr[j] ] ;
dp[i] %= MOD;
}
}
System.out.println(dp[k] );
}
}
}
it got accepted?
I'm not sure it's untested.
dont tell me that
ohh lemme test
But you don't need to create a reader yourself, there's built in objects that read from stdin in Java
public final class Scanner
implements Iterator<String>, Closeable
A simple text scanner which can parse primitive types and strings using regular expressions.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
For example, this code allows a user to read a number from System.in:
yup but scanner is too slow, and bufferedreader sometimes gives tle in cp
so i prefer using reader
Okay so if you want to micro-optimize then
Have you ever heard of branchless programming?
naah
need to learn that, i guess if it helps me with the java time
Depending on how big the input is too, you might be able to take advantage of multi-threading
As for your algorithm itself; beats me.
You might also be able to hack the memory limit by using off heap memory
although idk how fast it would be
umm, read it but i dont think cses needs it, like CSES problem set is beginners educational problem sheet to be precise, ummm i guess i will do dynamic section with cpp only, but this is the first time i got tle in reader class, i am using from more than a year, why java is not fast bruhhh 
You could also probably get away with 1 loop for i and j, and just derive j from i with an equation
See the thing is, Java is not meant to be micro optimized like this. ๐
i can use that but logically thats the same thing 
anyone got any idea? pls help
same continues, with more dp problems, well i am doing now with cpp, but this is concerning, i cant do it whole with only cpp, if rest have same prob
Your task is to find all possible combinations within an array of numbers to make a target value
yeah
Hmm
I'm gonna give you sometime, you tell me if it's correct because I'm gonna guess this AF
public static void main(String args[]) {
int n = 9;
int[] arr = {2,3,5};
int total = 0;
for (int j : arr) {
total += (j % n);
}
total = (total - arr.length) + 1;
System.out.println(total);
}
If it's wrong, sorry
not tle, but giving wa
well, no need to sorry, i think cses should give more time for java
?
i mean, it's wrong
ah okay
well, i got the same problem today, and i observe that string is adding multiple times, i just sort it out, so i only add string minimum times, and it got accepted
i think i need to find a way to lower the times, just something in code which is taking too much time, hmmm
Try tu use a profiler