Learn Programming through Logical Thinking Series – 7

This blog explains about Learn Programming through Logical Thinking Series – 7 and is given below :

_______________________________________________________________________________

Learn Programming through funny ways!  Here, we are going to solve Profit and Loss related Aptitude through our Logical Thinking series.

QUESTION

A, B, C subscribe Rs. 50,000 for a business. A subscribes Rs. 4000 more than B and B Rs. 5000 more than C. Out of a total profit of Rs. 35,000.  How much does A receive?

ANSWER:  14700

ALGORITHM

  1. Get the total profit value from user
  2. Initialize a=4000,b=5000,amt=50000
  3. Add the value of and b to get the a’s original amount
  4. Then add the a’s original amount and the b value and subtract it from amt and store it in cvalue
  5. Then divide the cvalue by 3
  6. Find the a&b value by adding cvalue with a’s original amount and b value
  7. Find the ratio of a,b,c by dividing the a,b,c value by 1000
  8. Now add the a,b,c ratios to get the total value
  9. Now find a’s share by multiplying profit value with a’s ratio and divide dy the total value

Profit Share Program in Java

package my.app2;

import java.util.Scanner;

public class App2 {

public static void main(String[] args) {

System.out.println(“enter the total profit”);

Scanner scanner = new Scanner(System.in);

int p = scanner.nextInt();

int a=4000,b=5000;

int a_modval=a+b, a_share, cvalue_by_ab;

int amt=50000, cValue,a_amount,b_amount,a_ratio,b_ratio,c_ratio,totalRatio;

int sumOffab+a_modval;

cValue_by_ab=amt-sumOfab;

cValue=cValue_by_ab/3;

a_amount=cValue+a_modval;

b_amount=cValue+b;

a_ratio=a_amount/1000;

b_ratio=b_amount/1000;

c_ratio=cValue/1000;

totalRatio=a_ratio+b_ratio+c_ratio;

// Calculating a’s share now. 

a_share=(p*a_ratio)/totalRatio;

System.out.print(“a’s share is: “+a_share);

}

}

_______________________________________________________________________________

Code

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class Solution{
public static void main(String[] args){
    Scanner stdin = new Scanner(System.in);
    int t = stdin.nextInt();
    for(int m=0;m<t;m++){
        long n = stdin.nextLong();
        long[] ar = new long[(int)n];
        for(int i=0;i<n;i++){
            ar[i] = stdin.nextLong();
        }
        long max = maximum(ar);
        System.out.println(max);
    }
}
public static long maximum(long[] ar){
    int j=0;
    long costPrice=0;
    long sellingPrice=0;
    int k=0;
    long max = maximumKey(j,ar);
    while(j<ar.length){
        if(ar[j]<max){
            costPrice += ar[j];
            j++;
        }
        else if(ar[j]==max){
            sellingPrice += ((j-k)*ar[j]);
            j++;
            k=j;
            max = maximumKey(j,ar);
        }            
    }
    return sellingPrice-costPrice;
}
    public static long maximumKey(int j, long[] ar){
        long max = 0;
        for(int i=j;i<ar.length;i++){
 _____________________________________________________________________________________________________

References : 

https://codereview.stackexchange.com/questions/57943/determining-maximum-profit-to-be-made-from-selling-shares