# Find Nth item distributed from infinite items of infinite types based on given conditions

Given a positive integer N, the task is to find the Nth item given out when there are infinite items of the infinite number of types such that the items are given out in the following fashion:Day 1: 1 item of Type-I are given out.Day 2: 2 items of the Type-II and 1 item of Type-I are given out.Day 3: 3 items of the Type-III, 2 items of the Type-II, and 1 item of Type-I are given out.Day 4: 4 items of the Type-IV, 3 items of the Type-III, 2 items of the Type-II, and 1 item of Type-I are given out.and so on…Examples: Input: N = 10Output: 1Explanation:Following are the orders of the items given out:Day 1: 1 item of Type-I are given out. The sequence is {1}.Day 2: 2 items of the Type-II and 1 item of Type-I are given out. The sequence is {1, 2, 2, 1}.Day 3: 3 items of the Type-III, 2 items of the Type-II, and 1 item of Type-I are given out. The sequence is {1, 2, 2, 1, 3, 3, 3, 2, 2, 1}.From the above order of items removed, the Nth(= 10th) item given out is 1. Therefore, print 1.Input: N = 399Output: 11Approach: The simplest approach to solve the given problem is to keep the track of the number of days and the count of the number of items given on each day by following the given order and print that item which is given out at the Nth turn.Below is the implementation of the above approach:C++ #include using namespace std; int itemType(int n){ int count = 0; for (int day = 1;; day++) { for (int type = day; type > 0; type–) { count += type; if (count >= n) return type; } }} int main(){ int N = 10; cout 0; type–) { count += type; if (count >= n) return type; } }} public static void main (String[] args) { int N = 10; System.out.println( itemType(N)); }} Python3 def itemType(n): count = 0 day = 1 while(True): for type in range(day, 0, -1): count += type if (count >= n): return type N = 10 print(itemType(N)) C#using System; public class GFG{ static int itemType(int n) { int count = 0; for (int day = 1;; day++) { for (int type = day; type > 0; type–) { count += type; if (count >= n) return type; } } } static public void Main () { int N = 10; Console.WriteLine( itemType(N)); }} Output: 1Time Complexity: O(N)Auxiliary Space: O(1)Efficient Approach: The above approach can also be optimized by using the fact that on a given particular day D the number of items given out is the sum of numbers from 1 to D and the sum of numbers from day 1 to that day should be less than equal to N. Follow the steps below to solve the problem:Initialize the variables, say count as 0 to store the number of items given and day to store the number of items for that day.Iterate a loop until the value of (count + day*(day + 1))/2 is less than N and perform the following steps:Add the value of day*(day + 1)/2 to the variable count.Increase the value of the day by 1.Iterate over a range [day, 0] using the variable type and performing the following tasks:Add the value of type to the variable count.If the value of count is greater than equal to N, then print the value of type as the resultant answer.Below is the implementation of the above approach:C++ #include using namespace std; int itemType(int n){ int count = 0; int day = 1; while (count + day * (day + 1) / 2 < n) { count += day * (day + 1) / 2; day++; } for (int type = day; type > 0; type–) { count += type; if (count >= n) { return type; } }} int main(){ int N = 10; cout