DSA Series Episode 1 Contest Editorial by Codechef JUIT

Truth and Lie

Practice

Author: Utkarsh Bhatnagar

DIFFICULTY:

Cakewalk

PROBLEM:

Alice, Bob and Chef are playing a game of truth and lie. Alice and Bob each speaks a statement and the Chef try to find if they are speaking the truth or they are lying.

But Chef is only able to detect the lie when they both have lied together.

Given two integers as input each represents the state(truth or lie) of the statement of Alice and Bob.

0 represent False

1 represent Truth

EXPLANATION:

Possible combinations are 0 0, 0 1, 1 0, 1 1.
When 0 0 then output will be DETECTED otherwise UNDETECTED.

SOLUTION:

Setter's Solution 1(C++)
#include<bits/stdc++.h>
#define ll long long int
#define endl "\n"
using namespace std;
 
int main()
{   
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll test;
    int a, b;
    cin >> a >> b;
    if(a || b)cout << "UNDETECTED";
    else cout << "DETECTED";
 
return 0;
}

Square and Add

Practice

Author: Utkarsh Bhatnagar

DIFFICULTY:

Cakewalk

PROBLEM:

On Chef Birthday his father gifted him a list of numbers but he is not happy with it. He wants to know the summation of the square of all the numbers in the list.

EXPLANATION:

Take the numbers as input in an array, square each one of them and add them to the sum.

SOLUTION:

Setter's Solution 1(C++)
#include<bits/stdc++.h>
#define ll long long int
#define endl "\n"
using namespace std;
 
int main()
{   
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll test;
    ll i, j, k, n, temp, count = 0, ans = 0, sum = 0;
    cin >> n;
    ll arr[n];
    
    for(i = 0; i < n; i++)
    {
        cin >> arr[i];
        ans += arr[i] * arr[i];
    }
    cout << ans << "\n";
 
return 0;
}

AB Marks

Practice

Author: Mudit Mahajan

DIFFICULTY:

Cakewalk

PROBLEM:

There is an exam in which a correct answer given you 4 marks while a wrong answer gives you -1 mark.

You are given the number of questions that Alice and Bob answered correctly, as well as the number of problems they answered wrongly.

Your task is to find who among Alice and Bob got higher marks.

If both of them got the same marks, print Tie.

EXPLANATION:

We just need to find the marks obtained by both Alice and Bob, which can be calculated by finding the marks obtained from correct answers by multiplying it by 4 and then subtracting the marks from the wrong answers.

After that, just compare the two and print the output.

SOLUTION:

Setter's Solution 1(C++)
#include "bits/stdc++.h"
using namespace std;
 
int main()
{
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int alice = a * 4 - b;
    int bob = c * 4 - d;
    if(alice > bob)cout << "Alice\n";
    else if(alice < bob)cout << "Bob\n";
    else cout << "Tie" << "\n";
    return 0;
}

This is Easy

Practice

Author: Mudit Mahajan

DIFFICULTY:

Cakewalk

PROBLEM:

Chef has an integer N.

He wants to know if it can be divided by either 3 or 7 or both.

Answer for T test cases.

EXPLANATION:

We just need to check if the number is divisible 3 or 7, which can be checked by using modulous operator.

SOLUTION:

Setter's Solution 1(C++)
#include <bits/stdc++.h>
#define ll long long int
#define endl "\n"
using namespace std;
 
int main()
{   
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll test;
    cin >> test;
    while(test--)
    {
        ll i, j, k, n, temp, count = 0, ans = 0, sum = 0;
        cin >> n;
        if(n % 3 == 0 || n % 7 == 0) cout << "YES" << endl;
        else cout << "NO" << endl;
 
    }
 
return 0;
}

Special Sum

Practice

Author: Mudit Mahajan

DIFFICULTY:

Easy

PROBLEM:

Chef has an integer N.

He wants to find the special sum of the digits of the number.

A special sum is such that the odd digits of the number are added to the sum and the even digits of the number are subtracted from the sum.

Help him to find the special sum of digits of the number.

Answer for T test cases.

EXPLANATION:

The problem is exactly the same as finding the digits of the number, except we also need to check the parity of each digit of the nummber.

We can find the digit one by one by using the modulous operator(%) on the number and dividing it at the same time by 10.

Then, we only need to check if the digit is even or odd and add or subtract it to the sum accordingly.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
#define ll long long int
#define endl "\n"
using namespace std;
 
int main()
{   
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll test;
    cin >> test;
    while(test--)
    {
        ll i, j, k, n, temp, count = 0, ans = 0, sum = 0;
        cin >> n;
        
        while(n)
        {
            temp = n % 10;
            if(temp % 2)
            {
                ans += temp;
            }
            else
            {
                ans -= temp;
            }
            n /= 10;
        }
        cout << ans << endl;
    }
 
return 0;
}

Reverse Factorial

Practice

Author: Mudit Mahajan

DIFFICULTY:

Easy-Medium

PROBLEM:

Chef has an integer N.

He knows that the factorial of a number a is a * (a - 1) * (a - 2) * . . . * 2 * 1.

Instead of finding the factorial of the interge N that he has, he wants to find if there exists a number M such that the factorial of M is N.

Help him find if there is a number M such that M! = N.

Answer for T test cases.

EXPLANATION:

According to the constraints, the maximum value that can be given in input is 10^9.

Now, 12! = 479001600, which is greater than 10^9.

Therfore, the maximum value would be 11!, which can be brute-forced easily.

In the brute force, we will check while the factorial is less than the input value and check it against the given number.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
#define ll long long int
#define endl "\n"
using namespace std;
 
bool solve(ll a) {
    ll n = 1;
    ll i = 1;
    while(n < a and i++) {
        n *= i;
    }
    if(n == a)return true;
    else return false;
}
 
int main()
{   
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll test;
    cin >> test;
    while(test--)
    {
        ll i, j, k, n, temp, count = 0, ans = 0, sum = 0;
        cin >> n;
        if(solve(n))cout << "YES" << endl;
        else cout << "NO" << endl;
    }
 
return 0;
}

Strictly

Practice

Author: Mudit Mahajan

DIFFICULTY:

Easy

PROBLEM:

Chef has an array A of n positive integers and he wants the array sequence to be strictly increasing.

In one operation, Chef can increase the value of one element of the array by 1.

Since Chef is an idiot, he wants your help to find the minimum number of operations that he will need to perform to make the array strictly increasing sequence.

Answer for T test cases.

Note: The input may not fit into int_32 type, so use an appropriate data type in your language.

EXPLANATION:

For a strictly increasing sequence a[i] < a[i+1].

If lets say a[i] > a[i+1] so to make this sequence strictly increasing , difference between a[i] and a[i+1] is diff=a[i]-a[i+1].

So add diff to a[i+1] then it becomes strictly increasing, repeat same for each index.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define    endl    "\n"

int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n; cin >> n;
        int a[n];
        for (int i = 0; i < n; i++)
            cin >> a[i];
        ll ans = 0;
        for (int i = 1; i < n; i++) {
            if (a[i - 1] >= a[i]) {
                ans += (a[i - 1] - a[i]) + 1;
                a[i] += (a[i - 1] - a[i]) + 1;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Palindromic

Practice

Author: Mudit Mahajan

DIFFICULTY:

Easy

PROBLEM:

Chef has a string S and he wants you to find if it is possible to make the given string a palindrome or not.

Example:“aabb” given string can be converted into palindrome -“abba” but the string “abcc” can not be converted into any palindrome.

Answer for T test cases.

EXPLANATION:

Palindromic String can be of even length or an odd length. So all types of palindromic string are of :
1- abba or baab
2 - aba

Case 1: Length of String is even.
"abba" when the length of a string is even then the frequency of each character will also be even.

Case 2: Length of the string is odd.
"aba" or "abbba" or "abcba" in all these, there is one character whose frequency is odd all other character frequency is even.

We take note of these facts.
So the frequency of only one character can be odd, if there is more than one character with odd frequency then it cannot be rearranged into a palindrome.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
using namespace std;

int main()
{
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int n = s.size();
    int a[26] = {0};

    for (int i = 0; i < n; i++) {
      a[s[i] - 'a']++;
    }

    int odd_element = 0;

    for (int i = 0; i < 26; i++) {
      if (a[i] % 2 != 0) odd_element++;
    }

    if (odd_element > 1)
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  }
  return 0;
}