SarthakKeshari / CPP-Questions-and-Solutions

This repository aims to solve and create new problems from different spheres of coding. A path to help students to get access to solutions and discuss their doubts.
MIT License
46 stars 132 forks source link

Create Non-zero Segments.cpp #458

Closed rohitkumar9710 closed 2 years ago

rohitkumar9710 commented 2 years ago

Choose your form and fill it -

1. Form 1(C++ Solution contributors)

2. Form 2(C++ Documentation Contributors)


1) Form 1

Issue Id you have worked upon -

436

Briefly explain your program logic -

There is a greedy approach to the problem, since there is an important thing mentioned in the question, which says, that you can insert a number as large as anything. So, you can simply iterate through the given array, and maintain a count of the sum of the segment you are currently in. If any sum gets repeated, means, you have found a segment with sum 0, and hence you would insert a very big number in the segment, so that the sum of that segment never becomes zero. This enables us to ignore this segment, and everything before, and start a new segment from this position and repeat the process.

Maintain an answer variable and increase it by 1, every time you had to insert a very big number. And this the answer.

Screenshots(Attach 2 screenshots of your own input and output) -

Segment


Checklist:

Eg - If your code follow the below guidelines. Kindly change [] to [x]

All the conditions should be fulfilled for considering your code for merging -


2) Form 2

Issue Id you have worked upon -

Kindly write your answer here


Checklist:

Eg - If your code follow the below guidelines. Kindly change [] to [x]

All the conditions should be fulfilled for considering your Pull Request for merging -