使用条件交换对数组进行排序

给定一个数组 包含来自 [1…到n] 。每个元素在数组中只显示一次 .给我一根绳子 str 长度 n-1 .字符串的每个字符 0 1. .在数组中,交换 第i次 元素 (i+1)-第 元素可以按我们想要的次数执行,如果 第i次 字符串的字符是 1. .我们的任务是确定是否可以按升序对数组进行排序。 例如:

null
Input : arr = {1, 2, 5, 3, 4, 6}        str = "01110"Output : YesExplanation :Here, we can swap arr[2] and arr[3], and then swap arr[3] and arr[4].Input : arr = {1, 2, 5, 3, 4, 6}        str = "01010"Output : NoExplanation :Here, the 3rd element of the array i.e. 5 can notbe swapped as the 3rd character of the string is 0. Therefore it is impossible to sort the array.

方法 循环到字符串的长度 str 然后计算 max_元素 0 每人 .在每次迭代中,如果 第i次 字符串的字符是 ‘0’ 还有 max_元素 大于 i+1 那么,就不可能对数组进行排序,否则就可能了。 上述方法的基本实施:

C++

// CPP program to Check if it
// is possible to sort the
// array in ascending order.
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible to
// sort the array
string possibleToSort( int * arr, int n, string str)
{
int max_element = -1;
for ( long i = 0; i < str.size(); i++) {
// Calculating max_element
// at each iteration.
max_element = max(max_element, arr[i]);
// if we can not swap the i-th element.
if (str[i] == '0' ) {
// if it is impossible to swap the
// max_element then we can not
// sort the array.
if (max_element > i + 1)
return "No" ;
}
}
// Otherwise, we can sort the array.
return "Yes" ;
}
// Driver function
int main()
{
int arr[] = { 1, 2, 5, 3, 4, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
string str = "01110" ;
cout << possibleToSort(arr, n, str);
return 0;
}


JAVA

// Java program to Check if it is possible to
// sort the array in ascending order.
import java.util.*;
import java.lang.*;
// Function to check if it is possible to
// sort the array
class GFG
{
public static String possibleToSort( int arr[],
int n, String str)
{
int max_element = - 1 ;
for ( int i = 0 ; i < str.length(); i++)
{
// Calculating max_element at each
// iteration.
max_element = Math.max(max_element,
arr[i]);
// if we can not swap the i-th
// element.
if (str.charAt(i) == '0' ) {
// if it is impossible to swap
// the max_element then we can
// not sort the array.
if (max_element > i + 1 )
return "No" ;
}
}
// Otherwise, we can sort the array.
return "Yes" ;
}
// Driven Program
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 5 , 3 , 4 , 6 };
int n = arr.length;
String str = "01110" ;
System.out.println(
possibleToSort(arr, n, str));
}
}
// This code is contributed by Prasad Kshirsagar


Python 3

# Python 3 program to Check if it
# is possible to sort the
# array in ascending order.
# Function to check if it is
# possible to sort the array
def possibleToSort(arr, n, str ):
max_element = - 1
for i in range ( len ( str )) :
# Calculating max_element
# at each iteration.
max_element = max (max_element, arr[i])
# if we can not swap the i-th element.
if ( str [i] = = '0' ) :
# if it is impossible to swap the
# max_element then we can not
# sort the array.
if (max_element > i + 1 ):
return "No"
# Otherwise, we can sort the array.
return "Yes"
# Driver Code
if __name__ = = "__main__" :
arr = [ 1 , 2 , 5 , 3 , 4 , 6 ]
n = len (arr)
str = "01110"
print (possibleToSort(arr, n, str ))
# This code is contributed
# by ChitraNayal


C#

// C# program to Check if it
// is possible to sort the
// array in ascending order
using System;
class GFG {
// Function to check if it
// is possible to sort the array
static string possibleToSort( int []arr,
int n,
string str)
{
int max_element = -1;
for ( int i = 0; i < str.Length; i++)
{
// Calculating max_element
// at each iteration.
max_element = Math.Max(max_element,
arr[i]);
// if we can not swap
// the i-th element.
if (str[i] == '0' )
{
// if it is impossible to swap the
// max_element then we can not
// sort the array.
if (max_element > i + 1)
return "No" ;
}
}
// Otherwise, we can
// sort the array.
return "Yes" ;
}
// Driver Code
static public void Main ()
{
int []arr = {1, 2, 5, 3, 4, 6};
int n = arr.Length;
string str = "01110" ;
Console.WriteLine(possibleToSort(arr, n, str));
}
}
// This code is contributed by anuj_67.


PHP

<?php
// PHP program to Check if it is possible
// to sort the array in ascending order.
// Function to check if it is possible
// to sort the array
function possibleToSort( $arr , $n , $str )
{
$max_element = -1;
for ( $i = 0; $i < sizeof( $str ); $i ++)
{
// Calculating max_element
// at each iteration.
$max_element = max( $max_element ,
$arr [ $i ]);
// if we can not swap the i-th element.
if ( $str [ $i ] == '0' )
{
// if it is impossible to swap the
// max_element then we can not
// sort the array.
if ( $max_element > $i + 1)
return "No" ;
}
}
// Otherwise, we can sort the array.
return "Yes" ;
}
// Driver Code
$arr = array (1, 2, 5, 3, 4, 6);
$n = sizeof( $arr );
$str = "01110" ;
echo possibleToSort( $arr , $n , $str );
// This code is contributed
// by Akanksha Rai
?>


Javascript

<script>
// Javascript program to Check if it
// is possible to sort the
// array in ascending order
// Function to check if it
// is possible to sort the array
function possibleToSort(arr, n, str)
{
let max_element = -1;
for (let i = 0; i < str.length; i++)
{
// Calculating max_element
// at each iteration.
max_element = Math.max(max_element, arr[i]);
// if we can not swap
// the i-th element.
if (str[i] == '0' )
{
// if it is impossible to swap the
// max_element then we can not
// sort the array.
if (max_element > i + 1)
return "No" ;
}
}
// Otherwise, we can
// sort the array.
return "Yes" ;
}
let arr = [1, 2, 5, 3, 4, 6];
let n = arr.Length;
let str = "01110" ;
document.write(possibleToSort(arr, n, str));
// This code is contributed by divyesh072019.
</script>


输出:

Yes

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享