Table of contents
1.
Introduction
1.1.
Problem Statement
2.
Solution
3.
Complexity Analysis
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024

Minimum Gifts

Author Akash Nagpal
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we’ll write a program to find out the minimum number of gifts required by a company. This coding problem is one of the TCS Codevita's previous year's questions. 


Now let us understand the problem statement clearly.

Problem Statement

A company has decided to offer all of its staff presents. Each employee has a rank in the company as a result of this. The company has established specific regulations for the distribution of gifts based on that level.

The following are the rules for gift distribution:

  • At least one gift must be given to each employee.
  • Employees with higher ranks receive more gifts than their coworkers.

What is the minimum number of gifts required by the company?

Constraints

1 < T < 10

1 < N < 100000

1 < Rank < 10^9

Input

The First line contains integer T, denoting the number of test cases.

For each test case:

First-line contains integer N, denoting the number of employees.

The Second-line contains N space-separated integers, denoting the rank of each employee.

Output

For every test case, print the number of minimum gifts required by the company on a new line.

Now let us understand the statement with the help of an example.

Input

2
5
1 2 1 5 2
2
1 2

Output

7
3

Explanation

For test case 1, adhering to the rules mentioned above,

Employee - 1 whose rank is 1 gets one gift

Employee - 2 whose rank is 2 gets two gifts

Employee - 3 whose rank is 1 gets one gift

Employee - 4 whose rank is 5 gets two gifts

Employee - 5 whose rank is 2 gets one gift

Therefore, total gifts required is 1 + 2 + 1 + 2 + 1 = 7

Similarly, for testcase 2, adhering to rules mentioned above,

Employee - 1 whose rank is 1 gets one gift

Employee - 2 whose rank is 2 gets two gifts

Therefore, total gifts required is 1 + 2 = 3

Solution

Java Solution

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T-- > 0) {
            int n = sc.nextInt();
            int[] ar = new int[n];
            for (int i = 0; i < n; i++)
                ar[i] = sc.nextInt();
            int[] gifts = new int[n];
            gifts[0] = 1;
            // Left to Right Neighbors
            for (int i = 1; i < n; i++) {
                if (ar[i] > ar[i - 1])
                   gifts[i] = gifts[i - 1] + 1;
                else
                    gifts[i] = 1;
            }
            // Right to Left Neighbors
            for (int i = n - 2; i >= 0; i--) {
                if (ar[i] > ar[i + 1] && gifts[i] <= gifts[i + 1])
                    gifts[i] = gifts[i + 1] + 1;
            }
            long total = 0;
            for (int gift : gifts)
                total += gift;
            System.out.println(total);
        }
    }
}

Output

2
5
1 2 1 5 2
2
1 2
7
3

 

C++ Solution

#include<bits/stdc++.h>
using namespace std;
long long arr[100010];
long long brr[100010];
int main()
{
  int t;
  cin >> t;
  for(int i = 1; i <= t; i++)
  {
    int n;
    long long gift_num = 0, temp = 0;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
      cin >> arr[i];
    }
    brr[0] = 1;
    for(int i = 1; i < n; i++)
    {
      if(arr[i] > arr[i-1])
      {
        brr[i] = brr[i-1] + 1;
      }
      else
      {
        brr[i] = 1;
      }
    }
   gift_num = brr[n-1];
    for(int i = n-2; i >= 0; i--)
    {
      if(arr[i] > arr[i+1])
      {
        temp = brr[i+1] + 1;
      }
      else
        temp = 1;
      gift_num = gift_num + max(temp, brr[i]);
      brr[i] = temp;
    }
    cout << gift_num << endl;
  }
  return 0 ;
}
You can also try this code with Online C++ Compiler
Run Code


Output

2
5
1 2 1 5 2
2
1 2
7
3
You can also try this code with Online C++ Compiler
Run Code

Complexity Analysis

  • Time Complexity: We have solved the given coding problem in O(N) time complexity.
  • Space Complexity: O(N) will be the space complexity because an extra array is used.

Frequently Asked Questions

  1. Why do we use import Java util * in Java?
    It involves bringing all the classes and interfaces to java.util package into the current class or interface and make them available for usage. This shortcut wildcard annotation is for importing all classes included within a certain package.
  2. What are exceptions in Java?
    Exceptions in Java are undesirable errors, flaws, or occurrences that prevent a programme from running normally. When an exception occurs, the program's execution is interrupted. On the screen, there appears an error message.

Conclusion

In this article, we have extensively discussed the TCS Codevita interview questions on Minimizing the number of gifts and their implementation in C++ and Java languages. We tried to cover it with the help of some examples.

Read more about Recursion here.

You can look at more exciting blogs and articles curated by the coding ninja's team by visiting Library.

We hope that this blog has helped you enhance your knowledge regarding minimizing the gifts and other similar types of problems and, if you would like to learn more, you can try to solve Codevita's Count Pairs Problem

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass