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

INFEED

HackwithINFY Practice Coding 1.1 Apr 27

 1. 


 Count numbers in given range such that sum of even digits is greater than sum of odd digits Given two
integers L and R denoting a range L, R. The task is to nd the total count of numbers in the given range
L,R whose sum of even digits is greater than the sum of odd digits.
Input Format
Enter two integer L and R.
Output Format
Corresponding output.

PYTHON 

 

def memo(index, evenSum, oddSum, tight):
    if index == len(v):
        if evenSum > oddSum:
            return 1
        else:
            return 0
    if dp[index][evenSum][oddSum][tight] != -1:
        return dp[index][evenSum][oddSum][tight]
    limit = v[index] if tight else 9
 
    ans = 0
 
    for d in range(limit + 1):
        currTight = 0
 
        if d == v[index]:
            currTight = tight
        if d % 2 != 0:
            ans += memo(index + 1, evenSum,
                        oddSum + d, currTight)
        else:
            ans += memo(index + 1, evenSum + d,
                            oddSum, currTight)
 
    dp[index][evenSum][oddSum][tight] = ans
    return ans

def countNum(n):
    global dp, v
 
    v.clear()
    num = []
    while n:
        v.append(n % 10)
        n //= 10
 
    v.reverse()
    dp = [[[[-1, -1] for i in range(180)] for j in range(180)]
        for k in range(18)]
    return memo(0, 0, 0, 1)
a,b=input().split()
dp = []
v = []
n=a.split('=')
n1=b.split('=')
R=int(n1[1])
L=int(n[1])
print(countNum(R) - countNum(L - 1)) 


**********************************************************************************


2.

Ramu has N dishes of different types arranged in a row: A1,A2,…,AN where Ai denotes the type of the ith
dish. He wants to choose as many dishes as possible from the given list but while satisfying two
conditions: He can choose only one type of dish. No two chosen dishes should be adjacent to each
other. Ramu wants to know which type of dish he should choose from, so that he can pick the
maximum number of dishes.
Example : Given N= 9 and A= 1,2,2,1,2,1,1,1,1 For type 1, Ramu can choose at most four dishes. One of the
ways to choose four dishes of type 1 is A1,A4, A7 and A9. For type 2, Ramu can choose at most two
dishes. One way is to choose A3 and A5. So in this case, Ramu should go for type 1, in which he can pick
more dishes.
Input Format
The rst line contains T, the number of test cases. Then the test cases follow.
For each test case, the rst line contains a single integer N.
The second line contains N integers A1,A2,…,AN.
Output Format
For each test case, print a single line containing one integer ― the type of the dish that Ramu should
choose from. If there are multiple answers, print the smallest one.


c++

#include <iostream>
using namespace std;
int main()
{
       int t,n,i,max,x;
       cin>>t;
       while(t--)
        {
             cin>>n;
             int arr[n];
             max=0;
            for(i=0;i<n;i++)
             {
                      cin>>arr[i];
             }
             int b[1001]={0};
             for(i=0;i<n;i++)
              {
                     b[arr[i]]++;
                     if(arr[i]==arr[i+1])
                              i++;
        }
        for(i=1;i<=1000;i++)
          {
                 if(b[i]>max)
                 {
                      max=b[i];
                      x=i;
                  }
        }
        cout<<x<<endl;
    }
    return 0;

}

 

*******************************************************************************

3.

Altaf has recently learned about number bases and is becoming fascinated. Altaf learned that for
bases greater than ten, new digit symbols need to be introduced, and that the convention is to use the
rst few letters of the English alphabet. For example, in base 16, the digits are 0123456789ABCDEF. Altaf
thought that this is unsustainable; the English alphabet only has 26 letters, so this scheme can only
work up to base 36. But this is no problem for Altaf, because Altaf is very creative and can just invent
new digit symbols when she needs them. (Altaf is very creative.) Altaf also noticed that in base two, all
positive integers start with the digit 1! However, this is the only base where this is true. So naturally,
Altaf wonders: Given some integer N, how many bases b are there such that the base-b representation
of N starts with a 1?
Input Format
The rst line of the input contains an integer T denoting the number of test cases. The description of T
test cases follows.
Each test case consists of one line containing a single integer N (in base ten).
Output Format
For each test case, output a single line containing the number of bases b, or INFINITY if there are an
innite number of them.
 

JAVA

 import java.util.*;
class Main {
  private static int howManyBasesStartWithOne(int num) {
    int count = 0;
    for (int i = 2; i <= num; ++i) {
        int highestBase = (int) (Math.log(num) / Math.log(i));
        int leadingDigit = num / (int) Math.pow(i, highestBase);
        if (leadingDigit == 1) {
            ++count;
        }
    }
    return count;
}
    public static void main(String[] args)
    {
       Scanner sc = new Scanner(System.in);
        int n,i;
        int t = sc.nextInt();
        for(i=1;i<=t;i++)
        {
            
            n = sc.nextInt();
            System.out.println(howManyBasesStartWithOne(n));
           
        }
    }
}

 

**************************************************************************

 

 

 

Post a Comment

Previous Post Next Post