Subtract Leading Digit CodeChef Solution

Problem -Subtract Leading Digit CodeChef Solution

This website is dedicated for CodeChef solution where we will publish right solution of all your favourite CodeChef problems along with detailed explanatory of different competitive programming concepts and languages.

Subtract Leading Digit CodeChef Solution in C++14

#include <iostream>
#include<math.h>
#define LONG long long int
using namespace std;

LONG digitCountFun(LONG n) {
    LONG c = 0;
    while(n!=0) {
        n=n/10;
        c++;
    }
    
    return c;
}

LONG findCount(LONG n) {
    LONG c = 0;
    LONG last = n;
    
    while(last!=0) {
        LONG digitCount = digitCountFun(last);
        digitCount--;
        
        LONG divisor = pow(10, digitCount);
        
        LONG firstDigit = last/divisor;
        LONG lastNumber = firstDigit*divisor;
        
        LONG skipped = (last - lastNumber)/firstDigit;
        skipped++;
        c+=skipped;
        
        last = last - (skipped * firstDigit);
    }
    
    
    return c+1;
}
LONG findNUM(LONG k,LONG left,LONG right){
    LONG mid=(right+left)/2;
    LONG len=findCount(mid);
    while(len!=k){
         mid=(right+left)/2;
     len=findCount(mid);
        if(len>k){
     right=mid;
        }
        else{
        left=mid;
    }
    }
    return mid;
}
int main() {
    LONG t;
	LONG k;
	cin>>t;
	
	for(int i=0;i<t;i++) {
	    cin>>k;
	    LONG num = findNUM(k,0,10*k);
	    
	    LONG len = findCount(num);
	    
	    while(len==k) {
	        if(findCount(num+1)!=k)
	            break;
	        num++;
	       
	    }
	   
	    cout<<num<<endl;
	    
	}

	return 0;
}

Subtract Leading Digit CodeChef Solution in PYTH 3

# cook your dish here
# cook your dish here
def findsubtract(num):
    n = num
    strn = str(n)
    count = 0
    while n > 0:
        fd = int(strn[0])
        p = len(strn) - 1
        if p == 0:
            n -= fd
            strn = str(n)
            count += 1
        else:
            n1 = n - fd * 10**p
            n2 = n1//fd+1
            count += n2
            n -= n2 * fd
            strn = str(n)
    return count + 1





for i in range(int(input())):
    k = int(input())
    if k == 2:
        print(9)
        continue
    n = None
    brutetop = 4 * k
    brutebottom = 0
    while abs(brutetop - brutebottom) > 20:
        v = round((brutetop + brutebottom) / 2)
        t = findsubtract(v)
        if t <= k:
            brutebottom = v
        else:
            brutetop = v
    for j in range(brutetop, brutebottom - 1, -1):
        n = findsubtract(j)
        if n == k:
            print(j)
            break

Subtract Leading Digit CodeChef Solution in C

#include <stdio.h>


long int get_seqlen(long int n)
{
    long int seqlen=0;
    long int d;
    long int l;
    long int round, remain, decade, seq;
    
    while (n>0)
    {

        d = n; l = 0;
        while (d >= 10) 
        {
            d /= 10;
            l += 1;
        }

        round = d;
        while(l--) 
        {
            round*= 10;
        }    
        remain = n % round;
        seq = remain / d;
        n -= seq*d;
        seqlen += seq;

        n -= d;
        ++seqlen;
    }
    return(seqlen+1);
}

long int find_sequence(long int desired)
{
    long int left, right, mid, sl;
    
    left = desired;
    right = desired * 10;
    mid = (left + right) / 2;
    sl = get_seqlen(mid);
    
    while(sl != desired)
    {
        mid = (left+right)/2;
        sl = get_seqlen(mid);
        if (sl > desired)   right = mid;
        else                left = mid;
    }
    
    while(sl == desired)
    {
        if (sl != get_seqlen(mid+1)) break;
        mid++; 
    }
    return(mid);
}

int main()
{
    long int cases, desired;
    
    scanf("%ld", &cases);
    for (int c=0; c<cases; c++)
    {
        scanf("%ld", &desired);
        long int mid = find_sequence(desired);
        printf("%ld\n", mid);
    }
}

Subtract Leading Digit CodeChef Solution in JAVA

import java.util.*;
class Codechef
{
    static Scanner sc=new Scanner(System.in);
    static long MAX=10000000000000L;
    static long firstDigit(long n)
    {
        long t=(long)Math.log10(n);
        long p=(long)Math.pow(10,t);
        return n/p;
    }

    static long remainder(long n)
    {
        long t=(long)Math.log10(n);
        long p=(long)Math.pow(10,t);
        return n%p;
    }

    static long count(long n)
    {
        if(n==0)
        {
            return 1;
        }
        else
        {
            long s=2;
            long f=firstDigit(n),r=remainder(n),p=(long)Math.log10(n);
            while(p>0)
            {
                s+=r/f+1;
                if(f==1)
                {
                    f=9;
                    p--;
                    r=(long)Math.pow(10,p)-1;
                }
                else
                {
                    r=(long)Math.pow(10,p)+r%f-f;
                    f--;
                }
            }       
            return s;
        }
    }

    static void find(long x)
    {
        long b=10,e=MAX;
        long ans=0;
        if(x<=2)
        {
            System.out.println(9);
        }
        else
        {
            while(b<=e)
            {
                long m=(b+e)/2;
                long c=count(m);
                if(c==x)
                {
                    ans=Math.max(ans,m);
                    b=m+1;
                }
                else if(c<x)
                {
                    b=m+1;
                }
                else
                {
                    e=m-1;
                }
            }
            System.out.println(ans);
        }
    }

    public static void main(String args[])
    {
        int T=sc.nextInt();
        for(int i=0;i<T;i++)
        {
            long k=sc.nextLong();
            find(k);
        }
    }
}

Subtract Leading Digit CodeChef Solution in PYPY 3

from bisect import *
t = int(input())
arr_k = [2, 3]
arr_n = [9, 10]
limit = 10**12
while arr_k[-1]<=limit:
	str_n = str(arr_n[-1])
	len_n = len(str_n)
	if str_n[0] != "9":
		if (10**(len_n-1)-int(str_n[1:])-1)%int(str_n[0]) == 0:
			arr_n.append(10**(len_n-1)-int(str_n[1:])-1-int(str_n[0])+arr_n[-1])
			arr_k.append(arr_k[-1]+((arr_n[-1]-arr_n[-2])//int(str_n[0])))
			arr_n.append(arr_n[-1]+int(str_n[0])+1)
			arr_k.append(arr_k[-1]+1)

		else:
			arr_n.append((((10**(len_n-1)-int(str_n[1:])-1)//int(str_n[0]))*int(str_n[0]))+arr_n[-1])
			arr_k.append(arr_k[-1]+((arr_n[-1]-arr_n[-2])//int(str_n[0])))
			arr_n.append(arr_n[-1]+int(str_n[0])+1)
			arr_k.append(arr_k[-1]+1)
		#print(arr_k)
		#print(arr_n)
	else:
		if (10**(len_n-1)-int(str_n[1:])-1)%int(str_n[0]) == 0:
			arr_n.append(10**(len_n)-1)
			arr_k.append(arr_k[-1]+((arr_n[-1]-arr_n[-2])//int(str_n[0])))
			arr_n.append(arr_n[-1]+1)
			arr_k.append(arr_k[-1]+1)
		else:
			arr_n.append((((10**(len_n-1)-int(str_n[1:])-1)//int(str_n[0]))*int(str_n[0]))+arr_n[-1])
			arr_k.append(arr_k[-1]+((arr_n[-1]-arr_n[-2])//int(str_n[0])))
			arr_n.append(10**(len_n)-1)
			arr_k.append(arr_k[-1]+1)
			arr_n.append(arr_n[-1]+1)
			arr_k.append(arr_k[-1]+1)
		#print(arr_k)
		#print(arr_n)


for i in range(t):
	k = int(input())
	ind = bisect_left(arr_k, k)
	if arr_k[ind] ==k:
		print(arr_n[ind])
	else:
		diff = int(str(arr_n[ind-1])[0])
		ans = arr_n[ind-1]+(diff*(k- arr_k[ind-1]))
		print(ans)

Subtract Leading Digit CodeChef Solution in PYTH


def countSteps(x):
    c = 1
    while x>0:
        xStr = str(x)
        first = int(xStr[0])
        b = 0 if len(xStr)<=1 else int(xStr[1:])
        times = b//first+1
        x -= first*times
        c += times
    return c

def findLargestWithStepCount(k):
    def fL(low, up):
        if up-low<100:
            for i in range(up, low-1, -1):
                if countSteps(i)==k: return i
        else:
            mid = (up+low)/2
            stepsMid = countSteps(mid)
            if stepsMid>k: return fL(low, mid)
            else: return fL(mid, up)
    useUp = 1
    while True:
        useUp *= 2
        if countSteps(useUp)>k: break
    return fL(0, useUp)

T = int(raw_input())

for i in range(T):
    k = int(raw_input())
    print(findLargestWithStepCount(k))
    

Subtract Leading Digit CodeChef Solution in GO

package main

import (
	"bufio"
	"fmt"
	"os"
)

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(scanner *bufio.Scanner) (a int) {
	scanner.Scan()
	readInt(scanner.Bytes(), 0, &a)
	return
}

func readTwoNums(scanner *bufio.Scanner) (a int, b int) {
	res := readNNums(scanner, 2)
	a, b = res[0], res[1]
	return
}

func readNNums(scanner *bufio.Scanner, n int) []int {
	res := make([]int, n)
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
	return res
}

func fillNNums(scanner *bufio.Scanner, n int, res []int) {
	x := 0
	scanner.Scan()
	for i := 0; i < n; i++ {
		for x < len(scanner.Bytes()) && scanner.Bytes()[x] == ' ' {
			x++
		}
		x = readInt(scanner.Bytes(), x, &res[i])
	}
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] != ' ' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

const MAX_K = 1e12 + 3

func main() {
	// var num, base int64

	// num = 9
	// base = 1
	// for {
	// 	k := compute(num)
	// 	if k > MAX_K {
	// 		break
	// 	}
	// 	fmt.Printf("%d: %d\n", num, k)
	// 	num = num*10 + 9
	// 	base *= 10
	// }

	scanner := bufio.NewScanner(os.Stdin)

	tc := readNum(scanner)

	for tc > 0 {
		tc--
		var k uint64
		scanner.Scan()
		readUint64(scanner.Bytes(), 0, &k)
		fmt.Println(solve(int64(k)))
	}
}

// var records []int64

// func init() {
// 	records = []int64{
// 		1,
// 		28,
// 		310,
// 		3138,
// 		31427,
// 		314323,
// 		3143290,
// 		31432972,
// 		314329797,
// 		3143298050,
// 		31432980589,
// 	}
// }

func solve(k int64) int64 {
	var left, right int64 = 0, 1e14

	for left < right {
		mid := left + (right-left)/2
		cnt := compute(mid)
		if cnt > k {
			right = mid
		} else {
			left = mid + 1
		}
	}
	return right - 1
}

func compute(n int64) int64 {
	var power int64 = 1
	for n >= power*10 {
		power *= 10
	}

	var steps int64
	for n > 0 {
		first := n / power
		rem := n % power
		tmp := rem/first + 1
		n = n - tmp*first
		steps += tmp
		if n < power {
			power /= 10
		}
	}
	return steps + 1
}
Subtract Leading Digit CodeChef Solution Review:

In our experience, we suggest you solve this Subtract Leading Digit CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Subtract Leading Digit CodeChef Solution.

Find on CodeChef

Conclusion:

I hope this Subtract Leading Digit CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

Cognitive Class Answer

CodeChef Solution

Microsoft Learn

Leave a Reply

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