对象已移动

可在此处找到该文档 Find original Array from given Array of GCD of prefix – 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

Find original Array from given Array of GCD of prefix

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


View Dialogue

Enhance Article

Save Article

Like Article

View Dialogue

Enhance Article

Save Article

Like Article

Given an array B[] of size N, the duty is to print an array A[] such that for each ith aspect B[i] is gcd of the primary i components of A[] i.e. Bi = gcd (A1, A2, …., Ai) and if no such array A[] exists, print −1.

Examples:

Enter: B = {4, 2} 
Output: 4 2
Clarification: One attainable reply is [4, 2] as a result of 
B may be generated as follows: B=[gcd(4), gcd(4, 2)]=[4, 2].

Enter: B = {2, 6, 8, 10} 
Output: -1
Clarification: No array exists which satisfies the given situation.

Method: The issue may be solved based mostly on the next remark: 

  • We all know that  Bi = gcd(A1, A2,  . . .  , Ai) and Bi+1 = gcd(A1, A2, . . ., Ai, Ai+1) = gcd(gcd(A1, A2, . . ., Ai), Ai+1) = gcd(Bi, Ai+1). This fashion, we will write Bi+1 = gcd(Bi , Ai+1).
  • The implication of that is that Bi+1 have to be an element of Bi , Since gcd of two numbers is divisor of each numbers. Therefore, situation Bi+1 divides Bi ought to maintain for all 1 ≤ i <N.
  • So if the given array has any such i the place Bi+1 doesn’t divide Bi , no such A can exist.
  • The given array B is a sound candidate for A as Bi+1 = gcd(Bi, Ai+1), however we’ve got Ai+1 = Bi+1 . Since Bi+1 divide Bi , gcd(Bi, Bi+1) = Bi+1. So the given array B satisfies our situation and may be printed as array A.

Observe the beneath steps to unravel the issue:

  • Initialize a boolean variable flag = true.
  • Iterate on the given array and verify the next:
    • If the subsequent aspect shouldn’t be an element of the present aspect:
      • Set flag = false.
      • Terminate the loop.
  • If the flag is true:
    • Print the given array B[].
    • else print -1.

Under is the implementation of the above strategy.

Java

  

class GFG {

  

    

    

    static void findOriginal(int arr[], int n)

    {

  

        

        boolean flag = true;

  

        for (int i = 0; i < n - 1; i++) {

  

            

            

            if (arr[i] % arr[i + 1] != 0) {

                flag = false;

                break;

            }

        }

  

        if (flag == false)

            System.out.println(-1);

        else {

            for (int val : arr) {

                System.out.print(val + " ");

            }

        }

    }

  

    

    public static void major(String[] args)

    {

        

        int B[] = { 4, 2 };

        int N = B.size;

  

        

        findOriginal(B, N);

    }

}

Time Complexity: O(N) for traversing the given array.
Auxiliary Area: O(1) as fixed house is used.



Source link

Tags: arrayFindGCDOriginalprefix
Previous Post

Top 7 Fall Jackets All Men Should Own For 2022

Next Post

10 Things to Think About Before Opening a Cleaning Business

Related Posts

NFT Aggregator Marketplace Development: Complete Overview
Softwares

NFT Aggregator Marketplace Development: Complete Overview

by admin
August 15, 2025
How agile is your crypto? Interview study explores opportunities and challenges of cryptographic update processes
Softwares

How agile is your crypto? Interview study explores opportunities and challenges of cryptographic update processes

by admin
August 12, 2025
20+ Best Free Futuristic Fonts in 2025 — Speckyboy
Softwares

20+ Best Free Futuristic Fonts in 2025 — Speckyboy

by admin
August 13, 2025
This week in AI dev tools: GPT-5, Claude Opus 4.1, and more (August 8, 2025)
Softwares

This week in AI dev tools: GPT-5, Claude Opus 4.1, and more (August 8, 2025)

by admin
August 9, 2025
How Agentic AI Powers Webkul eCommerce Marketplaces
Softwares

How Agentic AI Powers Webkul eCommerce Marketplaces

by admin
August 11, 2025
Next Post
10 Things to Think About Before Opening a Cleaning Business

10 Things to Think About Before Opening a Cleaning Business

Meta Invites Applications for the Third Phase of its ‘Community Accelerator’ Program

Meta Invites Applications for the Third Phase of its ‘Community Accelerator’ Program

  • Trending
  • Comments
  • Latest
More than 400 Canadian artists sign letter denouncing ‘anti-trans’ policies

More than 400 Canadian artists sign letter denouncing ‘anti-trans’ policies

April 1, 2024
Taylor Swift’s ‘Eras Tour’ movie: How and when you can stream in Canada – National

Taylor Swift’s ‘Eras Tour’ movie: How and when you can stream in Canada – National

November 27, 2023
Jacklyn Zeman, longtime ‘General Hospital’ actor, dies at 70 – National

Jacklyn Zeman, longtime ‘General Hospital’ actor, dies at 70 – National

May 11, 2023
Greyson Chance says Ellen DeGeneres ‘abandoned’ him, calls her ‘manipulative’ and ‘opportunistic’ – National

Greyson Chance says Ellen DeGeneres ‘abandoned’ him, calls her ‘manipulative’ and ‘opportunistic’ – National

September 26, 2022
Robert De Niro shows up to troll Donald Trump outside hush-money trial – National

Robert De Niro shows up to troll Donald Trump outside hush-money trial – National

May 29, 2024
Anne Heche to be taken off life support after compatible organ recipient found – National

Anne Heche to be taken off life support after compatible organ recipient found – National

August 15, 2022
Mike ‘The Situation’ Sorrentino saves 2-year-old son from choking in home video – National

Mike ‘The Situation’ Sorrentino saves 2-year-old son from choking in home video – National

February 5, 2024
‘Nope’ movie review: Jordan Peele does it again in masterful spectacle – National

‘Nope’ movie review: Jordan Peele does it again in masterful spectacle – National

July 22, 2022
Hip-Hop’s Biggest First-Week Sales for Projects in 2025

Hip-Hop’s Biggest First-Week Sales for Projects in 2025

August 15, 2025
HyperX’s claims its latest headset lasts 250 hours on a single charge

HyperX’s claims its latest headset lasts 250 hours on a single charge

August 15, 2025
Kate Middleton, Prince William Rare Message Amid Summer Break

Kate Middleton, Prince William Rare Message Amid Summer Break

August 15, 2025
As the Mule Strikes Again, Gaal and Demerzel’s Confrontation Takes an Unexpected Turn

As the Mule Strikes Again, Gaal and Demerzel’s Confrontation Takes an Unexpected Turn

August 15, 2025
YouTube Adds Custom App Promotions for Shorts

YouTube Adds Custom App Promotions for Shorts

August 15, 2025
Tragic Death at 2025 Elements Festival Under Investigation

Tragic Death at 2025 Elements Festival Under Investigation

August 14, 2025
J’Adore! Rebecca Adlington is c’est chic on This Morning wearing a ‘Postcard from Paris’ Boden dress

J’Adore! Rebecca Adlington is c’est chic on This Morning wearing a ‘Postcard from Paris’ Boden dress

August 14, 2025
John Slattery on if He’ll Ever Do a Mad Men Reboot

John Slattery on if He’ll Ever Do a Mad Men Reboot

August 14, 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

  • Hip-Hop’s Biggest First-Week Sales for Projects in 2025
  • HyperX’s claims its latest headset lasts 250 hours on a single charge
  • Kate Middleton, Prince William Rare Message Amid Summer Break
  • 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