CodeFriday-3 JUIT Solution

Is Even Odd

Practice

Author: Mudit Mahajan, Devansh Goel

DIFFICULTY:

Simple

PROBLEM:

For given n integers, check if they are even or odd.

If number a is even, output “EVEN”, else, “ODD” (without quotes).

EXPLANATION:

If a number is divisible by 2, then it is even otherwise odd.

SOLUTION:

Setter's Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define endl "\n"
 
int main()
{
        ll i, n, ans = 0;
        cin >> n;
        while(n--)
        {
                cin >> i;
                if(i % 2 == 0)
                {
                        cout << "EVEN" << endl;
                }
                else
                {
                        cout << "ODD" << endl;
                }
        }
return 0;
}

Long Words

Practice

Author: Mudit Mahajan, Devansh Goel

DIFFICULTY:

Simple

PROBLEM:

Bob hates writing very long words like “abbreviated” or “eagernesses”.

Bob considers a word too long if it has strictly more than 10 letters.

Bob creates abbreviations of a too long word by the following rule:

The too long word is replaced by the first and the last letter of the word with the number of letters between the first and last letter, i.e. the number of the letters except the first and the last letter, written in between the two.

For example, “abbreviated” will become “a9d” and “eagernesses” as “e9s”.

If the length of the word is less than or equal to 10, then the word remains unchanged.

Bob wants you to automate this process for some n words.

EXPLANATION:

Get the size of the string. After that, check if the size is greater than 10- if yes then print first_char+(size-2)+last_char.

SOLUTION:

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--)
    {
        int i, j, k, n, temp, count = 0, ans = 0, sum = 0;
        string s;
        cin >> s;

        n = s.size();

        if(n > 10)
        {
            cout << s[0] << n - 2 << s[n - 1] << endl;
        }
        else  
        {
            cout << s << endl;
        }
    }

return 0;
}

Two Product

Practice

Author: Mudit Mahajan, Devansh Goel

DIFFICULTY:

Simple

PROBLEM:

Chef is given a number N.

He wants to find out if it can be represented as the product of two numbers between 1 and 9 (inclusive).

For example, 20 can be represented as the product of 4 and 5.

If the number N can be represented as the product of two numbers between 1 and 9(inclusive), then print “YES” otherwise “NO” (without quotes).

EXPLANATION:

Compare the number N with the product of numbers 1-9. If N is equal to one of them, then print “YES” else “NO”.

SOLUTION:

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

int main()
{
 	int n;
 	cin >> n;
  	int i, j;
  	for(i = 1; i < 10; i++)
    
     	for(j = 1; j < 10; j++)
        
         	if(i * j == n)
            {
              cout << "Yes" << endl;
              return 0;
            }
  
  	
     	cout << "No" << endl; 
    
return 0;
}

Is Prime

Practice

Author: Mudit Mahajan, Devansh Goel

DIFFICULTY:

Simple

PROBLEM:

For given n integers, check if they are prime or not.

If number a is prime, output “YES”, else, “NO” (without quotes).

EXPLANATION:

If a number is less than or equal to 1, then it is not prime. For a number, check if it is not divisible by 2 to (number-1), then it is a prime number.

SOLUTION:

Setter's Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define endl "\n"
 
bool isPrime(ll n) 
{ 
    if (n <= 1) 
        return false; 
  
    for (ll i = 2; i < n; i++) 
        if (n % i == 0) 
            return false; 
  
    return true; 
}

int main()
{
        ll i, n, a;
        cin >> n;
        while(n--)
        {
                cin >> a;
                if(isPrime(a)) cout << "YES" << endl;
                else
                        cout << "NO" << endl;
        }
return 0;
}

Money Change

Practice

Author: Mudit Mahajan

DIFFICULTY:

Simple

PROBLEM:

Chef will buy a present for his friend for Rs. N at a shop.

Chef only has Rs. 1000 bills to pay the price.

He would like to know how much change he would get after using only
Rs. 1000 bills to pay the price for the present.

Help him to find the change he will receive.

EXPLANATION:

We can find the change easily by using the modulus (%) operator.

Suppose M = N % 1000.

If the value of M is zero, then no change will be given as the amount
to pay is a a multiple of 1000.

Otherwise to pay the rest of amount another Rs. 1000 bill will be
used and the change will be equal to (1000 - M).

SOLUTION:

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

    int main()
    {   
      int n;
      cin >> n;
      int m = n % 1000;
      if(m == 0)
      {
        cout << 0 << endl; 
      }
      else
      {
        cout << 1000 - m << endl; 
      }
      
      return 0;
    }

New String

Practice

Author: Devansh Goel

DIFFICULTY:

Simple

PROBLEM:

Chef has two strings A and B both of length n consisting of lowercase English letters.

He wants to create a new string S of length 2n as per the following rule:

The first letter of S contains the first letter of A, the second letter of S contains the first letter of B, and so on.

In other words:

S = A_1 B_1 A_2 B_2A_n B_n.

Help Chef find the new string S.

EXPLANATION:

Print the characters of the two strings in alternate fashion.

SOLUTION:

Setter's Solution
#include<stdio.h>

int main()
{
    //input the length of the string
    int n=0;
    scanf("%d",&n);

    //input the two strings
    char a[n],b[n];
    scanf("%s %s",a,b);

    int i=0,flag=0,j=0,k=0;
    //print the new string
    for(i=0;i<2*n;i++)
    {
        //print the character of the 1st string
        if(flag==0)
        {
            printf("%c",a[j]);
            j++;
            flag=1;
        }
        //print the character of the 2nd string
        else{
            printf("%c",b[k]);
            k++;
            flag=0;
        }
    }
}

Alice House

Practice

Author: Mudit Mahajan, Devansh Goel

DIFFICULTY:

Easy

PROBLEM:

Alice decided to visit her friend’s house one evening.

Alice’s house is located at point 0 and her friend’s house is located at point X (X > 0).

Alice has a special power because of which in one step, she can move 1, 2, 3, 4, or 5 steps forward.

Alice wants to find the minimum number of steps she will need to take to reach her friend’s house.

EXPLANATION:

First, found out the maximum no. of steps of size 5 possible. Then, distance left will be less than 5.

SOLUTION:

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;

    ll i, j, k, n, temp, count = 0, ans = 0, sum = 0;
    cin >> n;
    ans = n / 5; 
    if(n % 5 != 0)
    {
        ans++;
    }
    cout << ans << endl;

return 0;
}

Make All Odd

Practice

Author: Mudit Mahajan, Devansh Goel

DIFFICULTY:

Easy

PROBLEM:

Chef wants to gift his friend an array a of n integers as a birthday present.

But Chef knows that his friend does not like even numbers so he wants to make sure that there are no even numbers in the array.

In one operation, Chef will take two elements a_i and a_j (ij), replace a_i with (a_i + a_j).

Help find Chef find the minimum number of operations he needs to perform or determine if it is not possible.

If all the elements of the array are already odd, then the number of operations will be zero.

Answer for q queries.

EXPLANATION:

If all the elements of the array are even then it is not possible to gift the array. It is because sum of two even numbers is always even. Consider the case when one or more elements are odd. Then, we can simply add the even number with one odd number to get the odd number. Or, in other words no. of operations required to get odd array will be equal to no. of even elements in the array when at least one odd number is present in the array.

SOLUTION:

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;
        ll arr[n];
        
        for(i = 0; i < n; i++)
        {
            cin >> arr[i];
            if(arr[i] % 2 == 0)
            {
                count++;
            }
        }

        if(count == n)
        {
            cout << -1 << endl;
        }
        else 
        {
            cout << count << endl;
        }
    }

return 0;
}

Increasing Array

Practice

Author: Mudit Mahajan, Devansh Goel

DIFFICULTY:

Easy

PROBLEM:

Chef is given an array a of n integers. He wants to change the array so that it becomes increasing, i.e., every element is at least as large as the previous element.

In one operation, Chef can choose any element and increment it by 1.

He can choose any element as many times as needed and apply the operation to it.

Help him find the minimum number of operations required.

EXPLANATION:

No. of operations required to increase an element so at least it is as large as the previous element will be the there difference. Follow the same criteria for all elements.

SOLUTION:

Setter's Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define endl "\n"
 
int main()
{
        ll i, n, ans = 0;
        cin >> n;
        ll arr[n];
        for(i = 0; i < n; i++)
        {
                cin >> arr[i];
        }
 
        for(i = 1; i < n; i++)
        {
                if(arr[i] < arr[i - 1])
                {
                        ans += (arr[i - 1] - arr[i]);
                        arr[i] = arr[i - 1];
                }
        }
        cout << ans << endl;
return 0;
}

Array on Paper

Practice

Author: Mudit Mahajan, Devansh Goel

DIFFICULTY:

Easy

PROBLEM:

Chef has an array a of n integers.

He arranged the array as follows on a paper:

The number a_1 on the leftmost side of the paper.
The number a_2 on the rightmost side of the paper.
The number a_3 on the left side of paper but right to a_1.
The number a_4 on the right side of paper but left to a_2.
He wrote the entire array following the above pattern.
In short, array a was written as:

a_1, a_3, a_5, …, a_6,a_4, a_2

For example, if a = [1, 2, 3, 4, 5, 6, 7], it will be written as:

[1, 3, 5, 7, 6, 4, 2]

Chef shows the paper to his friend and he wants him to find the original sequence a.

Help him find the original array a for t test cases.

EXPLANATION:

Starting half elements of the array are the elements at even indices of original array.

SOLUTION:

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;
        cin >> n ;
        ll arr[n], res[n];

        for(i = 0; i < n; i++)
        {
            cin >> arr[i];
        }

        if(n % 2 != 0)
        {
            k = (n + 1) / 2;
        }
        else
        {
            k = n / 2;
        }

        j = 0;
        for(i = 0; i < k; i++)
        {
            res[j] = arr[i];
            //cout << j << " " << res[j] << endl;
            j += 2;
            
        }

        j = 1;
        for(i = n - 1; i >= k; i--)
        {
            res[j] = arr[i];
            //cout << j << " " << res[j] << endl;
            j += 2;
        }

        for(i = 0; i < n; i++)
        {
            cout << res[i] << " ";
        }

        cout << endl;
    }

return 0;
}
2 Likes