[Solved] The megastore offers a deal that states any product whose price is a golden price O would be given to them for free

The megastore offers a deal that states any product whose price is a golden price O would be given to them for free. A number G is said to have a guiden price if the difference betwe the highest digit and the sum of its digits equals the highest digit

Examples include 352 ((3+5+2)-5=5), 3003, and 32812 ((3+2+8+1+2)-8=8).

Narendra visits the megastore and purchases (Y, X+1) products with each of them in the price range of (X,Y) (X and Y are included in the product lot) and one product from each price value. He wants to know how much money he would have saved by picking products with the golden price.

Considering that you are a friend of Narendra, help Narendra with a program to figure out how much money he saved overall by buying Y-X+1 products between X and Y price range and every product price is different

Read the input from STDIN and write the output to STDOUT. You should not write arbitrary strings while reading the input and while printing as these contribute to the standard output

Input1

10 00

Output1

495

Input2

11 33

Output 2

66

Solution

import java.util.*;

class realCoder
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int x=sc.nextInt();
        int y=sc.nextInt();
        int sum=0;
        for(int i=x;i<=y;i++){
            if(isGolden(i)){
                sum+=i;
                // sauravhathi
            }
        }
        System.out.println(sum);
    }
    public static boolean isGolden(int n){
        int max=0;
        int sum=0;
        while(n>0){
            int rem=n%10;
            if(rem>max){
                max=rem;
            }
            sum+=rem;
            n=n/10;
        }
        if(max==(sum-max)){
            return true;
        }
        return false;
    }
}
def isGolden(n):
    max=0
    sum=0
    while(n>0):
        rem=n%10
        if(rem>max):
            max=rem
        sum+=rem
        n=n//10
    if(max==(sum-max)):
        return True
    return False

x,y=map(int,input().split())
sum=0
for i in range(x,y+1):
    if(isGolden(i)):
        # sauravhathi
        sum+=i
print(sum)
#include <bits/stdc++.h>
using namespace std;

bool isGolden(int n){
    int max=0;
    int sum=0;
    while(n>0){
        int rem=n%10;
        if(rem>max){
            max=rem;
        }
        sum+=rem;
        n=n/10;
    }
    if(max==(sum-max)){
        return true;
    }
    return false;
}

int main() {
    int x,y;
    cin>>x>>y;
    int sum=0;
    for(int i=x;i<=y;i++){
        if(isGolden(i)){
            sum+=i;
            // sauravhathi
        }
    }
    cout<<sum<<endl;
    return 0;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *