Header Ads Widget

Candy in the box | hackerearth solution

 Problem

You have N boxes numbered 1 through N and K candies numbered 1 through K. You put the candies in the boxes in the following order:

  • first candy in the first box,
  • second candy in the second box,
  • ....... 
  • ........
  • so up to N-th candy in the Nth box,
  • the next candy in (N - 1)-th box,
  • the next candy in (N - 2)-th box
  • ........
  • .......
  • and so on up to the first box, 
  • then the next candy in the second box 
  •  ......    and so on until there is no candy left.

So you put the candies in the boxes in the following order: 1,2,3,....,,1,2,....,2,1,2,3,....,,1,....

Find the index of the box where you put the K-th candy.

Input format

  • The first line contains denoting the number of test cases. The description of T test cases is as follows:
  • Each test case consists of a single line containing two integers N, and K.

Output format

For each test case, print the index of the box where you put the K-th candy.

Constraints

110521091109

Sample Input
3
5 2
3 5
10 27
Sample Output
2
1
9
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first test case, you put the first candy in the first box and the second candy in the second box.

In the second test case, you put the five candies in the boxes with numbers 1, 2, 3, 2, and 1 respectively. So you put the fifth candy in the first box,

Code(C++):-

#include <bits/stdc++.h>
using namespace std;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T-- > 0) {
int N, K;
cin >> N >> K;
K = (K - 1) % ((N - 1) * 2);
int ans;
if (K < N - 1) {
ans = K;
} else {
//K -= N - 1;
ans = (N - 1) * 2 - K;
}
cout << (ans + 1) << '\n';
}
return 0;
}

Code(JAVA):-

import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.util.stream.Collectors;
public class Main {
InputStream is;
PrintWriter out;
String INPUT = "";
void run() throws Exception {
is = System.in; out = new PrintWriter(System.out);
solve(); out.flush(); out.close();
}
public static void main(String[] args) throws Exception {
new Main().run();
}
// ArrayList, Integer, println, print, HashMap, HashSet, Integer, Long, MAX_VALUE, MIN_VALUE, getKey, getValue, Entry, entrySet, key, contains, equals, containsKey, Priorityqueue, Queue, Deque, LinkedList, BigInteger, BigDecimal, Collections, sort, remove, set, get, map, charAt, Comparator, reverseOrder, getOrDefault, put, indexOf, lastIndexOf, finished, work, case, option, default, multiply, compareTo, valueOf, divide, add, remainder, BigInteger, BigDecimal, matrix, grid, path, List, alpha, beta, gamma, phi, lambda, group, sequence, delta, subtract, done;
public byte[] inbuf = new byte[1 << 16];
public int lenbuf = 0, ptrbuf = 0;
public int readByte() {
if (lenbuf == -1) {
throw new InputMismatchException();
}
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
public boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
public int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
public double nd() {
return Double.parseDouble(ns());
}
public char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private int ni() {
return (int) nl();
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
class Pair {
int first;
int second;
Pair(int a, int b) {
first = a;
second = b;
}
}
long[] nal(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
long mod = 1000000007;
void solve() {
int test_case = 1;
test_case = ni();
while (test_case-- > 0) {
go();
}
}
// WRITE CODE FROM HERE :-
void go() {
long n = nl(), k = nl();
if(n >= k) {
out.println(k);
}
else {
k -= n;
if(k % (n - 1) == 0) {
long p = k / (n - 1);
if(p % 2 == 0) out.println(n);
else out.println(1);
}
else {
long rotation = k / (n - 1) + 1;
if(rotation % 2 == 0) {
long h = k % (n - 1);
out.println(h + 1);
}
else {
long h = k % (n - 1);
out.println(n - h);
}
}
}
}
}




Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-

Post a Comment

0 Comments