Breaking

Sunday 6 August 2017

Peanut Packing

                                Peanut Packing


A man is running a super market and he observes the pattern of customer buying peanuts in packs of 1 kg, 2 kgs, 3 kgs till N kgs. So based on the buying pattern and his observation he can price the packs of 1 kg, 2 kgs, ... N kgs  at rupees P(1), P(2), P(3), ... P(N) respectively. What is the maximum revenue he can earn when selling N kgs of peanuts based on the given pricing?

Input Format:
The first line contains N.
The second line contains the integer value denoting the price for 1 kg pack, 2 kgs pack till N kgs pack with each value separated by a space.

Output Format:
The first line contains the maximum revenue he can earn by packing and selling the N kgs peanuts based on the given pricing.

Boundary Conditions:
1 <= N <= 999

Example Input/Output 1:
Input:
4
120 250 360 490

Output:
500

Explanation:
While selling 4 kgs of peanuts, the maximum revenue of Rs.500 is obtained when he packs the 4 kgs as 2 kgs + 2kgs and sells them at Rs. 250 each.

Example Input/Output 2:
Input:
4
120 240 360 490

Output:
490

Explanation:
While selling 4 kgs of peanuts, the maximum revenue of Rs.490 is obtained when he packs the 4 kgs as a single 4 kgs pack and sells it ar Rs.490



Code:


#include<stdio.h>
#include <stdlib.h>

int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=1;i<=n;i++)
{
    scanf("%d",&a[i]);
}
int sum=0,max=0;
for(int i=1;i<=n;i++)
{
    if(n%i==0)
    {
        sum=(n/i)*a[i];
    }
    else
    sum=a[n%i]+(n/i)*a[i];
    if(sum>max)
    max=sum;
}
printf("%d",max);
}


Java:
import java.util.*;
public class Hello {

    public static void main(String[] args) {
        Scanner sd=new Scanner(System.in);
        int n=sd.nextInt();
        int a[]=new int[n];
        for(int i=0;i<n;i++)
        {
            a[i]=sd.nextInt();
        }
        int b[]=new int[n];
        int k=0;
        for(int i=1;i<=n;i++)
        {
            if(n%i==0)
            {
                b[k++]=a[n/i-1]*i;
            }
            else
            {
                b[k++]=(n/i*a[i-1])+(a[n%i-1]);
            }
            }
            Arrays.sort(b);
                System.out.println(b[n-1]);
            }

Please do comment If u have any Queries!

15 comments:

  1. //RMK
    import java.util.*;
    public class Hello {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    ArrayList prices = new ArrayList<>();
    for(int i =0;itotalprice)
    totalprice=sum;
    sum =0;
    }
    System.out.println(totalprice);
    }
    }

    ReplyDelete
    Replies
    1. import java.util.*;
      public class Hello {

      public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int N = sc.nextInt();
      ArrayList prices = new ArrayList<>();
      for(int i =0;itotalprice)
      totalprice=sum;
      sum =0;
      }
      System.out.println(totalprice);
      }
      }

      Delete
  2. public class Hello {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    ArrayList prices = new ArrayList<>();
    for (int i = 0; i < N; i++) {
    prices.add(sc.nextInt());
    }

    int j = 0;
    int sum = 0;
    int totalprice = 0;
    //to find divisors
    for (int i = 1; i <= N; i++) {
    if (N % i == 0) {
    j = i;
    }

    while (j <= N) {
    sum += prices.get(i - 1);
    j = j + i;
    }
    if (sum > totalprice) {
    totalprice = sum;
    }
    sum = 0;
    }
    System.out.println(totalprice);

    }
    }

    ReplyDelete
  3. //working code in c
    #include
    #include
    int fun(const void * a,const void * b)
    {
    return(*(int*)a-*(int*)b);
    }
    int main()
    {
    int n;
    scanf("%d\n",&n);
    int arr[n];
    for(int i=0;i<n;i++)
    {
    scanf("%d ",&arr[i]);
    }
    int a[n];
    int a1[n];
    for(int i=0;i<n;i++)
    a[i]=1;
    for(int i=0;i<n;i++)
    {
    for(int j=1;j<=n;j++)
    {
    if(((i+1)*j)<=n)
    a[i]=j;
    }
    }
    //for(int i=0;i<n;i++)
    //printf("%d ",a[i]);
    //printf("\n");
    for(int i=0;i<n;i++)
    {
    if((a[i]*(i+1))==n)
    a1[i]=arr[i]*a[i];
    else
    {
    int t=n-(a[i]*(i+1));
    a1[i]=(arr[i]*a[i])+(arr[t-1]);
    //printf("%d",a1[i]);
    }
    }
    //printf("\n");
    qsort(a1,n,sizeof(int),fun);
    printf("%d",a1[n-1]);
    }

    ReplyDelete
  4. //include header file
    //stdio.h
    //stdlib.h

    ReplyDelete
  5. comment crt answer

    ReplyDelete
    Replies
    1. we tried but the thing is some of the part of the programs are being deleted automatically .

      Delete
  6. import java.io.*;
    import java.util.Scanner;

    class str
    {

    static void printArray(int p[], int n,int arr[])
    { sum=0;
    for (int i = 0; i < n; i++)
    {




    sum=sum+arr[p[i]-1];


    }
    if(sum>max)
    max=sum;

    }


    static void printAllUniqueParts(int n,int arr[])
    {
    int[] p = new int[n];
    int k = 0;
    p[k] = n;


    while (true)
    {

    printArray(p, k+1,arr);


    int rem_val = 0;
    while (k >= 0 && p[k] == 1)
    {
    rem_val += p[k];
    k--;
    }


    if (k < 0) return;


    p[k]--;
    rem_val++;



    while (rem_val > p[k])
    {
    p[k+1] = p[k];
    rem_val = rem_val - p[k];
    k++;
    }


    p[k+1] = rem_val;
    k++;
    }
    }
    static int sum=0;

    static int max=0;
    public static void main (String[] args)
    {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int arr[]=new int[n];
    for(int i=0;i<n;i++)
    {
    arr[i]=sc.nextInt();
    }


    printAllUniqueParts(n,arr);
    str s=new str();
    System.out.println(s.max);
    }
    }

    ReplyDelete
  7. change d main class into Hello and too in creating its object

    ReplyDelete
  8. all the above codes does'nt work properly....!!

    ReplyDelete
    Replies
    1. code written in c , above is 100% working

      Delete
  9. code written in c , above is 100% working

    ReplyDelete

Like