You are viewing a single comment's thread. Return to all comments →
I am implementing same approach but it gives TLE
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int a0 = 0; a0 < t; a0++){ int n = in.nextInt(); int k = in.nextInt(); boolean flag = false; boolean[] b = new boolean[n+1]; String ans = ""; for(int i = 1; i <= n; i++){ int x = i + k; int y = i - k; if(y > 0 && y <= n && b[y] == false){ ans += y + " "; b[y] = true; }else if(x > 0 && x <= n && b[x] == false){ ans += x + " "; b[x] = true; }else{ flag = true; break; } } if(!flag){ System.out.println(ans); }else{ System.out.println(-1); } } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
I am implementing same approach but it gives TLE