对象已移动

可在此处找到该文档 Construct an Array of length N containing exactly K elements divisible by their positions – New Self New Life
New Self New Life
No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices
New Self New Life
No Result
View All Result
Home Softwares

Construct an Array of length N containing exactly K elements divisible by their positions

by admin
3 years ago
in Softwares
Best Coding Practices For Rest API Design
Share on FacebookShare on Twitter


Given two integers N and Okay, the duty is to assemble an array of size N containing precisely Okay components divisible by their positions.

Examples:

Enter: N = 6, Okay = 2
Output: {5, 1, 2, 3, 4, 6}
Rationalization: Contemplating the above array:
At Place 1, factor 5 is divisible by 1
At Place 2, factor 1 will not be divisible by 2
At Place 3, factor 2 will not be divisible by 3
At Place 4, factor 3 will not be divisible by 4
At Place 5, factor 4 will not be divisible by 5
At Place 6, factor 6 is divisible by 6
Due to this fact, there are precisely Okay components in array divisible by their positions, assembly the required standards.
Therefore the resultant array can be {5 1 2 3 4 6}.

Enter: N = 5, Okay = 5
Output: {1, 2, 3, 4, 5}

 

Strategy: The issue could be solved simply utilizing Grasping method based mostly on under observations:

For any integer X, we all know that:

  • X can be divisible by 1 and X all the time.
  • No integer better than X will ever be capable to divide X.

So utilizing these observations, we will assemble the array containing precisely Okay components divisible by their positions, as follows:

  • For place 1, place any factor better than 1, as a result of 1 will divide all integers
  • For positions better than 1, select Okay-1 positions, and place them within the array at corresponding indices.
  • The remaining N-Okay positions could be positioned at another place to match the required standards.

Illustrations:

Take into account an instance: N = 6, Okay = 5

The empty array of dimension 6 can be: 
arr[]:         _  _ _  _ _  _
positions: 1 2 3 4 5 6

Step 1: Fill place 1 with any integer better than 1

  • For 1st worth equal to its place, we’ve 2 choices – to insert 1 at 1, and to insert some integer better than 1 at 1. If we insert 1 at 1, there can be a case once we can not have Okay=5 values identical as their positions. So we are going to insert another worth better than 1 at place 1 (say 5):
    • arr[]:         5 _ _  _ _  _
      positions: 1 2 3 4 5 6

Step 2: Fill Okay-1 (=4) positions at corresponding indices

  • For 2nd worth equal to its place:
    • arr[]:         5 2 _  _ _ _
      positions: 1 2 3 4 5 6
  • For third worth equal to its place:
    • arr[]:         5 2 3  _ _ _
      positions: 1 2 3 4 5 6
  • For 4th worth equal to its place:
    • arr[]:         5 2 3 4 _ _
      positions: 1 2 3 4 5 6
  • For fifth worth equal to its place, we can not insert 5 at place 5, as it’s already used at place 1. So we are going to insert 1 at place 5, and 6 at place 6:
    • arr[]:         5 2 3 4 1 6
      positions: 1 2 3 4 5 6

Due to this fact the ultimate array can be: 5 2 3 4 1 6

Observe the steps under to implement the above method:

  • Create an array of N consecutive optimistic integers from 1 to N.
  • After the index N-Okay, there can be Okay-1 components left, we won’t intrude with these components. So, we’ve Okay-1 components, that are divisible by their place.
  • We’ll make First factor of the array equal to the factor at index N-Okay. This may even be divisible by its place.
  • We’ll make the remaining components (i.e. from index 1 to N-Okay) equal to the weather instant left to them. These all N-Okay components won’t be divisible by their place then and remaining Okay components could be divisible by their place.

Beneath is the implementation of the above method:

C++

#embody <bits/stdc++.h>

utilizing namespace std;

  

vector<int> constructArray(int N, int Okay)

{

    

    vector<int> A(N, 0);

  

    

    for (int i = 0; i < N; i++) {

        A[i] = i + 1;

    }

  

    

    

    

    int goal = N - Okay;

  

    

    

    int prev = A[0];

  

    

    

    A[0] = A[target];

  

    

    

    

    

    for (int i = 1; i <= goal; i++) {

        int temp = A[i];

        A[i] = prev;

        prev = temp;

    }

  

    return A;

}

  

int major()

{

    int N = 6, Okay = 2;

  

    

    

    vector<int> A = constructArray(N, Okay);

  

    

    for (int i = 0; i < N; i++)

        cout << A[i] << " ";

    cout << endl;

}

Time Complexity:  O(N)
Auxiliary House:  O(N)



Source link

Tags: arrayConstructdivisibleElementslengthpositions
Previous Post

What Is a Development Environment? How to Get Started Now

Next Post

What Renee Zellweger Did While On A Break From Acting

Related Posts

BrowserStack launches Figma plugin for detecting accessibility issues in design phase
Softwares

BrowserStack launches Figma plugin for detecting accessibility issues in design phase

by admin
July 22, 2025
Developer beats AI in coding battle
Softwares

Developer beats AI in coding battle

by admin
July 21, 2025
React latest version – React 19 to bring the React Compiler & more
Softwares

React latest version – React 19 to bring the React Compiler & more

by admin
July 20, 2025
Cross Exchange Crypto Arbitrage Bot: Automating the Trade
Softwares

Cross Exchange Crypto Arbitrage Bot: Automating the Trade

by admin
July 19, 2025
Improvements and crash fixes – Vivaldi Android Browser snapshot 3756.4
Softwares

Improvements and crash fixes – Vivaldi Android Browser snapshot 3756.4

by admin
July 18, 2025
Next Post
What Renee Zellweger Did While On A Break From Acting

What Renee Zellweger Did While On A Break From Acting

International Women’s Day – #ShutTheStereotype – Noise

International Women’s Day - #ShutTheStereotype – Noise

  • Trending
  • Comments
  • Latest
How to use Redis for Api Caching in CS-Cart

How to use Redis for Api Caching in CS-Cart

July 26, 2023
The Simpsons Producer Apologizes To Fans For Killing Off 35-Year-Old Character

The Simpsons Producer Apologizes To Fans For Killing Off 35-Year-Old Character

April 26, 2024
Indiana Evans: What happened to the H2O Australian actress Indiana Evans and what is she doing now? | Explainer

Indiana Evans: What happened to the H2O Australian actress Indiana Evans and what is she doing now? | Explainer

December 7, 2024
I Tried Calocurb For 90 Days. Here’s My Review.

I Tried Calocurb For 90 Days. Here’s My Review.

January 8, 2025
Yeat Claims He Met an Alien When He Was Kid and It Talked to Him

Yeat Claims He Met an Alien When He Was Kid and It Talked to Him

July 7, 2023
Jada Pinkett Smith reveals what Chris Rock said to her after Oscars slap – National

Jada Pinkett Smith reveals what Chris Rock said to her after Oscars slap – National

October 13, 2023
8BitDo Retro Mechanical Keyboard C64 Review

8BitDo Retro Mechanical Keyboard C64 Review

March 24, 2025
Bones: All Of Brennan’s Interns, Ranked

Bones: All Of Brennan’s Interns, Ranked

June 15, 2021

2025 XXL Freshman Cypher With BabyChiefDoIt, Ian, Lazer Dim 700

July 22, 2025
SALTGATOR Debuts Desktop Soft-Gel Injection Machine on Kickstarter — A Game-Changer for Makers

SALTGATOR Debuts Desktop Soft-Gel Injection Machine on Kickstarter — A Game-Changer for Makers

July 22, 2025
BrowserStack launches Figma plugin for detecting accessibility issues in design phase

BrowserStack launches Figma plugin for detecting accessibility issues in design phase

July 22, 2025
TV Shows Canceled in 2025-2026: Which Aren’t Renewed for More Seasons?

TV Shows Canceled in 2025-2026: Which Aren’t Renewed for More Seasons?

July 22, 2025
Study Shows Teens Are Increasingly Relying on AI Chatbots for Social Interaction

Study Shows Teens Are Increasingly Relying on AI Chatbots for Social Interaction

July 22, 2025
Jeffrey Epstein's 'Little Black Book' Was Published In 2015 — The Website Was Destroyed, But You Can Still See It HERE!

Jeffrey Epstein's 'Little Black Book' Was Published In 2015 — The Website Was Destroyed, But You Can Still See It HERE!

July 22, 2025
Andy Sachs is back! Anne Hathaway shares 1st look at ‘The Devil Wears Prada 2’

Andy Sachs is back! Anne Hathaway shares 1st look at ‘The Devil Wears Prada 2’

July 22, 2025
Developer beats AI in coding battle

Developer beats AI in coding battle

July 21, 2025
New Self New Life

Your source for entertainment news, celebrities, celebrity news, and Music, Cinema, Digital Lifestyle and Social Media and More !

Categories

  • Celebrity
  • Cinema
  • Devices
  • Digital Lifestyle
  • Entertainment
  • Music
  • Social Media
  • Softwares
  • Uncategorized

Recent Posts

  • 2025 XXL Freshman Cypher With BabyChiefDoIt, Ian, Lazer Dim 700
  • SALTGATOR Debuts Desktop Soft-Gel Injection Machine on Kickstarter — A Game-Changer for Makers
  • BrowserStack launches Figma plugin for detecting accessibility issues in design phase
  • Home
  • Disclaimer
  • DMCA
  • Privacy Policy
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites. slotsfree  creator solana token

No Result
View All Result
  • Home
  • Entertainment
  • Celebrity
  • Cinema
  • Music
  • Digital Lifestyle
  • Social Media
  • Softwares
  • Devices

Copyright © 2021 New Self New Life.
New Self New Life is not responsible for the content of external sites.

New Self New Life