给定一个由N 个整数组成的数组arr[] ,任务是针对每个数组元素,比如说arr[i] ,找到小于arr[i]的数组元素的数量。
例子:
Input: arr[] = {3, 4, 1, 1, 2}
Output: 3 4 0 0 2
Explanation:
The elements which are smaller than arr[0](= 3) are {1, 1, 2}. Hence, the count is 3.
The elements which are smaller than arr[1](= 4) are {1, 1, 2, 3}. Hence, the count is 4.
The elements arr[2](= 1) and arr[3](= 1) are the smallest possible. Hence, the count is 0.
The elements which are smaller than arr[4](= 2) are {1, 1}. Hence, the count is 2.
Input: arr[] = {1, 2, 3, 4}
Output: 0 1 2 3
朴素的方法:最简单的方法是遍历数组,对于每个数组元素,计算小于它们的数组元素的数量并打印获得的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count for each array
// element, the number of elements
// that are smaller than that element
void smallerNumbers(int arr[], int N)
{
// Traverse the array
for (int i = 0; i < N; i++) {
// Stores the count
int count = 0;
// Traverse the array
for (int j = 0; j < N; j++) {
// Increment count
if (arr[j] < arr[i]) {
count++;
}
}
// Print the count of smaller
// elements for the current element
cout << count << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 3, 4, 1, 1, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
smallerNumbers(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to count for each array
// element, the number of elements
// that are smaller than that element
static void smallerNumbers(int arr[], int N)
{
// Traverse the array
for(int i = 0; i < N; i++)
{
// Stores the count
int count = 0;
// Traverse the array
for(int j = 0; j < N; j++)
{
// Increment count
if (arr[j] < arr[i])
{
count++;
}
}
// Print the count of smaller
// elements for the current element
System.out.print(count + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 4, 1, 1, 2 };
int N = arr.length;
smallerNumbers(arr, N);
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to count for each array
# element, the number of elements
# that are smaller than that element
def smallerNumbers(arr, N):
# Traverse the array
for i in range(N):
# Stores the count
count = 0
# Traverse the array
for j in range(N):
# Increment count
if (arr[j] < arr[i]):
count += 1
# Print the count of smaller
# elements for the current element
print(count, end=" ")
# Driver Code
if __name__ == "__main__":
arr = [3, 4, 1, 1, 2]
N = len(arr)
smallerNumbers(arr, N)
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to count for each array
// element, the number of elements
// that are smaller than that element
static void smallerNumbers(int[] arr, int N)
{
// Traverse the array
for(int i = 0; i < N; i++)
{
// Stores the count
int count = 0;
// Traverse the array
for(int j = 0; j < N; j++)
{
// Increment count
if (arr[j] < arr[i])
{
count++;
}
}
// Print the count of smaller
// elements for the current element
Console.Write(count + " ");
}
}
// Driver Code
static public void Main()
{
int[] arr = { 3, 4, 1, 1, 2 };
int N = arr.Length;
smallerNumbers(arr, N);
}
}
// This code is contributed by sanjoy_62,
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count for each array
// element, the number of elements
// that are smaller than that element
void smallerNumbers(int arr[], int N)
{
// Stores the frequencies
// of array elements
int hash[100000] = { 0 };
// Traverse the array
for (int i = 0; i < N; i++)
// Update frequency of arr[i]
hash[arr[i]]++;
// Initialize sum with 0
int sum = 0;
// Compute prefix sum of the array hash[]
for (int i = 1; i < 100000; i++) {
hash[i] += hash[i - 1];
}
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If current element is 0
if (arr[i] == 0) {
cout << "0";
continue;
}
// Print the resultant count
cout << hash[arr[i] - 1]
<< " ";
}
}
// Driver Code
int main()
{
int arr[] = { 3, 4, 1, 1, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
smallerNumbers(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to count for each array
// element, the number of elements
// that are smaller than that element
static void smallerNumbers(int arr[], int N)
{
// Stores the frequencies
// of array elements
int hash[] = new int[100000];
// Traverse the array
for(int i = 0; i < N; i++)
// Update frequency of arr[i]
hash[arr[i]]++;
// Initialize sum with 0
int sum = 0;
// Compute prefix sum of the array hash[]
for(int i = 1; i < 100000; i++)
{
hash[i] += hash[i - 1];
}
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// If current element is 0
if (arr[i] == 0)
{
System.out.println("0");
continue;
}
// Print the resultant count
System.out.print(hash[arr[i] - 1] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 4, 1, 1, 2 };
int N = arr.length;
smallerNumbers(arr, N);
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to count for each array
# element, the number of elements
# that are smaller than that element
def smallerNumbers(arr, N):
# Stores the frequencies
# of array elements
hash = [0] * 100000
# Traverse the array
for i in range(N):
# Update frequency of arr[i]
hash[arr[i]] += 1
# Initialize sum with 0
sum = 0
# Compute prefix sum of the array hash[]
for i in range(1, 100000):
hash[i] += hash[i - 1]
# Traverse the array arr[]
for i in range(N):
# If current element is 0
if (arr[i] == 0):
print("0")
continue
# Print the resultant count
print(hash[arr[i] - 1], end = " ")
# Driver Code
if __name__ == "__main__":
arr = [ 3, 4, 1, 1, 2 ]
N = len(arr)
smallerNumbers(arr, N)
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
class GFG{
// Function to count for each array
// element, the number of elements
// that are smaller than that element
static void smallerNumbers(int []arr, int N)
{
// Stores the frequencies
// of array elements
int []hash = new int[100000];
// Traverse the array
for(int i = 0; i < N; i++)
// Update frequency of arr[i]
hash[arr[i]]++;
// Initialize sum with 0
//int sum = 0;
// Compute prefix sum of the array hash[]
for(int i = 1; i < 100000; i++)
{
hash[i] += hash[i - 1];
}
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// If current element is 0
if (arr[i] == 0)
{
Console.WriteLine("0");
continue;
}
// Print the resultant count
Console.Write(hash[arr[i] - 1] + " ");
}
}
// Driver Code
public static void Main(string[] args)
{
int []arr = { 3, 4, 1, 1, 2 };
int N = arr.Length;
smallerNumbers(arr, N);
}
}
// This code is contributed by AnkThon
3 4 0 0 2
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:上述方法可以通过使用 Hashing 进行优化。请按照以下步骤解决问题:
- 初始化一个大小为10 5的辅助数组hash[]并将所有数组元素初始化为0来存储每个数组元素的频率。
- 遍历给定的数组arr[]并将数组hash[]中arr[i]的频率增加1作为hash[arr[i]]++ 。
- 找到数组hash[]的前缀和。
- 再次遍历给定的数组,对于每个数组元素arr[] ,打印hash[arr[i] – 1] 的值作为较小元素的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count for each array
// element, the number of elements
// that are smaller than that element
void smallerNumbers(int arr[], int N)
{
// Stores the frequencies
// of array elements
int hash[100000] = { 0 };
// Traverse the array
for (int i = 0; i < N; i++)
// Update frequency of arr[i]
hash[arr[i]]++;
// Initialize sum with 0
int sum = 0;
// Compute prefix sum of the array hash[]
for (int i = 1; i < 100000; i++) {
hash[i] += hash[i - 1];
}
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// If current element is 0
if (arr[i] == 0) {
cout << "0";
continue;
}
// Print the resultant count
cout << hash[arr[i] - 1]
<< " ";
}
}
// Driver Code
int main()
{
int arr[] = { 3, 4, 1, 1, 2 };
int N = sizeof(arr) / sizeof(arr[0]);
smallerNumbers(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to count for each array
// element, the number of elements
// that are smaller than that element
static void smallerNumbers(int arr[], int N)
{
// Stores the frequencies
// of array elements
int hash[] = new int[100000];
// Traverse the array
for(int i = 0; i < N; i++)
// Update frequency of arr[i]
hash[arr[i]]++;
// Initialize sum with 0
int sum = 0;
// Compute prefix sum of the array hash[]
for(int i = 1; i < 100000; i++)
{
hash[i] += hash[i - 1];
}
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// If current element is 0
if (arr[i] == 0)
{
System.out.println("0");
continue;
}
// Print the resultant count
System.out.print(hash[arr[i] - 1] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 4, 1, 1, 2 };
int N = arr.length;
smallerNumbers(arr, N);
}
}
// This code is contributed by Kingash
蟒蛇3
# Python3 program for the above approach
# Function to count for each array
# element, the number of elements
# that are smaller than that element
def smallerNumbers(arr, N):
# Stores the frequencies
# of array elements
hash = [0] * 100000
# Traverse the array
for i in range(N):
# Update frequency of arr[i]
hash[arr[i]] += 1
# Initialize sum with 0
sum = 0
# Compute prefix sum of the array hash[]
for i in range(1, 100000):
hash[i] += hash[i - 1]
# Traverse the array arr[]
for i in range(N):
# If current element is 0
if (arr[i] == 0):
print("0")
continue
# Print the resultant count
print(hash[arr[i] - 1], end = " ")
# Driver Code
if __name__ == "__main__":
arr = [ 3, 4, 1, 1, 2 ]
N = len(arr)
smallerNumbers(arr, N)
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
class GFG{
// Function to count for each array
// element, the number of elements
// that are smaller than that element
static void smallerNumbers(int []arr, int N)
{
// Stores the frequencies
// of array elements
int []hash = new int[100000];
// Traverse the array
for(int i = 0; i < N; i++)
// Update frequency of arr[i]
hash[arr[i]]++;
// Initialize sum with 0
//int sum = 0;
// Compute prefix sum of the array hash[]
for(int i = 1; i < 100000; i++)
{
hash[i] += hash[i - 1];
}
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// If current element is 0
if (arr[i] == 0)
{
Console.WriteLine("0");
continue;
}
// Print the resultant count
Console.Write(hash[arr[i] - 1] + " ");
}
}
// Driver Code
public static void Main(string[] args)
{
int []arr = { 3, 4, 1, 1, 2 };
int N = arr.Length;
smallerNumbers(arr, N);
}
}
// This code is contributed by AnkThon
3 4 0 0 2
时间复杂度: O(N)
辅助空间: O(10 5 )
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live