Vinhtrinh - Tin Tức Hay, Siêu Hot Cập Nhật Hàng Ngày
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Luật
  • Tài Chính
No Result
View All Result
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Luật
  • Tài Chính
No Result
View All Result
Vinhtrinh - Tin Tức Hay, Siêu Hot Cập Nhật Hàng Ngày
No Result
View All Result

Count frequencies of array elements in range 1 to n

admin by admin
April 26, 2020
in Công Nghệ
0
Count frequencies of array elements in range 1 to n



Given an array of length n having integers 1 to n with some elements being repeated. Count frequencies of all elements from 1 to n.

Example:
Input Array: {2, 3, 3, 2, 5}
Output:
1 0
2 2
3 2
4 0
5 1

Algorithm 1:
Find frequency of elements 1 – n one by one using 2 loops:
1. Outer loop runs from i = 1 to n.
2. Inner loop calculates the count of i in the input array.
3. Print the count of i when inner loop ends.
Time Complexity: O(n^2)
Space Complexity: O(1)

Algorithm 2:
1. Create a count array of size n with all elements from index i = 0 to n-1 initialized to 0.Here, count[i] is count of i+1.
2. Traverse the array once. For i = 0 to n-1, increment count[input[i]-1] by 1.
3. Traverse count array and print count array.
Time Complexity: O(n)
Space Complexity: O(n)

Algorithm 3(Optimized):
1. Reduce all elements by 1 so that the elements are converted in the range 0 to n-1.
2. Traverse the input array. For i = 0 to n-1, add n to element at index (input[i]%n). After all the elements are completed, element at index i will be incremented by n*k where k is the number of times i occurs in the array.
3. Finally, print counts of elements and simultaneously convert the array back to original array.
a: Print count of i+1 as input[i]/n.
b: Set input[i] = input[i]%n+1.
Time Complexity: O(n)
Space Complexity: O(1)

Code and Algorithm Visualization:

Website:

Facebook:

Nguồn: https://vinhtrinh.com.vn

Xem thêm bài viết khác: https://vinhtrinh.com.vn/cong-nghe/

Xem thêm Bài Viết:

  • Phần mềm quản lý bất động sản là gì?
  • Dash là gì? Tìm hiểu chi tiết về đồng tiền ảo Dashcoin
  • Top 3 ứng dụng xem phim online chất lượng cao trên di động
  • Giải pháp thiết kế website in ấn chuyên nghiệp, giá rẻ và chuẩn SEO
  • Khách hàng mục tiêu là gì và cách để xác định khách hàng mục tiêu hiệu quả
Previous Post

Tư vấn luật: Quy trình pháp lý phát triển dự án bất động sản

Next Post

Người đưa tin 24G (6g30 ngày 25/04/2020)

Next Post
Người đưa tin 24G (6g30 ngày 25/04/2020)

Người đưa tin 24G (6g30 ngày 25/04/2020)

Leave a Reply Cancel reply

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

Xem Thêm

Bạn nên khảo sát, tìm hiểu thật kỹ, so sánh để tìm được sự lựa chọn phù hợp

Hướng dẫn mua bán nhà Quận 7 dưới 3 tỷ đảm bảo chất lượng sống 

November 30, 2022
Bạn không nên mua bán đất nền Lai Châu chung sổ

5 lưu ý quan trọng khi mua đất nền Lai Châu mà bạn cần biết

October 27, 2022
Hình ảnh mua bán biệt thự quận Đống Đa 2

6 điểm cần lưu ý trước khi mua bán biệt thự quận Đống Đa

October 21, 2022
Hình ảnh chung cư Nam Từ Liêm 3

5 kinh nghiệm mua chung cư Nam Từ Liêm an toàn hữu ích

October 11, 2022
Hình ảnh bán nhà mặt phố quận Hai Bà Trưng 2

Kinh nghiệm bán nhà mặt phố quận Hai Bà Trưng

October 4, 2022
Hình ảnh mua bán nhà riêng quận đống đa 1

9 điều cần lưu ý khi mua bán nhà riêng quận Đống Đa lần đầu

September 27, 2022
  • Liên Hệ
  • Chính Sách Bảo Mật

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Bất Động Sản
  • Công Nghệ
  • Luật
  • Tài Chính

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.