In-feeds

Total views

Solutions LTI Infinity Coding Challenge 2021

 

Problems and Code 



Serena and Flowers

Serena has a garden with N flower pots arranged in a row Every morning she walks through the row and plucks flowers She targets to visit the first K pots in the row
Find the number of different flowers that Serena would pluck on an angle walk through the k pots

Input Specification:

input1: N, number of flower pots in Serena's garden 
input2: K number of pots Serena visits one walk 
inputs: An array containing pot index P to represent each pot by unique numbers (1<=P<=1000). 
pots having the same flower will have the same pot index

Output Specification:

Your function should return the number of different flowers plucked by Serena

Example 1:

Input1: 5
Input2 : 3
Input3: (1,1,2,1,5)
Output: 2
Explanation:
Till the 3 d pot, Serena could find only 2 distinct flowers (1,2).    

Example 2:

Input1 :10
input2:7
input3 (2,1,2,1,3.1,4,5,6,1)
Output 4
Explanation:
Till the 7th pot. Serena could find only 4 distinct flowers (1,2.3,4).   

Solution : 

import java.util.HashSet;
import java.util.Scanner;

class SerenaFlowers{
    public static void main(String args[])
    {
         Scanner sc = new Scanner(System.in);
         int n = sc.nextInt(); 
         int k = sc.nextInt(); 
         sc.nextLine(); 
         int arr[] = new int[n]; 
         String a[] = sc.nextLine().split(" ");
         HashSet<Integer> hs = new HashSet<>();
         for(int i =0 ; i<a.length ;i++)
         {
             arr[i] = Integer.parseInt(a[i]); 
             if(i<k)
                 hs.add(arr[i]);
         }
         System.out.println(hs.size());
    }
}

Ronny's confidence

Ronny's confidence level (C) is 0. You have a series of K exercises each of which increases Ronny's confidence by P[K] units (where P[K] is a set of K prime numbers, [2, 3, 5, 7, .... ]), respectively
Ronny practices exactly 1 exercise each day. The same exercise can be practiced over
and over again. 
Find the minimum number of days it will take for his confidence level to reach exactly D.

Input Specification:
input1: D, final confidence level.
input2: K, the total different number of exercises.

Output Specification:
Your function should return the minimum number of days required to achieve the target. if not possible. output -1     
Example 1:
Input1: 12
Input2: 5
Output: 2
Explanation: now since k is 5 therefore p[k]={2,3,5,7,11}
exercise of p[k] value 5 and 7 will chosen to reach exactly 12 

Solution

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main
{
public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int D ,K;
        D= sc.nextInt();
        K= sc.nextInt();
        Integer[] dp = new Integer[1000];
        Arrays.fill(dp,new Integer(-1));
        List<Integer>  DP = Arrays.asList(dp);
        ArrayList<Integer>  P = new ArrayList<>();
        int i=2,k=0;
        while(k < K){
            if(isPrime(i)){
                P.add(i);
                k++;
            }
            i++;
        }
       System.out.println(calculateConfidence(D,P,DP));
     }
    static final int INT_MAX = Integer.MAX_VALUE;
    static int calculateConfidence(int D, ArrayList<Integer> P, List<Integer> dp)
    {
        if(D==0) return 0;
        if(D<0) return INT_MAX;        
        if(dp.get(D) != -1)
            return dp.get(D);
        dp.set(D,INT_MAX);
        for(int i=0;i<P.size()-1;i++)
        {
            int current = calculateConfidence(D-P.get(i),P,dp);            
            if(current == INT_MAX) continue;            
            dp.set(D,Math.min(dp.get(D),current+1));            
        }        
        return dp.get(D);
    }
    static boolean isPrime(int n){        
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
        if (n % 2 == 0 || n % 3 == 0)
            return false;      
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;      
        return true;
    }
}


Zombies

In a country of zombies, each city has a certain percentage of zombies. Cities are designated as
1. A city[i] is magical if city[i] and city[i+1] have no common divisor other than 1.
2. A city is good if the percentage of zombies in the city[i] is more than the percentage of zombies in city[i+1]
Find a city that is perfect, where perfect means both good and magical,
if there are more than one perfect city, output the left-most city index. Also, the minimum number of cities in a country is 2 and there will be at least one perfect city

Input Specification:

input1: An array representing the percentage of zombies in each city 
input2: Number of cities in the country

Output Specification:

Return the favorable city index "i"

Example 1:

Input1: (1,1,3,6,7,3) 
Input2: 6
Output: 4
Explanation: city[4] = 7 

Solution

a=[int(x) for x in input().split()] 
n=int(input()) 
l=[] 
for i in range(len(a)-1):
    c=0 
    print(a[i],a[i+1]) 
    for j in range(2, min(a[i], a[i+1])+1):
        if a[i]%j==a[i+1]%j==0:
            c=1 
    if c==0 and a[i]>a[i+1]:
        l.append(i)
print(l[0]) 

Long Number Possibilities

Samwed takes out N bowls in a straight line and put a few marbles randomly in each bowl such that
1. A bowl can never have more than 
2. A bowl can have n marbles at a time
Now, Samwed friend adds 1 more marble to the last bowl After this addition, all the bowls must still be aligned with the rules mentioned above Adding a marble follows the same rules as of addition with carryover
You are given the initial list of the number of marbles in each bowl Find the position of the bowl which was last modified. (Assuming indexing starts from 1)
Note: If a situation arises where you have to add one more bowl, output 0.

Input Specification:
input1: N. the number of bowls
input2: Array of number of marbles in each bow

Output Specification:
Position of the bowl which is last modified

Example 1:

Input1: 2
Input2: (9,9)
Output: 0
Explanation:
When we add 1 marble to the last bowl, the new configuration of the bows is (0,0) No bowl is the last modified

Example 2:

Input1: 5
Input2: (1,2,9,9,9)
Output: 2
Explanation:
When we add 1 marble to the last bowl the new configuration of the bowls is

{1,3,0,0,0).

Bowl number 2 is the last modified

Solution

public class LongNumberPoss
{
public static void main(String[] args) {
Scanner  sc = new Scanner(System.in);
        int n = sc.nextInt();
        int arr[] = new int[n];
        for(int i=0 ; i<n ; i++){
            arr[i] = sc.nextInt();
        }
        System.out.println(last(n,arr));
}
static int last(int input1,int input2[]) 
    {     
        int n = input1;
        int i=n-1;
        while(i>=0)
        {
            if(input2[i]<9)
            {
                return i+1;
            }
            i--;
        }
        return 0;
    }
}


Food Stalls

Rubin goes to a food festival along with N-1 friends Robin is labeled as 1 and his friends are labeled from 2 to N. Each of them has a set of colored coupons The food festival has M food stalls numbered from 1 to M. Every food stall accepts particular color coupons only.
There are 10 different color coupons represented by numbers ranging from 1 to 10. You are given a certain number of queries Q. Find the sum of the outputs of all the queries.
Input Specification:

input1: N, the total size of the group of friends including Robin
input2: M, number of stalls
Input3: A two-dimensional array of size M*10, where cell(i,j) = 1 denotes that stall  i accepts coupon j
input4: A two-dimensional array of size N*10, where cell(i,j )=1 denotes that person i has coupon j 
input5: Q number of queries
Input6: A two-dimensional array of size Q * 2, containing sets for which the query has to be answered. For each row [i,j], if a person i can eat at stall j, then the output of the query is 1 else output in 0

Output Specification:

Your function should return the sum of the output of all the queries

Example 1:

Input1: 1
Input2: 1
Input3: { {1, 0, 0, 0, 0, 0, 0, 0, 0, 0} }
Input4: { {1, 0, 0, 1, 0, 0, 0, 0, 0, 0} }
Input5: 1
Input6: { { 1,1} }
Output: 1
Explanation:
Person 1 has coupons 1 and 4, and the stall accepts coupon 1.
So, the output of whether person 1 can eat at stall 1 is 1.

Example 2

Input1: 1
Input2: 2
Input3: { {1, 0, 0,1, 0, 0, 0, 0, 0, 0},{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} }
Input4: { {1, 0, 0, 0, 0, 0, 0, 0, 0, 0} }
Input5: 2
Input6: { {1, 1} , {1, 2} }
Output:1
Explanation:
Person 1 has coupon 1, and stall 1 accepts coupon 1 and 4 .So, the output of whether person 1 can eat at stall 1 is 1 
Person 1 has coupon 1 and stall 2 accepts coupon 10.So, the output of whether person 1 can eat at stall 2 is 0.
Thus, sum of the outputs of bath queries 10-1

Solution

import java.util.*;
class FoodStalls{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();

/** Storing in sets so to store only those color 
numbers that stall supports ([i][j] == 1)*/
ArrayList<HashSet<Integer>> stallSet 
= new ArrayList<HashSet<Integer>>(m);
ArrayList<HashSet<Integer>> personSet 
= new ArrayList<HashSet<Integer>>(n);
for(int i=0;i<m;i++){
stallSet.add(new HashSet<Integer>());
for(int j=0;j<10;j++){
if(sc.nextInt()==1)
stallSet.get(i).add(j);
}
}
for(int i=0;i<n;i++){
personSet.add(new HashSet<Integer>());
for(int j=0;j<10;j++){
if(sc.nextInt()==1)
personSet.get(i).add(j);
}
}
int q = sc.nextInt();
int sum =0;
for(int i=0;i<q;i++){
int r = sc.nextInt();
int c = sc.nextInt();

/** If personSet and stallsSet 
* intersect then add 1 to sum */

Set<Integer> intersection 
= new HashSet<>(personSet.get(r-1));

intersection.retainAll(stallSet.get(c-1));
if(!intersection.isEmpty()){
sum += 1;
}
}
System.out.println(sum);
}

}

Let us play a simple quiz

If you got some help please like👍 the button at the top. Also, poll your programming choice at the bottom 👇 of the article

Comments

Post a Comment

We need people who give us feedback and this is how we grow. Please do comment if you find something faulty, wrong , helpful, valuable in anyway. You can comment anonymously. Thanks for your contribution.

Popular posts from this blog

Persistent Systems

LTI Infinity Coding Challenge 2021