Need help with this question using c language | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 3

Need help with this question using c language

Jack and Jill were going through a jungle to city. They encountered a monster who told they will only be allowed to escape when they solve a puzzle for him. They didn't have a choice so they agreed. He states problem like : I have n buckets having 0 fruits in each bucket initially. I will give you n numbers denoting fruits required at nth position. But you need to keep 2 simple rules: 1) Either you can increment fruit count by 1 in each bucket i.e. Incremental Operation 2) Or you can double the fruits in each bucket i.e. Doubling operation. Function Description: Provide implementation for method play_the_game(target). play_the_game has the following parameter(s): target: an integer list denoting numbers of fruits required at nth position. Example: Input: 23 Output: 4 Explanation: To get the target bucket from (0, 0), we first increment both elements by 1 (2 operations), then double the array (1 operation). Finally increment second element (1 more operation) Input: 16 16 16 Output: 7

23rd Sep 2021, 7:19 PM
Mansi Talavadekar
Mansi Talavadekar - avatar
6 Answers
0
29th Oct 2021, 11:12 AM
181A468 Aparna
181A468 Aparna - avatar
0
//THIS PROGRAM IS DONE BY C++ NOT BY C PROGRAM. #include <bits/stdc++.h> using namespace std; //function starts here int co(int arr[], int n) { int a = 0; while (1) { int noofzeros = 0; int i; for (i = 0; i < n; i++) { if (arr[i] % 2 == 1) // checking for odd num break; else if (arr[i] == 0) // checking for zeroes in array noofzeros++; } if (noofzeros == n) return a; // if every element is zero return the final value if (i == n) // at last if all elements are even { for (int j = 0; j < n; j++) { arr[j] = arr[j] / 2; } a++; } // for odd values making them even by decrement by 1 for (int z = 0; z < n; z++) { if (arr[z] % 2 == 1) { arr[z]--; a++; } } } } int main() { int arr[1000]; int count = 0, i = 0; // taking inputs from console while (1) { cin >> arr[i]; if (cin.get() == '\n') { break; } i++; count++; } int n = count + 1; // length of the array // calling function as output cout << co(arr, n); return 0; }
11th Nov 2021, 3:50 PM
Chetan Jogi
0
Jack and Jill were passing through a jungle on there way. They encountered a monster who told they will be only be allowed to escape when they solve a puzzle for him. They did't have choice so agreed. Given N number of empty buckets, we need to minimize steps to fill required number of fruits in each bucket. Only below mentioned operations are allowed to add fruits. 1. Double the count of fruits - All buckets will double the number of fruits in single operation. 2. Increment individual bucket fruit count by 1.Single operation for each bucket 3. We cannot remove/decrease number of fruits in a bucket. CONSTRAINTS: 1<=target[i]<=1000, where target[i] is the element at index Y. 1<=i<= 1000
14th Dec 2022, 8:55 AM
Neha Ks
0
Need this code in c
11th Jan 2023, 5:49 AM
Vallabhaneni Madhu
- 1
Please, show your code/attempt, after that ask what the problem is. So, we can help. Thanks for understanding. Happy coding!
23rd Sep 2021, 7:27 PM
mesarthim
mesarthim - avatar
- 1
29th Oct 2021, 11:11 AM
181A468 Aparna
181A468 Aparna - avatar