Skip to content

Commit

Permalink
Added GFG POTD December 21, 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
DineshK3012 committed Dec 21, 2023
1 parent a0e3517 commit 0cb490a
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 36 deletions.
64 changes: 64 additions & 0 deletions .markdowns/2023/december/21-Candy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
## GFG Problem Of The Day

### Date - 21 December, 2023
### Ques - [Candy](https://www.geeksforgeeks.org/problems/candy/1)
![](https://badgen.net/badge/Level/Medium/yellow)

There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.You are giving candies to these children subjected to the following requirements:
- Each child must have atleast one candy.
- Children with a higher rating than its neighbors get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute.

### Approach Used
**Initialization:** Check if the number of kids N is zero. If true, return 0 candies.

**Candies Array:** Initialize an array candies to store the number of candies each kid receives. Initially, set each kid's candies to 1.

**Traverse Left to Right:** Traverse the ratings array from left to right. If the current kid's rating is higher than the previous one, update the candies accordingly.

**Traverse Right to Left:** Traverse the ratings array from right to left. If the current kid's rating is higher than the next one, update the candies, taking the maximum of the current value and the updated value.

**Total Candies:** Calculate the total number of candies needed by summing up the values in the candies array.

**Return:** Return the total number of candies.

### Time and Auxiliary Space Complexity

- **Time Complexity :** `O(N)`
- **Auxiliary Space Complexity :** `O(N)`, because of array to store number of candies

### Code (C++)
```cpp
class Solution {
public:
int minCandy(int N, vector<int> &ratings) {
vector<int> v(N,1);
for(int i=1;i<N;i++)
{
if(ratings[i]>ratings[i-1])
v[i]=v[i-1]+1;
}

for(int i=N-2;i>=0;i--)
{
if(ratings[i]>ratings[i+1])
v[i]=max(v[i],v[i+1]+1);
}

int sum=0;
for(int i=0;i<N;i++)
sum+=v[i];

return sum;
}
};
```
### Contribution and Support
If you have a better solution or any queries / discussions , please visit our [discussion section](https://github.com/SHRbharat/gfg_potd/discussions).
If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [SHRbharat/gfg_potd](https://github.com/SHRbharat/gfg_potd) repository.
![Total number of repository visitors](https://komarev.com/ghpvc/?username=SHRbharat&color=blueviolet&&label=Visitors)
[![Total number of repository stars](https://img.shields.io/github/stars/SHRbharat/gfg_potd.svg)](https://github.com/SHRbharat/gfg_potd/stargazers)
69 changes: 33 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
## GFG Problem Of The Day

### Date - 20 December, 2023
### Ques - [Modified Game Of Nim](https://www.geeksforgeeks.org/problems/variation-in-nim-game4317/1)
### Date - 21 December, 2023
### Ques - [Candy](https://www.geeksforgeeks.org/problems/candy/1)
![](https://badgen.net/badge/Level/Medium/yellow)

You are given an array A of n elements. There are two players player 1 and player 2.
A player can choose any of element from an array and remove it. If the bitwise XOR of all remaining elements equals 0 after removal of the selected element, then that player loses. Find out the winner if player 1 starts the game and they both play their best.
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.You are giving candies to these children subjected to the following requirements:
- Each child must have atleast one candy.
- Children with a higher rating than its neighbors get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute.

### Approach Used
Classify the elements as either occurring odd number of times or even number of times.If it occurs even number of times, we can ignore it in our calculation as both players pick it equal number of times.
**Initialization:** Check if the number of kids N is zero. If true, return 0 candies.

- If there are odd number of odd count elements => Player 1 will lose
- If there are even number of odd count elements => Player 1 will win
**Candies Array:** Initialize an array candies to store the number of candies each kid receives. Initially, set each kid's candies to 1.

**How to get number of elements that occur odd number of times?**
- We don't need the number, we just need to know if there are odd or even such numbers.
- odd + even = odd
- even + even = even
**Traverse Left to Right:** Traverse the ratings array from left to right. If the current kid's rating is higher than the previous one, update the candies accordingly.

- We know that the even counting elements always occur even times in the array. So, the total number of elements (n) and the number of odd-count elements - must both be odd or both be even.
**Traverse Right to Left:** Traverse the ratings array from right to left. If the current kid's rating is higher than the next one, update the candies, taking the maximum of the current value and the updated value.

- Thus, to find if there are odd/even number of odd-count elements: just use n directly.
- if n is odd -> Player 2 wins
- if n is even -> Player 1 wins
**Total Candies:** Calculate the total number of candies needed by summing up the values in the candies array.

**Edge case:**
- If the xor value is already 0, Player 1 wins irrespective of remaining elements. Check for that first.

### Example : n = 3, A = [3, 3, 2]
### Output : 2

## Explanation:
- Optimal removal of values are 3, 2, 3 sequentially. Then the array is empty. So player 2 wins.
**Return:** Return the total number of candies.

### Time and Auxiliary Space Complexity

- **Time Complexity :** `O(n)`
- **Auxiliary Space Complexity :** `O(1)`
- **Time Complexity :** `O(N)`
- **Auxiliary Space Complexity :** `O(N)`, because of array to store number of candies

### Code (C++)
```cpp
class Solution{
public:
int findWinner(int n, int A[]){
int xorr = 0;
class Solution {
public:
int minCandy(int N, vector<int> &ratings) {
vector<int> v(N,1);
for(int i=1;i<N;i++)
{
if(ratings[i]>ratings[i-1])
v[i]=v[i-1]+1;
}

for(int i=N-2;i>=0;i--)
{
if(ratings[i]>ratings[i+1])
v[i]=max(v[i],v[i+1]+1);
}

for(int i = 0; i<n; i++)
xorr ^= A[i];
int sum=0;
for(int i=0;i<N;i++)
sum+=v[i];

//Edge Case: if xor of all element is already 0, then player 1 wins
if(xorr == 0)
return 1;
else
return (n%2) + 1;
return sum;
}
};
```
Expand Down
49 changes: 49 additions & 0 deletions cpp_codes/2023/december/21-Candy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends

class Solution {
public:
int minCandy(int N, vector<int> &ratings) {
vector<int> v(N,1);
for(int i=1;i<N;i++)
{
if(ratings[i]>ratings[i-1])
v[i]=v[i-1]+1;
}

for(int i=N-2;i>=0;i--)
{
if(ratings[i]>ratings[i+1])
v[i]=max(v[i],v[i+1]+1);
}

int sum=0;
for(int i=0;i<N;i++)
sum+=v[i];

return sum;
}
};

//{ Driver Code Starts.

int main() {
int t;
cin >> t;
while (t--) {
int N;
cin >> N;

vector<int> ratings(N);
for (int i = 0; i < N; i++) {
cin >> ratings[i];
}
Solution obj;
cout << obj.minCandy(N, ratings) << endl;
}
return 0;
}
// } Driver Code Ends
54 changes: 54 additions & 0 deletions java_codes/2023/december/21-Candy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//{ Driver Code Starts
// Initial Template for Java

import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;

class GFG {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
int n = sc.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
Solution obj = new Solution();
int ans = obj.minCandy(n, a);
System.out.println(ans);
}
}
}

// } Driver Code Ends

// User function Template for Java
class Solution {
static int minCandy(int N, int ratings[]) {
int candy[] = new int[N];
Arrays.fill(candy , 1);

//left to right
for(int i = 1 ; i < N ; i++){
if(ratings[i] > ratings[i-1]){
candy[i] = candy[i-1] + 1;
}
}

//right to left
for(int i = N-2 ; i >= 0 ; i--){
if(ratings[i] > ratings[i+1]){
candy[i] = Math.max(candy[i+1] + 1 , candy[i]);
}
}

int count = 0;
for(int i = 0 ; i < N ; i++){
count += candy[i];
}
return count;
}
}

0 comments on commit 0cb490a

Please sign in to comment.