Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

PBT | DAILY TEST | 17 JUNE

 PBT PROGRAMMING 

** RUN THE 3RD PROGRAM 2 TIMES TO GET THE DESIRED OUTPUT **

black and gray laptop displaying codes

PROBLEM 1

Problem Statement

Let's consider all integers in the range from 1 to n (inclusive).

Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor

of integers in pair. Formally, find the maximum value of gcd(a,b), where 1≤a

The greatest common divisor, gcd(a,b), of two positive integers a and b is the biggest integer that is a

divisor of both a and b.

Input Format

The first line contains a single integer t (1≤t≤100) — the number of test cases. The description of the

test cases follows.

The only line of each test case contains a single integer n

Constraints

(2≤n≤10^6).

Output Format

For each test case, output the maximum value of gcd(a,b) among all 1≤a

In the first test case, gcd(1,2)=gcd(2,3)=gcd(1,3)=1.


SOLUTION :

C++ CODE :

#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
    int t;
    cin >> t;
    
    while(t--) {
        int n;
        cin >> n;
        cout << n / 2 << endl;
    }
    
    return 0;
}

 

PROBLEM 2 

Problem Statement
Given two numbers A and B. The task is to find out their LCM and GCD.
Input Format
Single line of input contains two space separated integers A and B
Constraints
1 <= A,B <= 10^18
Output Format
A single line of output contains two space separated integers, first one refers to the LCM and second
one refers to the GCD. 

 

SOLUTION :

C CODE : 

#include <stdio.h>
int main() {
   int a,b;
   scanf("%d %d",&a,&b);
   int gcd,lcm,n,d,r;
   if(a>b)
   {
      n=a;
      d=b;
   }
   else
   {
     n=b;
     d=a;
   }
   r=n%d;
   while(r!=0)
   {
     n=d;
     d=r;
     r=n%d;
   }
   gcd=d;
   lcm=a*b/gcd;
   printf("%d %d",lcm,gcd);
   return 0;
}


PROBLEM 3 

Problem Statement
Today the kindergarten has a new group of n kids who need to be seated at the dinner table. The chairs
at the table are numbered from 1 to 4n. Two kids can't sit on the same chair. It is known that two kids
who sit on chairs with numbers a and b (a≠b) will indulge if:
1.gcd(a,b)=1 or,
2.a divides b or b divides a.
gcd(a,b) — the maximum number x such that a is divisible by x and b is divisible by x.
For example, if n=3 and the kids sit on chairs with numbers 2, 3, 4, then they will indulge since 4 is
divided by 2 and gcd(2,3)=1. If kids sit on chairs with numbers 4, 6, 10, then they will not indulge.
The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no 2 of
the kid that can indulge. More formally, she wants no pair of chairs a and b that the kids occupy to fulfill
the condition above.
Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem.
Input Format
The first line contains one integer t — the number of test cases. Then t test cases follow.
Each test case consists of one line containing an integer n — the number of kids.
Constraints
(1≤t≤100) (1≤n≤100)
Output Format
Output t lines, which contain n distinct integers from 1 to 4n — the numbers of chairs that the kids
should occupy in the corresponding test case. If there are multiple answers, print any of them. You can
print n numbers in any order.

 

SOLUTION 

C CODE  

#include <stdio.h>
int main() {
   int n;
   scanf("%d",&n);
   while(n--)
   {
     int a,c=0;
     scanf("%d",&a);
     int x=a*4;
     for(int i=x;i>=0;i=i-2)
     {
        if(c==a)
          break;
        else
        {
          printf("%d ",i);
          c++;
        }
     }
    printf("\n");
    
   }
   return 0;
}

 

*** RUN THE 3 RD PROGRAM 2 TIMES ***

 

 

Post a Comment

Previous Post Next Post