Skip to main content

Program for Target Marbles Coding Problem

Target Marbles Coding Problem



At a Town, we love to play with marbles. We have many marble games, but the most popular one is “Target Marbles”. Now, our marbles are unique. Each marble has a number on it.

In Target Marbles, the player is given a number in the starting and this number is called target. The player is also given N number of marbles to play with. Now, player has to arrange the marbles in a specific way such that sum of the values of at least one of the continuous subset of the arrangement is equal to given target.

Now, Coder came to play this game and made an arrangement of marbles. The judges of the game need your help. You have to determine if Coder has won it or not.

Input Format :

First line contains number of marbles(N) and target (target_number) that was assigned to Coder. Second line contains N space separated integers, which represent arrangement of the marbles and value written on that particular marble.

Constraints:

1<= N <=100

1<=target_number<=10000

Value on the marbles lies in the range [0, 1000].

Output Format :

You have to print “true”, if the Coder wins with the given arrangement and you have to print the values of the continuous subsets. If there are more that one continuous subsets, then you have to print the values of first continuous subset. If the Coder is unable to win, you just have to print “false”.

Sample Input 1 :

10 10

9 1 2 3 4 5 5 16 17 19

Sample Output 1 :

true

9 1

Explanation :

Here, if the Coder arranges the given 10 marbles in this arrangement, then he/she will win the game. Now, there are many continuous subsets of marbles that will win the game such as (9,1) or (1, 2, 3, 4). Out of these winning combinations, you have to print the first one which is (9,1).

Sample Input 2 :

10 10

19 11 12 131 14 15 5 16 17 19

Sample Output 2:

false


Program for Target Marbles Coding Problem in C++


#include <iostream>

using namespace std;


int main()

{

    int n,target;

    cin >> n >> target;

    int* arr = new int [n];

    

    for(int i=0;i<n;i++){

        cin>>arr[i];

    }

    

    for(int i=0;i<n;i++){

        int sum=0;

        for(int j=i;j<n;j++){

            sum = sum + arr[j];

            if(sum==target){

                cout<<"True"<<endl;

                for(;i<=j;i++){

                    cout<<arr[i]<<" ";

                }

                cout<<endl;

                return 0;

            }

        }

    }

    cout<<"False";

    return 0;

}


Output 1 :

10 10

9 1 2 3 4 5 5 16 17 19

true

9 1

Explanation :

Here, if the Coder arranges the given 10 marbles in this arrangement, then he/she will win the game. Now, there are many continuous subsets of marbles that will win the game such as (9,1) or (1, 2, 3, 4). Out of these winning combinations, you have to print the first one which is (9,1).


Output 2 :

10 10

19 11 12 131 14 15 5 16 17 19

false

Comments

Popular posts from this blog

TechGig Code Gladiators 2023 Coding Solutions

TechGig Open Coding Round Code Gladiators 2023 Answers Round 1  - Problem 1 Forest Fire Coding Solution Figure out the minimum energy level P such that they can get exactly X animals to transport.  Test Case: 5 4  1 3 2 4 5 Output: 2 Coding Solution in Python 3: from bisect import bisect_left #Telegram - @PLACEMENTLELO n, m = map ( int , input ().split()) a = list ( map ( int , input ().split())) a.sort() #Telegram - @PLACEMENTLELO if n - bisect_left(a, a[n-m]) != m: print ( -1 ) else : print (a[n - m]) #Telegram - @PLACEMENTLELO ------------------------------------------- Round 2  - Problem 2 The Magic Coding Solution The cost of using wand for operation on element equal to absolute difference between value of element and desired value after operation. Test Case: 5 3 1 2 3 4 5 5 2 1 Output: 10 7 10 Coding Solution in Python 3: from bisect import bisect_left #Telegram - @PLACEMENTLELO n, m = map ( int , input ().split()) A = list ( map ( int , input ().split())) Q = list

The Divisibility Dilemma Code Techgig Geek Goddess Solution

The Divisibility Dilemma Code Techgig Geek Goddess Solution 100% Working Code uploaded on Youtube: https://youtu.be/6cn7nNtSiKo 100% Working Code uploaded on Telegram: https://telegram.me/PLACEMENTLELO The Divisibility Dilemma (100 Marks) In the picturesque village of Numeropolis, the annual "Divisibility Day" is celebrated with great enthusiasm. This day is marked by various mathematical challenges and games that put the villagers' arithmetic skills to the test. This year, the villagers are embarking on a quest to explore the distinctive properties of subarrays within integer arrays. The village's renowned mathematician, Professor Arithmo, has crafted an intriguing challenge. He has provided the villagers with an integer array nums`and two additional integers, K and P. Professor Arithmo's challenge revolves around discovering and counting distinct subarrays from the array nums. The uniqueness of these subarrays is determined by their divisibility characteristics.

Google Girl Hackathon 3 June 2023 Solutions

Google Girl Hackathon 3 June 2023 Solutions Google Girl Hackathon 2023   Maximize equal numbers Code in Python Task Determine the maximum length of the subsequence of array a, such that all numbers in that subsequence are equal after applying the given operation. Code in Python def MaximizeEqualNumber(n, k, a):     #Telegram - @PLACEMENTLELO     max_length = 0     for i in range(n):     #Telegram - @PLACEMENTLELO         count = 1          for x in range(-k, k+1):     #Telegram - @PLACEMENTLELO             if a[i] + x in a[i+1:]:                 count += 1     #Telegram - @PLACEMENTLELO         max_length = max(max_length, count)     return max_length n = 4 k = 1 a = [2, 5, 1, 2] # Output: 3 n = 4 k = 0 a = [2, 2, 5, 6] # Output: 2 n = 3 k = 2 a = [1,5,6] # Output: 2 That is Perfection Code in Python  def is perfect number (n):      divisors_sum = sum(i for i in range(1, n) if n % i == 0)      return divisors_sum == n Balanced Brackets You are given a string S of length N. It consists