Prime #### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime #### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Hack With Infy Coding Questions and Answers

## Hack With Infy 2020

information about HackWithInfy Coding Round . Below you will get  Similar pattern based HackWithInfy Coding Round Questions that are asked constantly in HackWithInfy coding rounds. The languages that you can use in the test are – ## Hack With Infy Coding Round-2 Information There are  3 coding round in Hack With Infy exam:-

• #### Round 1:-

In 1st round  of HackWithInfy 3 coding questions are asked that has to get solved in 3 hours.

• #### Round 2:-

In 2st round  of HackWithInfy 3 coding questions are asked that has to get solved in 3 hours. The Candidates who will perform well in round 2 will get a get a pre-placement interview opportunity for the role of System Engineer and Power Programmer.

• #### Round 3

This is the Grand Finale round that will be held in Infosys Pune headquarter.. Top 100 students from round 2 will get shortlisted in round 3.

## Question 1:-

Question 1 :

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:

1. He can choose only one type of dish.
2. 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 first line contains T, the number of test cases. Then the test cases follow.
• For each test case, the first 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.

CONSTRAINTS :

• 1 <= T <= 10^3
• 1 <= N <= 10^3
• 1 <= Ai <= 10^3

Sample Input :

3

5

1 2 2 1 2

6

1 1 1 1 1 1

8

1 2 2 2 3 4 2 1

Sample Output :

1

1

2

#### Solution in 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={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;}`

## Question 2:-

There are three piles of stones. The first pile contains a stones, the second pile contains b stones and the third pile contains c stones. You must choose one of the piles and split the stones from it to the other two piles; specifically, if the chosen pile initially contained s stones, you should choose an integer k (0≤k≤s), move k stones from the chosen pile onto one of the remaining two piles and s−k stones onto the other remaining pile. Determine if it is possible for the two remaining piles (in any order) to contain x stones and y stones respectively after performing this action.

INPUT FORMAT :

• The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
• The first and only line of each test case contains five space-separated integers

a,b,c, x and y.

OUTPUT FORMAT :

For each test case, print a single line containing the string “YES” if it is possible to obtain piles of the given sizes or “NO” if it is impossible.

CONSTRAINTS :

• 1 <= T <= 100
• 1 <= a,b,c,x,y <= 10^9

SAMPLE INPUT :

4

1 2 3 2 4

3 2 5 6 5

2 4 2 6 2

6 5 2 12 1

SAMPLE OUTPUT :

YES

NO

YES

NO

#### Solution in C++

Test case 1: You can take the two stones on the second pile, put one of them on the first pile and the other one on the third pile.

Test case  2: You do not have enough stones.

Test case 3: You can choose the first pile and put all stones from it on the second pile.

`#include <bits/stdc++.h>using namespace std;using ll = long long;int main() {    int t;cin >> t;    while(t--) {        int a, b, c, x, y;        cin >> a >> b >> c >> x >> y;        if((a + b + c ) != (x + y) ){            cout << "NO" << endl;        } else {            if(y < min(a, min(b, c)) || x < min(a, min(b, c))) {                cout << "NO" << endl;            }else {                cout << "YES" << endl;            }        }    }}`

## Question 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 first 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 first 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 infinite number of them.

CONSTRAINTS :

• 1 <= T <= 10^5
• 0 <= N < 10^12

SAMPLE INPUT :

4

6

9

11

24

SAMPLE OUTPUT :

4

7

8

14

SOLUTION (IN C++) :

In the first test case, 6 has a leading digit 1 in bases 2, 4, 5 and 6: 610 = 1102 = 124 = 115 = 106.

`#include<bits/stdc++.h>using namespace std;vector<long long int>v;int main(){    long long int i,j;    for(i=2;i<=(int)1e6;i++)    {        long long int tem=i;        for(j=2;j<=40;j++)        {            tem=tem*i;            if(tem>(long long int)1e12)                break;            v[j].push_back(tem);        }    }    int t;    scanf("%d",&t);    while(t--)    {        long long int m;        scanf("%lld",&m);        if(m==1)        {            printf("INFINITY\n");            continue;        }        long long int ans=(m+1)/2;        long long int p=m/2+1;        for(i=2;i<=40;i++)                ans=ans+(lower_bound(v[i].begin(),v[i].end(),m+1)-lower_bound(v[i].begin(),v[i].end(),p));        printf("%lld\n",ans);    }    return 0;}`

## Hack With Infy Syllabus

Here below you will get the Syllabus of Hack With Infy rounds. This topics are most asked questions of HackWithInfy round.

#### HackWithInfy Coding Questions and Answers

• Dynamic Programming
• Greedy Algorithms
• Backtracking
• Stack
• Queue
• Mapping Concepts

#### HackWithInfy Coding Questions With Solutions

• Array manipulation
• String manipulation
• Tree
• Graph
• Bit Mapping and Hashing
• Recursion and Heap
• Divide and Conquer 