Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Sachin likes sweets a lot. So, he goes to a market of sweets. There is a row of sweet stalls. Every sweet stall has different sweets. To save some time, he decided to buy sweets from contiguous stalls. So, he can buy from as many stalls as he wants, but all of those stalls need to be contiguous. He also decided to buy only 1 kg of sweets from each of those stalls. Cost of 1 kg of sweets for each stall is given. There is a strange rule of billing in that market. And that rule is as follows- Total cost of all sweets bought is sum of the cost of all sweets multiplied by the cost of sweet he bought at the end. e.g. if he buys sweets having cost 2, 3, 4 in the same order than total cost of sweets will be 2*4+3*4+4*4=36.

Now he wonders what will be the total cost of all possible ways of buying sweets. Can you help him. Because this number could be large, you should take modulo of the final result by 10^9+7.

INPUT SPECIFICATION Your function contains a single argument- A One dimensional Integer array of Size N in which ith element denotes the cost of 1 kg sweets from ith stall. First line of input contains an Integer N denoting the size of Array. (1<=N<=10^5) Next N lines of input each containing a single integer from 1 to 9.

OUTPUT SPECIFICATION You must return an integer- sum of the cost of all possible ways of buying sweets modulo 10^9+7.

EXAMPLES Sample Test Case 1- Input 3 1 2 3

Output 53

Explanation Possible ways of buying sweets are-

a) 1

b) 1 2

c) 2

d) 1 2 3

e) 2 3

f) 3

cost of each of these is following-

a) 1*1= 1

b) 1*2+2*2= 6

c) 2*2= 4

d) 1*3+2*3+3*3= 18

e) 2*3+3*3= 15

f) 3*3= 9

Hence total cost will be 1+6+4+18+15+9=53

What I have tried:

Java
class A {

    static void printSubsets(int set[]) {
        int n = set.length;
        for (int i = 0; i < (1 << n); i++) {
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) > 0) {
                    System.out.print(set[j] + " ");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int set[] = { 1, 2, 3 };
        printSubsets(set);
    }
}

What additional code should be added to get the above result.
Posted
Updated 4-Oct-18 7:08am
v2
Comments
ZurdoDev 4-Oct-17 13:53pm    
No one will write it all for you and it's very rude to ask someone to. Where exactly are you stuck?
Member 13316008 4-Oct-17 14:02pm    
I am not able to find the cost part of the program .Can anyone give me the idea about it.
Richard MacCutchan 5-Oct-17 5:28am    
This is a mathematics question, so you need to solve that first. Once you have the solution then writing the code will be easy.

Quote:
How we can solve this puzzle using java ?

Not 'we', just 'you', this puzzle is a kind of contest/challenge. The goal is you find a smart way to solve the puzzle as fast as possible.
What I can say is that brute force is never the wanted solution.
My advice:
1) Create a brute force program. Once it works, add code to count the number of operations it need adds, multiplies to get the result.
2) Do runs with dataset of 1, then 2 and so on. Note how counts evolve.
3) Think about ways to reduce those counts with smarter code.
Said otherwise, you have to create algorithm or combine known ones.

Quote:
I am not able to find the cost part of the program .

you have a detailed example, with the debugger, it is easy to compare what your code is doing against what it should.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
**FOR JAVA:**

import java.util.Scanner;
public class xyz {
public static void main(String[] args)
{

Scanner scan = new Scanner(System.in);

int N,i,j,k,s=0,c;

System.out.println("enter the number of stalls(MUST BE IN BETWEEN 1 TO 100000)\n");

N=scan.nextInt();

if(N>0&&N<10000)

{

int a[]=new int[N];

c=0;

for(i=0;i<N;i++)

{

System.out.print("enter cost of sweet in "+ (i+1) +"th stall:\n");

a[i]=scan.nextInt();

if(a[i]>9||a[i]<1)

c++;

}

if(c==0)

{

for(j=1;j<=N;j++)

{

for(k=j;k<=N;k++)


{

for(i=j;i<=k;i++)

{

s=s+a[i-1]*a[k-1];


}


}

}

System.out.print(s%1000000007);

}

}

}

}



**FOR C**


#include<stdio.h>
int main(){

int N,i,j,k,s=0;

puts("enter the number of stalls(MUST BE IN BETWEEN 1 TO 100000)\n");

scanf("%d",&N);

if(N>0&&N<10000)

{

int a[N];


for(i=0;i<N;i++)
{
q:printf("enter cost of sweet in %d th stall:\n",i+1);

scanf("%d",&a[i]);

if(a[i]>9||a[i]<1)

{
puts("value out of range 0-9 ");

goto q;
}
}


for(j=1;j<=N;j++)

{

for(k=j;k<=N;k++)

{

for(i=j;i<=k;i++)
{
s=s+a[i-1]*a[k-1];
}
}
}
printf("%d",s%1000000007);


}
}
 
Share this answer
 
Comments
Dave Kreskowiak 4-Oct-18 15:40pm    
Doing other peoples work for them does not help them at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900