检查左半部分的数字总和是否可以在N的最大排列中被右半部分的数字总和整除
给定一个正整数N ,任务是通过重新排列数字来最大化整数N并检查左半数的总和是否可以被右半数的总和整除。如果发现是真的,则打印“是” 。否则,打印“否” 。
If the number of digits(say D) in the given number N is odd, then consider any of the two possibilities:
- Consider the first (D/2) digit in the left half.
- Consider the first (D/2 + 1) digit in the left half.
例子:
Input: N = 43729
Output: Yes
Explanation:
Maximum value of N possible by rearranging the digits is 97432.
Case 1:
Sum of digits in the left half = 9 + 7 = 16.
Sum of digits in the right half = 4 + 3 + 2 = 9.
Since, 16 is not divisible by 9, the condition is not satisfied.
Case 2:
Sum of digits in the left half = 9 + 7 + 4 = 20.
Sum of digits in the right half = 3 + 2 = 5.
Since, 20 is divisible by 5, the condition is satisfied.
Input: N = 3746
Output: No
方法:给定的问题可以通过将数字N的给定数字按降序排序,最大化N的值,然后检查左右半数之和的可分性来解决。请按照以下步骤解决问题:
- 初始化一个变量,比如D ,它存储给定数字N的位数。
- 将给定的数字作为字符串存储在变量中,比如temp 。
- 按降序对字符串temp进行排序。
- 现在检查前(D/2)位数字的总和是否可以被最后(D/2)位数字的总和整除,然后打印“Yes” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to check if sum of digits
// in left half is divisible by the sum
// of digits in the right half or not
void checkDivisible(int N)
{
// Convert N to its equivalent string
string temp = to_string(N);
// Count of digits
int D = temp.length();
// Sort the digits in decreasing order
sort(temp.begin(), temp.end(),
greater());
int leftSum = 0, rightSum = 0;
// Find the sum of digits
for (int i = 0; temp[i]; i++) {
rightSum += (temp[i] - '0');
}
for (int i = 0; i < D / 2; i++) {
// Update the leftSum and the
// rightSum
leftSum += (temp[i] - '0');
rightSum -= (temp[i] - '0');
}
if (D & 1) {
// Consider Case 1: Check divisibility
if (leftSum % rightSum == 0) {
cout << "Yes";
}
else {
leftSum += (temp[D / 2] - '0');
rightSum -= (temp[D / 2] - '0');
// Consider Case 2: Check divisibility
if (leftSum % rightSum == 0) {
cout << "Yes";
}
else {
cout << "No";
}
}
}
else {
// Check divisibility
if (leftSum % rightSum == 0) {
cout << "Yes";
}
else {
cout << "No";
}
}
}
// Driver Code
int main()
{
int N = 43729;
checkDivisible(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
public class GFG
{
// Utility function to sort array in
// descending order
static void descOrder(char[] s)
{
Arrays.sort(s);
reverse(s);
}
// Utility function to reverse an array
static void reverse(char[] a)
{
int i, n = a.length;
char t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
// Function to check if sum of digits
// in left half is divisible by the sum
// of digits in the right half or not
static void checkDivisible(int N)
{
// Convert N to its equivalent string
String temp = Integer.toString(N);
char []arr = temp.toCharArray();
// Count of digits
int D = arr.length;
// Sort the digits in decreasing order
descOrder(arr);
int leftSum = 0, rightSum = 0;
// Find the sum of digits
for (int i = 0; i < D; i++) {
rightSum += (arr[i] - '0');
}
for (int i = 0; i < D / 2; i++) {
// Update the leftSum and the
// rightSum
leftSum += (arr[i] - '0');
rightSum -= (arr[i] - '0');
}
if (D%2 == 1) {
// Consider Case 1: Check divisibility
if (leftSum % rightSum == 0) {
System.out.print("Yes");
}
else {
leftSum += (arr[D / 2] - '0');
rightSum -= (arr[D / 2] - '0');
// Consider Case 2: Check divisibility
if (leftSum % rightSum == 0) {
System.out.print("Yes");
}
else {
System.out.print("No");
}
}
}
else {
// Check divisibility
if (leftSum % rightSum == 0) {
System.out.print("Yes");
}
else {
System.out.print("No");
}
}
}
// Driver Code
public static void main(String args[])
{
int N = 43729;
checkDivisible(N);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for the above approach
# Function to check if sum of digits
# in left half is divisible by the sum
# of digits in the right half or not
def checkDivisible(N):
# Convert N to its equivalent string
temp = str(N)
# Count of digits
D = len(temp)
# Sort the digits in decreasing order
temp = ''.join(sorted(temp, reverse = True))
leftSum,rightSum = 0,0
# Find the sum of digits
for i in range(D):
rightSum += ord(temp[i]) - ord('0')
for i in range(D // 2):
# Update the leftSum and the
# rightSum
leftSum += ord(temp[i]) - ord('0')
rightSum -= ord(temp[i]) - ord('0')
if (D % 2 == 1):
# Consider Case 1: Check divisibility
if (leftSum % rightSum == 0):
print("Yes")
else:
leftSum += ord(temp[D // 2]) - ord('0')
rightSum -= ord(temp[D // 2]) - ord('0')
# Consider Case 2: Check divisibility
if (leftSum % rightSum == 0):
print("Yes")
else:
print("No")
else:
# Check divisibility
if (leftSum % rightSum == 0):
print("Yes")
else:
print("No")
# Driver Code
N = 43729
checkDivisible(N)
# This code is contributed by shinjanpatra
C#
// C# program for the above approach
using System;
using System.Collections;
public class GFG
{
// Utility function to sort array in
// descending order
static void descOrder(char []s)
{
Array.Sort(s);
reverse(s);
}
// Utility function to reverse an array
static void reverse(char []a)
{
int i, n = a.Length;
char t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
// Function to check if sum of digits
// in left half is divisible by the sum
// of digits in the right half or not
static void checkDivisible(int N)
{
// Convert N to its equivalent string
String temp = N.ToString();
char []arr = temp.ToCharArray();
// Count of digits
int D = arr.Length;
// Sort the digits in decreasing order
descOrder(arr);
int leftSum = 0, rightSum = 0;
// Find the sum of digits
for (int i = 0; i < D; i++) {
rightSum += (arr[i] - '0');
}
for (int i = 0; i < D / 2; i++) {
// Update the leftSum and the
// rightSum
leftSum += (arr[i] - '0');
rightSum -= (arr[i] - '0');
}
if (D%2 == 1) {
// Consider Case 1: Check divisibility
if (leftSum % rightSum == 0) {
Console.Write("Yes");
}
else {
leftSum += (arr[D / 2] - '0');
rightSum -= (arr[D / 2] - '0');
// Consider Case 2: Check divisibility
if (leftSum % rightSum == 0) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
}
else {
// Check divisibility
if (leftSum % rightSum == 0) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
}
// Driver Code
public static void Main()
{
int N = 43729;
checkDivisible(N);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
Yes
时间复杂度: O(log 10 N)
辅助空间: O(log 10 N)