给定数字N,请精确删除一位数字以使数字可被6整除(使其尽可能大)。打印必须除去的位置,如果不可能,则打印-1。
例子:
Input: 123
Output: 3
Explanation: Remove 3rd position element and
hence the number is 12, which is divisible
by 6 and is the greatest possible.
Input: 134
Output: -1
Explanation: Not possible to remove any and
make it divisible by 6.
Input: 4510222
Output: 1
Explanation: Remove either 4 or 1 to make it
divisible by 6. The numbers after removing 4
and 1 are 510222 and 450222 respectively.
So, remove 1st position to make it the
greatest possible.
天真的方法:
遍历每个元素,并检查是否减去该数字是否可被6整除,然后存储最大可能数。当N是一个非常大的数字,它的整数不能被%运算符检查时,这将无法使用。它仅适用于较小的N。
高效方法:
将输入作为字符串并考虑可能的情况。如果可以将较大的数除以2和3,那么也可以将其除以6,也可以将其除以6。将会出现两种情况:
1)当单位数字为奇数时
当最后一位数字为奇数时,唯一可能的方法是删除最后一位数字以使其能被6整除。因此,删除最后一位数字并检查总和%3 == 0以确保在删除最后一位数字后, N的倒数第二个数字是偶数且其总和%3 == 0,则您得到的答案是必须删除的第N个位置。如果任何一种情况失败,那么您将无法删除任何数字以使其被6整除。
2)当单位数字为偶数时
在这种情况下,有多个选择。设sum为给定数字的数字总和。我们可以删除总和%3 == d%3的任何数字d(如果十位数字是奇数,则不包括单位位置数字,因此数字保持2的倍数)。之所以起作用,是因为删除该数字后,总和是a 3的倍数。
现在,要最大化此数字,我们需要在满足上述条件的最大位置找到该数字。
例子:
1.号码= 4510222
数字总和= 4 + 5 + 1 + 2 + 2 + 2 = 16
总和%3 = 1
现在,我们可以删除数字1和4(因为1%3 = 3和4%3 = 1)
如果删除1,我们得到数字450222。如果删除4,我们得到数字510222
我们删除下一个数字大于删除的数字的最大数字(最左边)。
2.号码= 7510222
数字总和= 7 + 5 + 1 + 0 + 2 + 2 + 2 = 19
总和%3 = 1
可以根据上述解决方案删除数字7和1,分别给出数字510222和750222。在这里,删除最大(最左边)的索引将得到较小的结果,因为7>5。这在上述情况下有效,因为4 <5。
正确的解决方案:
查找同时满足两个约束的最左边的数字
1.总和%3 ==数字%3
2.位数<立即下一位
如果您不能最大程度地增加某些东西,请尽量减少它的减少。如果找不到数字,使得该数字小于立即右数,则遵循上述方法。如果您从右侧删除某些内容,则该数字将是最大的值,因为您要删除最低的位数。
完成后,请打印索引(如果找到任何此类元素),否则只需打印-1。
下面是上述方法的实现:
C++
// C++ program to print digit's position
// to be removed to make number
// divisible by 6
#include
using namespace std;
// function to print the number divisible
// by 6 after exactly removing a digit
void greatest(string s)
{
int n = s.length();
int a[n];
// stores the sum of all elements
int sum = 0;
// traverses the string and converts
// string to number array and sums up
for (int i = 0; i < n; i++) {
a[i] = s[i] - '0';
sum += a[i];
}
if (a[n - 1] % 2) // ODD CHECK
{
// if second last is odd or
// sum of n-1 elements are not
// divisible by 3.
if (a[n - 2] % 2 != 0 or (sum - a[n - 1]) % 3 != 0) {
cout << "-1" << endl;
}
// second last is even and
// print n-1 elements
// removing last digit
else {
// last digit removed
cout << n << endl;
}
}
else {
int re = sum % 3;
int del = -1;
// counter to check if any
// element after removing,
// its sum%3==0
int flag = 0;
// traverse till second last element
for (int i = 0; i < n - 1; i++) {
// to check if any element
// after removing,
// its sum%3==0
if ((a[i]) % 3 == re) {
// the leftmost element
if (a[i + 1] > a[i]) {
del = i;
flag = 1;
// break at the leftmost
// element
break;
}
else {
// stores the right most
// element
del = i;
}
}
}
// if no element has been found
// as a[i+1]>a[i]
if (flag == 0) {
// if second last is even, then
// remove last if (sum-last)%3==0
if (a[n - 2] % 2 == 0 and re == a[n - 1] % 3)
del = n - 1;
}
// if no element which on removing
// gives sum%3==0
if (del == -1)
cout << -1 << endl;
else {
cout << del + 1 << endl;
}
}
}
// driver program to test the above function
int main()
{
string s = "7510222";
greatest(s);
return 0;
}
Java
// Java program to print digit's position
// to be removed to make number
// divisible by 6
import java.util.*;
class solution
{
// function to print the number divisible
// by 6 after exactly removing a digit
static void greatest(String s)
{
int n = s.length();
int[] a = new int[n];
// stores the sum of all elements
int sum = 0;
// traverses the string and converts
// string to number array and sums up
for (int i = 0; i < n; i++)
{
a[i] = s.charAt(i) - '0';
sum += a[i];
}
if (a[n - 1] % 2 !=0) // ODD CHECK
{
// if second last is odd or
// sum of n-1 elements are not
// divisible by 3.
if (a[n - 2] % 2 != 0 || (sum - a[n - 1]) % 3 != 0)
{
System.out.println("-1");
}
// second last is even and
// print n-1 elements
// removing last digit
else
{
// last digit removed
System.out.println(n);
}
}
else
{
int re = sum % 3;
int del = -1;
// counter to check if any
// element after removing,
// its sum%3==0
int flag = 0;
// traverse till second last element
for (int i = 0; i < n - 1; i++)
{
// to check if any element
// after removing,
// its sum%3==0
if ((a[i]) % 3 == re)
{
// the leftmost element
if (a[i + 1] > a[i])
{
del = i;
flag = 1;
// break at the leftmost
// element
break;
}
else
{
// stores the right most
// element
del = i;
}
}
}
// if no element has been found
// as a[i+1]>a[i]
if (flag == 0)
{
// if second last is even, then
// remove last if (sum-last)%3==0
if (a[n - 2] % 2 == 0 && re == a[n - 1] % 3)
del = n - 1;
}
// if no element which on removing
// gives sum%3==0
if (del == -1)
System.out.println(-1);
else
{
System.out.println(del + 1);
}
}
}
// driver program to test the above function
public static void main(String args[])
{
String s = "7510222";
greatest(s);
}
}
//This code is contributed by
//Surendra_Gangwar
Python3
# Python program to print digit's position
# to be removed to make number
# divisible by 6
import math as mt
# function to print the number divisible
# by 6 after exactly removing a digit
def greatest(s):
n = len(s)
a=[0 for i in range(n)]
# stores the Sum of all elements
Sum = 0
# traverses the string and converts
# string to number array and Sums up
for i in range(n):
a[i] = ord(s[i]) - ord('0')
Sum += a[i]
if (a[n - 1] % 2): # ODD CHECK
# if second last is odd or
# Sum of n-1 elements are not
# divisible by 3.
if (a[n - 2] % 2 != 0 or (Sum - a[n - 1]) % 3 != 0):
print("-1")
# second last is even and
# prn-1 elements
# removing last digit
else:
# last digit removed
print(n)
else:
re = Sum % 3
dell = -1
# counter to check if any
# element after removing,
# its Sum%3==0
flag = 0
# traverse till second last element
for i in range(n-1):
# to check if any element
# after removing,
# its Sum%3==0
if ((a[i]) % 3 == re):
# the leftmost element
if (a[i + 1] > a[i]):
dell = i
flag = 1
# break at the leftmost
# element
break
else:
# stores the right most
# element
dell = i
# if no element has been found
# as a[i+1]>a[i]
if (flag == 0):
# if second last is even, then
# remove last if (Sum-last)%3==0
if (a[n - 2] % 2 == 0 and re == a[n - 1] % 3):
dell = n - 1
# if no element which on removing
# gives Sum%3==0
if (dell == -1):
print("-1")
else:
print(dell + 1)
# driver program to test the above function
s = "7510222"
greatest(s)
#This code is contributed by Mohit kumar 29
C#
// C# program to print digit's position
// to be removed to make number
// divisible by 6
using System;
class GFG
{
// function to print the number divisible
// by 6 after exactly removing a digit
static void greatest(string s)
{
int n = s.Length;
int[] a = new int[n];
// stores the sum of all elements
int sum = 0;
// traverses the string and converts
// string to number array and sums up
for (int i = 0; i < n; i++)
{
a[i] = s[i] - '0';
sum += a[i];
}
if (a[n - 1] % 2 != 0) // ODD CHECK
{
// if second last is odd or
// sum of n-1 elements are not
// divisible by 3.
if (a[n - 2] % 2 != 0 ||
(sum - a[n - 1]) % 3 != 0)
{
Console.Write("-1");
}
// second last is even and
// print n-1 elements
// removing last digit
else
{
// last digit removed
Console.Write(n);
}
}
else
{
int re = sum % 3;
int del = -1;
// counter to check if any
// element after removing,
// its sum%3==0
int flag = 0;
// traverse till second last element
for (int i = 0; i < n - 1; i++)
{
// to check if any element
// after removing,
// its sum%3==0
if ((a[i]) % 3 == re)
{
// the leftmost element
if (a[i + 1] > a[i])
{
del = i;
flag = 1;
// break at the leftmost
// element
break;
}
else
{
// stores the right most
// element
del = i;
}
}
}
// if no element has been found
// as a[i+1]>a[i]
if (flag == 0)
{
// if second last is even, then
// remove last if (sum-last)%3==0
if (a[n - 2] % 2 == 0 &&
re == a[n - 1] % 3)
del = n - 1;
}
// if no element which on removing
// gives sum%3==0
if (del == -1)
Console.Write(-1);
else
{
Console.Write(del + 1);
}
}
}
// Driver Code
public static void Main()
{
string s = "7510222";
greatest(s);
}
}
// This code is contributed
// by ChitraNayal
PHP
$a[$i])
{
$del = $i;
$flag = 1;
// break at the leftmost
// element
break;
}
else
{
// stores the right most
// element
$del = $i;
}
}
}
// if no element has been found
// as a[i+1]>a[i]
if ($flag == 0)
{
// if second last is even, then
// remove last if (sum-last)%3==0
if ($a[$n - 2] % 2 == 0 and
$re == $a[$n - 1] % 3)
$del = $n - 1;
}
// if no element which on removing
// gives sum%3==0
if ($del == -1)
echo -1, "\n";
else
{
echo $del + 1, "\n";
}
}
}
// Driver Code
$s = "7510222";
greatest($s);
// This code is contributed by ajit
?>
Javascript
输出:
3
时间复杂度: O(无数字)