Aniket Narvankar

Aniket Narvankar

  • 562
  • 2.1k
  • 605k

Write a Program

May 28 2023 5:17 AM

A Candidate has applied for dream jobs. He has done with all rounds and now is time for result. He picks a flower from pot and starts picking petal from flower saying he will get and he will not get. He always start with positive approach he will get.

There are N flowers in pot and each flower has different number of petals. if he picks any flower he picks up all the petals of that flower. we have to get the maximum sum of number of petals of the flowers so he finishes at positive outcome i.e. he will get. if its not possible print -1.

Example

There are 3 flowers in a pot. So N=3

The number of petals all flower  respectively {3,1,3}

Picking all the flowers will end up at he will get for example

3:He will get , He will not get , He will get

1: He will not get

3:He will get , He will not get , He will get

So output will be 7 as he reaches at He will get at 7

Sample Input

N = 3(Number of flowers)

Number of petals in each flower (3,1,3). so 3 + 1 +3 =7

have written program for sample test case

int n = 3;

int i = 0;

int[] noofpetals = {3,1,3};

int sum = 0;

do{

int total = noofpetals[i];

for(int j = 1 ; j <=total;j++)

{

       sum = sum + 1;

}

i++;

}

while(i < n);

Console.WriteLine(sum);

this works for sample test case but fails for other. for example

N = 2(number of flowers)

noofpetals in each flower = {2,2}

He picks all flowers and all petals

He will get he will not get

He will get he will not get

Here we will print -1 as he never reached he will get

suppose noofpetals in each flower is {2,3}

He will get he will not get

he will get he will not get he will get

output will be 5 as he rached at he will get at 5.

suppose number of flower N = 3

suppose noofpetals in each flower is {2,3,2}

he will get he will not get

he will get he will not get he will get

he will not get he will get

here it will be 7 as he reached he will get at 7

suppose number of flower N = 4

suppose noofpetals in each flower is {2,3,2,3}

he will get he will not get

he will get he will not get he will get

he will not get he will get

he will not get he will get he will not get

here it will be -1 as he never reached he will get

how can this be done


Answers (2)