CSES Solutions - Collecting Numbers
Last Updated :
26 Mar, 2024
You are given an array arr[] that contains each number between 1 ... N exactly once. Your task is to collect the numbers from 1 to N in increasing order. On each round, you go through the array from left to right and collect as many numbers as possible. What will be the total number of rounds?
Examples:
Input: N = 5, arr[] = {4, 2, 1, 5, 3}
Output: 3
Explanation:
- In the first round, we collect {1}.
- In the second round, we collect {2, 3}.
- In the third round, we collect {4, 5}.
Input: N = 4, arr[] = {2, 1, 4, 3}
Output: 3
Explanation:
- In the first round, we collect {1}.
- In the second round, we collect {2, 3}.
- In the third round, we collect {4}.
Approach: To solve the problem, follow the below idea:
The problem states that we have to collect numbers from 1 to N in increasing order. So, it means that we have to collect numbers strictly in the order: 1, 2, 3, 4 .... N. We cannot collect 1 and 3 in one round and then 2 in the next round. In other words, for every number X > 1, we cannot collect X if (X - 1) hasn't been collected yet.
The idea to solve the problem is: If the number X occurs before X + 1 then we can collect both X and X + 1 in a single round. Otherwise, if X comes after X + 1 then we cannot take them in the single round hence we add 1 to the final answer. Initially, we store the indices of all the elements. Now, for every number X from 1 to N - 1, check if X + 1 occurs before or after X in the array arr[]. If X + 1 occurs before X, we can increment the answer by 1.
Step-by-step algorithm:
- Maintain an array indices[] of size N + 1 to store the index of all the elements.
- Declare a variable ans to store the total number of rounds.
- For every number num from 1 to N - 1, check if (num + 1) has index lesser or greater.
- If the index of (num + 1) is less than index of num, this means that they both will be collected in different rounds, so we increment ans by 1.
- After comparing all the numbers, return the final answer as ans.
Below is the implementation of the algorithm:
C++
#include <iostream>
#include <vector>
using namespace std;
// Function to find the number of rounds
long solve(long arr[], int N) {
// Variable to store the final answer
long ans = 1;
// Array to store the index of numbers from 1 to N
vector<long> indices(N + 1);
// Store the index of all elements of arr[]
for (int i = 0; i < N; i++) {
indices[arr[i]] = i;
}
// If num occurs after (num + 1), increment ans by 1
for (int num = 1; num < N; num++) {
if (indices[num + 1] < indices[num])
ans++;
}
return ans;
}
int main() {
// Sample Input
int N = 4;
long arr[] = { 2, 1, 4, 3 };
cout << solve(arr, N) << endl;
return 0;
}
Java
import java.util.Arrays;
public class NumberOfRounds {
// Function to find the number of rounds
static long solve(long[] arr, int N)
{
// Variable to store the final answer
long ans = 1;
// Array to store the index of numbers from 1 to N
long[] indices = new long[N + 1];
// Store the index of all elements of arr[]
for (int i = 0; i < N; i++) {
indices[(int)arr[i]] = i;
}
// If num occurs after (num + 1), increment ans by 1
for (int num = 1; num < N; num++) {
if (indices[num + 1] < indices[num])
ans++;
}
return ans;
}
public static void main(String[] args)
{
// Sample Input
int N = 4;
long[] arr = { 2, 1, 4, 3 };
System.out.println(solve(arr, N));
}
}
// This code is contributed by rambabuguphka
C#
using System;
public class Program
{
// Function to find the number of rounds
static long Solve(long[] arr, int N)
{
// Variable to store the final answer
long ans = 1;
// Array to store the index of numbers from 1 to N
long[] indices = new long[N + 1];
// Store the index of all elements of arr[]
for (int i = 0; i < N; i++)
{
indices[arr[i]] = i;
}
// If num occurs after (num + 1), increment ans by 1
for (int num = 1; num < N; num++)
{
if (indices[num + 1] < indices[num])
ans++;
}
return ans;
}
public static void Main()
{
// Sample Input
int N = 4;
long[] arr = { 2, 1, 4, 3 };
Console.WriteLine(Solve(arr, N));
}
}
// This code is contributed by shivamgupta0987654321
JavaScript
// function to find the number of rounds
function solve(arr) {
// Variable to store the final answer
let ans = 1;
// Array to store the index of numbers from 1 to N
const indices = new Array(arr.length + 1);
// Store the index of all elements of arr[]
for (let i = 0; i < arr.length; i++) {
indices[arr[i]] = i;
}
// If num occurs after (num + 1), increment ans by 1
for (let num = 1; num < arr.length; num++) {
if (indices[num + 1] < indices[num]) {
ans++;
}
}
return ans;
}
// Sample Input
const arr = [2, 1, 4, 3];
console.log(solve(arr));
// This code is contributed by Ayush Mishra
Python3
# function to find the number of rounds
def solve(arr, N):
# Variable to store the final answer
ans = 1
# Array to store the index of numbers from 1 to N
indices = [0] * (N + 1)
# Store the index of all elements of arr[]
for i in range(N):
indices[arr[i]] = i
# If num occurs after (num + 1), increment ans by 1
for num in range(1, N):
if indices[num + 1] < indices[num]:
ans += 1
return ans
# Sample Input
N = 4
arr = [2, 1, 4, 3]
print(solve(arr, N))
Time Complexity: O(N), where N is the size of input array arr[].
Auxiliary Space: O(N)
Similar Reads
CSES Solutions - Coin Combinations I Consider a money system consisting of N coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ways you can produce a money sum X using the available coins. Examples: Input: N = 3, X = 9, coins[] = {2, 3, 5}Output: 8Explanation: There are 8 number of ways to
8 min read
Are all whole numbers counting numbers? The method to represent and work with numbers is known as the number system. A number system is a system of writing to represent numbers. It is the mathematical notation used to represent numbers of a given set by using digits or other symbols. It allows us to operate arithmetic operations such as d
4 min read
CSES Solutions - Two Sets Given N numbers 1,2,3,... N. Your task is to divide the numbers into two sets of equal sum. Print "YES", if the division is possible, and "NO" otherwise. If the division is possible, print how to create the sets. First, print the number of elements in the first set followed by the elements themselve
10 min read
CSES Problem Set Solutions In this article, we have compiled comprehensive, high-quality tutorials on the CSES Problem Set Solutions to assist you in understanding the problem set for learning algorithmic programming. What is CSES Problem Set?CSES Problem Set is a collection of competitive programming tasks hosted on the CSES
8 min read
CSES Solutions - Money Sums You have N coins with certain values. Your task is to find all money sums you can create using these coins. Examples: Input: N = 4, coins[] = {4, 2, 5, 2}Output: 92 4 5 6 7 8 9 11 13Explanation: To create sum = 2, we can use the coin with value = 2.To create sum = 4, we can use both the coins with v
8 min read
Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c Given a, b and c which are part of the equation x = b * ( sumdigits(x) ^ a ) + c.Where sumdigits(x) determines the sum of all digits of the number x. The task is to find out all integer solutions for x that satisfy the equation and print them in increasing order. Given that, 1<=x<=109Examples:
9 min read