Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Problem :-
- ----------
- There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements:
- 1. Each child must have at least one candy.
- 2. Children with a higher rating get more candies than their neighbors.
- What are the minimum candies you must give?
- Test Case 1 :-
- Input: [1, 2, 3, 4, 5]
- Output: 15
- Explanation : 1 + 2 + 3 + 4 + 5
- Test Case 2:
- Input: [3, 5, 9]
- Output: 6
- Explanation : 1 + 2 + 3
- Test Case 3:
- Input: [8, 5, 7, 1, 1]
- Output: 11
- Explanation : 4 + 2 + 3 + 1 + 1
- */
- #include <stdio.h>
- void sort(int arr[], int n){
- int temp;
- for(int i=0; i<n; i++){
- for(int j=i+1; j<n; j++){
- if(arr[i]>arr[j]){
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- }
- int count(int arr[], int n, int val){
- int count=0;
- for(int i=0; i<n; i++){
- if(arr[i]==val){count++;}
- }
- return count;
- }
- int main(){
- int lst[]={1,2,3,4,5}, n=5;
- int candies=0,i=0,candy=1, countval;
- sort(lst, n);
- while(i<n){
- countval = count(lst, n, lst[i]);
- if(countval>=2){candies += (countval*candy);}
- else{candies += candy;}
- i += countval;
- candy++;
- }
- printf("%d", candies);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement